{"meta":{"title":"从机密扫描中排除文件夹和文件","intro":"您可以通过在存储库中配置 secret scanning 文件，自定义 secret_scanning.yml 以自动关闭在特定目录或文件中发现的机密警报。","product":"安全性和代码质量","breadcrumbs":[{"href":"/zh/code-security","title":"安全性和代码质量"},{"href":"/zh/code-security/how-tos","title":"How-tos"},{"href":"/zh/code-security/how-tos/secure-your-secrets","title":"保护机密"},{"href":"/zh/code-security/how-tos/secure-your-secrets/customize-leak-detection","title":"自定义泄漏检测"},{"href":"/zh/code-security/how-tos/secure-your-secrets/customize-leak-detection/exclude-folders-and-files","title":"排除文件夹和文件"}],"documentType":"article"},"body":"# 从机密扫描中排除文件夹和文件\n\n您可以通过在存储库中配置 secret scanning 文件，自定义 secret_scanning.yml 以自动关闭在特定目录或文件中发现的机密警报。\n\n可能存在需要将机密提交到存储库的情况，例如在文档或示例应用中提供虚假机密时。 在这些场景中，可以快速驳回警报并记录原因。 但在某些情况下，可能希望完全忽略某个目录，以避免大规模产生误报警报。 例如，你可能有一个包含多个集成的整体应用程序，其中包含一个虚拟密钥文件，可能会引发大量虚假警报，需要进行会审。\n\n您可以配置 `secret_scanning.yml` 文件，以自动关闭在特定目录中从 secret scanning 找到的警报，并将这些目录排除在推送保护之外。 这些警报将被关闭，并标记为“根据配置忽略”。\n\n## 从 用户机密扫描警报 中排除目录\n\n1. 在 GitHub 上，导航到存储库的主页面。\n2. 在文件列表上方，选择“Add file”<svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-triangle-down\" aria-label=\"The downwards-facing triangle icon\" role=\"img\"><path d=\"m4.427 7.427 3.396 3.396a.25.25 0 0 0 .354 0l3.396-3.396A.25.25 0 0 0 11.396 7H4.604a.25.25 0 0 0-.177.427Z\"></path></svg> 下拉菜单，然后单击“<svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-plus\" aria-label=\"plus\" role=\"img\"><path d=\"M7.75 2a.75.75 0 0 1 .75.75V7h4.25a.75.75 0 0 1 0 1.5H8.5v4.25a.75.75 0 0 1-1.5 0V8.5H2.75a.75.75 0 0 1 0-1.5H7V2.75A.75.75 0 0 1 7.75 2Z\"></path></svg> Create new file”\\*\\*\\*\\*\\*\\*\\*\\*。\n\n   也可在左侧的文件树状视图中单击 <svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-plus\" aria-label=\"The plus sign icon\" role=\"img\"><path d=\"M7.75 2a.75.75 0 0 1 .75.75V7h4.25a.75.75 0 0 1 0 1.5H8.5v4.25a.75.75 0 0 1-1.5 0V8.5H2.75a.75.75 0 0 1 0-1.5H7V2.75A.75.75 0 0 1 7.75 2Z\"></path></svg>。\n\n   ![仓库主页的屏幕截图，其中突出显示了“添加文件”和“加号”两个图标（如上所述以橙色框出）。](/assets/images/help/repository/add-file-buttons.png)\n3. 在“文件名”字段中，输入“.github/secret\\_scanning.yml”。\n4. 在 **“编辑新文件”** 下，键入 `paths-ignore:`，然后输入要从 secret scanning 中排除的路径。\n\n   ```yaml copy\n   paths-ignore:\n     - \"docs/**\"\n   ```\n\n   这会指示 secret scanning 自动关闭 `docs` 目录中所有内容的警报。 可以使用此示例文件作为模板来添加想要从自己的存储库中排除的文件和文件夹。\n\n   也可以使用特殊字符（如 `*`）筛选路径。 有关筛选模式的更多信息，请参阅 [GitHub Actions 的工作流语法](/zh/actions/reference/workflows-and-actions/workflow-syntax#filter-pattern-cheat-sheet)。\n\n   ```yaml copy\n   paths-ignore:\n     - \"foo/bar/*.js\"\n   ```\n\n   > \\[!NOTE]\n   >\n   > * 如果存在 `paths-ignore`超过 1,000 个条目， secret scanning 则只会从扫描中排除前 1,000 个目录。\n   > * 如果 `secret_scanning.yml` 大于 1 MB， secret scanning 将忽略整个文件。\n\n## 验证该文件夹是否已从 secret scanning 中排除\n\n1. 在已从机密扫描中排除的目录中打开一个文件\n\n2. 粘贴一个预先失效的机密或测试机密。\n\n3. 提交更改。\n\n4. 在 GitHub 上，导航到存储库的主页面。\n\n5. 在存储库名称下，单击 **<svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-shield\" aria-label=\"shield\" role=\"img\"><path d=\"M7.467.133a1.748 1.748 0 0 1 1.066 0l5.25 1.68A1.75 1.75 0 0 1 15 3.48V7c0 1.566-.32 3.182-1.303 4.682-.983 1.498-2.585 2.813-5.032 3.855a1.697 1.697 0 0 1-1.33 0c-2.447-1.042-4.049-2.357-5.032-3.855C1.32 10.182 1 8.566 1 7V3.48a1.75 1.75 0 0 1 1.217-1.667Zm.61 1.429a.25.25 0 0 0-.153 0l-5.25 1.68a.25.25 0 0 0-.174.238V7c0 1.358.275 2.666 1.057 3.86.784 1.194 2.121 2.34 4.366 3.297a.196.196 0 0 0 .154 0c2.245-.956 3.582-2.104 4.366-3.298C13.225 9.666 13.5 8.36 13.5 7V3.48a.251.251 0 0 0-.174-.237l-5.25-1.68ZM8.75 4.75v3a.75.75 0 0 1-1.5 0v-3a.75.75 0 0 1 1.5 0ZM9 10.5a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"></path></svg> Security and quality** 选项卡。如果看不到“<svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-shield\" aria-label=\"shield\" role=\"img\"><path d=\"M7.467.133a1.748 1.748 0 0 1 1.066 0l5.25 1.68A1.75 1.75 0 0 1 15 3.48V7c0 1.566-.32 3.182-1.303 4.682-.983 1.498-2.585 2.813-5.032 3.855a1.697 1.697 0 0 1-1.33 0c-2.447-1.042-4.049-2.357-5.032-3.855C1.32 10.182 1 8.566 1 7V3.48a1.75 1.75 0 0 1 1.217-1.667Zm.61 1.429a.25.25 0 0 0-.153 0l-5.25 1.68a.25.25 0 0 0-.174.238V7c0 1.358.275 2.666 1.057 3.86.784 1.194 2.121 2.34 4.366 3.297a.196.196 0 0 0 .154 0c2.245-.956 3.582-2.104 4.366-3.298C13.225 9.666 13.5 8.36 13.5 7V3.48a.251.251 0 0 0-.174-.237l-5.25-1.68ZM8.75 4.75v3a.75.75 0 0 1-1.5 0v-3a.75.75 0 0 1 1.5 0ZM9 10.5a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"></path></svg> Security and quality”选项卡，请选择 **<svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-kebab-horizontal\" aria-label=\"kebab-horizontal\" role=\"img\"><path d=\"M8 9a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3ZM1.5 9a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Zm13 0a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z\"></path></svg>** 下拉菜单，然后单击 **<svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-shield\" aria-label=\"shield\" role=\"img\"><path d=\"M7.467.133a1.748 1.748 0 0 1 1.066 0l5.25 1.68A1.75 1.75 0 0 1 15 3.48V7c0 1.566-.32 3.182-1.303 4.682-.983 1.498-2.585 2.813-5.032 3.855a1.697 1.697 0 0 1-1.33 0c-2.447-1.042-4.049-2.357-5.032-3.855C1.32 10.182 1 8.566 1 7V3.48a1.75 1.75 0 0 1 1.217-1.667Zm.61 1.429a.25.25 0 0 0-.153 0l-5.25 1.68a.25.25 0 0 0-.174.238V7c0 1.358.275 2.666 1.057 3.86.784 1.194 2.121 2.34 4.366 3.297a.196.196 0 0 0 .154 0c2.245-.956 3.582-2.104 4.366-3.298C13.225 9.666 13.5 8.36 13.5 7V3.48a.251.251 0 0 0-.174-.237l-5.25-1.68ZM8.75 4.75v3a.75.75 0 0 1-1.5 0v-3a.75.75 0 0 1 1.5 0ZM9 10.5a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"></path></svg> Security and quality**。 对于刚刚引入到文件中的机密，不应有新的打开警报。\n\n## 最佳做法\n\n最佳做法包括：\n\n* 尽量减少排除的目录数量，定义排除规则时尽可能精确。 这可确保说明尽可能清晰，且排除规则按预期生效。\n* 在 `secret_scanning.yml` 文件的注释中说明排除特定文件或文件夹的原因。 与常规代码一样，使用注释可明确意图，让他人更容易理解预期行为。\n* 定期查看 `secret_scanning.yml` 文件。 随着时间推移，某些排除规则可能不再适用，保持文件整洁和最新是良好做法。 如前所述，使用注释有助于实现这一点。\n* 告知安全团队已排除的文件和文件夹及其原因。 良好的沟通对于确保所有人达成共识、理解排除特定文件夹或文件的原因至关重要。"}