Sub topic 12
Catching secrets before they are committed
This chapter assumes you can commit and push with Git, you already keep your key in a git-ignored .env file, and you can run commands in a terminal. A .gitignore entry protects one file. It does not stop you from pasting a key into a source file, a README, or a config file that Git does track. Secret scanning is a second layer that checks what you are about to commit.
Why .gitignore is not enough
.gitignore only works on file paths. It cannot see what is inside the files you do track. Typical misses:
- A key pasted into a test file "just for a minute".
- A key copied into
config.jsonor a notebook cell. - A
.env.localor.env.productionfile that your ignore rule does not match. - A key inside a command saved in a shell script.
A scanner looks at file contents. It matches known key formats and flags high-randomness strings that look like secrets.
Where to catch a secret
You can check at three points. The earlier the check, the cheaper the fix.
- Pre-commit hook. A script that Git runs before it records a commit. If the script fails, the commit does not happen. The secret never enters history.
- Push protection. Hosting services such as GitHub can reject a push that contains a recognized key format.
- CI scan. A job that scans every pull request. It is your backup if someone skips the local hook.
Setting up a pre-commit scan
The pre-commit tool manages hooks from a config file. This example uses Gitleaks, an open-source scanner.
pip install pre-commitCreate .pre-commit-config.yaml in the repository root:
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.4 # pin a release; check the project for the latest tag
hooks:
- id: gitleaksThen install the hook into your local repository:
pre-commit installFrom now on, git commit scans the staged changes. If it finds something that looks like a key, the commit stops and prints the file and line. Remove the key, move it to your environment, and commit again.
To scan files already in the repository, run this:
pre-commit run gitleaks --all-filesHooks live in your local .git folder, so each teammate must run pre-commit install after cloning.
Handling false positives
Scanners sometimes flag harmless strings, such as an example value in documentation. Do not disable the hook. Use the scanner's allowlist or ignore file for that one entry, and keep a short comment explaining why.
What scanners cannot do
- They miss keys with unusual formats unless the entropy check catches them.
- They cannot protect a key that was already pushed before you installed the tool.
- A local hook can be skipped, so it is not a guarantee. Pair it with push protection or a CI scan.
If a scan finds a key that was already committed, treat it as leaked. Removing the line in a new commit does not remove it from history. Revocation is covered in a later chapter.
Summary of the layers
| Layer | Runs where | Can be skipped? | Catches |
|---|---|---|---|
| `.gitignore` | Local | Yes, by force-adding | Known secret file paths |
| Pre-commit hook | Local | Yes, with `--no-verify` | Secrets in staged content |
| Push protection | Hosting service | Only by admin policy | Recognized key formats |
| CI scan | Server | Not without changing the workflow | Anything the rules match |
Exercise
A teammate says, "My key is in .env and .env is in .gitignore, so I don't need a scanner." Give one way a key could still reach the repository, and name the earliest point where a scanner would stop it.
Show answer
The key could be pasted into a tracked file, such as a test script or config.json, because .gitignore only filters paths and does not inspect contents. A pre-commit hook such as Gitleaks would stop it at the moment of git commit, before the key enters Git history.
Exercise
Why must every teammate run pre-commit install after cloning, and what second layer covers someone who forgets?
Show answer
Git hooks are stored in the local .git directory, which is not shared through clones. A CI scan or server-side push protection checks every push regardless of local setup.