Skip to main content

Overview

The PatternScanner class performs regex-based pattern detection to identify hardcoded secrets, PII, and other privacy issues.

Constructor

new PatternScanner()
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

Usage Examples

Basic Pattern Scanning

const PatternScanner = require('kafkacode/dist/PatternScanner');

const scanner = new PatternScanner();
const code = `
  const apiKey = "AKIAIOSFODNN7EXAMPLE";
  const email = "[email protected]";
`;

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