/** * CompressionDetector * * Uses Normalized Compression Distance (NCD) to detect prompt injection * by measuring structural similarity between input and known attack templates. * * Technique: "Embedding similarity without embeddings" * - Uses Node.js built-in zlib (zero external dependencies) * - Compresses input concatenated with each attack template * - Similar strings compress more efficiently together → lower NCD score * - NCD ∈ [0, 1]: 0 = identical structure, 1 = maximally dissimilar * * Formula: NCD(x, y) = (C(xy) - min(C(x), C(y))) / max(C(x), C(y)) * * Research basis: * - "Low-Resource Text Classification with Compressors" (ACL 2023) * - PromptGuard layered detection (Nature Scientific Reports 2025) * * Expected improvement: +3-5% F1 over regex-only detection */ export interface CompressionDetectorConfig { /** NCD threshold below which input is flagged as attack-similar (0-1, default: 0.55) */ threshold?: number; /** Maximum number of templates to check per category (default: all) */ maxTemplateChecks?: number; /** Custom attack templates to add to the built-in corpus */ customTemplates?: Array<{ category: string; template: string; }>; /** Minimum input length to analyze (default: 20) */ minInputLength?: number; /** Maximum input length to analyze — truncates longer inputs (default: 2000) */ maxInputLength?: number; } export interface CompressionDetectorResult { allowed: boolean; reason?: string; violations: string[]; ncdAnalysis: { /** Lowest NCD score found across all templates */ minNCD: number; /** Category of the closest-matching template */ closestCategory: string; /** Average NCD across all checked templates */ avgNCD: number; /** Number of templates checked */ templatesChecked: number; /** Time taken in milliseconds */ timeMs: number; }; } /** * CompressionDetector — NCD-based prompt injection detection * * "Embedding similarity without embeddings" — uses gzip compression * to measure structural similarity between input and known attack templates. */ export declare class CompressionDetector { private config; private templates; constructor(config?: CompressionDetectorConfig); /** * Detect if input is structurally similar to known attack templates */ detect(input: string): CompressionDetectorResult; /** * Compute NCD between input and a template * NCD(x, y) = (C(xy) - min(C(x), C(y))) / max(C(x), C(y)) */ private ncd; /** * Get compressed length of text using deflateRaw (no gzip header overhead) */ private compressedLength; /** * Get the number of built-in templates */ get templateCount(): number; /** * Get categories and their template counts */ get categories(): Record; }