This guide explains how to contribute new patterns to Redflag and outlines best practices for pattern development.
Each pattern in Redflag is defined by four components:
[[patterns]]
name = "pattern-name"
pattern = "regex-pattern"
description = "Human-readable description"
severity = "Critical" # Options: Critical, High, Medium, Lowname: A unique identifier for the pattern (kebab-case recommended)pattern: A regular expression that matches the secretdescription: A clear description of what the pattern detectsseverity: The risk level of the detected secret
- Credentials that provide direct access to sensitive systems
- Examples: AWS keys, database passwords, private keys
- Immediate action required
- Sensitive information that could be part of a larger attack
- Examples: API keys, OAuth tokens, encryption keys
- Action required soon
- Potentially sensitive information requiring review
- Examples: Internal URLs, non-production credentials
- Should be reviewed
- Items that should be checked but may be acceptable
- Examples: Test credentials, documentation tokens
- Review when convenient
❌ Bad:
pattern = "password=.*" # Too broad, many false positives✅ Good:
pattern = '''(?i)password\s*=\s*['""][^'""]{8,}['""]''' # Specific format- Use
(?i)prefix for case-insensitive matching where appropriate - Consider variations in naming (e.g.,
api_key,apikey,api-key)
pattern = '''(?i)api[_-]?key\s*=\s*['""][a-zA-Z0-9]{32,}['""]'''- Consider different assignment operators (
=,:,=>) - Account for various quote types (
',",""") - Allow for flexible whitespace with
\s*
pattern = '''(?i)(api[_-]?key|access[_-]?token)\s*[:=]>\s*['""][a-zA-Z0-9-_]{32,}['""]'''- Include minimum length requirements for secrets
- Use quantifiers to prevent short matches
- Consider maximum lengths for specific formats
pattern = '''(?i)github[_-]?token\s*=\s*gh[pousr]_[a-zA-Z0-9]{36}''' # Exact GitHub token length[[patterns]]
name = "generic-api-key"
pattern = '''(?i)api[_-]?key\s*=\s*['""][a-zA-Z0-9-_]{32,}['""]'''
description = "Generic API key with minimum length of 32 characters"[[patterns]]
name = "oauth-token"
pattern = '''(?i)(oauth|access)[_-]?token\s*=\s*['""][a-zA-Z0-9-_]{32,}['""]'''
description = "OAuth or Access Token"[[patterns]]
name = "database-url"
pattern = '''(?i)(mongodb|postgresql|mysql)://([\w-]+:[\w-]+@)?[\w.-]+[:]\d+/[\w-]+'''
description = "Database connection string with potential credentials"[[patterns]]
name = "private-key"
pattern = '''-----BEGIN\s+(RSA|DSA|EC|OPENSSH)\s+PRIVATE\s+KEY(\s+ENCRYPTED)?-----'''
description = "Private key file header"- Create a test file with both positive and negative examples
- Test the pattern against real-world examples
- Verify minimal false positives
- Check performance impact
Build a test file without storing complete synthetic secrets in the repository:
api_key_first="abcd1234efgh5678"
api_key_second="ijkl9012mnop3456"
token_first="zyxw9876vutsrqpon"
token_second="mlkjihgfedcba"
printf 'API_KEY="%s%s"\n' "$api_key_first" "$api_key_second" > pattern-fixture.env
printf "access_token='%s%s'\n" "$token_first" "$token_second" >> pattern-fixture.env
printf 'api_prefix="test"\nnot_an_api_key="short"\n' >> pattern-fixture.envBefore submitting a pattern:
- Uniqueness: Ensure it doesn't duplicate existing patterns
- Performance: Test with large codebases to verify performance
- False Positives: Minimise false positives with specific matches
- Documentation: Include clear description and examples
- Over-matching: Patterns that are too broad
- Under-matching: Missing common variations
- Performance Issues: Complex regex with excessive backtracking
- False Positives: Not accounting for common code patterns
- Fork the repository
- Add your pattern to
redflag.example.toml - Add tests for your pattern
- Submit a pull request with:
- Pattern description
- Example matches
- Test cases
- Use case explanation
Use these tools to test your patterns:
- regex101.com - Interactive regex testing
- regexr.com - Visual regex explanation
- Local testing:
# Test your pattern redflag scan --config your-pattern.toml ./test-dir
- Open an issue for pattern discussion
- Join our community discussions
- Check existing patterns for examples
Remember: Security tools are only as good as their patterns. Help us improve Redflag by contributing high-quality, well-tested patterns!