Caloogy Article
中文

Secret Scanning: Don't Push Keys to GitHub

Date
Sep 11, 2025
Series
Infra
Reading
4 min
Topics
InfraSecret scanningGitGithub

The one-sentence version

Committing API keys, database passwords, or private keys into code repositories is one of the most common and easily avoidable security incidents. Once pushed to a public repository, automated scanning bots will often find and exploit them within minutes. Even worse, Git remembers everything: even if you delete the file and commit again, the key still lies within the history. There are three layers of defense: intercepting with hooks before committing, platform interception during pushing, and continuous scanning after the fact. Once a leak occurs, the only reliable remedy is to immediately rotate the keys.

This article has three layers:

  1. Layer 1: The Intuition: Leaving your keys on a public bulletin board
  2. Layer 2: Real-World Use: Uber and Toyota incidents, GitHub push protection
  3. Layer 3: Principles and engineering key points: How to identify keys, the three lines of defense, and post-leak procedures

Layer 1: The Intuition

1.1 Leaving your keys on a public bulletin board

You post a photo of your house keys on a public bulletin board in your residential community, realize your mistake a few minutes later, and quickly tear it down.

However:

At this point, the only solution is to change the locks. A public Git repository is exactly like this bulletin board.

1.2 Why people keep making this mistake


Layer 2: Real-World Use


Layer 3: Principles and Engineering

3.1 How to identify keys

Method Description
Fixed Format Many services have fixed prefixes and lengths. For example, AWS access keys start with AKIA, GitHub tokens start with ghp_, and private key files start with -----BEGIN ... PRIVATE KEY-----. High accuracy.
High-Entropy Strings A long string that looks random might be a key. High false positive rate.
Context Assignments where variable names are password, secret, or token.
Validation Some tools attempt to call the service provider's API using the discovered key to confirm if it is still valid, helping to prioritize response.

Service providers design prefixed key formats specifically to make scanning more accurate.

3.2 Three lines of defense

Defense Tool Role
Before Committing gitleaks, detect-secrets, etc., used as pre-commit hooks Intercepts locally so the key never enters the history.
During Pushing Push protection on code hosting platforms Intercepts even if no local hooks are installed.
After the Fact Platform secret scanning, trufflehog, etc., scanning the entire history Detects leaks that have already occurred.

Combined with source-level measures:

3.3 Post-leak response workflow

Yes

No

Detect Key Leak

Immediate Rotation: Revoke old key, generate new key

Check usage logs during the leak period

Anomalous usage?

Handle as incident: Assess impact, notify stakeholders

Log and close case

Clean history (optional)

Post-mortem: Why did the three lines of defense fail?

Key points:

  1. Rotation is the first step, and the only reliable step. Deleting commits, rewriting history, or making the repository private is not enough: the key may have already been copied, and it may still exist in forks, caches, or mirrors;
  2. Rewriting Git history (e.g., using git filter-repo) can reduce the chance of it being discovered again in the future, but only after it has been rotated;
  3. Check the service provider's access logs to see if there were any anomalous calls within the leak window;
  4. Conduct a post-mortem and patch the missing defenses (see "Incident Response and Post-mortems").

3.4 Minimizing the impact of a leak

Even with best practices, leaks can happen. Preparing these in advance can significantly reduce the impact:


Summary

Layer Key points
Risk Keys in public repositories can be exploited within minutes; Git history remembers everything
Identification Fixed prefix formats are most accurate; high entropy and context serve as supplements
Defense Pre-commit hooks, push protection, post-event scanning
Post-leak Immediate rotation; deleting records is insufficient; check logs; post-mortem