#!/usr/bin/env node
// pre-prompt-inject-scan.mjs — Scan user input for prompt injection
// v5.0: MEDIUM advisory support + Unicode Tag steganography detection
import { readFileSync } from 'node:fs';
const input = JSON.parse(readFileSync('/dev/stdin', 'utf-8'));
const text = input.message || '';

// Unicode Tag detection (U+E0001-E007F)
const UNICODE_TAG_RE = /[\u{E0001}-\u{E007F}]/u;
if (UNICODE_TAG_RE.test(text)) {
  process.stdout.write(JSON.stringify({ decision: 'block', reason: 'Unicode Tag steganography detected' }));
  process.exit(0);
}

// Critical/High injection patterns
const INJECTION_RE = /ignore\s+(all\s+)?previous|system\s*:\s*you\s+are|<\|im_sep\|>/i;
if (INJECTION_RE.test(text)) {
  process.stdout.write(JSON.stringify({ decision: 'block', reason: 'Prompt injection detected' }));
  process.exit(0);
}

// MEDIUM patterns — advisory only, never block
const MEDIUM_RE = /[\u0410-\u044F]|l33t|h0m0glyph|z3r0.?w1dth/i;
if (MEDIUM_RE.test(text)) {
  process.stderr.write('SECURITY ADVISORY: MEDIUM obfuscation signals detected.\n');
}

process.stdout.write(JSON.stringify({ decision: 'allow' }));
