debian-mirror-gitlab/doc/user/application_security/secret_detection/index.md

367 lines
17 KiB
Markdown
Raw Normal View History

2020-05-24 23:13:21 +05:30
---
type: reference, howto
2020-06-23 00:09:42 +05:30
stage: Secure
group: Static Analysis
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-05-24 23:13:21 +05:30
---
2021-04-17 20:07:23 +05:30
# Secret Detection **(FREE)**
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
> - [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.
> - Made [available in all tiers](https://gitlab.com/gitlab-org/gitlab/-/issues/222788) in 13.3.
2020-05-24 23:13:21 +05:30
A recurring problem when developing applications is that developers may unintentionally commit
secrets and credentials to their remote repositories. If other people have access to the source,
or if the project is public, the sensitive information is then exposed and can be leveraged by
malicious users to gain access to resources like deployment environments.
GitLab 11.9 includes a new check called Secret Detection. It scans the content of the repository
to find API keys and other information that should not be there.
2020-10-24 23:57:45 +05:30
GitLab displays identified secrets visibly in a few places:
2020-05-24 23:13:21 +05:30
- [Security Dashboard](../security_dashboard/)
- Pipelines' **Security** tab
- Report in the merge request widget
2020-07-28 23:09:34 +05:30
![Secret Detection in merge request widget](img/secret_detection_v13_2.png)
2020-05-24 23:13:21 +05:30
## Use cases
2020-06-23 00:09:42 +05:30
- Detecting unintentional commit of secrets like keys, passwords, and API tokens.
2020-05-24 23:13:21 +05:30
- Performing a single or recurring scan of the full history of your repository for secrets.
2021-01-29 00:20:46 +05:30
## Supported secrets
Secret Detection detects a variety of common secrets by default. You can also customize the secret detection patterns using [custom rulesets](#custom-rulesets).
The [default ruleset provided by Gitleaks](https://gitlab.com/gitlab-org/security-products/analyzers/secrets/-/blob/master/gitleaks/gitleaks.toml) includes the following key types:
- Cloud services:
- Amazon Web Services (AWS)
- Google Cloud Platform (GCP)
2021-02-22 17:27:13 +05:30
- Heroku API
- Encryption keys:
2021-01-29 00:20:46 +05:30
- PKCS8
- RSA
- SSH
- PGP
2021-02-22 17:27:13 +05:30
- DSA
- EC
2021-01-29 00:20:46 +05:30
- Social media platforms:
- Facebook API
- Twitter API
- Cloud SaaS vendors:
- GitHub API
2021-03-08 18:12:59 +05:30
- Shopify API
2021-01-29 00:20:46 +05:30
- Slack Token
2021-02-22 17:27:13 +05:30
- Slack Webhook
2021-01-29 00:20:46 +05:30
- Stripe API
2021-02-22 17:27:13 +05:30
- Twilio API
2021-01-29 00:20:46 +05:30
- Generic API key strings starting with `api-`
2021-02-22 17:27:13 +05:30
- Password in URL
- U.S. Social Security Number
2021-01-29 00:20:46 +05:30
2020-06-23 00:09:42 +05:30
## Requirements
To run Secret Detection jobs, by default, you need GitLab Runner with the
[`docker`](https://docs.gitlab.com/runner/executors/docker.html) or
[`kubernetes`](https://docs.gitlab.com/runner/install/kubernetes.html) executor.
2020-11-24 15:15:51 +05:30
If you're using the shared runners on GitLab.com, this is enabled by default.
2020-06-23 00:09:42 +05:30
2021-02-22 17:27:13 +05:30
WARNING:
2021-01-03 14:25:43 +05:30
Our Secret Detection jobs expect a Linux container type. Windows containers are not supported.
2020-06-23 00:09:42 +05:30
2021-02-22 17:27:13 +05:30
WARNING:
2020-11-24 15:15:51 +05:30
If you use your own runners, make sure the Docker version installed
2020-06-23 00:09:42 +05:30
is **not** `19.03.0`. See [troubleshooting information](../sast#error-response-from-daemon-error-processing-tar-file-docker-tar-relocation-error) for details.
2020-10-24 23:57:45 +05:30
### Making Secret Detection available to all GitLab tiers
To make Secret Detection available to as many customers as possible, we have enabled it for all GitLab tiers.
However not all features are available on every tier. See the breakdown below for more details.
#### Summary of features per tier
Different features are available in different [GitLab tiers](https://about.gitlab.com/pricing/),
as shown in the following table:
2021-03-11 19:13:27 +05:30
| Capability | In Free | In Ultimate |
|:----------------------------------------------------------------|:--------------------|:-------------------|
| [Configure Secret Detection Scanners](#configuration) | **{check-circle}** | **{check-circle}** |
| [Customize Secret Detection Settings](#customizing-settings) | **{check-circle}** | **{check-circle}** |
| View [JSON Report](../sast/index.md#reports-json-format) | **{check-circle}** | **{check-circle}** |
| Presentation of JSON Report in Merge Request | **{dotted-circle}** | **{check-circle}** |
2020-10-24 23:57:45 +05:30
| [Interaction with Vulnerabilities](../vulnerabilities/index.md) | **{dotted-circle}** | **{check-circle}** |
2021-03-11 19:13:27 +05:30
| [Access to Security Dashboard](../security_dashboard/index.md) | **{dotted-circle}** | **{check-circle}** |
2020-10-24 23:57:45 +05:30
2020-05-24 23:13:21 +05:30
## Configuration
2021-01-03 14:25:43 +05:30
> GitLab 13.1 splits Secret Detection from the [SAST configuration](../sast#configuration) into its own CI/CD template. If you're using GitLab 13.0 or earlier and SAST is enabled, then Secret Detection is already enabled.
2020-05-24 23:13:21 +05:30
2020-06-23 00:09:42 +05:30
Secret Detection is performed by a [specific analyzer](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Security/Secret-Detection.gitlab-ci.yml)
2021-01-03 14:25:43 +05:30
during the `secret-detection` job. It runs regardless of your app's programming language.
2020-05-24 23:13:21 +05:30
2021-04-17 20:07:23 +05:30
The Secret Detection analyzer includes [Gitleaks](https://github.com/zricethezav/gitleaks) checks.
2020-05-24 23:13:21 +05:30
2021-01-03 14:25:43 +05:30
Note that the Secret Detection analyzer ignores Password-in-URL vulnerabilities if the password
begins with a dollar sign (`$`), as this likely indicates the password is an environment variable.
For example, `https://username:$password@example.com/path/to/repo` isn't detected, while
`https://username:password@example.com/path/to/repo` is.
2020-05-24 23:13:21 +05:30
2021-02-22 17:27:13 +05:30
NOTE:
2021-01-03 14:25:43 +05:30
You don't have to configure Secret Detection manually as shown in this section if you're using
2021-04-17 20:07:23 +05:30
[Auto Secret Detection](../../../topics/autodevops/stages.md#auto-secret-detection),
2020-06-23 00:09:42 +05:30
provided by [Auto DevOps](../../../topics/autodevops/index.md).
2021-01-03 14:25:43 +05:30
To enable Secret Detection for GitLab 13.1 and later, you must include the
`Secret-Detection.gitlab-ci.yml` template that's provided as a part of your GitLab installation. For
GitLab versions earlier than 11.9, you can copy and use the job as defined in that template.
2020-06-23 00:09:42 +05:30
Add the following to your `.gitlab-ci.yml` file:
```yaml
include:
2021-02-22 17:27:13 +05:30
- template: Security/Secret-Detection.gitlab-ci.yml
2020-06-23 00:09:42 +05:30
```
The included template creates Secret Detection jobs in your CI/CD pipeline and scans
your project's source code for secrets.
The results are saved as a
2020-11-24 15:15:51 +05:30
[Secret Detection report artifact](../../../ci/pipelines/job_artifacts.md#artifactsreportssecret_detection)
2020-06-23 00:09:42 +05:30
that you can later download and analyze. Due to implementation limitations, we
always take the latest Secret Detection artifact available.
2021-01-29 00:20:46 +05:30
### Post-processing
> [Introduced](https://gitlab.com/groups/gitlab-org/-/epics/4639) in GitLab 13.6.
2021-02-22 17:27:13 +05:30
Upon detection of a secret, GitLab supports post processing hooks. These can be used to take actions like notifying the cloud service who issued the secret. The cloud provider can confirm the credentials and take remediation actions like revoking or reissuing a new secret and notifying the creator of the secret. Post-processing workflows vary by supported cloud providers.
2021-01-29 00:20:46 +05:30
GitLab currently supports post-processing for following service providers:
- Amazon Web Services (AWS)
2021-02-22 17:27:13 +05:30
Third party cloud and SaaS providers can [express integration interest by filling out this form](https://forms.gle/wWpvrtLRK21Q2WJL9). Learn more about the [technical details of post-processing secrets](https://gitlab.com/groups/gitlab-org/-/epics/4639).
2021-01-29 00:20:46 +05:30
2020-06-23 00:09:42 +05:30
### Customizing settings
2021-03-11 19:13:27 +05:30
The Secret Detection scan settings can be changed through [CI/CD variables](#available-variables)
2020-06-23 00:09:42 +05:30
by using the
[`variables`](../../../ci/yaml/README.md#variables) parameter in `.gitlab-ci.yml`.
To override a job definition, (for example, change properties like `variables` or `dependencies`),
declare a job with the same name as the SAST job to override. Place this new job after the template
inclusion and specify any additional keys under it.
2021-03-11 19:13:27 +05:30
WARNING:
Beginning in GitLab 13.0, the use of [`only` and `except`](../../../ci/yaml/README.md#onlyexcept-basic)
is no longer supported. When overriding the template, you must use [`rules`](../../../ci/yaml/README.md#rules) instead.
#### GIT_DEPTH
The [`GIT_DEPTH` CI/CD variable](../../../ci/runners/README.md#shallow-cloning) affects Secret Detection.
The Secret Detection analyzer relies on generating patches between commits to scan content for
secrets. If you override the default, ensure the value is greater than 1. If the number of commits
in an MR is greater than the GIT_DEPTH value, Secret Detection will [fail to detect secrets](#error-couldnt-run-the-gitleaks-command-exit-status-2).
#### Custom settings example
2020-06-23 00:09:42 +05:30
In the following example, we include the Secret Detection template and at the same time we
2021-03-11 19:13:27 +05:30
override the `secret_detection` job with the `SECRET_DETECTION_HISTORIC_SCAN` CI/CD variable to `true`:
2020-06-23 00:09:42 +05:30
```yaml
include:
2021-02-22 17:27:13 +05:30
- template: Security/Secret-Detection.gitlab-ci.yml
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
secret_detection:
2020-06-23 00:09:42 +05:30
variables:
2020-07-28 23:09:34 +05:30
SECRET_DETECTION_HISTORIC_SCAN: "true"
2020-06-23 00:09:42 +05:30
```
Because the template is [evaluated before](../../../ci/yaml/README.md#include)
the pipeline configuration, the last mention of the variable takes precedence.
#### Available variables
2021-03-11 19:13:27 +05:30
Secret Detection can be customized by defining available CI/CD variables:
2020-06-23 00:09:42 +05:30
2021-03-11 19:13:27 +05:30
| CI/CD variable | Default value | Description |
|-----------------------------------|---------------|-------------|
| `SECRET_DETECTION_COMMIT_FROM` | - | The commit a Gitleaks scan starts at. |
| `SECRET_DETECTION_COMMIT_TO` | - | The commit a Gitleaks scan ends at. |
| `SECRET_DETECTION_EXCLUDED_PATHS` | "" | Exclude vulnerabilities from output based on the paths. This is a comma-separated list of patterns. Patterns can be globs, or file or folder paths (for example, `doc,spec` ). Parent directories also match patterns. [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/225273) in GitLab 13.3. |
| `SECRET_DETECTION_HISTORIC_SCAN` | false | Flag to enable a historic Gitleaks scan. |
2020-06-23 00:09:42 +05:30
2021-01-29 00:20:46 +05:30
### Custom rulesets **(ULTIMATE)**
2021-01-03 14:25:43 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/211387) in GitLab 13.5.
You can customize the default secret detection rules provided with GitLab.
2021-04-17 20:07:23 +05:30
Customization allows replace the default secret detection rules with rules that you define.
2021-01-03 14:25:43 +05:30
To create a custom ruleset:
1. Create a `.gitlab` directory at the root of your project, if one doesn't already exist.
1. Create a custom ruleset file named `secret-detection-ruleset.toml` in the `.gitlab` directory.
1. In the `secret-detection-ruleset.toml` file, do one of the following:
- Define a custom ruleset:
```toml
[secrets]
description = 'secrets custom rules configuration'
[[secrets.passthrough]]
type = "raw"
target = "gitleaks.toml"
value = """\
title = "gitleaks config"
# add regexes to the regex table
[[rules]]
description = "Test for Raw Custom Rulesets"
regex = '''Custom Raw Ruleset T[est]{3}'''
"""
```
- Provide the name of the file containing a custom ruleset:
```toml
[secrets]
description = 'secrets custom rules configuration'
[[secrets.passthrough]]
type = "file"
target = "gitleaks.toml"
value = "config/gitleaks.toml"
```
2020-10-24 23:57:45 +05:30
### Logging level
2021-03-11 19:13:27 +05:30
To control the verbosity of logs set the `SECURE_LOG_LEVEL` CI/CD variable. Messages of this logging level or higher are output. [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/10880) in GitLab 13.1.
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
From highest to lowest severity, the logging levels are:
2020-07-28 23:09:34 +05:30
- `fatal`
- `error`
- `warn`
2020-10-24 23:57:45 +05:30
- `info` (default)
2020-07-28 23:09:34 +05:30
- `debug`
2020-05-24 23:13:21 +05:30
## Full History Secret Scan
2020-06-23 00:09:42 +05:30
GitLab 12.11 introduced support for scanning the full history of a repository. This new functionality
2020-05-24 23:13:21 +05:30
is particularly useful when you are enabling Secret Detection in a repository for the first time and you
want to perform a full secret scan. Running a secret scan on the full history can take a long time,
2021-03-11 19:13:27 +05:30
especially for larger repositories with lengthy Git histories. We recommend not setting this CI/CD variable
2020-06-23 00:09:42 +05:30
as part of your normal job definition.
2020-05-24 23:13:21 +05:30
2021-04-17 20:07:23 +05:30
A new configuration variable ([`SECRET_DETECTION_HISTORIC_SCAN`](#available-variables))
2020-05-24 23:13:21 +05:30
can be set to change the behavior of the GitLab Secret Detection scan to run on the entire Git history of a repository.
We have created a [short video walkthrough](https://youtu.be/wDtc_K00Y0A) showcasing how you can perform a full history secret scan.
<div class="video-fallback">
See the video: <a href="https://www.youtube.com/watch?v=wDtc_K00Y0A">Walkthrough of historical secret scan</a>.
</div>
<figure class="video-container">
<iframe src="https://www.youtube.com/embed/wDtc_K00Y0A" frameborder="0" allowfullscreen="true"> </iframe>
</figure>
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
## Running Secret Detection in an offline environment
For self-managed GitLab instances in an environment with limited, restricted, or intermittent access
to external resources through the internet, some adjustments are required for the Secret Detection job to
run successfully. For more information, see [Offline environments](../offline_deployments/index.md).
### Requirements for offline Secret Detection
To use Secret Detection in an offline environment, you need:
- GitLab Runner with the [`docker` or `kubernetes` executor](#requirements).
- A Docker Container Registry with locally available copy of Secret Detection [analyzer](https://gitlab.com/gitlab-org/security-products/analyzers) images.
- Configure certificate checking of packages (optional).
GitLab Runner has a [default `pull policy` of `always`](https://docs.gitlab.com/runner/executors/docker.html#using-the-always-pull-policy),
meaning the runner tries to pull Docker images from the GitLab container registry even if a local
copy is available. The GitLab Runner [`pull_policy` can be set to `if-not-present`](https://docs.gitlab.com/runner/executors/docker.html#using-the-if-not-present-pull-policy)
in an offline environment if you prefer using only locally available Docker images. However, we
recommend keeping the pull policy setting to `always` if not in an offline environment, as this
enables the use of updated scanners in your CI/CD pipelines.
2021-01-03 14:25:43 +05:30
### Make GitLab Secret Detection analyzer image available inside your Docker registry
Import the following default Secret Detection analyzer images from `registry.gitlab.com` into your
[local Docker container registry](../../packages/container_registry/index.md):
```plaintext
registry.gitlab.com/gitlab-org/security-products/analyzers/secrets:3
```
The process for importing Docker images into a local offline Docker registry depends on
**your network security policy**. Please consult your IT staff to find an accepted and approved
process by which external resources can be imported or temporarily accessed. Note that these scanners are [updated periodically](../index.md#maintenance-and-update-of-the-vulnerabilities-database)
with new definitions, so consider if you're able to make periodic updates yourself.
For details on saving and transporting Docker images as a file, see Docker's documentation on
[`docker save`](https://docs.docker.com/engine/reference/commandline/save/), [`docker load`](https://docs.docker.com/engine/reference/commandline/load/),
[`docker export`](https://docs.docker.com/engine/reference/commandline/export/), and [`docker import`](https://docs.docker.com/engine/reference/commandline/import/).
#### If support for Custom Certificate Authorities are needed
Support for custom certificate authorities was introduced in the following versions.
| Analyzer | Version |
| -------- | ------- |
| secrets | [v3.0.0](https://gitlab.com/gitlab-org/security-products/analyzers/secrets/-/releases/v3.0.0) |
2021-03-11 19:13:27 +05:30
### Set Secret Detection CI/CD variables to use local Secret Detection analyzer
2021-02-22 17:27:13 +05:30
Add the following configuration to your `.gitlab-ci.yml` file. You must replace
`SECURE_ANALYZERS_PREFIX` to refer to your local Docker container registry:
```yaml
include:
- template: Security/Secret-Detection.gitlab-ci.yml
variables:
SECURE_ANALYZERS_PREFIX: "localhost:5000/analyzers"
```
The Secret Detection job should now use local copies of the Secret Detection analyzer to scan your code and generate
security reports without requiring internet access.
2021-01-03 14:25:43 +05:30
## Troubleshooting
### Getting warning message `gl-secret-detection-report.json: no matching files`
For information on this, see the [general Application Security troubleshooting section](../../../ci/pipelines/job_artifacts.md#error-message-no-files-to-upload).
2021-02-22 17:27:13 +05:30
### Error: `Couldn't run the gitleaks command: exit status 2`
2021-03-11 19:13:27 +05:30
If a pipeline is triggered from a Merge Request containing 60 commits while the `GIT_DEPTH` variable
is set to 50 (a [project default](../../../ci/pipelines/settings.md#git-shallow-clone)),
the Secret Detection job fails as the clone is not deep enough to contain all of the
relevant commits.
2021-02-22 17:27:13 +05:30
2021-03-11 19:13:27 +05:30
To confirm this as the cause of the error, set the
[logging level](../../application_security/secret_detection/index.md#logging-level) to `debug`, then
rerun the pipeline. The logs should look similar to the following example. The text "object not
found" is a symptom of this error.
2021-02-22 17:27:13 +05:30
```plaintext
ERRO[2020-11-18T18:05:52Z] object not found
[ERRO] [secrets] [2020-11-18T18:05:52Z] ▶ Couldn't run the gitleaks command: exit status 2
[ERRO] [secrets] [2020-11-18T18:05:52Z] ▶ Gitleaks analysis failed: exit status 2
```
2021-03-11 19:13:27 +05:30
To resolve the issue, set the [`GIT_DEPTH` CI/CD variable](../../../ci/runners/README.md#shallow-cloning)
to a higher value. To apply this only to the Secret Detection job, the following can be added to
your `.gitlab-ci.yml` file:
2021-02-22 17:27:13 +05:30
```yaml
secret_detection:
variables:
GIT_DEPTH: 100
```