DEV Community

Attila Albert
Attila Albert

Posted on

AI coding tools can commit your API key too: put gitleaks in a pre-commit hook so a leak never reaches Git

By Tomás Herrera, NBT

AI coding tools are good at getting things working, and the fastest way to get an API call working is to paste the key right where it's used. You ask for "a script that posts to our Slack channel", you give the agent the webhook URL so it can test, and it puts the URL in the script. Two prompts later you say "commit this", and it does. The secret is now in your Git history, which is a much harder place to get it out of than a file.

You can tell the agent not to do this, and you should. But instructions are a request, not a guarantee. The reliable fix is a check that runs on every commit, whoever or whatever makes it, and stops the commit when it finds something that looks like a secret. gitleaks is a widely used open-source tool for exactly that. This piece sets it up as a pre-commit hook and adds the instructions that make an agent work with it rather than around it.

Step 1: install gitleaks

On a Mac, the gitleaks README lists:

brew install gitleaks
Enter fullscreen mode Exit fullscreen mode

It also offers Docker images, a Go install and binaries from the releases page for other systems.

One thing worth knowing before you rely on it: the README now says gitleaks is feature complete and that future releases will be security patches only, with the author moving to a new project. For a pre-commit check that's fine. The detection rules work today. Just know you won't see new features.

Step 2: scan what you already have

Before adding a hook, find out whether something has already leaked. From the repo root:

gitleaks git -v
Enter fullscreen mode Exit fullscreen mode

The git command scans your repository's history; the README says it uses git log -p under the hood. To scan files on disk regardless of Git:

gitleaks dir -v .
Enter fullscreen mode Exit fullscreen mode

The exit code tells you the result: 0 means no leaks, 1 means leaks found (or an error). Add --redact so the secrets themselves don't get printed to your terminal or logs.

If the history scan finds a real secret, the important step isn't removing it from Git. It's rotating the secret: revoke it with the provider and issue a new one. Assume anything that reached a commit, and especially anything that reached a remote, has been seen.

Step 3: add the pre-commit hook

If you use the pre-commit framework, the gitleaks README gives this configuration:

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.24.2
    hooks:
      - id: gitleaks
Enter fullscreen mode Exit fullscreen mode

The rev in the README example is v8.24.2; check the releases page and pin the newest tag. Then install the hook:

pre-commit install
Enter fullscreen mode Exit fullscreen mode

From now on, every git commit runs gitleaks first. If it finds something, the commit stops and you see what it found and where.

Commit the .pre-commit-config.yaml file so teammates get the same check, and ask everyone to run pre-commit install once after cloning.

Step 4: tell the agent what to do when the hook fires

This is the step that makes the hook work with AI coding tools instead of against them. Without guidance, an agent whose commit fails may try to "fix" the failure the quickest way: by skipping the hook. Add this to your project instructions (CLAUDE.md, AGENTS.md or whatever your tool reads):

## Secrets
- Never write API keys, tokens, passwords or webhook URLs into source files. Read them from environment variables, and add the variable name (not the value) to .env.example.
- .env files are never committed.
- A gitleaks pre-commit hook runs on every commit. If it blocks a commit, stop and tell me what it found. Never use --no-verify, never add a gitleaks:allow comment, and never edit .gitleaksignore unless I ask.
Enter fullscreen mode Exit fullscreen mode

That last line is the important one. If your tool supports permission rules, you can back it up by denying git commit --no-verify as a command pattern, so the agent can't skip the hook even if it tries.

Step 5: handle false positives the right way

Sometimes gitleaks flags something that isn't a secret: a test fixture with a fake key, an example in the docs. The README gives two ways to allow it:

  • Inline: add a gitleaks:allow comment on that line. The finding is ignored for that line.
  • By fingerprint: each finding has a fingerprint that uniquely identifies it. Put the fingerprint in a .gitleaksignore file at the root of the repo. The README marks this feature as experimental.

Both should be decisions a person makes, which is why the instructions above tell the agent not to add them. A useful prompt when the hook blocks a commit:

The gitleaks hook blocked this commit. Show me each finding with the file and line, redacted. For each one, tell me whether it looks like a real secret or a test value, and why. Don't change anything.

Then you decide. Real secrets get moved to environment variables and rotated. Fake ones get an allow comment that you add, with a short note saying what it is.

Step 6: deal with old findings without blocking new work

If step 2 found old findings you can't remove right away (say, a long-revoked key deep in history), you don't want every scan to fail because of them. The README describes a baseline: save a report once, then pass it with --baseline-path so gitleaks ignores findings already in it and only reports new ones.

gitleaks git --report-path gitleaks-report.json
gitleaks git --baseline-path gitleaks-report.json --report-path findings.json
Enter fullscreen mode Exit fullscreen mode

Rotate the old secrets anyway. The baseline keeps the scan useful; it doesn't make the old keys safe.

Step 7: run it in CI too

A pre-commit hook only runs where it's installed. Someone who skipped pre-commit install, or a tool that commits with hooks turned off, gets past it. Running gitleaks git in CI on every push catches what the local hook missed. It can't stop the secret reaching the remote, but it tells you within minutes instead of months.

Why this matters more with AI coding tools

None of this is new advice for teams. What changes with AI coding tools is the volume and speed: more commits, made faster, often by an agent that saw the secret because you gave it the secret to test with. A check that runs on every commit doesn't care who wrote the code, which is exactly the property you want.

Keeping secrets out of Git is one part of keeping AI coding tools safe. The other side is what you install into them. Our article on the fake Claude Code extension attack and how to keep your AI coding tools safe covers that half.

Sources, checked 2026-12-06: gitleaks README on GitHub (install, git and dir commands, exit codes, --redact, pre-commit configuration, gitleaks:allow, .gitleaksignore fingerprints, baseline, project status).

Top comments (1)

Collapse
 
stackurity profile image
Stackurity •

The baseline step is worth a caveat: it hides old findings from the scan, but the key itself stays valid until you revoke it, and rewriting history doesn't reach clones, forks or CI caches that already pulled the commit. So rotation is the only real fix, as you say.

One gap with the hook approach is that it only runs where pre-commit install was run. Your CI step covers that, and a server-side check on push, such as GitHub's secret scanning push protection if the repo is on GitHub, catches commits made from a machine without the hook. An agent running in a fresh container or worktree is exactly that case, so I'd run the history scan (gitleaks git) in CI on every PR as well.