Moved
from GitLab Ultimate to GitLab
Free in 13.3.
In GitLab 14.0
, Secret Detection jobs
secret_detection_default_branch
and
secret_detection
were consolidated into one job,
secret_detection
.
People sometimes accidentally commit secrets like keys or API tokens to Git repositories.
After a sensitive value is pushed to a remote repository, anyone with access to the repository can impersonate the authorized user of the secret for malicious purposes.
Most organizations require exposed secrets to be revoked and replaced to address this risk.
Secret Detection scans your repository to help prevent your secrets from being exposed.
Secret Detection scanning works on all text files, regardless of the language or framework used.
Most Secret Detection patterns search for specific types of secrets.
Many services add prefixes or other structural details to their secrets so they can be identified if they’re leaked.
For example, GitLab
adds a
glpat-
prefix
to project, group, and personal access tokens by default.
To provide more reliable, high-confidence results, Secret Detection only looks for passwords or other unstructured secrets in specific contexts like URLs.
If the
SECRET_DETECTION_LOG_OPTS
variable is set, the secrets analyzer fetches the entire
history of the branch or reference the pipeline is being run for. Secret Detection then runs,
scanning the commit range specified.
Default branch
When Secret Detection is run on the default branch, the Git repository is treated as a plain
folder. Only the contents of the repository at the current HEAD are scanned. Commit history is not scanned.
Push event
On a push event, Secret Detection determines what commit range to scan, given the information
available in the runner. To determine the commit range, the variables
CI_COMMIT_SHA
and
CI_COMMIT_BEFORE_SHA
are important.
CI_COMMIT_SHA
is the commit at HEAD for a given branch. This variable is always set for push events.
CI_COMMIT_BEFORE_SHA
is set in most cases. However, it is not set for the first push event on
a new branch, nor for merge pipelines. Because of this, Secret Detection can’t be guaranteed
when multiple commits are committed to a new branch.
Merge request
In a merge request, Secret Detection scans every commit made on the source branch. To use this
feature, you must use the
latest
Secret Detection template
, as it supports
merge request pipelines
. Secret Detection’s
results are only available after the pipeline is completed.
The latest version of the template may include breaking changes. Use the stable template unless you
need a feature provided only in the latest template.
This method automatically prepares a merge request, with the Secret Detection template included in
the
.gitlab-ci.yml
file. You then merge the merge request to enable Secret Detection.
This method works best with no existing
.gitlab-ci.yml
file, or with a minimal configuration
file. If you have a complex GitLab configuration file it may not be parsed successfully, and an
error may occur. In that case, use the
manual
method instead.
To enable Secret Detection:
On the top bar, select
Main menu > Projects
and find your project.
On the left sidebar, select
Security and Compliance > Security configuration
.
In the
Secret Detection
row, select
Configure with a merge request
.
All configuration of GitLab security scanning tools should be tested in a merge request before
merging these changes to the default branch. Failure to do so can give unexpected results,
including a large number of false positives.
To override a job definition, (for example, change properties like
variables
or
dependencies
),
declare a job with the same name as the secret detection job to override. Place this new job after
the template inclusion and specify any additional keys under it.
In the following example
extract
of a
.gitlab-ci.yml
file:
In the
secret_detection
job, the CI/CD variable
SECRET_DETECTION_HISTORIC_SCAN
is set to
true
. Because the template is evaluated before the pipeline configuration, the last mention of
the variable takes precedence.
Exclude vulnerabilities from output based on the paths. The paths are a comma-separated list of patterns. Patterns can be globs (see
doublestar.Match
for supported patterns), or file or folder paths (for example,
doc,spec
). Parent directories also match patterns.
Introduced
in GitLab 13.3.
SECRET_DETECTION_HISTORIC_SCAN
false
Flag to enable a historic Gitleaks scan.
SECRET_DETECTION_IMAGE_SUFFIX
””
Suffix added to the image name. If set to
-fips
,
FIPS-enabled
images are used for scan. See
Use FIPS-enabled images
for more details.
Introduced
in GitLab 14.10.
The default scanner images are built off a base Alpine image for size and maintainability. GitLab
offers
Red Hat UBI
versions of the images that are FIPS-enabled.
To use the FIPS-enabled images, either:
Set the
SECRET_DETECTION_IMAGE_SUFFIX
CI/CD variable to
-fips
.
Add the
-fips
extension to the default image name.
In the following
file
passthrough example, the string
glpat-1234567890abcdefghij
is ignored by Secret Detection. That GitLab personal access token (PAT) is used in test cases. Detection of it would be a false positive.
The
secret-detection-ruleset.toml
file defines that the configuration in
extended-gitleaks-config.toml
file is to be included. The
extended-gitleaks-config.toml
file defines the custom Gitleaks configuration. The
allowlist
stanza defines a regular expression that matches the secret that is to be ignored (“allowed”).
# extended-gitleaks-config.tomltitle="extension of gitlab's default gitleaks config"[extend]# Extends default packaged pathpath="/gitleaks.toml"[allowlist]description="allow list of test tokens to ignore in detection"regexTarget="match"regexes=['''glpat-1234567890abcdefghij''',
The
SECRET_DETECTION_RULESET_GIT_REFERENCE
variable uses an SCP-style syntax for specifying a URI,
optional authentication, and optional Git SHA. The variable uses the following format:
Loading local project configuration takes precedence over SECRET_DETECTION_RULESET_GIT_REFERENCE values.
The following example includes the Secret Detection template in a project to be scanned and specifies
the
SECRET_DETECTION_RULESET_GIT_REFERENCE
variable for referencing a separate project configuration.
When you create an issue, propose a merge request, or write a comment, you might accidentally post a sensitive value.
For example, you might paste in the details of an API request or an environment variable that contains an authentication token.
GitLab checks if the text of your issue description, merge request description, comment, or reply contains a sensitive token.
If a token is found, a warning message is displayed. You can then edit your message before posting it.
This check happens in your browser before the message is sent to the server.
The check is always on; you don’t have to set it up.
Your text is checked for the following secret types:
This feature is separate from Secret Detection scanning, which checks your Git repository for leaked secrets.
Issue 405147
tracks efforts to align these two types of protection.
Set the logging level to
debug
when you need diagnostic information in a Secret Detection job log.
Debug logging can be a serious security risk. The output may contain the content of environment
variables and other secrets available to the job. The output is uploaded to the GitLab server and
visible in job logs.
In the
.gitlab-ci.yml
file, set the
SECURE_LOG_LEVEL
CI/CD variable to
debug
.
Run the Secret Detection job.
Analyze the content of the Secret Detection job.
In the
.gitlab-ci.yml
file, set the
SECURE_LOG_LEVEL
CI/CD variable to
info
(default).
Warning:
gl-secret-detection-report.json: no matching files
For example, you could have a pipeline triggered from a merge request containing 60 commits and the
GIT_DEPTH
variable set to less than 60. In that case the Secret Detection job fails because the
clone is not deep enough to contain all of the relevant commits. To verify the current value, see
pipeline configuration
.
To confirm this as the cause of the error, set the
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.
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
To resolve the issue, set the
GIT_DEPTH
CI/CD variable
to a higher value. To apply this only to the Secret Detection job, the following can be added to
your
.gitlab-ci.yml
file:
To resolve the issue, make sure to correctly
set your default branch
on your repository. You should set it to a branch that has related history with the branch you run
the
secret-detection
job on.
exec /bin/sh: exec format error
message in job log
If you didn't find what you were looking for,
search the docs
.
If you want help with something specific and could use community support,
post on the GitLab forum
.
For problems setting up or using this feature (depending on your GitLab
subscription).