← Back to Blog

Preventing API Key Leaks with Pre-Commit Secrets Scanning

How I built a reliable WhatsApp AI shopping assistant for Clickmothercare that survives hallucinated products, silent save failures, and multi-agent handoff bugs.

Anas Rhimi
Anas Rhimi August 2026 • 8 min read

Leaking API keys, AWS credentials, or private keys into source control is a fatal error that often leads to automated exploitation within minutes. Relying solely on server-side scanning is insufficient; by the time an alert fires, the secret is already in the git history. The most effective defense is "shifting left" by intercepting secrets before they are ever committed.

The Problem: Accidental Credential Exposure

Developers often hardcode credentials during local testing and inadvertently include them in a git commit. Once pushed, revoking and rotating keys is a highly disruptive operational incident.

The Solution: Pre-Commit Hooks with TruffleHog

We will implement a client-side pre-commit framework configuration that utilizes TruffleHog to scan the diff of staged files. TruffleHog is superior to basic regex scanners because it verifies secrets dynamically.

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/trufflesecurity/trufflehog
    rev: v3.63.7
    hooks:
      - id: trufflehog
        name: TruffleHog Secret Scanner
        description: Detects verified secrets in committed code.
        entry: bash -c 'trufflehog git file://. --since-commit HEAD --only-verified --fail'
        language: system
        stages: [commit]
        pass_filenames: false

Configuration Breakdown

Let's analyze the configuration to understand how it intercepts the git workflow effectively:

  • repo: https://github.com/trufflesecurity/trufflehog: Instructs the pre-commit framework to pull the hook definition from the official TruffleHog repository, ensuring we use a trusted source.
  • entry: bash -c '...': This overrides the default hook command to provide a customized execution strategy optimized for local developer environments.
  • trufflehog git file://. --since-commit HEAD: Instead of scanning the entire repository history (which is slow), this command targets only the current diff (the uncommitted changes staged in the index) relative to HEAD. This ensures the hook executes in milliseconds, maintaining developer velocity.
  • --only-verified: This is the critical parameter. TruffleHog will actively attempt to authenticate against the respective API provider using the found secret. If it fails, it is considered a false positive or an inactive key, and the commit proceeds. This drastically reduces alert fatigue.
  • --fail: Ensures the command returns a non-zero exit code if a verified secret is found. The pre-commit framework intercepts this exit code and aborts the git commit operation entirely.
  • pass_filenames: false: We tell the pre-commit framework not to append the list of staged files to the end of our command string, as TruffleHog handles the diff analysis natively.

By enforcing this hook locally, you create an airtight perimeter that catches human error before it becomes a security incident.

Is your AI agent's infrastructure secure and reliable?

Book a Free 15-Min Technical Audit
Hire Me