Course contentsShow
← Security
Lesson 2050 of 3,05241. Secure Development LifecycleFree lesson

Secret Detection in Commits

Preventing hardcoded credentials from entering repositories using automated secret scanning tools.

Secret Detection in Commits

What You'll Learn

You'll learn how to prevent hardcoded credentials from entering your repositories using automated secret scanning tools that catch sensitive data before it's committed.

The Problem with Hardcoded Secrets

Developers sometimes accidentally commit API keys, passwords, database connection strings, or tokens directly into code. Once pushed to a repository—even a private one—these secrets become nearly impossible to fully revoke because they exist in Git history forever. Think of it like accidentally printing your credit card number in a book: even if you black it out later, the earlier editions are already out there.

How Secret Detection Works

Automated secret scanning tools analyze commit contents before they're pushed (or immediately after). They use:

  • Pattern matching: Regular expressions to identify formats like AWS_SECRET_KEY=xxxxxxxxxxxx or api_key: "abc123def456"
  • Entropy analysis: Detecting random-looking strings that might be tokens
  • Known secret formats: Recognizing provider-specific patterns (GitHub tokens, Slack webhooks, etc.)

These tools can run as pre-commit hooks (client-side) or in your CI/CD pipeline (server-side), providing multiple safety layers.

Common Tools and Integration Points

Popular secret detection tools include:

  • git-secrets (AWS-focused)
  • detect-secrets (Yelp's tool)
  • TruffleHog (entropy-based scanning)
  • GitGuardian, GitHub Advanced Security (commercial/integrated)

When a secret is detected, the commit is blocked and the developer receives immediate feedback about which file and line contains the sensitive data.

Prevention Is Key

Secret detection should happen before code reaches the repository. Configure pre-commit hooks so developers catch issues locally, and add pipeline scanning as a backup. This prevents the "oops, let me delete that commit" scenario—because in Git, deletion isn't really deletion.

Key Takeaway: Automated secret scanning tools act as gatekeepers that prevent hardcoded credentials from entering repository history, protecting sensitive data before it becomes a persistent security risk.