ai-shield-core 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/audit/logger.d.ts.map +1 -1
  2. package/dist/audit/logger.js +13 -14
  3. package/dist/audit/types.js +1 -2
  4. package/dist/cache/lru.js +1 -5
  5. package/dist/canary/memory.d.ts +75 -0
  6. package/dist/canary/memory.d.ts.map +1 -0
  7. package/dist/canary/memory.js +194 -0
  8. package/dist/context/wrap-context.d.ts +105 -0
  9. package/dist/context/wrap-context.d.ts.map +1 -0
  10. package/dist/context/wrap-context.js +188 -0
  11. package/dist/cost/anomaly.js +1 -4
  12. package/dist/cost/pricing.d.ts.map +1 -1
  13. package/dist/cost/pricing.js +18 -19
  14. package/dist/cost/tracker.d.ts +19 -1
  15. package/dist/cost/tracker.d.ts.map +1 -1
  16. package/dist/cost/tracker.js +27 -10
  17. package/dist/index.d.ts +31 -2
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +51 -37
  20. package/dist/policy/circuit-breaker.d.ts +70 -0
  21. package/dist/policy/circuit-breaker.d.ts.map +1 -0
  22. package/dist/policy/circuit-breaker.js +376 -0
  23. package/dist/policy/engine.js +1 -5
  24. package/dist/policy/tools.js +4 -8
  25. package/dist/scanner/canary.js +4 -8
  26. package/dist/scanner/chain.js +1 -5
  27. package/dist/scanner/heuristic.d.ts +13 -0
  28. package/dist/scanner/heuristic.d.ts.map +1 -1
  29. package/dist/scanner/heuristic.js +50 -7
  30. package/dist/scanner/ingestion.d.ts +116 -0
  31. package/dist/scanner/ingestion.d.ts.map +1 -0
  32. package/dist/scanner/ingestion.js +452 -0
  33. package/dist/scanner/pii.d.ts.map +1 -1
  34. package/dist/scanner/pii.js +24 -12
  35. package/dist/shield.d.ts.map +1 -1
  36. package/dist/shield.js +34 -26
  37. package/dist/types.d.ts +140 -2
  38. package/dist/types.d.ts.map +1 -1
  39. package/dist/types.js +1 -2
  40. package/package.json +4 -3
  41. package/src/audit/logger.ts +6 -1
  42. package/src/canary/memory.ts +259 -0
  43. package/src/context/wrap-context.ts +304 -0
  44. package/src/cost/pricing.ts +13 -9
  45. package/src/cost/tracker.ts +35 -1
  46. package/src/index.ts +82 -1
  47. package/src/policy/circuit-breaker.ts +449 -0
  48. package/src/scanner/heuristic.ts +49 -2
  49. package/src/scanner/ingestion.ts +550 -0
  50. package/src/scanner/pii.ts +21 -7
  51. package/src/shield.ts +15 -2
  52. package/src/types.ts +175 -2
  53. package/tsconfig.json +2 -1
  54. package/dist/audit/logger.js.map +0 -1
  55. package/dist/audit/types.js.map +0 -1
  56. package/dist/cache/lru.js.map +0 -1
  57. package/dist/cost/anomaly.js.map +0 -1
  58. package/dist/cost/pricing.js.map +0 -1
  59. package/dist/cost/tracker.js.map +0 -1
  60. package/dist/index.js.map +0 -1
  61. package/dist/policy/engine.js.map +0 -1
  62. package/dist/policy/tools.js.map +0 -1
  63. package/dist/scanner/canary.js.map +0 -1
  64. package/dist/scanner/chain.js.map +0 -1
  65. package/dist/scanner/heuristic.js.map +0 -1
  66. package/dist/scanner/pii.js.map +0 -1
  67. package/dist/shield.js.map +0 -1
  68. package/dist/types.js.map +0 -1
