Skip to main content

Defining custom patterns for secret scanning

Protect your unique secret types by defining custom patterns with regular expressions.

谁可以使用此功能?

具有管理员角色的存储库所有者、组织所有者、安全管理员、企业管理员和用户

启用了 GitHub Team 的 GitHub Enterprise 或 GitHub Secret Protection 上的组织拥有的存储库

Defining a custom pattern with Copilot

You can use Copilot 机密扫描 to generate regular expressions based on a text description of the type of pattern you would like to detect, including optional example strings that should be detected. See 使用 Copilot 机密扫描为自定义模式生成正则表达式.

Defining a custom pattern for a repository

Before defining a custom pattern, you must ensure that Secret Protection is enabled on your repository. For more information, see Enabling secret scanning for your repository.

  1. 在 GitHub 上,导航到存储库的主页面。

  2. 在仓库名称下,单击 “Settings”****。 如果看不到“设置”选项卡,请选择“”下拉菜单,然后单击“设置”。

    存储库标头的屏幕截图,其中显示了选项卡。 “设置”选项卡以深橙色边框突出显示。

  3. 在边栏的“Security”部分中,单击“ Advanced Security”****。

  4. Under "Secret Protection", to the right of "Custom patterns", click New pattern.

  5. 输入新自定义模式的详细信息。 您至少必须提供模式的名称,以及秘密模式格式的正则表达式。

    1. 在“模式名称”字段中,键入模式的名称。
    2. 在“机密格式”字段中,键入机密模式格式的正则表达式。
    3. 可以单击“More options ”来提供密钥格式的其他周围内容或额外匹配要求****。 请参阅“自定义模式参考”。
    4. 提供一个示例测试字符串,用于确保配置与预期模式匹配。

    填充自定义 secret scanning 模式窗体的屏幕截图。

  6. When you're ready to test your new custom pattern, to identify matches in the repository without creating alerts, click Save and dry run.

  7. 试运行完成后,你将看到结果示例(最多 1000 个)。 查看结果并确定任何误报结果。

    显示试运行结果的屏幕截图。

  8. 编辑新的自定义模式以修复结果的任何问题,然后测试更改,单击“保存并试运行”。

  9. 如果对新的自定义模式感到满意,请单击“发布模式”。

  10. Optionally, to enable push protection for your custom pattern, click Enable. For more information, see Push protection.

    注意

    The "Enable" button isn't available until after the dry run succeeds and you publish the pattern.

After your pattern is created, secret scanning 将在 GitHub 仓库中存在的所有分支上扫描整个 Git 历史记录的任何密钥。 For more information on viewing 机密扫描警报, see 管理机密扫描警报.

Defining a custom pattern for an organization

Before defining a custom pattern, you must ensure that you enable secret scanning for the repositories that you want to scan in your organization. You can use security configurations to enable secret scanning on all repositories in your organization. For more information, see Enabling security features at scale.

  1. 在 GitHub 的右上角,单击个人资料图片,然后单击“ Your organizations”****。

  2. 单击以选择一个组织。

  3. 在组织名称下,单击 “Settings”****。 如果看不到“设置”选项卡,请选择“”下拉菜单,然后单击“设置”********。

    组织配置文件中选项卡的屏幕截图。 “设置”选项卡以深橙色标出。

  4. In the "Security" section of the sidebar, select the Advanced Security dropdown menu, then click Global settings.

  5. 在“自定义模式”下,单击“ 新建模式”。

  6. 输入新自定义模式的详细信息。 您至少必须提供模式的名称,以及秘密模式格式的正则表达式。

    1. 在“模式名称”字段中,键入模式的名称。
    2. 在“机密格式”字段中,键入机密模式格式的正则表达式。
    3. 可以单击“More options ”来提供密钥格式的其他周围内容或额外匹配要求****。 请参阅“自定义模式参考”。
    4. 提供一个示例测试字符串,用于确保配置与预期模式匹配。

    填充自定义 secret scanning 模式窗体的屏幕截图。

  7. When you're ready to test your new custom pattern, to identify matches in select repositories without creating alerts, click Save and dry run.

  8. 选择要在其中执行试运行的存储库。

    • 若要在整个组织中执行试运行,请选择“组织中的所有存储库”。****
    • 若要指定要在其中执行试运行的存储库,请选择“所选存储库”,然后搜索并选择最多 10 个存储库。
  9. 准备好测试新的自定义模式后,请单击“运行”。

  10. 试运行完成后,你将看到结果示例(最多 1000 个)。 查看结果并确定任何误报结果。

    显示试运行结果的屏幕截图。

  11. 编辑新的自定义模式以修复结果的任何问题,然后测试更改,单击“保存并试运行”。

  12. 如果对新的自定义模式感到满意,请单击“发布模式”。

  13. Optionally, to enable push protection for your custom pattern, click Enable. For more information, see Push protection.

    注意

    • 启用推送保护的选项仅对已发布的模式可见。
    • 自定义模式的推送保护仅适用于组织中启用了 secret scanning 作为推送保护的存储库。
    • 为常见的自定义模式启用推送保护可能会对参与者造成干扰。

