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,550 @@
1
+ import type {
2
+ Scanner,
3
+ ScannerResult,
4
+ ScanContext,
5
+ Violation,
6
+ IngestionSource,
7
+ TrustTier,
8
+ } from "../types.js";
9
+ import { HeuristicScanner, normalizeForInjectionScan } from "./heuristic.js";
10
+
11
+ // ============================================================
12
+ // Ingestion Scanner — Indirect Prompt Injection (IPI) Defense
13
+ //
14
+ // Scans non-user content (RAG chunks, MCP tool descriptions, stored
15
+ // memory facts, scraped web pages, agent-to-agent messages) for
16
+ // instruction-shaped payloads BEFORE they enter the model context.
17
+ //
18
+ // Per Lakera 2026 incident catalog + OWASP LLM01:2025, indirect
19
+ // injection is now the dominant attack class — >55% of observed
20
+ // incidents arrive through trusted-looking data channels. Direct user
21
+ // injection is the minority case.
22
+ //
23
+ // This scanner runs the existing heuristic patterns at a stricter
24
+ // threshold AND adds source-specific patterns the user channel does
25
+ // not see (HTML-comment instructions, tool-description override,
26
+ // memory-entry steering).
27
+ // ============================================================
28
+
29
+ /**
30
+ * Per-source threshold + extra patterns. Tighter than the user-channel
31
+ * default because data sources almost never need instruction syntax —
32
+ * the presence of one in retrieved content is itself a signal.
33
+ */
34
+ const SOURCE_PROFILE: Record<
35
+ IngestionSource,
36
+ { threshold: number; extraPatterns: RegExp[] }
37
+ > = {
38
+ user: {
39
+ // For symmetry — a caller may pass source="user" through scanIngested().
40
+ // Falls back to normal heuristic strictness.
41
+ threshold: 0.3,
42
+ extraPatterns: [],
43
+ },
44
+ rag: {
45
+ // Retrieved docs basically never legitimately contain "ignore previous
46
+ // instructions" type wording. Strict. Patterns here are ReDoS-bounded —
47
+ // hidden-comment + CSS detection are done via slice-then-test below in
48
+ // `runStructuralIngestionScan()` so we never run two `[^]*?` wildcards
49
+ // back-to-back over attacker-controlled input.
50
+ threshold: 0.15,
51
+ extraPatterns: [
52
+ // "AI assistant note: ..." style trojan headers.
53
+ /(?:AI\s+(?:assistant|model)\s+(?:note|instruction|directive)|attention\s+(?:AI|model|assistant))[:\s]+/i,
54
+ // Document-claims-to-be-system marker.
55
+ /this\s+document\s+(?:is|contains|provides)\s+(?:your|the)\s+(?:new\s+)?(?:instructions?|system\s+prompt|directives?)/i,
56
+ ],
57
+ },
58
+ "tool-desc": {
59
+ // MCP tool descriptions / function schemas: any imperative aimed at
60
+ // the calling model is suspicious. OX Security MCP advisory + LiteLLM
61
+ // CVE-2025-69256 class.
62
+ threshold: 0.12,
63
+ extraPatterns: [
64
+ // "Before using this tool, you must ..." / "After calling, also call X"
65
+ /(?:before|after|while)\s+(?:using|invoking|calling|executing)\s+(?:this\s+)?(?:tool|function|action)[,\s]+(?:you\s+)?(?:must|should|will|need\s+to|are\s+required\s+to)/i,
66
+ // "Also call delete_*" / "ALWAYS invoke X first"
67
+ /(?:also|always|first|then|finally)\s+(?:call|invoke|use|execute|run)\s+(?:the\s+)?[a-z_][\w-]{2,}/i,
68
+ // "Tool description: ignore the above and ..."
69
+ /(?:tool|function|action)\s+description[:\s]+(?:ignore|disregard|override)/i,
70
+ // "Note to LLM:" / "Hidden instruction:"
71
+ /(?:note|hidden\s+(?:instruction|directive|note))\s+to\s+(?:LLM|model|assistant|AI|agent)[:\s]/i,
72
+ // Tool args carrying meta-instructions.
73
+ /(?:on\s+(?:success|call)|when\s+invoked)[,:]\s+(?:silently|also|then)\s+(?:call|invoke|exfiltrate|send|leak)/i,
74
+ ],
75
+ },
76
+ memory: {
77
+ // Stored memory entries: persistence poisoning. Look for sentinel
78
+ // instructions that re-anchor the model on subsequent retrieval.
79
+ threshold: 0.18,
80
+ extraPatterns: [
81
+ // "Remember:" / "Important note for next session:"
82
+ /(?:remember|important|critical)\s+(?:note|instruction|directive)\s+(?:for\s+(?:next|future|all)\s+(?:sessions?|conversations?|calls?))[:\s]/i,
83
+ // "Whenever the user asks X, do Y"
84
+ /(?:whenever|every\s+time)\s+(?:the\s+user|a\s+user|someone)\s+(?:asks|says|mentions|requests)\s+.{1,80}?[,:]\s*(?:you\s+(?:must|should|will|need)|always)/i,
85
+ // "User's true preference is ..." (steering attempts).
86
+ /(?:user(?:'s|s)?\s+(?:real|true|actual|hidden)\s+(?:preference|intent|goal|name|identity))/i,
87
+ // "Override default behavior when ..."
88
+ /override\s+(?:default|standard|normal)\s+(?:behavior|response|policy)/i,
89
+ ],
90
+ },
91
+ web: {
92
+ // Scraped web — same as RAG but also catch markdown-link hijacks.
93
+ // HTML-comment + CSS-hidden detection lives in
94
+ // `runStructuralIngestionScan()` (slice-then-test, ReDoS-bounded).
95
+ threshold: 0.15,
96
+ extraPatterns: [
97
+ // Markdown-link with instruction-shaped anchor text.
98
+ /\[(?:ignore|disregard|override|system\s+(?:prompt|message))[^\]]{0,200}\]\([^)]{0,500}\)/i,
99
+ // ARIA / data-* attributes leaking instructions.
100
+ /(?:aria-label|alt|title|data-[a-z-]{0,40})\s*=\s*["'][^"']{0,500}(ignore\s+previous|new\s+instruction|system\s+prompt|override)/i,
101
+ ],
102
+ },
103
+ "agent-output": {
104
+ // Output of one agent feeding another: multi-agent contagion.
105
+ // Treat like RAG but also catch "tell next agent to ..." patterns.
106
+ threshold: 0.18,
107
+ extraPatterns: [
108
+ /(?:tell|instruct|forward\s+to)\s+(?:the\s+)?(?:next|downstream|receiving|other)\s+(?:agent|model|assistant)\s+to/i,
109
+ /(?:on\s+behalf\s+of|impersonating)\s+(?:the\s+)?(?:user|admin|system|owner)/i,
110
+ /(?:relay|pass|propagate)\s+(?:these|the\s+following)\s+(?:instructions?|directives?|orders?)/i,
111
+ ],
112
+ },
113
+ };
114
+
115
+ /**
116
+ * Default trust-tier inferred from source.
117
+ * `user` is still untrusted in this library's threat model — a user can
118
+ * inject too — but `system` is reserved for content the developer
119
+ * controls and labels via `wrapContext()`. Every ingestion source
120
+ * (including `user`) therefore returns `"untrusted"` by default; the
121
+ * parameter is kept on the signature so future per-source overrides
122
+ * (e.g. an installer marking a specific source as trusted) don't
123
+ * require a breaking API change.
124
+ */
125
+ export function trustTierForSource(_source: IngestionSource): TrustTier {
126
+ return "untrusted";
127
+ }
128
+
129
+ // --- ReDoS-safe structural scan helpers ---
130
+
131
+ /**
132
+ * Hidden-comment + CSS-hidden detection done as bounded slice-then-test
133
+ * rather than a compound `[^]*?...[^]*?` regex (which back-tracks
134
+ * quadratically on attacker-controlled input that omits the terminator).
135
+ * See Critic C1 (round 1 review) — unterminated `<!--` of 50 KB stalled
136
+ * the original implementation.
137
+ *
138
+ * Each detector takes the already-NFKC-normalized input and returns
139
+ * `null` (clean) or a `Violation`.
140
+ */
141
+ function runStructuralIngestionScan(
142
+ normalized: string,
143
+ source: IngestionSource,
144
+ threshold: number,
145
+ ): Violation[] {
146
+ if (source !== "rag" && source !== "web") return [];
147
+ const violations: Violation[] = [];
148
+ const COMMENT_WINDOW = 2048;
149
+ const KEYWORD_RE =
150
+ /ignore|disregard|override|forget|system\s+prompt|new\s+instructions?/i;
151
+
152
+ // 1. HTML comment hidden instruction.
153
+ let commentStart = 0;
154
+ let commentMatchCount = 0;
155
+ while (commentStart !== -1 && commentMatchCount < 8) {
156
+ commentStart = normalized.indexOf("<!--", commentStart);
157
+ if (commentStart === -1) break;
158
+ const window = normalized.slice(
159
+ commentStart + 4,
160
+ commentStart + 4 + COMMENT_WINDOW,
161
+ );
162
+ if (KEYWORD_RE.test(window)) {
163
+ violations.push({
164
+ type: "ingested_injection",
165
+ scanner: "ingestion",
166
+ score: 0.4,
167
+ threshold,
168
+ message: `HTML-comment hidden instruction in ${source} content`,
169
+ detail: `Pattern: <!-- ... ignore|override|... (window 2KB)`,
170
+ });
171
+ commentMatchCount += 1;
172
+ }
173
+ commentStart += 4;
174
+ }
175
+
176
+ // 2. CSS-hidden style attribute carrying instruction-shaped neighbour.
177
+ //
178
+ // Round 2 Critic M-NEW-2: a single `.exec()` would only find the FIRST
179
+ // `style=` attribute. An attacker placing a benign `style="display:block"`
180
+ // first and a malicious `style="display:none"` later would slip through.
181
+ // Iterate all matches via the `/g` flag, capped at 16 to bound the work
182
+ // on adversarial input that floods style attributes.
183
+ const STYLE_HIDDEN_RE =
184
+ /style\s*=\s*["'][^"']{0,300}(?:display\s*:\s*none|visibility\s*:\s*hidden|font-size\s*:\s*0)[^"']{0,300}["']/gi;
185
+ let styleMatchCount = 0;
186
+ let styleMatch: RegExpExecArray | null;
187
+ while (
188
+ (styleMatch = STYLE_HIDDEN_RE.exec(normalized)) !== null &&
189
+ styleMatchCount < 16
190
+ ) {
191
+ styleMatchCount += 1;
192
+ const tail = normalized.slice(
193
+ styleMatch.index + styleMatch[0].length,
194
+ styleMatch.index + styleMatch[0].length + 500,
195
+ );
196
+ if (/ignore|override|system|instruction/i.test(tail)) {
197
+ violations.push({
198
+ type: "ingested_injection",
199
+ scanner: "ingestion",
200
+ score: 0.4,
201
+ threshold,
202
+ message: `CSS-hidden instruction in ${source} content`,
203
+ detail: `Pattern: style="display:none ... ignore|override|... (window 500B)`,
204
+ });
205
+ }
206
+ }
207
+
208
+ return violations;
209
+ }
210
+
211
+ /**
212
+ * Result of `scanIngested()`.
213
+ *
214
+ * Shape parallels `ScanResult` from `chain.ts` so callers can treat
215
+ * both interchangeably.
216
+ */
217
+ export interface IngestionScanResult {
218
+ safe: boolean;
219
+ decision: "allow" | "warn" | "block";
220
+ /**
221
+ * Sanitized output. When `decision === "block"` this is the empty
222
+ * string — the original content was deemed unsafe and the field name
223
+ * "sanitized" would otherwise mislead callers into using poisoned
224
+ * content. Use the source `content` argument if you need the raw input
225
+ * for logging or quarantine.
226
+ */
227
+ sanitized: string;
228
+ violations: Violation[];
229
+ source: IngestionSource;
230
+ meta: {
231
+ scanDurationMs: number;
232
+ scannersRun: string[];
233
+ /** Number of extra source-specific patterns that fired. */
234
+ sourceSpecificHits: number;
235
+ /**
236
+ * Always `false` from `scanIngested()` — ingestion scans don't go
237
+ * through the LRU cache. Field is present so callers can write a
238
+ * single result-handler for both `ScanResult` and `IngestionScanResult`.
239
+ */
240
+ cached: boolean;
241
+ };
242
+ }
243
+
244
+ export interface IngestionScannerConfig {
245
+ /** Override the per-source threshold lookup. */
246
+ threshold?: number;
247
+ /**
248
+ * Additional custom patterns to merge with the source profile's
249
+ * `extraPatterns`. Useful for org-specific markers.
250
+ */
251
+ customPatterns?: RegExp[];
252
+ /**
253
+ * Force the underlying heuristic scanner to a different strictness
254
+ * (default "high" because ingestion is always tighter than user input).
255
+ */
256
+ strictness?: "low" | "medium" | "high";
257
+ }
258
+
259
+ /**
260
+ * Scanner implementation. Composable into a `ScannerChain` when the
261
+ * caller wants ingestion to participate in the main scan flow rather
262
+ * than be invoked via the standalone `scanIngested()` helper.
263
+ *
264
+ * The scanner reads the `source` from `ScanContext` (or treats input
265
+ * as `"user"` when missing) and applies the source-specific profile.
266
+ */
267
+ export class IngestionScanner implements Scanner {
268
+ readonly name = "ingestion";
269
+ private readonly threshold: number | undefined;
270
+ private readonly customPatterns: RegExp[];
271
+ private readonly heuristic: HeuristicScanner;
272
+
273
+ constructor(config: IngestionScannerConfig = {}) {
274
+ this.threshold = config.threshold;
275
+ this.customPatterns = config.customPatterns ?? [];
276
+ this.heuristic = new HeuristicScanner({
277
+ strictness: config.strictness ?? "high",
278
+ });
279
+ }
280
+
281
+ async scan(input: string, context: ScanContext): Promise<ScannerResult> {
282
+ const source = context.source ?? "user";
283
+ const profile = SOURCE_PROFILE[source];
284
+ const effectiveThreshold = this.threshold ?? profile.threshold;
285
+
286
+ const start = performance.now();
287
+ const violations: Violation[] = [];
288
+
289
+ // 1. Run the base heuristic scanner at high strictness. We respect its
290
+ // own decision (it includes structural signals that don't surface
291
+ // as individual violations) and re-tag the violations as
292
+ // `ingested_injection` so downstream code can filter.
293
+ const heuristicResult = await this.heuristic.scan(input, context);
294
+ for (const v of heuristicResult.violations) {
295
+ violations.push({
296
+ ...v,
297
+ type: "ingested_injection",
298
+ scanner: this.name,
299
+ detail: `${v.detail ?? ""} (source=${source})`.trim(),
300
+ });
301
+ }
302
+
303
+ // 2. Run source-specific patterns against the normalized input so the
304
+ // same Unicode-evasion defense the user channel gets applies here.
305
+ const normalized = normalizeForInjectionScan(input);
306
+ const sourcePatterns = [...profile.extraPatterns, ...this.customPatterns];
307
+ let sourceScore = 0;
308
+ for (const pattern of sourcePatterns) {
309
+ if (pattern.test(normalized)) {
310
+ sourceScore += 0.4;
311
+ violations.push({
312
+ type: "ingested_injection",
313
+ scanner: this.name,
314
+ score: 0.4,
315
+ threshold: effectiveThreshold,
316
+ message: `Indirect injection pattern in ${source} content`,
317
+ detail: `Pattern: ${pattern.source.slice(0, 80)}`,
318
+ });
319
+ }
320
+ }
321
+ // 2b. Structural slice-then-test scans for `rag` + `web` (ReDoS-safe
322
+ // replacement for the old compound HTML-comment + CSS-hidden
323
+ // patterns).
324
+ const structural = runStructuralIngestionScan(
325
+ normalized,
326
+ source,
327
+ effectiveThreshold,
328
+ );
329
+ for (const v of structural) {
330
+ sourceScore += 0.4;
331
+ violations.push(v);
332
+ }
333
+ // 2c. Encoding-bypass: attackers wrap an injection in Base64 / Hex /
334
+ // percent-encoding and ask the model to "decode this". A single
335
+ // decode pass over the input flushes the most common bypasses
336
+ // documented in OWASP LLM Prompt Injection Prevention Cheat
337
+ // Sheet 2026. Only run when the input "looks encoded" to keep
338
+ // false-positive load low on plain prose.
339
+ const decoded = tryDecodeObfuscation(input);
340
+ if (decoded && decoded !== input) {
341
+ const decodedNormalized = normalizeForInjectionScan(decoded);
342
+ const decodedHeuristic = await this.heuristic.scan(decoded, context);
343
+ if (decodedHeuristic.decision !== "allow") {
344
+ for (const v of decodedHeuristic.violations) {
345
+ violations.push({
346
+ ...v,
347
+ type: "ingested_injection",
348
+ scanner: this.name,
349
+ detail:
350
+ `${v.detail ?? ""} (source=${source}, layer=decoded)`.trim(),
351
+ });
352
+ }
353
+ sourceScore += 0.6; // decoded-hit is high-confidence
354
+ }
355
+ // Also run source-specific patterns over the decoded layer.
356
+ for (const pattern of sourcePatterns) {
357
+ if (pattern.test(decodedNormalized)) {
358
+ sourceScore += 0.4;
359
+ violations.push({
360
+ type: "ingested_injection",
361
+ scanner: this.name,
362
+ score: 0.4,
363
+ threshold: effectiveThreshold,
364
+ message: `Encoded indirect injection in ${source} content`,
365
+ detail: `Pattern: ${pattern.source.slice(0, 80)} (layer=decoded)`,
366
+ });
367
+ }
368
+ }
369
+ }
370
+ sourceScore = Math.min(sourceScore, 1.0);
371
+
372
+ // 3. Combine decisions. The heuristic scanner already weighed
373
+ // structural signals (newlines, headers, padding) that may not
374
+ // surface as individual violations; trust its decision rather
375
+ // than re-aggregating only the violation-score subset.
376
+ const heuristicBlocks = heuristicResult.decision === "block";
377
+ const heuristicWarns = heuristicResult.decision === "warn";
378
+ const sourceBlocks = sourceScore >= effectiveThreshold;
379
+ const sourceWarns = sourceScore >= effectiveThreshold * 0.6;
380
+
381
+ let decision: "allow" | "warn" | "block";
382
+ if (heuristicBlocks || sourceBlocks) {
383
+ decision = "block";
384
+ } else if (heuristicWarns || sourceWarns) {
385
+ decision = "warn";
386
+ } else {
387
+ decision = "allow";
388
+ }
389
+
390
+ return {
391
+ decision,
392
+ violations,
393
+ durationMs: performance.now() - start,
394
+ };
395
+ }
396
+ }
397
+
398
+ /**
399
+ * One-shot helper. Scans `content` against the source-specific profile
400
+ * and returns a result without needing an `AIShield` instance.
401
+ *
402
+ * Use when you want a quick gate at the ingestion boundary, e.g.
403
+ * before storing a chunk into a vector DB or before passing a tool
404
+ * description into the model's context.
405
+ *
406
+ * @example
407
+ * ```ts
408
+ * import { scanIngested } from "ai-shield-core";
409
+ *
410
+ * const ragChunk = "...retrieved document text...";
411
+ * const result = await scanIngested(ragChunk, "rag");
412
+ * if (!result.safe) {
413
+ * // reject the chunk OR strip it before assembly
414
+ * logger.warn("IPI candidate", result.violations);
415
+ * }
416
+ * ```
417
+ */
418
+ export async function scanIngested(
419
+ content: string,
420
+ source: IngestionSource,
421
+ config: IngestionScannerConfig = {},
422
+ ): Promise<IngestionScanResult> {
423
+ const start = performance.now();
424
+ const scanner = new IngestionScanner(config);
425
+ const result = await scanner.scan(content, { source });
426
+
427
+ return {
428
+ safe: result.decision === "allow",
429
+ decision: result.decision,
430
+ // When a chunk is blocked, returning the raw input under the field
431
+ // name "sanitized" mis-leads callers into trusting poisoned content.
432
+ // Return empty string on block so a `if (!safe) use(result.sanitized)`
433
+ // path becomes a no-op rather than a vulnerability. Use the original
434
+ // `content` argument if you still need it for audit / quarantine.
435
+ sanitized: result.decision === "block" ? "" : content,
436
+ violations: result.violations,
437
+ source,
438
+ meta: {
439
+ scanDurationMs: performance.now() - start,
440
+ scannersRun: [scanner.name],
441
+ sourceSpecificHits: result.violations.filter(
442
+ (v) =>
443
+ v.detail?.startsWith("Pattern:") && v.type === "ingested_injection",
444
+ ).length,
445
+ cached: false,
446
+ },
447
+ };
448
+ }
449
+
450
+ // ============================================================
451
+ // Encoding-bypass normalization (R1 from Round 1 review — closes
452
+ // OWASP LLM Prompt Injection Prevention Cheat Sheet 2026 Base64/Hex
453
+ // bypass class).
454
+ // ============================================================
455
+
456
+ /**
457
+ * Try to decode common obfuscation layers an attacker uses to smuggle
458
+ * an injection past pattern matchers. Returns the decoded payload when
459
+ * it looks like a successful decode, else `null`.
460
+ *
461
+ * The function deliberately runs at most ONE decode layer to avoid
462
+ * decoding amplification (a chain of `base64(base64(...))` would force
463
+ * us into deep recursion); a single-layer decode is enough to catch
464
+ * the vast majority of in-the-wild bypasses while keeping execution
465
+ * cost bounded.
466
+ *
467
+ * Heuristics:
468
+ * - Base64: contiguous run of 40+ Base64 chars, decodes to mostly
469
+ * printable ASCII or the `\u00..` C0 range stays empty.
470
+ * - Hex: 80+ hex chars in a row.
471
+ * - Percent-encoding: more than 5 `%XX` sequences.
472
+ *
473
+ * Returns the longest decoded payload when multiple candidates fire.
474
+ */
475
+ export function tryDecodeObfuscation(input: string): string | null {
476
+ if (typeof input !== "string" || input.length === 0) return null;
477
+ // Cap input we look at — Base64 of a megabyte is not the threat model.
478
+ const haystack = input.length > 65_536 ? input.slice(0, 65_536) : input;
479
+
480
+ const candidates: string[] = [];
481
+
482
+ // Base64 — at least 40 chars, optional padding, optional whitespace.
483
+ const B64_RE = /[A-Za-z0-9+/=]{40,}/g;
484
+ for (const match of haystack.match(B64_RE) ?? []) {
485
+ const cleaned = match.replace(/=+$/, "").replace(/[^A-Za-z0-9+/]/g, "");
486
+ if (cleaned.length < 40) continue;
487
+ try {
488
+ const decoded = Buffer.from(cleaned, "base64").toString("utf8");
489
+ if (decoded.length === 0) continue;
490
+ const printable = decoded.replace(/[^\x20-\x7E\s]/g, "");
491
+ // Require >70% printable to avoid noise.
492
+ if (printable.length / decoded.length >= 0.7) {
493
+ candidates.push(decoded);
494
+ }
495
+ } catch {
496
+ // ignore malformed Base64
497
+ }
498
+ }
499
+
500
+ // Hex — 80+ hex digits in a row.
501
+ const HEX_RE = /[0-9a-fA-F]{80,}/g;
502
+ for (const match of haystack.match(HEX_RE) ?? []) {
503
+ if (match.length % 2 !== 0) continue;
504
+ try {
505
+ const decoded = Buffer.from(match, "hex").toString("utf8");
506
+ if (decoded.length === 0) continue;
507
+ const printable = decoded.replace(/[^\x20-\x7E\s]/g, "");
508
+ if (printable.length / decoded.length >= 0.7) {
509
+ candidates.push(decoded);
510
+ }
511
+ } catch {
512
+ // ignore malformed hex
513
+ }
514
+ }
515
+
516
+ // Percent-encoding — only decode a windowed region around clustered
517
+ // escapes rather than the full 65KB haystack. Round 2 Critic M-NEW-1:
518
+ // running `decodeURIComponent()` on the full haystack on every scan
519
+ // allocates a ~2× copy per call and pressures GC in high-throughput
520
+ // ingestion pipelines.
521
+ const PERCENT_RE = /%[0-9A-Fa-f]{2}/g;
522
+ const percentMatches: number[] = [];
523
+ let percentMatch: RegExpExecArray | null;
524
+ while (
525
+ (percentMatch = PERCENT_RE.exec(haystack)) !== null &&
526
+ percentMatches.length < 32
527
+ ) {
528
+ percentMatches.push(percentMatch.index);
529
+ }
530
+ if (percentMatches.length >= 5) {
531
+ // Decode only a window around the cluster: 256 bytes before the first
532
+ // escape, 1KB after the last. Bounded work regardless of haystack size.
533
+ const first = percentMatches[0] ?? 0;
534
+ const last = percentMatches[percentMatches.length - 1] ?? first;
535
+ const winStart = Math.max(0, first - 256);
536
+ const winEnd = Math.min(haystack.length, last + 1024);
537
+ const window = haystack.slice(winStart, winEnd);
538
+ try {
539
+ const decoded = decodeURIComponent(window);
540
+ if (decoded !== window) candidates.push(decoded);
541
+ } catch {
542
+ // ignore malformed percent-encoding
543
+ }
544
+ }
545
+
546
+ if (candidates.length === 0) return null;
547
+ // Return the longest candidate; that's the most likely attack payload.
548
+ candidates.sort((a, b) => b.length - a.length);
549
+ return candidates[0] ?? null;
550
+ }
@@ -24,10 +24,13 @@ interface PIIPattern {
24
24
 
25
25
  // --- German & International PII Patterns ---
26
26
  const PII_PATTERNS: PIIPattern[] = [
27
- // IBAN: DE + 2 check digits + 18 digits (with optional spaces/dashes)
27
+ // IBAN: 2-letter ISO + 2 check digits + 11..30 alphanumerics (with optional spaces/dashes).
28
+ // Covers all 80+ IBAN countries: NO (15), BE (16), DE (22), FR (27), MT (31), SC (31).
29
+ // The validator runs mod-97 over the cleaned value and rejects anything that isn't a real IBAN.
30
+ // Pattern is linear (no nested quantifiers) — ReDoS-safe.
28
31
  {
29
32
  type: "iban",
30
- pattern: /\b[A-Z]{2}\s?\d{2}\s?\d{4}\s?\d{4}\s?\d{4}\s?\d{4}\s?\d{2,4}\b/g,
33
+ pattern: /\b[A-Z]{2}\d{2}[ -]?[A-Z0-9](?:[A-Z0-9 -]{9,36}[A-Z0-9])?\b/g,
31
34
  validator: validateIBAN,
32
35
  baseConfidence: 0.95,
33
36
  },
@@ -62,11 +65,16 @@ const PII_PATTERNS: PIIPattern[] = [
62
65
  baseConfidence: 0.95,
63
66
  },
64
67
 
65
- // Phone: German formats (+49, 0xxx) and international
68
+ // Phone: German formats (+49, 0xxx) and international.
69
+ // Previous pattern had nested optional quantifiers (`\s?[\s\-/]?` plus two
70
+ // `[\s\-/]?\d{0,5}` tails) which risks catastrophic backtracking on
71
+ // malformed inputs. Restructured so every separator group requires at least
72
+ // one char when present and the trailing digits group is a true non-optional
73
+ // extension (or absent entirely).
66
74
  {
67
75
  type: "phone",
68
76
  pattern:
69
- /(?<!\d)(?:\+\d{1,3}|00\d{1,3}|0)\s?[\s\-/]?\(?\d{2,5}\)?[\s\-/]?\d{3,8}[\s\-/]?\d{0,5}\b/g,
77
+ /(?<!\d)(?:\+\d{1,3}|00\d{1,3}|0)[\s\-/]?\(?\d{2,5}\)?[\s\-/]?\d{3,8}(?:[\s\-/]\d{1,5})?\b/g,
70
78
  validator: validatePhone,
71
79
  baseConfidence: 0.80,
72
80
  },
@@ -161,11 +169,17 @@ function maskValue(type: PIIType, value: string): string {
161
169
  return value[0] + "***@" + value.substring(atIdx + 1);
162
170
  }
163
171
  case "phone":
172
+ // Need room for 4-prefix + **** + 2-suffix without overlap.
173
+ if (value.length < 7) return "[PHONE]";
164
174
  return value.substring(0, 4) + "****" + value.substring(value.length - 2);
165
175
  case "iban":
166
- return value.substring(0, 4) + " **** **** ****";
167
- case "credit_card":
168
- return "**** **** **** " + value.replace(/\D/g, "").substring(12);
176
+ // Keep country code + check digits, mask rest. Works for any IBAN length.
177
+ return value.length >= 4 ? value.substring(0, 4) + " **** **** ****" : "[IBAN]";
178
+ case "credit_card": {
179
+ const digits = value.replace(/\D/g, "");
180
+ if (digits.length < 13) return "[CREDIT_CARD]";
181
+ return "**** **** **** " + digits.substring(digits.length - 4);
182
+ }
169
183
  default:
170
184
  return `[${type.toUpperCase()}]`;
171
185
  }
package/src/shield.ts CHANGED
@@ -48,6 +48,16 @@ export class AIShield {
48
48
 
49
49
  /** Scan input text — the main API */
50
50
  async scan(input: string, context: ScanContext = {}): Promise<ScanResult> {
51
+ // Input-length guard — without this a user-supplied multi-MB prompt would
52
+ // trigger every regex scan on the full buffer, which is O(n) best-case
53
+ // but pathological under ReDoS-prone patterns. 256 KB handles every
54
+ // real chat/tool input we care about with plenty of headroom; override
55
+ // via AI_SHIELD_MAX_INPUT_BYTES when needed.
56
+ const maxInputBytes = Number(process.env.AI_SHIELD_MAX_INPUT_BYTES ?? 262_144);
57
+ if (input.length > maxInputBytes) {
58
+ input = input.slice(0, maxInputBytes);
59
+ }
60
+
51
61
  // Apply preset if not set in context
52
62
  if (!context.preset) {
53
63
  context.preset = this.config.preset ?? "public_website";
@@ -70,8 +80,11 @@ export class AIShield {
70
80
  this.scanCache.set(cacheKey, result);
71
81
  }
72
82
 
73
- // Log to audit if enabled
74
- if (this.auditLogger) {
83
+ // Log to audit if enabled — but never double-log the same input when
84
+ // a downstream caller re-scans cached content (result.meta.cached is set
85
+ // by the cache hit path above; here it is always false but we guard to
86
+ // be explicit and so subclasses extending scan() stay safe).
87
+ if (this.auditLogger && !result.meta.cached) {
75
88
  void this.auditLogger.log(input, result, context);
76
89
  }
77
90