@@ -0,0 +1,449 @@
1
+ import type {
2
+ CircuitBreakerConfig,
3
+ CircuitBreakerDecision,
4
+ CircuitState,
5
+ CounterStoreLike,
6
+ ScanContext,
7
+ ToolCall,
8
+ ViolationType,
9
+ } from "../types.js";
10
+
11
+ // ============================================================
12
+ // Circuit Breaker — Tool-Policy Runtime Guard
13
+ //
14
+ // The existing `ToolPolicyScanner` (policy/tools.ts) is a *static*
15
+ // gate: allow / deny lists, manifest pin, dangerous patterns. It
16
+ // runs once per call.
17
+ //
18
+ // The circuit breaker layers *runtime* defense on top:
19
+ // - Rate limit per (tool, scope) within a rolling window.
20
+ // - "Blast radius" cap: max writes per window (for destructive ops).
21
+ // - Trip + cooldown: after N anomalies the tool is blocked for a
22
+ // period regardless of static policy.
23
+ // - Optional Human-In-The-Loop hook for destructive operations
24
+ // ("type the tool name to confirm").
25
+ //
26
+ // Counters can live in-process (default) or in any `ioredis`-shaped
27
+ // store so the breaker tracks state across replicas.
28
+ // ============================================================
29
+
30
+ const DESTRUCTIVE_DEFAULTS = [
31
+ "delete_",
32
+ "remove_",
33
+ "drop_",
34
+ "destroy_",
35
+ "wipe_",
36
+ "shutdown_",
37
+ "purge_",
38
+ "truncate_",
39
+ "send_email",
40
+ "transfer_",
41
+ "payment_",
42
+ ];
43
+
44
+ const DEFAULTS: Required<
45
+ Pick<
46
+ CircuitBreakerConfig,
47
+ "failureThreshold" | "windowMs" | "cooldownMs"
48
+ >
49
+ > = {
50
+ failureThreshold: 5,
51
+ windowMs: 60_000,
52
+ cooldownMs: 60_000,
53
+ };
54
+
55
+ interface InternalState {
56
+ state: CircuitState;
57
+ openedAt: number;
58
+ failures: number[]; // timestamps within current window
59
+ calls: number[]; // timestamps within current window
60
+ writes: number[]; // timestamps within current window
61
+ }
62
+
63
+ class InMemoryCounter implements CounterStoreLike {
64
+ private data = new Map<string, { value: string; expiresAt?: number }>();
65
+
66
+ async get(key: string): Promise<string | null> {
67
+ const e = this.data.get(key);
68
+ if (!e) return null;
69
+ if (e.expiresAt && Date.now() > e.expiresAt) {
70
+ this.data.delete(key);
71
+ return null;
72
+ }
73
+ return e.value;
74
+ }
75
+ async incrbyfloat(key: string, increment: number): Promise<string> {
76
+ const cur = parseFloat((await this.get(key)) ?? "0");
77
+ const next = (cur + increment).toString();
78
+ const e = this.data.get(key);
79
+ this.data.set(key, { value: next, expiresAt: e?.expiresAt });
80
+ return next;
81
+ }
82
+ async expire(key: string, seconds: number): Promise<number> {
83
+ const e = this.data.get(key);
84
+ if (!e) return 0;
85
+ e.expiresAt = Date.now() + seconds * 1000;
86
+ return 1;
87
+ }
88
+ }
89
+
90
+ export interface CircuitBreakerOptions {
91
+ /** Optional distributed counter store (ioredis-compatible). */
92
+ counterStore?: CounterStoreLike;
93
+ /**
94
+ * Cap on the number of (tool, scope) pairs tracked in-process.
95
+ * Prevents unbounded growth in long-lived runtimes. Default: 5_000.
96
+ * Override via env `AI_SHIELD_CIRCUIT_MAX_KEYS`.
97
+ */
98
+ maxKeys?: number;
99
+ }
100
+
101
+ /**
102
+ * Registry of breakers keyed by `${tool}::${scope}`. The registry
103
+ * owns config + state; per-(tool, scope) breakers are created lazily.
104
+ */
105
+ export class CircuitBreakerRegistry {
106
+ private configs = new Map<string, Required<CircuitBreakerConfig>>();
107
+ private states = new Map<string, InternalState>();
108
+ /**
109
+ * Reserved for distributed-counter mode (e.g. cross-replica state).
110
+ * The in-process path is the supported v0.2 surface; the store is
111
+ * accepted so callers wiring up an `ioredis`-shaped backend get a
112
+ * stable constructor option, and downstream releases can swap the
113
+ * internal accounting to use it without breaking the API.
114
+ */
115
+ protected readonly store: CounterStoreLike;
116
+ private readonly maxKeys: number;
117
+
118
+ constructor(
119
+ configs: CircuitBreakerConfig[] = [],
120
+ options: CircuitBreakerOptions = {},
121
+ ) {
122
+ this.store = options.counterStore ?? new InMemoryCounter();
123
+ const envCap = Number(process.env.AI_SHIELD_CIRCUIT_MAX_KEYS);
124
+ this.maxKeys =
125
+ options.maxKeys ??
126
+ (Number.isFinite(envCap) && envCap > 0 ? envCap : 5_000);
127
+ for (const cfg of configs) {
128
+ this.configure(cfg);
129
+ }
130
+ }
131
+
132
+ /** Configure (or re-configure) a breaker. Idempotent. */
133
+ configure(config: CircuitBreakerConfig): void {
134
+ const key = keyFor(config.tool, config.scope);
135
+ this.configs.set(key, {
136
+ tool: config.tool,
137
+ scope: config.scope ?? "",
138
+ failureThreshold:
139
+ config.failureThreshold ?? DEFAULTS.failureThreshold,
140
+ windowMs: config.windowMs ?? DEFAULTS.windowMs,
141
+ cooldownMs: config.cooldownMs ?? DEFAULTS.cooldownMs,
142
+ maxCallsPerWindow: config.maxCallsPerWindow ?? Infinity,
143
+ maxWritesPerWindow: config.maxWritesPerWindow ?? Infinity,
144
+ onDestructive: config.onDestructive ?? (() => true),
145
+ isDestructive:
146
+ config.isDestructive ?? isLikelyDestructive(config.tool),
147
+ });
148
+ }
149
+
150
+ /**
151
+ * Check whether a tool call is allowed. Records the attempt either
152
+ * way; callers must invoke `recordSuccess()`/`recordFailure()` AFTER
153
+ * the actual call so anomaly counts stay honest.
154
+ */
155
+ async check(
156
+ tool: ToolCall,
157
+ context: ScanContext = {},
158
+ ): Promise<CircuitBreakerDecision> {
159
+ const scope = scopeFor(context);
160
+ const key = keyFor(tool.name, scope);
161
+ const config = this.configs.get(key) ?? this.configs.get(keyFor(tool.name, ""));
162
+
163
+ // No config → no breaker → allow. The caller may still use
164
+ // the static ToolPolicyScanner for default deny.
165
+ if (!config) {
166
+ return { allowed: true, state: "closed" };
167
+ }
168
+
169
+ const state = this.getOrInitState(key);
170
+ const now = Date.now();
171
+ prune(state, now, config.windowMs);
172
+
173
+ // 1. Open / half-open transitions.
174
+ if (state.state === "open") {
175
+ if (now - state.openedAt >= config.cooldownMs) {
176
+ state.state = "half-open";
177
+ } else {
178
+ return {
179
+ allowed: false,
180
+ state: "open",
181
+ reason: "circuit_open",
182
+ retryAfterMs: config.cooldownMs - (now - state.openedAt),
183
+ message: `Circuit OPEN for ${tool.name}${scope ? `@${scope}` : ""}`,
184
+ };
185
+ }
186
+ }
187
+
188
+ // 2. Rate-limit cap.
189
+ if (state.calls.length >= config.maxCallsPerWindow) {
190
+ return {
191
+ allowed: false,
192
+ state: state.state,
193
+ reason: "rate_limit",
194
+ retryAfterMs: config.windowMs,
195
+ message: `Rate limit ${config.maxCallsPerWindow}/${config.windowMs}ms exceeded for ${tool.name}`,
196
+ };
197
+ }
198
+
199
+ // 3. Blast-radius cap for destructive tools.
200
+ if (
201
+ config.isDestructive &&
202
+ state.writes.length >= config.maxWritesPerWindow
203
+ ) {
204
+ return {
205
+ allowed: false,
206
+ state: state.state,
207
+ reason: "blast_radius_exceeded",
208
+ retryAfterMs: config.windowMs,
209
+ message: `Blast-radius cap ${config.maxWritesPerWindow}/${config.windowMs}ms hit for ${tool.name}`,
210
+ };
211
+ }
212
+
213
+ // 4. HITL gate for destructive ops.
214
+ //
215
+ // Record the call/write OPTIMISTICALLY first, BEFORE awaiting the
216
+ // HITL hook. Two concurrent destructive calls otherwise both see
217
+ // `state.writes.length === 0` and both get past the blast-radius
218
+ // gate (Critic M3 round 1 — TOCTOU on shared mutable state).
219
+ //
220
+ // Round 2 Critic H-NEW-1: rolling back via `pop()` is unsafe under
221
+ // Node.js's cooperative scheduler — a concurrent push between our
222
+ // push and our pop can shift positions, so `pop()` removes the wrong
223
+ // entry. Capture the SENTINEL value we pushed and remove that exact
224
+ // entry on rollback. Two concurrent rollbacks of identical-now
225
+ // timestamps could theoretically still touch each other's entry,
226
+ // but at worst they remove a sibling rather than letting a counter
227
+ // run away — semantically equivalent for rate-limit purposes.
228
+ const callSentinel: number = now;
229
+ state.calls.push(callSentinel);
230
+ let writeSentinel: number | null = null;
231
+ if (config.isDestructive) {
232
+ writeSentinel = now;
233
+ state.writes.push(writeSentinel);
234
+ }
235
+ const rollbackOptimisticRecord = (): void => {
236
+ // Remove the LAST occurrence of the sentinel (the one we pushed)
237
+ // so concurrent rollbacks don't touch each other's entries.
238
+ const callIdx = state.calls.lastIndexOf(callSentinel);
239
+ if (callIdx >= 0) state.calls.splice(callIdx, 1);
240
+ if (writeSentinel !== null) {
241
+ const writeIdx = state.writes.lastIndexOf(writeSentinel);
242
+ if (writeIdx >= 0) state.writes.splice(writeIdx, 1);
243
+ }
244
+ };
245
+
246
+ if (config.isDestructive) {
247
+ let rawResult: unknown;
248
+ try {
249
+ rawResult = await Promise.resolve(
250
+ config.onDestructive({
251
+ tool: tool.name,
252
+ scope: config.scope,
253
+ context,
254
+ }),
255
+ );
256
+ } catch (err) {
257
+ rollbackOptimisticRecord();
258
+ return {
259
+ allowed: false,
260
+ state: state.state,
261
+ reason: "hitl_denied",
262
+ message: `HITL hook threw: ${(err as Error).message}`,
263
+ };
264
+ }
265
+ // Critic H3 — a hook that returns `undefined` (async function
266
+ // without explicit `return`) or any non-boolean value is the most
267
+ // common HITL footgun. Fail safe AND surface the programming
268
+ // error rather than silently coerce.
269
+ if (typeof rawResult !== "boolean") {
270
+ rollbackOptimisticRecord();
271
+ return {
272
+ allowed: false,
273
+ state: state.state,
274
+ reason: "hitl_denied",
275
+ message: `HITL hook for '${tool.name}' returned non-boolean (${typeof rawResult}); treating as denial`,
276
+ };
277
+ }
278
+ if (!rawResult) {
279
+ rollbackOptimisticRecord();
280
+ return {
281
+ allowed: false,
282
+ state: state.state,
283
+ reason: "hitl_denied",
284
+ message: `Human-in-the-loop denied ${tool.name}`,
285
+ };
286
+ }
287
+ }
288
+
289
+ return { allowed: true, state: state.state };
290
+ }
291
+
292
+ /** Record a successful tool invocation. Closes a half-open breaker. */
293
+ recordSuccess(toolName: string, context: ScanContext = {}): void {
294
+ const scope = scopeFor(context);
295
+ const key = keyFor(toolName, scope);
296
+ const state = this.states.get(key);
297
+ if (!state) return;
298
+ if (state.state === "half-open") {
299
+ state.state = "closed";
300
+ state.failures = [];
301
+ }
302
+ }
303
+
304
+ /**
305
+ * Record a failed tool invocation. Trips the breaker once
306
+ * `failureThreshold` failures accumulate within the window.
307
+ */
308
+ recordFailure(toolName: string, context: ScanContext = {}): void {
309
+ const scope = scopeFor(context);
310
+ const key = keyFor(toolName, scope);
311
+ const config = this.configs.get(key) ?? this.configs.get(keyFor(toolName, ""));
312
+ if (!config) return;
313
+ const state = this.getOrInitState(key);
314
+ const now = Date.now();
315
+ prune(state, now, config.windowMs);
316
+ state.failures.push(now);
317
+
318
+ if (state.failures.length >= config.failureThreshold) {
319
+ state.state = "open";
320
+ state.openedAt = now;
321
+ }
322
+ }
323
+
324
+ /** Manually force a breaker into a state — useful for tests / ops. */
325
+ trip(toolName: string, scope?: string): void {
326
+ const key = keyFor(toolName, scope ?? "");
327
+ const state = this.getOrInitState(key);
328
+ state.state = "open";
329
+ state.openedAt = Date.now();
330
+ }
331
+
332
+ reset(toolName: string, scope?: string): void {
333
+ const key = keyFor(toolName, scope ?? "");
334
+ this.states.delete(key);
335
+ }
336
+
337
+ /** Inspect current state — for dashboards / audit. */
338
+ inspect(toolName: string, scope?: string): {
339
+ state: CircuitState;
340
+ callsInWindow: number;
341
+ writesInWindow: number;
342
+ failuresInWindow: number;
343
+ } | null {
344
+ const key = keyFor(toolName, scope ?? "");
345
+ const state = this.states.get(key);
346
+ const config = this.configs.get(key) ?? this.configs.get(keyFor(toolName, ""));
347
+ if (!state || !config) return null;
348
+ const now = Date.now();
349
+ prune(state, now, config.windowMs);
350
+ return {
351
+ state: state.state,
352
+ callsInWindow: state.calls.length,
353
+ writesInWindow: state.writes.length,
354
+ failuresInWindow: state.failures.length,
355
+ };
356
+ }
357
+
358
+ /** Suggested ViolationType for a denied decision — useful in audit logs. */
359
+ static violationType(decision: CircuitBreakerDecision): ViolationType {
360
+ if (decision.reason === "circuit_open") return "circuit_breaker_open";
361
+ if (decision.reason === "blast_radius_exceeded")
362
+ return "blast_radius_exceeded";
363
+ if (decision.reason === "rate_limit") return "tool_rate_limit";
364
+ return "tool_denied";
365
+ }
366
+
367
+ // --- internal ---
368
+
369
+ private getOrInitState(key: string): InternalState {
370
+ let state = this.states.get(key);
371
+ if (state) {
372
+ // Touch — promote to MRU. JS Map preserves insertion order;
373
+ // delete + set moves the entry to the tail (Analyst A5 round 1).
374
+ this.states.delete(key);
375
+ this.states.set(key, state);
376
+ return state;
377
+ }
378
+ // True-LRU eviction: oldest key (head of Map) is dropped first.
379
+ // Combined with the touch-on-access above this gives correct LRU
380
+ // semantics and prevents key-explosion attacks from evicting
381
+ // long-lived legitimate breakers.
382
+ if (this.states.size >= this.maxKeys) {
383
+ const oldestKey = this.states.keys().next().value;
384
+ if (oldestKey) this.states.delete(oldestKey);
385
+ }
386
+ state = {
387
+ state: "closed",
388
+ openedAt: 0,
389
+ failures: [],
390
+ calls: [],
391
+ writes: [],
392
+ };
393
+ this.states.set(key, state);
394
+ return state;
395
+ }
396
+ }
397
+
398
+ // --- helpers ---
399
+
400
+ // NUL byte cannot appear in valid tool names or agent/session IDs.
401
+ // `keyFor` uses TWO NULs as the tool↔scope boundary; `makeBreakerScope`
402
+ // uses ONE NUL between agentId and sessionId. Two-NUL boundary disambig-
403
+ // uates tool name from scope payload even when the scope itself contains
404
+ // a single NUL — Analyst A6 round 1 + Critic L-NEW-1 round 2.
405
+ // Callers MUST go through `makeBreakerScope()` rather than handcraft
406
+ // scope strings; passing a string that contains `\x00\x00` would alias
407
+ // the boundary marker.
408
+ const KEY_SEP = "\x00";
409
+
410
+ function keyFor(tool: string, scope?: string): string {
411
+ return `${tool}${KEY_SEP}${KEY_SEP}${scope ?? ""}`;
412
+ }
413
+
414
+ function scopeFor(context: ScanContext): string {
415
+ return makeBreakerScope(context.agentId, context.sessionId);
416
+ }
417
+
418
+ /**
419
+ * Build the scope string the circuit breaker uses internally for a
420
+ * given (agentId, sessionId) pair. Exposed so callers of `inspect()`,
421
+ * `trip()`, and `reset()` don't have to know the separator convention.
422
+ *
423
+ * @example
424
+ * ```ts
425
+ * const scope = makeBreakerScope("agent-a", "session-1");
426
+ * const snap = registry.inspect("delete_user", scope);
427
+ * ```
428
+ */
429
+ export function makeBreakerScope(
430
+ agentId?: string,
431
+ sessionId?: string,
432
+ ): string {
433
+ if (agentId && sessionId) {
434
+ return `${agentId}${KEY_SEP}${sessionId}`;
435
+ }
436
+ return agentId ?? sessionId ?? "";
437
+ }
438
+
439
+ function prune(state: InternalState, now: number, windowMs: number): void {
440
+ const cutoff = now - windowMs;
441
+ state.failures = state.failures.filter((t) => t >= cutoff);
442
+ state.calls = state.calls.filter((t) => t >= cutoff);
443
+ state.writes = state.writes.filter((t) => t >= cutoff);
444
+ }
445
+
446
+ function isLikelyDestructive(toolName: string): boolean {
447
+ const lc = toolName.toLowerCase();
448
+ return DESTRUCTIVE_DEFAULTS.some((prefix) => lc.startsWith(prefix));
449
+ }
@@ -3,8 +3,48 @@ import type { Scanner, ScannerResult, ScanContext, Violation } from "../types.js
3
3
  // ============================================================
