Sub topic 12
Catching secrets before they are committed
This chapter assumes you know basic Git, can use a command line, and understand what an API key is. It covers the secret scanning step of the plan: stopping credentials from entering your repository, whether you typed them or an AI assistant did.
Why scanning matters with AI tools
Assistants generate code quickly. They sometimes copy a key from a file in context into a new script, or paste a "working example" with a real token. Once a secret is committed and pushed, it lives in history and anyone with access to the repository can read it. A scanner is a program that searches text for patterns that look like credentials. It gives you a check that does not depend on anyone noticing the problem during review.
Three places to scan
Each layer catches what the one before it misses.
- Pre-commit hook. Runs on your machine before the commit is created. It is the cheapest place to fix a mistake, because nothing has left your computer.
- CI scan. Runs on the server for every push or pull request. It catches commits made without the hook, such as from a fresh clone or with
--no-verify. - Full history scan. Run it once on an existing repository to find old leaks.
Set up a pre-commit hook
Gitleaks is an open-source scanner. The pre-commit tool runs it for you. Add this file as .pre-commit-config.yaml in the repository root:
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.4
hooks:
- id: gitleaksPin rev to a current release tag of the project, and update it from time to time so detection rules stay current. Then install the tool, register the hook, and scan every tracked file once:
pip install pre-commit
pre-commit install
pre-commit run --all-filesAfter pre-commit install, each git commit scans the staged changes. If Gitleaks finds a match, the commit is rejected and the output shows the file, line, and rule that matched.
Handling findings
When the scanner reports something, work through it in this order:
- Decide whether it is a real credential or a false positive, such as a placeholder or test fixture.
- If it is real and was never committed, remove it from the file and load it from the environment, as the previous chapter showed.
- If it was already pushed, treat it as leaked. Revoke and rotate it first. Rewriting history comes second, because copies may already exist.
For false positives, add a narrow allowlist entry instead of disabling the rule. A .gitleaks.toml file in the repository root can ignore a specific path or a specific known-safe value. Keep entries small so a real secret in the same file is still caught.
Limits you should know
- Scanners match patterns and entropy (how random a string looks). They miss secrets with unusual formats and can flag harmless random strings.
- A hook is local. Anyone can skip it with
git commit --no-verify, and a new clone has no hook untilpre-commit installis run. This is why the CI scan is needed. - Scanning does not stop a secret from being sent to an AI tool. It only checks what goes into Git. Ignore files and prompt redaction cover the other path.
Exercise
Exercise
A teammate clones the repository, never runs pre-commit install, and pushes a commit containing a live API key. Which layer can still catch it, and what should you do first once it is found?
Show answer
The CI scan can still catch it, because it runs on the server regardless of local hooks. Once found, revoke and rotate the key first, since it is already pushed and may have been copied. Then remove it from the code and, if needed, clean the history.
Exercise
Why is a narrow allowlist entry better than disabling a scanner rule for a noisy file?
Show answer
A narrow entry ignores only the known-safe value or path. If a real secret is later added to the same file, the rule still flags it. Disabling the rule hides every future finding there.