SECURITY ENGINEERING · PART 9 · DEVSECOPS AND SUPPLY CHAIN · SECRETS · 129 OF 229

Secret Scanning With Gitleaks and TruffleHog

Hard-coded secrets in code are one of the most common and most exploited mistakes in software. This episode covers secret scanning: why committed secrets are so dangerous, how scanners find them (patterns, entropy, verification), where to scan, the tools (Gitleaks, TruffleHog, push protection), and the rule that a leaked secret must be rotated, not just deleted.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 10

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Committed secrets are public and exploited fast
  • scan pre-commit, CI, history and at push
  • rotate a leaked secret

Notes

Why it is so bad

Why is a committed secret so dangerous? It is readable by everyone with repository access, and by the whole internet if the repository is public. Git history keeps it even after you delete the line. Automated bots constantly scan public code and exploit leaked keys within minutes. And a single leaked cloud credential can lead to a full account takeover, data theft or a large cryptomining bill. This is one of the highest-frequency, highest-impact mistakes there is.

The danger
  • a key in a commit is readable by anyone with repo access, and by anyone if public
  • git history keeps it even after you delete the line
  • bots scan public registries and exploit keys in minutes
  • one leaked cloud key can mean a full account takeover
  • deleting the file does NOT remove it from history

How scanners find them

Scanners find secrets three ways. Patterns match known formats: an AWS key, a GitHub token and a Stripe key each have a recognisable shape a regular expression catches. Entropy detection flags random-looking, high-entropy strings, a hallmark of a key even when the format is unknown. And verification, the most powerful, tests a found credential against the provider to see if it is live, cutting false positives by separating real, active secrets from example or expired ones. The best tools combine all three.

Detection methods
  • patterns: known formats (AWS keys, GitHub tokens, Stripe keys) via regex
  • entropy: random-looking high- entropy strings that may be secrets
  • verification: actually test the credential against the provider to confirm it is live (cuts false positives)

Where to scan

Scan at every layer, because each catches what the others miss. A pre-commit hook blocks a secret before it is committed, the best place, because then it never enters history. CI scans every pull request to catch what slipped past the hook, since hooks can be bypassed. A full history scan finds old secrets already in the repository from before you turned scanning on. And provider-side push protection, such as GitHub's, blocks known formats at push. Use all of them together.

Scan at every layer
  • pre-commit hook: block the secret before it is even committed (best)
  • CI on every PR: catch what slips past the hook
  • whole git history: find old secrets already in there
  • provider push protection: GitHub blocks known secret formats at push
  • defence in depth: all of them

The golden rule

Here is the rule that matters most, and that people get wrong. The moment a secret is committed, treat it as compromised, full stop. Deleting the line, or even rewriting git history, does not un-leak it, because a bot or a person may already have copied it. The correct response is the rotation runbook from Part four: issue a new secret, switch everything to it, then revoke the old one. Cleaning history is good hygiene afterwards, but it is not the fix. Rotation is the fix, and it is urgent.

Found a secret? Rotate it
  • the moment a secret is committed, treat it as compromised
  • deleting the line or rewriting history does NOT un-leak it
  • someone may already have copied it
  • correct response: issue a new secret, switch everything, revoke the old one then clean history

The tools

The tools are free and easy to adopt. Gitleaks is fast and popular, running in pre-commit hooks and CI, and scanning full history. TruffleHog is strong on verification, testing whether found keys are live. GitHub has built-in secret scanning and push protection, and cloud providers offer their own. Beyond the built-in rules, add custom patterns for your organisation's own internal token formats, so your home-grown credentials are caught too, not just the well-known third-party ones.

Secret scanners
  • Gitleaks: fast, popular, runs in pre-commit and CI, scans history
  • TruffleHog: strong on verification (tests if keys are live)
  • GitHub secret scanning + push protection: built in cloud/vendor scanners too
  • add custom rules for your own internal token formats