After your pattern is created, secret scanning scans for any secrets in repositories in your organization, including their entire Git history on all branches. Organization owners and repository administrators will be alerted to any secrets found and can review the alert in the repository where the secret is found. For more information on viewing 机密扫描警报, see 管理机密扫描警报.

Defining a custom pattern for an enterprise account

注意

  • At the enterprise level, only the creator of a custom pattern can edit the pattern, and use it in a dry run.
  • 只能对具有管理访问权限的存储库执行试运行。 如果企业所有者想要对组织中的任何存储库执行试运行,则必须分配有组织所有者角色才可以。 有关详细信息,请参阅“管理您在企业所属组织中的角色”。
  1. 导航到您的企业。 例如,从 GitHub.com 上的 公司 页面。

  2. 在页面顶部,单击“ Policies”。

  3. “Policies”下,单击“Advanced SecurityCode security”****。

  4. Under "Advanced SecurityCode security", click Security features.

  5. Under "Secret scanning custom patterns", click New pattern.

  6. 输入新自定义模式的详细信息。 您至少必须提供模式的名称,以及秘密模式格式的正则表达式。

    1. 在“模式名称”字段中,键入模式的名称。
    2. 在“机密格式”字段中,键入机密模式格式的正则表达式。
    3. 可以单击“More options ”来提供密钥格式的其他周围内容或额外匹配要求****。 请参阅“自定义模式参考”。
    4. 提供一个示例测试字符串,用于确保配置与预期模式匹配。

    填充自定义 secret scanning 模式窗体的屏幕截图。

  7. When you're ready to test your new custom pattern, to identify matches in the enterprise without creating alerts, click Save and dry run.

  8. 搜索并选择最多 10 个要在其中执行试运行的存储库。

  9. 准备好测试新的自定义模式后,请单击“运行”。

  10. 试运行完成后,你将看到结果示例(最多 1000 个)。 查看结果并确定任何误报结果。

    显示试运行结果的屏幕截图。

  11. 编辑新的自定义模式以修复结果的任何问题,然后测试更改,单击“保存并试运行”。

  12. 如果对新的自定义模式感到满意,请单击“发布模式”。

  13. Optionally, to enable push protection for your custom pattern, click Enable. For more information, see Push protection.

    注意

    • 若要为自定义模式启用推送保护,需要在企业级启用 secret scanning 作为推送保护。
    • 为常见的自定义模式启用推送保护可能会对参与者造成干扰。

After your pattern is created, secret scanning scans for any secrets in repositories within your organizations with GitHub Secret Protection enabled, including their entire Git history on all branches. Organization owners and repository administrators will be alerted to any secrets found, and can review the alert in the repository where the secret is found. For more information on viewing 机密扫描警报, see 管理机密扫描警报.