doc/user/application_security/secret_detection/pipeline/tutorial.md
If your application uses external resources, you usually need to authenticate your application with a secret, like a token or key. If a secret is pushed to a remote repository, anyone with access to the repository can impersonate you or your application.
Pipeline secret detection uses a CI/CD job to check your GitLab project for secrets. In this tutorial, you'll create a project, configure pipeline secret detection, and learn how to analyze its results:
Before you begin this tutorial, make sure you have the following:
First, create a project and enable secret detection:
A new project is created and initialized with a README and .gitlab-ci.yml file.
The CI/CD configuration includes the Security/Secret-Detection.gitlab-ci.yml template,
which enables pipeline secret detection in the project.
Pipeline secret detection runs in a CI/CD job called secret_detection.
Scan results are written to the CI/CD job log. Each scan also produces a comprehensive report as a job artifact.
To check the results of the most recent scan:
secret_detection job. If you haven't run a new pipeline, there should be only one job.no leaks found.So far, you have used pipeline secret detection to scan commits in the default branch. But to analyze commits in merge requests before you merge them to the default branch, you need to enable merge request pipelines.
To do this:
Add the following lines to your .gitlab-ci.yml file:
variables:
AST_ENABLE_MR_PIPELINES: "true"
Save the changes and commit them to the main branch of your project.
Next, let's complicate the output of the job by "leaking" a fake secret in a merge request:
Check out a new branch:
git checkout -b pipeline-sd-tutorial
Edit your project README and add the following lines.
Be sure to remove the spaces before and after the - to match the exact format of a personal access token:
# To make the example work, remove
# the spaces before and after the dash:
glpat - 12345678901234567890
Commit and push your changes, then open a merge request to merge them to the default branch.
A merge request pipeline is automatically run.
Wait for the pipeline to finish, then check the job log. You should see WRN leaks found: 1.
Download the job artifact and check to make sure it contains the following information:
The secret type. In this example, the type is "GitLab personal access token".
A description of what the secret is used for, with some steps you can take to remediate the leak.
The severity of the leak. Because personal access tokens can be used to impersonate users on GitLab.com, this leak is Critical.
The raw text of the secret.
Some information about where the secret is located:
"file": "README.md",
"line_start": 97,
"line_end": 97,
In this example, the secret is on line 97 of the file README.md.
{{< details >}}
{{< /details >}}
A secret detected on a non-default branch is called a "finding." When a finding is merged to the default branch, it becomes a "vulnerability."
The merge request security widget displays a list of findings that could become vulnerabilities if the merge request is merged.
To view the widget:
For a detailed view of all the findings in a merge request, select View all pipeline findings.
{{< details >}}
{{< /details >}}
On GitLab Ultimate, job output is also written to:
To demonstrate how you can triage a secret by using the UI, let's create a vulnerability and change its status in the vulnerability report:
Merge the MR you created in the last step, then wait for the pipeline to finish.
The fake secret is added to main, which causes the finding to become a vulnerability.
In the left sidebar, select Secure > Vulnerability report.
Select the vulnerability's Description to view:
Select Edit vulnerability > Change status.
The vulnerability no longer appears on the front page of the vulnerability report.
If you add a secret to a remote repository, that secret is no longer secure and must be revoked as soon as possible. You should revoke and replace secrets even if they haven't been merged to your default branch.
The exact steps you take to remediate a leak will depend on your organization's security policies, but at a minimum, you should:
Specific remediation guidance is written to the secret-detection job log, and is available on the vulnerability report details page.