💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Secret Detection > 原文:[https://docs.gitlab.com/ee/user/application_security/secret_detection/](https://docs.gitlab.com/ee/user/application_security/secret_detection/) * [Overview](#overview) * [Use cases](#use-cases) * [Requirements](#requirements) * [Configuration](#configuration) * [Using the SAST Template](#using-the-sast-template) * [Customizing settings](#customizing-settings) * [Available variables](#available-variables) * [Logging Level](#logging-level) * [Full History Secret Scan](#full-history-secret-scan) # Secret Detection[](#secret-detection-ultimate "Permalink") [Introduced](https://about.gitlab.com/releases/2019/03/22/gitlab-11-9-released/#detect-secrets-and-credentials-in-the-repository) in [GitLab Ultimate](https://about.gitlab.com/pricing/) 11.9. ## Overview[](#overview "Permalink") 开发应用程序时经常出现的问题是,开发人员可能会无意间将秘密和凭据提交到其远程存储库. 如果其他人可以访问源,或者项目是公开的,则敏感信息将被公开,恶意用户可以利用这些信息来访问诸如部署环境之类的资源. GitLab 11.9 包含一个称为"秘密检测"的新检查. 它扫描存储库的内容以查找 API 密钥和其他不应存在的信息. manbetx 客户端打不开显示识别的秘密作为 SAST 报告的一部分在几个地方: * [Security Dashboard](../security_dashboard/) * Pipelines’ **Security** tab * 合并请求小部件中的报告 [![Secret Detection in merge request widget](https://img.kancloud.cn/63/aa/63aa45dd632960fc086fa3bd929194e2_908x185.png)](img/secret_detection_v13_2.png) ## Use cases[](#use-cases "Permalink") * 检测密钥,密码和 API 令牌等机密信息的意外提交. * 对存储库的完整历史记录执行一次或重复扫描以查找机密信息. ## Requirements[](#requirements "Permalink") 要运行检测的秘密工作,默认情况下,你需要 GitLab 亚军与[`docker`](https://docs.gitlab.com/runner/executors/docker.html)或[`kubernetes`](https://docs.gitlab.com/runner/install/kubernetes.html)执行. 如果您在 GitLab.com 上使用共享的 Runners,则默认启用该功能. **注意:**目前我们的秘密检测作业需要 Linux 容器类型. Windows 容器尚不支持.**注意:**如果使用自己的 Runners,请确保安装的 Docker 版本**不是** `19.03.0` . 有关详细[信息](../sast#error-response-from-daemon-error-processing-tar-file-docker-tar-relocation-error) ,请参见[故障排除信息](../sast#error-response-from-daemon-error-processing-tar-file-docker-tar-relocation-error) . ## Configuration[](#configuration "Permalink") **注意:**在 GitLab 13.1 中,秘密检测被拆分为自己的 CI / CD 模板. 秘密检测是在`secret-detection`作业期间由[特定的分析器](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Security/Secret-Detection.gitlab-ci.yml)执行的. 无论您的应用程序使用哪种编程语言,它都可以运行. 秘密检测分析器包括[Gitleaks](https://github.com/zricethezav/gitleaks)和[TruffleHog](https://github.com/dxa4481/truffleHog)检查. ***Note:** The Secret Detection analyzer will ignore "Password in URL" vulnerabilities if the password begins with a dollar sign ( `$` ) as this likely indicates the password being used is an environment variable. **注意:**如果密码以美元符号( `$` )开头,则秘密检测分析器将忽略" URL 中的密码"漏洞,因为这很可能表明所使用的密码是环境变量. For example, `https://username:$password@example.com/path/to/repo` won't be detected, whereas `https://username:password@example.com/path/to/repo` would be detected. 例如,将不会检测到`https://username:$password@example.com/path/to/repo` ,而将检测到`https://username:password@example.com/path/to/repo` .***注意:**如果您使用的是[Auto DevOps](../../../topics/autodevops/index.html)提供的[自动](../../../topics/autodevops/index.html) [秘密检测,](../../../topics/autodevops/stages.html#auto-secret-detection-ultimate)则不必按照本节中的说明手动配置秘密检测. 要为 GitLab 13.1 和更高版本启用 Secret Detection,您必须包括`Secret-Detection.gitlab-ci.yml`模板,该模板作为 GitLab 安装的一部分提供. 对于 11.9 之前的 GitLab 版本,您可以复制和使用该模板中定义的作业. 将以下内容添加到您的`.gitlab-ci.yml`文件中: ``` include: - template: Secret-Detection.gitlab-ci.yml ``` 包含的模板在 CI / CD 管道中创建"秘密检测"作业,并扫描项目的源代码中的秘密. 结果将保存为" [秘密检测"报告工件](../../../ci/pipelines/job_artifacts.html#artifactsreportssecret_detection-ultimate) ,您以后可以下载和分析该[工件](../../../ci/pipelines/job_artifacts.html#artifactsreportssecret_detection-ultimate) . 由于实施限制,我们始终采用最新的秘密检测工件. ### Using the SAST Template[](#using-the-sast-template "Permalink") 在 GitLab 13.1 之前,秘密检测是[SAST 配置的](../sast#configuration)一部分. 如果您已经在 GitLab 13.1 之前为您的应用配置了启用 SAST,则无需手动配置它. **计划的弃用:**在以后的 GitLab 版本中,将不建议使用 SAST 模板配置秘密检测. 请开始使用`Secret-Detection.gitlab-ci.yml`以防止将来出现问题. 我们制作了一个[视频,指导您完成过渡](https://www.youtube.com/watch?v=W2tjcQreDwQ)到此新模板的过程.观看视频: [历史秘密扫描演练](https://www.youtube.com/watch?v=W2tjcQreDwQ) . <figure class="video-container"><iframe src="https://www.youtube.com/embed/W2tjcQreDwQ" frameborder="0" allowfullscreen=""></iframe></figure> 使用 SAST 模板时,秘密检测由[特定分析器](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Security/SAST.gitlab-ci.yml#L180)在`sast`作业期间执行. 它的运行与应用程序的编程语言无关,并且您无需更改 CI / CD 配置文件即可启用它. 结果可在 SAST 报告中获得. ### Customizing settings[](#customizing-settings "Permalink") 可以使用`.gitlab-ci.yml`的[`variables`](../../../ci/yaml/README.html#variables)参数通过[环境变量](#available-variables)更改秘密检测扫描设置. 要覆盖作业定义(例如,更改`variables`或`dependencies`类的属性),请声明与要覆盖的 SAST 作业同名的作业. 将此新作业放置在包含模板之后,并在其下指定其他任何键. 在下面的示例中,我们包括"秘密检测"模板,同时使用`SECRET_DETECTION_HISTORIC_SCAN`变量将`secret_detection`作业覆盖为`true` : ``` include: - template: Secret-Detection.gitlab-ci.yml secret_detection: variables: SECRET_DETECTION_HISTORIC_SCAN: "true" ``` 因为模板是[在](../../../ci/yaml/README.html#include)管道配置[之前进行评估](../../../ci/yaml/README.html#include)的,所以最后提到的变量优先. **弃用:**从 GitLab 13.0 开始,不再支持[`only`和`except`](../../../ci/yaml/README.html#onlyexcept-basic)的使用. 覆盖模板时,必须使用[`rules`](../../../ci/yaml/README.html#rules) . #### Available variables[](#available-variables "Permalink") 可以通过定义可用变量来自定义秘密检测: | 环境变量 | 默认值 | Description | | --- | --- | --- | | `SECRET_DETECTION_COMMIT_FROM` | - | 提交 Gitleaks 扫描始于. | | `SECRET_DETECTION_COMMIT_TO` | - | Gitleaks 扫描的提交结束于. | | `SECRET_DETECTION_HISTORIC_SCAN` | false | 标记以启用历史性的 Gitleaks 扫描. | ### Logging Level[](#logging-level "Permalink") 您可以通过设置`SECURE_LOG_LEVEL` env var 来控制日志的详细程度. 默认设置为`info` ,您可以将其设置为以下任意级别: * `fatal` * `error` * `warn` * `info` * `debug` ## Full History Secret Scan[](#full-history-secret-scan "Permalink") GitLab 12.11 引入了对扫描存储库完整历史记录的支持. 当您首次在存储库中启用秘密检测并且想要执行完整的秘密扫描时,此新功能特别有用. 对整个历史记录进行秘密扫描可能会花费很长时间,尤其是对于 Git 历史记录较长的大型存储库. 我们建议不要将此变量设置为常规作业定义的一部分. 可以设置一个新的配置变量( [`SECRET_DETECTION_HISTORIC_SCAN`](../sast/#vulnerability-filters) )来更改 GitLab 秘密检测扫描的行为,使其在存储库的整个 Git 历史记录上运行. 我们创建了一个[简短的视频演练,](https://youtu.be/wDtc_K00Y0A)展示了如何执行完整的历史秘密扫描. 观看视频: [历史秘密扫描演练](https://www.youtube.com/watch?v=wDtc_K00Y0A) . <figure class="video-container"><iframe src="https://www.youtube.com/embed/wDtc_K00Y0A" frameborder="0" allowfullscreen=""></iframe></figure>