4
4
  // Heuristic Prompt Injection Scanner
5
5
  // Score-based: multiple matches = higher confidence
6
+ // Unicode-normalizes input before pattern matching so that
7
+ // homoglyph/zero-width/fullwidth evasion attempts still hit.
6
8
  // ============================================================
7
9
 
10
+ // Common Cyrillic/Greek Latin-lookalikes mapped to ASCII.
11
+ // Keep minimal — false-mappings in real content are worse than
12
+ // false-negatives in an attack attempt.
13
+ const HOMOGLYPH_MAP: Record<string, string> = {
14
+ "а": "a", "е": "e", "і": "i", "ј": "j", "о": "o", "р": "p", "с": "c", "ѕ": "s",
15
+ "у": "y", "х": "x", "А": "A", "В": "B", "Е": "E", "І": "I", "К": "K", "М": "M",
16
+ "Н": "H", "О": "O", "Р": "P", "С": "C", "Т": "T", "Х": "X",
17
+ "α": "a", "ο": "o", "ρ": "p", "ε": "e", "υ": "y", "χ": "x", "Α": "A", "Β": "B",
18
+ "Ε": "E", "Ζ": "Z", "Η": "H", "Ι": "I", "Κ": "K", "Μ": "M", "Ν": "N", "Ο": "O",
19
+ "Ρ": "P", "Τ": "T", "Υ": "Y", "Χ": "X",
20
+ };
21
+
22
+ const HOMOGLYPH_RE = new RegExp(Object.keys(HOMOGLYPH_MAP).join("|"), "g");
23
+ // Zero-width chars + BOM — used to split words like "ig<ZWSP>nore" across
24
+ // the pattern boundary (U+200B..U+200D, U+2060, U+FEFF).
25
+ const ZERO_WIDTH_RE = /[​-‍⁠]/g;
26
+ // Combining marks (diacritics) after NFKC can still slip through (U+0300..U+036F).
27
+ const COMBINING_RE = /[̀-ͯ]/g;
28
+
29
+ /**
30
+ * Normalize input for pattern matching. Returns the canonicalized string
31
+ * used only for scan decisions; the sanitized output passed to callers
32
+ * is still the original input.
33
+ *
34
+ * Order matters:
35
+ * 1. NFKD folds compatibility forms (fullwidth → ASCII, ligatures) AND
36
+ * decomposes precomposed accented letters into base + combining mark.
37
+ * 2. Strip zero-width chars so "ig<ZWSP>nore" collapses to "ignore".
38
+ * 3. Strip combining marks (diacritics) left behind by NFKD.
39
+ * 4. Map remaining Cyrillic/Greek look-alikes to Latin.
40
+ */
41
+ export function normalizeForInjectionScan(input: string): string {
42
+ const nfkd = input.normalize("NFKD");
43
+ const noZW = nfkd.replace(ZERO_WIDTH_RE, "");
44
+ const noCombining = noZW.replace(COMBINING_RE, "");
45
+ return noCombining.replace(HOMOGLYPH_RE, (ch) => HOMOGLYPH_MAP[ch] ?? ch);
46
+ }
47
+
8
48
  interface PatternRule {
9
49
  id: string;
10
50
  category: InjectionCategory;
@@ -357,8 +397,13 @@ export class HeuristicScanner implements Scanner {
357
397
  const violations: Violation[] = [];
358
398
  let totalScore = 0;
359
399
 
400
+ // Normalize once — pattern matching runs against the canonical form so
401
+ // homoglyph/zero-width evasion doesn't bypass the rules. The caller
402
+ // still sees the original input in `sanitized`.
403
+ const normalized = normalizeForInjectionScan(input);
404
+
360
405
  for (const rule of this.patterns) {
361
- if (rule.pattern.test(input)) {
406
+ if (rule.pattern.test(normalized)) {
362
407
  totalScore += rule.weight;
363
408
  violations.push({
364
409
  type: "prompt_injection",
@@ -371,7 +416,9 @@ export class HeuristicScanner implements Scanner {
371
416
  }
372
417
  }
373
418
 
374
- // Structural signals (cumulative)
419
+ // Structural signals (cumulative) — intentionally run on the original
420
+ // input so real structural attacks (many newlines, long paddings) can
421
+ // still trip even when the textual patterns were evaded.
375
422
  const structuralScore = this.checkStructuralSignals(input);
376
423
  totalScore += structuralScore;
377
424