Documentation Index
Fetch the complete documentation index at: https://docs.kafkalabs.com/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The PatternScanner class performs regex-based pattern detection to identify hardcoded secrets, PII, and other privacy issues.
Constructor
Example:
const PatternScanner = require('kafkacode/dist/PatternScanner');
const scanner = new PatternScanner();
Methods
scanContent(filePath, content)
Scans file content for privacy issues using regex patterns.
scanContent(filePath: string, content: string): Finding[]
Parameters:
filePath (string): Path to the file being scanned
content (string): File content to scan
Returns: Array of Finding objects
Example:
const fs = require('fs');
const PatternScanner = require('kafkacode/dist/PatternScanner');
const scanner = new PatternScanner();
const content = fs.readFileSync('./src/config.js', 'utf-8');
const findings = scanner.scanContent('./src/config.js', content);
console.log(`Found ${findings.length} pattern matches`);
Detection Patterns
The PatternScanner uses these regex patterns:
Critical Patterns
High Severity
Medium Severity
Low Severity
// AWS Access Keys
/AKIA[0-9A-Z]{16}/g
// Private Keys
/-----BEGIN (RSA |EC )?PRIVATE KEY-----/g
// Stripe Keys
/sk_live_[0-9a-zA-Z]{24}/g
// Generic API Keys
/api[_-]?key.*?[=:]\s*['""]([a-zA-Z0-9_\-]+)['""]$/gmi
// OAuth Tokens
/ghp_[a-zA-Z0-9]{36}/g // GitHub
/glpat-[a-zA-Z0-9\-_]{20}/g // GitLab
// Email Addresses
/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g
// Phone Numbers
/\+?[1-9]\d{1,14}/g
// High Entropy Strings
// Calculated programmatically
// IP Addresses
/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g
// URLs
/https?:\/\/[^\s]+/g
Usage Examples
Basic Pattern Scanning
const PatternScanner = require('kafkacode/dist/PatternScanner');
const scanner = new PatternScanner();
const code = `
const apiKey = "AKIAIOSFODNN7EXAMPLE";
const email = "admin@company.com";
`;
const findings = scanner.scanContent('test.js', code);
findings.forEach(finding => {
console.log(`${finding.severity}: ${finding.description}`);
});
Custom Pattern Detection
class CustomScanner extends PatternScanner {
constructor() {
super();
// Add custom patterns
this.patterns.customSecret = {
regex: /MY_SECRET_[A-Z0-9]{16}/g,
severity: 'critical',
type: 'Custom Secret',
recommendation: 'Move to environment variables'
};
}
}
const scanner = new CustomScanner();
const findings = scanner.scanContent('file.js', content);
Next Steps
LLMAnalyzer
AI-powered contextual analysis
Detection Methods
Learn about detection patterns