Prevent, not just detect

Scanning is a safety net, but the real goal is that secrets never reach the code. Inject them at runtime from a vault or secret manager, never hard-coded in source or config. Use short-lived credentials so even a leaked one expires quickly, which is the whole next episode. And make correct local secret handling easy, because people hard-code secrets most when the right way is painful. Scanning catches the mistakes; good secret management prevents them.

Stop secrets reaching code
  • secrets from a vault or secret manager at runtime, never in code or config
  • short-lived credentials so a leak expires fast (next episode)
  • developer education and easy local secret handling
  • scanning is the safety net, not the strategy

Common mistakes

Mistakes. Deleting a leaked secret without rotating it, so the live credential is still out there. Scanning new commits but never the existing history, where old secrets hide. Relying only on a pre-commit hook, which developers can skip, instead of also enforcing in CI. Ignoring custom-format secrets the default rules miss. And the biggest: treating secrets in code as normal and scanning as the only control, instead of moving secrets into a manager.

Avoid these
  • deleting a leaked secret without rotating it
  • scanning new commits but not git history
  • pre-commit hook only, skippable, not in CI too
  • ignoring low-entropy or custom-format secrets
  • secrets in code as the norm, scanning as the only control

Say it like this

In the interview: I scan at every layer, pre-commit to block before history, CI on every pull request, a full history scan for old secrets, and provider push protection, using Gitleaks and TruffleHog that combine patterns, entropy and live verification. The rule I stress is that a committed secret is compromised the moment it lands: I rotate it immediately, new one issued and old one revoked, and only then clean history. And scanning is a safety net; the real fix is a vault with short-lived credentials.

In the interview
  • "Scan at every layer: pre- commit, CI, full history, and provider push protection, with Gitleaks or TruffleHog using
  • patterns, entropy and verification. A found secret
  • is compromised: rotate it, then clean history. But the real fix is a vault and short- lived credentials, not scanning."

Interview questions

Q1Why are hard-coded secrets in code so dangerous?

They are readable by anyone with repository access, or the whole internet if public, remain in git history after deletion, and are harvested by automated bots that exploit leaked keys within minutes. A single leaked cloud credential can lead to full account compromise.

Q2How do secret scanners detect secrets?

By pattern matching known credential formats with regular expressions, by flagging high-entropy random-looking strings, and by verification, actively testing a found credential against its provider to confirm it is live. Combining all three improves accuracy and reduces false positives.

Q3Where should secret scanning run?

At multiple layers: pre-commit hooks to block secrets before they are committed, CI on every pull request to catch bypasses, full git history scans to find existing secrets, and provider-side push protection such as GitHub's. Each layer catches what the others miss.

Q4What should you do when you find a leaked secret?

Treat it as compromised immediately and rotate it: issue a new secret, switch all consumers to it, and revoke the old one. Deleting the line or rewriting history does not undo the leak because the secret may already have been copied. Cleaning history is a secondary step.

Q5Why is deleting a committed secret not enough?

Because it remains in the repository's git history and may already have been cloned, scraped by bots or copied by a person. Only revoking and rotating the credential removes the attacker's ability to use it.

Q6What are Gitleaks and TruffleHog?

Popular open-source secret scanners. Gitleaks is fast and integrates into pre-commit hooks and CI and scans history. TruffleHog is notable for verification, testing whether detected credentials are live. Both can be extended with custom rules for internal token formats.

Q7What is push protection for secrets?

A provider-side control, such as GitHub's, that blocks a push when it detects a recognised secret format, stopping the secret from entering the remote repository at all. It complements local hooks and CI scanning.

Q8How do you prevent secrets from entering code in the first place?

Store secrets in a vault or secret manager and inject them at runtime rather than hard-coding them, use short-lived credentials so leaks expire quickly, and make correct local secret handling easy for developers. Scanning is a safety net, not the primary control.