Scan your repo for leaked secrets with gitleaks before an agent pushes Gitleaks 8.30.1, an open source secret scanner, ships with 222 built-in rules and can block coding agents from committing or pushing leaked API keys by wiring its non-zero exit code in front of git commit, git push and CI, according to a tutorial by Flavio. The tutorial notes that gitleaks v8.19.0 replaced the older `gitleaks detect` and `gitleaks protect` commands with `gitleaks git`, `gitleaks dir` and `gitleaks stdin`, and that the tool's `--max-decode-depth` defaults to 5 so base64-encoded tokens are still caught. The scanner relies on keyword pre-filtering and entropy checks, with the `generic-api-key` rule flagging random-looking strings that lack a known prefix. Scan your repo for leaked secrets with gitleaks before an agent pushes By Flavio https://flaviocopes.com/about/ Use gitleaks to find API keys in Git history and files, tune it with a config, and block commits and pushes from coding agents with hooks and CI. A coding agent trying to get an S3 upload working will sometimes paste the access key straight into the code. Or it commits a .env file “for local testing”, or drops a real API response with a token inside into a test fixture. Agents commit fast. Once a key lands in a commit, it stays in your Git history even if the very next commit deletes it. gitleaks https://github.com/gitleaks/gitleaks is a small open source scanner that catches those keys before they get out. It reads your Git history, your files on disk, or anything you pipe into it, and exits with an error when it spots a secret. That exit code is what makes it so useful. We can wire it in front of git commit , git push , and CI so leaks get blocked automatically. I’m using gitleaks 8.30.1 throughout this tutorial. If you see gitleaks detect or gitleaks protect in older posts and READMEs, those were replaced in v8.19.0 by gitleaks git , gitleaks dir , and gitleaks stdin . The old commands still work for now, though they’re hidden from --help . How gitleaks finds a secret In version 8.30.1, gitleaks ships with 222 built-in rules. Each rule defines an id like aws-access-token or github-pat , a regular expression, and usually a list of keywords. Keywords act as a fast pre-filter. A rule only runs its regex when the content contains one of its keywords, which keeps scans fast. Some rules also check entropy , which measures how random a string looks. A repeated string like aaaaaaaaaaaaaaaaaaaaaaaa has very low entropy, while a real API key looks like random noise. The generic-api-key rule uses entropy to catch secrets that don’t share a known prefix. You can test this right away with gitleaks stdin , which scans whatever you pipe into it: echo 'API KEY=aaaaaaaaaaaaaaaaaaaaaaaa' | gitleaks stdin --no-banner 3:08PM INF no leaks found Now try the same variable with a random-looking value: echo 'API KEY=Zq7vN2xR8kL4pT9wM3bC6hJ1' | gitleaks stdin --no-banner 3:08PM WRN leaks found: 1 Before a match becomes a finding, gitleaks checks your allowlists, any gitleaks:allow comments, and the .gitleaksignore file. We’ll set those up later, and here is the full flow it follows: gitleaks also decodes encoded text before running its rules. In 8.30.1, --max-decode-depth defaults to 5, so even a base64-encoded GitHub token gets caught: echo "token: $ printf 'ghp R7x2Kq9LmT4vB8nZpW3cY6dF1sH5jA0gUeXo' | base64 " | gitleaks stdin --no-banner -v Finding: token: Z2hwX1I3eDJLcTlMbVQ0dkI4blpwVzNjWTZkRjFzSDVqQTBnVWVYbw== Secret: Z2hwX1I3eDJLcTlMbVQ0dkI4blpwVzNjWTZkRjFzSDVqQTBnVWVYbw== RuleID: generic-api-key Entropy: 4.988171 Finding: ghp R7x2Kq9LmT4vB8nZpW3cY6dF1sH5jA0gUeXo Secret: ghp R7x2Kq9LmT4vB8nZpW3cY6dF1sH5jA0gUeXo RuleID: github-pat Entropy: 5.221928 Every key in this post is fake. They match the rule patterns, but none of them belong to a real account. Install gitleaks On macOS, install gitleaks with Homebrew: brew install gitleaks Check the installed version next, since some flags changed across releases: gitleaks version 8.30.1 On Linux, you can grab a prebuilt binary from the releases page https://github.com/gitleaks/gitleaks/releases , such as gitleaks 8.30.1 linux x64.tar.gz . There’s also a Docker image available. Plant some secrets in a test repo Let’s build a small repo that repeats the mistakes coding agents tend to make. We’ll add a server, an S3 client with a hardcoded AWS key, and a .env file that gets committed and later untracked. mkdir invoice-app && cd invoice-app git init -b main Here’s our S3 client in src/s3.js : js import { S3Client } from '@aws-sdk/client-s3' export const s3 = new S3Client { region: 'eu-west-1', credentials: { accessKeyId: 'AKIAZ4X7QF3NRT2LWVDK', secretAccessKey: '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' } } Next, let’s commit a .env file. Even a global .gitignore that lists .env won’t stop git add -f , and an agent trying to “make the tests work in CI” can run that exact command: echo "GITHUB TOKEN=ghp R7x2Kq9LmT4vB8nZpW3cY6dF1sH5jA0gUeXo" .env git add -f .env git commit -m "Add env for local testing" A commit later, we notice the mistake and untrack the file: git rm --cached .env echo .env .gitignore git add .gitignore git commit -m "Stop tracking .env" .env is gone from the latest commit, but the commit that added it is still sitting in the history: git log --oneline 99c5dba Stop tracking .env 9b83811 Add env for local testing 2341b2f Upload invoices to S3 5516d0b Initial server Scan the Git history with gitleaks git gitleaks git runs git log -p under the hood and scans the diff of every commit. Run it from your repo root with -v so it prints the details of each finding: gitleaks git -v . ○ │╲ │ ○ ○ ░ ░ gitleaks Finding: ghp R7x2Kq9LmT4vB8nZpW3cY6dF1sH5jA0gUeXo Secret: ghp R7x2Kq9LmT4vB8nZpW3cY6dF1sH5jA0gUeXo RuleID: github-pat Entropy: 5.221928 File: .env Line: 1 Commit: 9b83811fe42a601ec72dd63a9206a7221df037a0 Author: Flavio Copes Email: flavio@flaviocopes.com Date: 2026-11-04T14:06:36Z Fingerprint: 9b83811fe42a601ec72dd63a9206a7221df037a0:.env:github-pat:1 Finding: secretAccessKey: 'n8Vq2pXkT7mR4sLw9YbE1cHf6JdZ3aGu0oKiNyWe' Secret: n8Vq2pXkT7mR4sLw9YbE1cHf6JdZ3aGu0oKiNyWe RuleID: generic-api-key Entropy: 5.321928 File: src/s3.js Line: 7 Commit: 2341b2f6c2f5323b862d2ed9efc7f5ed1630b74d Author: Flavio Copes Email: flavio@flaviocopes.com Date: 2026-11-04T14:06:31Z Fingerprint: 2341b2f6c2f5323b862d2ed9efc7f5ed1630b74d:src/s3.js:generic-api-key:7 Finding: AKIAZ4X7QF3NRT2LWVDK Secret: AKIAZ4X7QF3NRT2LWVDK RuleID: aws-access-token Entropy: 4.121928 File: src/s3.js Line: 6 Commit: 2341b2f6c2f5323b862d2ed9efc7f5ed1630b74d Author: Flavio Copes Email: flavio@flaviocopes.com Date: 2026-11-04T14:06:31Z Fingerprint: 2341b2f6c2f5323b862d2ed9efc7f5ed1630b74d:src/s3.js:aws-access-token:6 3:06PM INF 4 commits scanned. 3:06PM INF scanned ~412 bytes 412 bytes in 48ms 3:06PM WRN leaks found: 3 Without -v , gitleaks only prints the last three summary lines. Each finding gives you everything you need to track it down: - RuleID is the rule that matched. Here, generic-api-key the entropy-based rule caught the AWS secret because AWS secret keys don’t have a fixed prefix. - File , Line , and Commit point straight to the patch. You can inspect it with git show 9b83811 . - Finding is the full matched text, while Secret is the specific part the rule extracted. - Fingerprint is the unique id for this occurrence. We’ll use it later when we want to ignore a finding. Look at that .env finding at the top. The file is no longer in the latest commit, so a plain grep of tracked files would miss it, whereas the history scan catches it immediately. When you don’t want to scan the whole history, pass --log-opts to forward options to git log . For example, this checks only the last two commits: gitleaks git --log-opts="HEAD~2..HEAD" . We’ll use that same flag in our pre-push hook later. Scan files on disk with gitleaks dir gitleaks dir scans files straight from disk without touching Git. It also has two aliases, gitleaks file and gitleaks directory : gitleaks dir -v . Finding: ghp R7x2Kq9LmT4vB8nZpW3cY6dF1sH5jA0gUeXo Secret: ghp R7x2Kq9LmT4vB8nZpW3cY6dF1sH5jA0gUeXo RuleID: github-pat Entropy: 5.221928 File: .env Line: 1 Fingerprint: .env:github-pat:1 Finding: secretAccessKey: 'n8Vq2pXkT7mR4sLw9YbE1cHf6JdZ3aGu0oKiNyWe' Secret: n8Vq2pXkT7mR4sLw9YbE1cHf6JdZ3aGu0oKiNyWe RuleID: generic-api-key Entropy: 5.321928 File: src/s3.js Line: 7 Fingerprint: src/s3.js:generic-api-key:7 Finding: AKIAZ4X7QF3NRT2LWVDK Secret: AKIAZ4X7QF3NRT2LWVDK RuleID: aws-access-token Entropy: 4.121928 File: src/s3.js Line: 6 Fingerprint: src/s3.js:aws-access-token:6 3:06PM INF scanned ~412 bytes 412 bytes in 1.94ms 3:06PM WRN leaks found: 3 Notice the fingerprints don’t include a commit hash because Git isn’t involved. And .env still gets scanned even though it’s listed in .gitignore , since dir ignores .gitignore . That makes dir great for checking a build folder before you deploy or upload it. If a bundler accidentally inlined a key into dist , this catches it: gitleaks dir --no-banner dist In short, use git to check if a secret was ever committed, and dir to check the files sitting on disk right now. stdin handles everything else, like command output an agent is about to paste into a GitHub issue. Redact secrets in the output By default, gitleaks prints the secrets right in the scan output. That’s a problem when the output lands in CI logs or inside a coding agent’s transcript. Add --redact to replace every secret with REDACTED : echo "export GITHUB TOKEN=ghp R7x2Kq9LmT4vB8nZpW3cY6dF1sH5jA0gUeXo" | gitleaks stdin --no-banner --redact -v Finding: REDACTED Secret: REDACTED RuleID: github-pat Entropy: 5.221928 3:06PM INF scanned ~61 bytes 61 bytes in 12.3ms 3:06PM WRN leaks found: 1 If you want to keep part of the value visible, pass a percentage instead. With --redact=20 , gitleaks hides the last 20% of the string, leaving enough visible to tell two tokens apart: gitleaks git -v --redact=20 . Finding: ghp R7x2Kq9LmT4vB8nZpW3cY6dF1sH5... Secret: ghp R7x2Kq9LmT4vB8nZpW3cY6dF1sH5... RuleID: github-pat I recommend always passing --redact in hooks and CI. Remember that hook output goes straight into the agent’s context. Write reports in JSON, SARIF, or your own format Use --report-format or -f to pick an output format: json , csv , junit , sarif , or template . Then set --report-path or -r to the destination file, or - to write to stdout: gitleaks git --redact -f json -r gitleaks-report.json . In JSON, each finding is an object with extra metadata that the terminal doesn’t print, including the commit message: { "RuleID": "github-pat", "Description": "Uncovered a GitHub Personal Access Token, potentially leading to unauthorized repository access and sensitive content exposure.", "StartLine": 1, "EndLine": 1, "StartColumn": 14, "EndColumn": 53, "Match": "REDACTED", "Secret": "REDACTED", "File": ".env", "SymlinkFile": "", "Commit": "9b83811fe42a601ec72dd63a9206a7221df037a0", "Entropy": 5.221928, "Author": "Flavio Copes", "Email": "flavio@flaviocopes.com", "Date": "2026-11-04T14:06:36Z", "Message": "Add env for local testing", "Tags": , "Fingerprint": "9b83811fe42a601ec72dd63a9206a7221df037a0:.env:github-pat:1" } You can also export SARIF, the standard security report format that GitHub code scanning and many code editors understand: gitleaks git --redact -f sarif -r gitleaks.sarif . For a report meant for humans, you can write a Go template. gitleaks passes the list of findings as . and supports sprig https://masterminds.github.io/sprig/ helper functions like substr . Here’s a minimal report.html.tmpl : < doctype html