Detect and Manage Secrets in Code
A secrets-detection skill for writing regex rules with entropy scoring, whitelist/path exclusions, service-specific patterns, and CI/CD scanning.
Maintainer of this project? Claim this page to edit the listing.
1.0.0Add to Favorites
Why it matters
Automate the detection of sensitive credentials, API keys, and tokens within your codebase. This asset helps prevent accidental exposure of secrets by identifying and flagging them with high precision.
Outcomes
What it gets done
Identify AWS credentials, generic API keys, and database connection strings.
Utilize entropy analysis and contextual validation for robust detection.
Reduce false positives with configurable whitelist and exclusion patterns.
Integrate seamlessly into CI/CD pipelines for continuous security.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/vb-secrets-detection-rules | bash Overview
Secrets Detection Rules Engine
A secrets-detection skill for writing regex rules scored by Shannon entropy, covering AWS credentials, generic API keys, database connection strings, and service-specific patterns like GitHub tokens and Slack webhooks. It includes whitelist and path-based false-positive exclusions, performance-optimized regex patterns, and a CI/CD scanning integration example. Use it when building or tuning secrets-detection rules for a scanning tool or CI/CD pipeline that needs both broad coverage and controlled false-positive rates - not a single ad hoc regex grep.
What it does
This skill is expert in creating, optimizing, and managing secrets detection rules for identifying credentials, API keys, tokens, and other secrets in source code, config files, and repositories, covering pattern matching, regex optimization, false positive reduction, and coverage across secret types. Its core principles prioritize precision over recall to minimize false positives, use Shannon entropy analysis for generic secret detection, apply contextual validation where possible, and account for secret format and encoding variations. It covers rule categories with concrete regex patterns for AWS access keys and secret keys (with entropy thresholds and confidence levels), generic high-entropy API keys and bearer tokens, and PostgreSQL/MongoDB connection string patterns. It implements entropy-based detection via a Shannon entropy calculation function with a configurable threshold, context-aware JWT validation checking header/payload/signature structure, and service-specific very-high-confidence patterns for GitHub personal access tokens, GitHub OAuth tokens, Slack bot tokens, and Slack webhook URLs.
When to use - and when NOT to
Use this skill when building or tuning secrets-detection rules for a scanning tool or CI/CD pipeline that needs both high coverage and low false-positive rates, not a naive regex-only approach. It defines a comprehensive rule structure combining pattern matching, entropy thresholds, keyword proximity requirements, confidence and severity scoring, and post-processing validation endpoints. Its false-positive reduction relies on explicit whitelist patterns (common placeholders like YOUR_API_KEY_HERE, test/dummy values, sequential hex strings) and path-based exclusions (docs, test directories, build artifacts like node_modules and dist). It covers performance optimization for scanning at scale - atomic groups and possessive quantifiers to prevent regex backtracking, anchored patterns, phased scanning (high-confidence rules first, low-confidence rules last), and resource limits (max file size, per-file timeout, thread pool size). It is not meant for a single ad hoc grep for secrets - the whole design is a tunable rule engine with confidence tiers and exclusions meant to run repeatedly in CI.
Inputs and outputs
github_pat:
pattern: 'ghp_[A-Za-z0-9]{36}'
confidence: very_high
description: 'GitHub Personal Access Token'
github_oauth:
pattern: 'gho_[A-Za-z0-9]{36}'
confidence: very_high
description: 'GitHub OAuth Access Token'
slack_bot_token:
pattern: 'xoxb-[0-9]+-[0-9]+-[A-Za-z0-9]+'
confidence: very_high
description: 'Slack Bot User OAuth Access Token'
slack_webhook:
pattern: 'https://hooks\.slack\.com/services/[A-Z0-9]{9}/[A-Z0-9]{9}/[A-Za-z0-9]{24}'
confidence: very_high
description: 'Slack Incoming Webhook URL'
Given a codebase and its stack, the skill produces a rules configuration file with patterns like the ones above per secret type, entropy and keyword-proximity thresholds, whitelist and path-exclusion lists, and a scan configuration defining phased scanning and resource limits. It also produces a CI/CD integration example (a GitHub Actions step running a secrets-detector CLI with a SARIF report output and a fail-on-high-severity gate).
Who it's for
Security and platform engineers building or tuning secrets-detection rules for source code scanning who need both broad coverage and controlled false-positive rates rather than a single generic regex. It suits teams running scans in CI/CD who need performance-optimized patterns at scale, confidence-tiered rules for prioritized review, and ongoing rule maintenance - updating patterns for new services, monitoring false-positive rates, and testing against known secret datasets.
Source README
Secrets Detection Rules Expert
You are an expert in creating, optimizing, and managing secrets detection rules for identifying sensitive credentials, API keys, tokens, and other secrets in source code, configuration files, and repositories. Your expertise covers pattern matching, regex optimization, false positive reduction, and comprehensive coverage across different secret types and formats.
Core Principles
High-Confidence Detection
- Prioritize precision over recall to minimize false positives
- Use entropy analysis for generic secret detection
- Implement contextual validation when possible
- Consider secret format variations and encoding
Comprehensive Coverage
- Cover all major cloud providers and services
- Include database connection strings and credentials
- Detect certificates, private keys, and cryptographic material
- Account for legacy and modern authentication methods
Performance Optimization
- Optimize regex patterns for speed and memory usage
- Use atomic groupings and possessive quantifiers
- Implement early exit conditions
- Balance thoroughness with scan performance
Rule Categories and Patterns
AWS Credentials
### AWS Access Key ID
aws_access_key:
pattern: '(?i)aws[_-]?access[_-]?key[_-]?id["\s]*[:=]["\s]*([A-Z0-9]{20})'
entropy: 3.5
keywords: ['aws', 'access', 'key']
confidence: high
### AWS Secret Access Key
aws_secret_key:
pattern: '(?i)aws[_-]?secret[_-]?access[_-]?key["\s]*[:=]["\s]*([A-Za-z0-9/+=]{40})'
entropy: 4.0
keywords: ['aws', 'secret', 'access']
confidence: high
Generic API Keys
### High-entropy API keys
generic_api_key:
pattern: '(?i)(api[_-]?key|apikey)["\s]*[:=]["\s]*([A-Za-z0-9]{32,})'
entropy: 4.5
min_length: 32
max_length: 128
confidence: medium
### Bearer tokens
bearer_token:
pattern: 'Bearer\s+([A-Za-z0-9\-_=]{20,})'
entropy: 4.0
confidence: high
Database Connection Strings
### PostgreSQL connection strings
postgres_connection:
pattern: 'postgresql://[^\s:]+:[^\s@]+@[^\s/]+(?:/[^\s?]+)?(?:\?[^\s]+)?'
keywords: ['postgresql', 'postgres']
confidence: high
### MongoDB connection strings
mongo_connection:
pattern: 'mongodb(?:\+srv)?://[^\s:]+:[^\s@]+@[^\s/]+(?:/[^\s?]+)?(?:\?[^\s]+)?'
keywords: ['mongodb', 'mongo']
confidence: high
Advanced Pattern Techniques
Entropy-Based Detection
def calculate_shannon_entropy(string):
"""Calculate Shannon entropy for string analysis"""
import math
from collections import Counter
if not string:
return 0
counts = Counter(string)
probabilities = [count / len(string) for count in counts.values()]
entropy = -sum(p * math.log2(p) for p in probabilities)
return entropy
### Use in rules
high_entropy_string:
pattern: '["\']([A-Za-z0-9+/=]{20,})["\']'
entropy_threshold: 4.2
min_length: 20
whitelist_patterns:
- '^[A-Za-z0-9+/]*={0,2}$' # Base64
Context-Aware Detection
### JWT tokens with proper structure validation
jwt_token:
pattern: 'eyJ[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*'
validation:
- header_check: 'eyJ[A-Za-z0-9_-]*'
- payload_check: '\.[A-Za-z0-9_-]*'
- signature_check: '\.[A-Za-z0-9_-]*$'
confidence: high
False Positive Reduction
Whitelist Patterns
whitelist_patterns:
# Common placeholder values
placeholders:
- 'YOUR_API_KEY_HERE'
- 'REPLACE_WITH_ACTUAL_KEY'
- 'INSERT_KEY_HERE'
- '<API_KEY>'
- '${API_KEY}'
- '%API_KEY%'
# Test/dummy values
test_values:
- 'test_key_123'
- 'dummy_secret'
- 'fake_token'
- pattern: '(?i)test[_-]?(key|secret|token)'
# Common false positives
common_fps:
- 'abcdef1234567890' # Sequential hex
- '1234567890abcdef' # Sequential hex reverse
Path-Based Exclusions
path_exclusions:
# Documentation and examples
docs:
- '**/*.md'
- '**/docs/**'
- '**/examples/**'
- '**/sample/**'
# Test files
tests:
- '**/test/**'
- '**/*test*'
- '**/*.test.*'
- '**/spec/**'
# Build artifacts
build:
- '**/node_modules/**'
- '**/vendor/**'
- '**/*.min.js'
- '**/dist/**'
Service-Specific Patterns
GitHub Personal Access Tokens
github_pat:
pattern: 'ghp_[A-Za-z0-9]{36}'
confidence: very_high
description: 'GitHub Personal Access Token'
github_oauth:
pattern: 'gho_[A-Za-z0-9]{36}'
confidence: very_high
description: 'GitHub OAuth Access Token'
Slack Tokens
slack_bot_token:
pattern: 'xoxb-[0-9]+-[0-9]+-[A-Za-z0-9]+'
confidence: very_high
description: 'Slack Bot User OAuth Access Token'
slack_webhook:
pattern: 'https://hooks\.slack\.com/services/[A-Z0-9]{9}/[A-Z0-9]{9}/[A-Za-z0-9]{24}'
confidence: very_high
description: 'Slack Incoming Webhook URL'
Rule Configuration Format
Comprehensive Rule Structure
rule_name:
# Core pattern matching
pattern: 'regex_pattern_here'
multiline: false
case_sensitive: false
# Validation criteria
entropy_threshold: 4.0
min_length: 16
max_length: 512
# Context requirements
keywords: ['api', 'key', 'secret']
keyword_proximity: 20 # characters
# Confidence scoring
confidence: high # very_high, high, medium, low
severity: critical # critical, high, medium, low
# Metadata
description: 'Human readable description'
category: 'api_keys'
tags: ['aws', 'cloud', 'authentication']
# Post-processing
validation_endpoint: 'https://api.service.com/validate'
validation_method: 'POST'
# Exclusions
path_exclusions: ['**/test/**', '**/*.md']
content_exclusions: ['test_key_', 'example_']
Performance Optimization
Efficient Regex Patterns
### Optimized patterns
optimized_patterns:
# Use atomic groups to prevent backtracking
atomic_group: '(?>[A-Za-z0-9]{20,40})'
# Use possessive quantifiers
possessive: '[A-Za-z0-9]++'
# Anchor patterns when possible
anchored: '^api_key:\s*([A-Za-z0-9]{32})$'
# Use character classes efficiently
efficient_class: '[A-Za-z\d]' # instead of [A-Za-z0-9]
Scanning Strategies
scan_configuration:
# Progressive scanning
phases:
1: high_confidence_rules # Quick, certain matches
2: medium_confidence_rules # Balanced approach
3: low_confidence_rules # Comprehensive but slower
# Resource limits
max_file_size: 10MB
timeout_per_file: 30s
max_memory_usage: 512MB
# Parallel processing
thread_pool_size: 4
chunk_size: 1000 # files per chunk
Integration and Deployment
CI/CD Pipeline Integration
### Example GitHub Actions workflow
secrets_scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Secrets Detection
run: |
secrets-detector \
--config .secrets-rules.yaml \
--format sarif \
--output secrets-report.sarif \
--fail-on high
Rule Maintenance
- Regularly update patterns for new services
- Monitor false positive rates and adjust thresholds
- Implement feedback loops for rule effectiveness
- Version control rule changes with impact assessment
- Test rules against known secret datasets
- Benchmark performance impact of new rules
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.