Secret Patterns and Regular Expressions
What you'll learn: How to use regular expressions to automatically detect hardcoded secrets hiding in your codebase.
Now that you understand why hardcoded secrets are dangerous, you need a way to find them systematically. Imagine searching a library for books containing phone numbers—you'd look for patterns like "(555) 123-4567" rather than reading every word. Regular expressions (regex) let you do the same for secrets in code.
Why Patterns Matter
API keys, passwords, and tokens follow predictable formats. An AWS access key always starts with AKIA followed by 16 alphanumeric characters. A GitHub token begins with ghp_ plus 36 characters. Private keys have distinctive -----BEGIN headers. These consistent structures make them detectable.
Common Secret Patterns
Here are patterns security scanners look for:
AWS Access Key:
AKIA[0-9A-Z]{16}
Generic API Key Pattern:
[a|A][p|P][i|I][_]?[k|K][e|E][y|Y].*['\"][0-9a-zA-Z]{32,45}['\"]
Private Key Headers:
-----BEGIN (RSA|DSA|EC|OPENSSH) PRIVATE KEY-----
High-Entropy Strings (likely secrets): Look for long random-looking strings that have high character variety—often 32+ characters mixing uppercase, lowercase, and numbers.
The Hunt Strategy
Scan for assignment patterns like password = "...", secret: "...", or token="..." combined with suspicious values. Configuration files (.env, config.json, .yaml) are prime hunting grounds.
Key Takeaway: Regular expressions let you automate secret detection by matching the predictable formats of API keys, tokens, and credentials—turning manual code review into systematic security scanning.