Secret detection

Catch a committed token, and why deleting it is not enough.

Intermediate12 min · lesson 12 of 17
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (19 files, 7 KB): secretscan.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-gitlab/secretscan.tar.gz && tar -xzf secretscan.tar.gz, which creates ~/lab/secretscan/. SHA-256: 9bc478297eb15b5bf22987a669af05292836bcb81ef58ccd382a17b81a733ee6

Someone pastes a token into a settings file to test something quickly, commits, and pushes. A minute later they notice and delete it in the next commit. The file is clean, the merge request diff is clean, and the token is still in the repository for anyone who clones it. Secret detection catches the first part. This lesson is also about the second part, which no scanner can undo.

Secret detection on every push

app/.gitlab-ci.yml
# GitLab's secret detection template on the Free tier, plus a gate that fails on what it finds.
include:
- template: Jobs/Secret-Detection.gitlab-ci.yml
stages: [test, gate]
secrets-gate:
stage: gate
image: secopslog-py:3.15
needs:
- job: secret_detection
artifacts: true
script:
- python gate.py gl-secret-detection-report.json Critical High

The template adds a secret_detection job that runs Gitleaks-based rules over the commits in the push. As with SAST, the job reports and doesn't fail, so secrets-gate reads the report with the same gate.py from the SAST lesson. A first push with no secrets passes:

ubuntu@secopslog-cicd:~/lab/secretscan · GitLab 19.5.3
$ ./up.sh
runner lab-runner: online (tags: lab,docker) GitLab 19.5.3 is up on http://127.0.0.1:8929
$ ./seed.sh app app/ && PIPE_WAIT=600 ./pipeline.sh app/ | sed -n "/--- job secrets-gate/,\$p"
pushed app: 25404ef first commit --- job secrets-gate (gate): success Removing gl-secret-detection-report.json Downloading artifacts $ python gate.py gl-secret-detection-report.json Critical High 0 findings, 0 at Critical/High pipeline #1: success

Commit a token

The lab's token is made up. It has the shape of a GitLab personal access token, which is all a rule can check:

settings.py
# Lab only: a made-up token in the shape of a GitLab personal access token. It never existed.
GITLAB_TOKEN = "glpat-Xk4mP9qL2vR7tW1nB8cZ"
ubuntu@secopslog-cicd:~/lab/secretscan · GitLab 19.5.3
$ cp settings.py app/ && ./commit.sh app/ "add settings" && PIPE_WAIT=600 ./pipeline.sh app/ | sed -n "/--- job secrets-gate/,\$p"; exit ${PIPESTATUS[0]}
pushed: 25ad1a2 add settings to main --- job secrets-gate (gate): failed Removing gl-secret-detection-report.json Downloading artifacts $ python gate.py gl-secret-detection-report.json Critical High Critical settings.py:2 GitLab personal access token 1 findings, 1 at Critical/High ERROR: Job failed: exit code 1 pipeline #2: failed

The scanner recognised the glpat- prefix and reported a Critical finding on settings.py line 2, and the gate failed the pipeline. In a real project this is the moment to revoke the token, before anything else.

Delete it in the next commit

ubuntu@secopslog-cicd:~/lab/secretscan · GitLab 19.5.3
$ git -C app rm -q settings.py && ./commit.sh app/ "remove settings" && PIPE_WAIT=600 ./pipeline.sh app/ | sed -n "/--- job secrets-gate/,\$p"
pushed: 7a105aa remove settings to main --- job secrets-gate (gate): success Removing gl-secret-detection-report.json Downloading artifacts $ python gate.py gl-secret-detection-report.json Critical High 0 findings, 0 at Critical/High pipeline #3: success
$ git -C app log --oneline | cat; git -C app show HEAD~1:settings.py | grep -c glpat-
7a105aa remove settings 25ad1a2 add settings 25404ef first commit 1

The pipeline is green again, because by default the scan looks at the commits being pushed, and the newest commit only deletes a file. Yet git show HEAD~1:settings.py still finds the token, and so will anyone with a clone, a fork or a cached CI checkout. A historic scan goes through every commit:

ubuntu@secopslog-cicd:~/lab/secretscan · GitLab 19.5.3
$ ./run-pipeline.sh app SECRET_DETECTION_HISTORIC_SCAN=true && sleep 3 && PIPE_WAIT=600 ./pipeline.sh app/ | sed -n "/--- job secrets-gate/,\$p"; exit ${PIPESTATUS[0]}
pipeline #4 created source=api --- job secrets-gate (gate): failed Removing gl-secret-detection-report.json Downloading artifacts $ python gate.py gl-secret-detection-report.json Critical High Critical settings.py:2 GitLab personal access token 1 findings, 1 at Critical/High ERROR: Job failed: exit code 1 pipeline #4: failed

Same finding, same file and line, from a commit that is no longer at the top of main. Run a historic scan once when you adopt secret detection, and again after any incident, because the default scan only ever sees new commits.

After a secret is pushedDoes it help?
Delete it in a new commitNo. It stays in history and in every clone
Rewrite history and force-pushBarely. GitLab keeps refs to old pipelines and merge requests, and clones, forks and caches still have it
Revoke or rotate it at the sourceYes. The leaked value stops working
Scan before the pushPrevents the next one. A pre-commit hook can be skipped. On self-managed GitLab a server-side pre-receive hook running Gitleaks can't
Watch out
Treat every pushed secret as leaked, even in a private project and even if it was there for one minute. Rotate it first, then check the token's usage or audit logs from the time it was exposed, and clean up last. Rewriting history is tidying, not containment.

Hands-on

Try to make the history clean. Reset main to the first commit with git -C app reset --hard HEAD~2 and force-push it. When that fails, allow force pushes on main with ./gl.sh /projects/root%2Fapp/protected_branches/main -X PATCH -d allow_force_push=true, push again, and run the historic scan with ./run-pipeline.sh app SECRET_DETECTION_HISTORIC_SCAN=true. What does it find now, and who still has the token? Finish with ./down.sh.

What you should see: the first force-push is refused, You are not allowed to force push code to a protected branch, because protected branches block force pushes by default. After allowing it, main holds only the first commit, and yet the historic scan still finds the token: 1 findings, 1 at Critical/High. The old commit is still reachable from the job's checkout. Rewriting main doesn't remove the refs GitLab keeps for earlier pipelines and merge requests, or the copy in a runner's reused checkout. Removing a secret from GitLab itself takes the repository cleanup tools, and every clone and fork keeps its copy regardless. Rotation is the only step that works. Turn force-push protection back on afterwards: -d allow_force_push=false.

Remember

Gate on secret detection in every pipeline and run a historic scan once. When a secret is pushed, rotate it first: deleting or rewriting the commit does not take it back from the people and systems that already have it.

Quick check
01A token is pushed, then deleted in the next commit. The next pipeline's secret detection passes. Why?
Incorrect — It has no memory between pipelines.
Incorrect — GitLab does not change history.
Incorrect — Only the token's issuer can revoke it.
Correct — The new commit only deletes the file, so the token is not in what was scanned. A historic scan found the token again in the lab.
02What should happen first when a real token is found in a pushed commit?
Incorrect — That doesn't reach existing clones. Do it later, if at all.
Correct — Once the value stops working, the copies in history are harmless.
Incorrect — Everyone who already cloned it keeps the token.
Incorrect — That hides the problem without fixing it.
03Why does the lab need a secrets-gate job after secret_detection?
Correct — That is true on every tier. The gate turns a Critical finding into a failed pipeline.
Incorrect — It found the token in settings.py.
Incorrect — It only reads the report and exits.
Incorrect — There is no such requirement.

Related