Secret detection
Catch a committed token, and why deleting it is not enough.
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: 9bc478297eb15b5bf22987a669af05292836bcb81ef58ccd382a17b81a733ee6Someone 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
# 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.ymlstages: [test, gate]secrets-gate:stage: gateimage: secopslog-py:3.15needs:- job: secret_detectionartifacts: truescript:- 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:
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:
# Lab only: a made-up token in the shape of a GitLab personal access token. It never existed.GITLAB_TOKEN = "glpat-Xk4mP9qL2vR7tW1nB8cZ"
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
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:
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 pushed | Does it help? |
|---|---|
| Delete it in a new commit | No. It stays in history and in every clone |
| Rewrite history and force-push | Barely. GitLab keeps refs to old pipelines and merge requests, and clones, forks and caches still have it |
| Revoke or rotate it at the source | Yes. The leaked value stops working |
| Scan before the push | Prevents the next one. A pre-commit hook can be skipped. On self-managed GitLab a server-side pre-receive hook running Gitleaks can't |
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.
secrets-gate job after secret_detection?settings.py.