{"version":3,"file":"injection.mjs","names":[],"sources":["../../../../../../../../ai/src/guard/detectors/injection.ts"],"sourcesContent":["import type {\n  GuardrailMatch,\n  GuardrailVerdict,\n  InjectionDetectorOptions,\n  SyncGuardrailDetector,\n} from \"../contracts\";\n\nconst DETECTOR_NAME = \"injection\";\n\n/**\n * Built-in jailbreak / prompt-injection marker phrases. Each entry is a\n * case-insensitive substring (matched lowercased) paired with the rule\n * label surfaced on the {@link GuardrailMatch} (`injection.<label>`).\n *\n * The set targets the canonical override / role-reset / exfiltration\n * patterns rather than trying to be exhaustive — a curated, low-false-\n * positive seed that callers extend with their own `markers`. Phrases are\n * deliberately specific (`\"ignore previous instructions\"`, not the bare\n * word `\"ignore\"`) so ordinary prose does not trip the rule.\n */\nconst BUILT_IN_MARKERS: readonly { readonly phrase: string; readonly label: string }[] = [\n  { phrase: \"ignore previous instructions\", label: \"override\" },\n  { phrase: \"ignore all previous instructions\", label: \"override\" },\n  { phrase: \"ignore the above instructions\", label: \"override\" },\n  { phrase: \"disregard previous instructions\", label: \"override\" },\n  { phrase: \"disregard all previous instructions\", label: \"override\" },\n  { phrase: \"forget previous instructions\", label: \"override\" },\n  { phrase: \"forget all previous instructions\", label: \"override\" },\n  { phrase: \"ignore your instructions\", label: \"override\" },\n  { phrase: \"override your instructions\", label: \"override\" },\n  { phrase: \"do not follow your instructions\", label: \"override\" },\n  { phrase: \"you are now\", label: \"role-reset\" },\n  { phrase: \"act as\", label: \"role-reset\" },\n  { phrase: \"pretend to be\", label: \"role-reset\" },\n  { phrase: \"developer mode\", label: \"jailbreak\" },\n  { phrase: \"jailbreak\", label: \"jailbreak\" },\n  { phrase: \"dan mode\", label: \"jailbreak\" },\n  { phrase: \"do anything now\", label: \"jailbreak\" },\n  { phrase: \"bypass your\", label: \"jailbreak\" },\n  { phrase: \"ignore your guidelines\", label: \"jailbreak\" },\n  { phrase: \"ignore your safety\", label: \"jailbreak\" },\n  { phrase: \"ignore the rules\", label: \"jailbreak\" },\n  { phrase: \"without any restrictions\", label: \"jailbreak\" },\n  { phrase: \"reveal your system prompt\", label: \"exfiltration\" },\n  { phrase: \"print your system prompt\", label: \"exfiltration\" },\n  { phrase: \"show your system prompt\", label: \"exfiltration\" },\n  { phrase: \"repeat your instructions\", label: \"exfiltration\" },\n  { phrase: \"what are your instructions\", label: \"exfiltration\" },\n  { phrase: \"reveal your prompt\", label: \"exfiltration\" },\n];\n\n/**\n * A compiled marker — either a literal substring (matched case-insensitively\n * against the lowercased text) or a caller-supplied `RegExp` (tested as-is).\n * `label` is the namespaced rule suffix (`injection.<label>`); for built-in\n * phrases it is the threat category, for caller markers the index.\n */\ninterface CompiledMarker {\n  readonly label: string;\n  readonly phrase?: string;\n  readonly pattern?: RegExp;\n}\n\n/**\n * The zero-dependency built-in injection detector — the internal class\n * behind the {@link injection} factory. Scans for jailbreak / prompt-\n * injection marker phrases (built-in set + caller `markers`) and returns a\n * `block` or `flag` verdict (per `onMatch`) listing every match, or `allow`\n * when the text is clean.\n *\n * Detection only: a detector never throws or mutates the pipeline — the\n * `guard()` factory translates the verdict into the trip's throw / record\n * mechanics.\n */\nclass InjectionDetector implements SyncGuardrailDetector {\n  public readonly name = DETECTOR_NAME;\n\n  /** The compiled built-in + caller markers, scanned in registration order. */\n  private readonly markers: readonly CompiledMarker[];\n\n  /** Whether a match escalates to `block` (`true`) or stays a `flag`. */\n  private readonly block: boolean;\n\n  public constructor(options: InjectionDetectorOptions = {}) {\n    this.block = options.onMatch === \"block\";\n    this.markers = compileMarkers(options.markers ?? []);\n  }\n\n  /**\n   * Inspect `text` for any built-in or caller marker. Returns `allow` when\n   * none hit, otherwise the configured `block` / `flag` verdict carrying a\n   * {@link GuardrailMatch} per hit (with a `[start, end]` span for literal\n   * substrings; regex hits report a span only when the match is locatable).\n   */\n  public check(text: string): GuardrailVerdict {\n    const matches = this.scan(text);\n\n    if (matches.length === 0) {\n      return { type: \"allow\" };\n    }\n\n    const reason = `Detected ${matches.length} prompt-injection marker(s).`;\n\n    if (this.block) {\n      return { type: \"block\", reason, matches };\n    }\n\n    return { type: \"flag\", reason, matches };\n  }\n\n  /** Collect every marker hit in `text`, in marker registration order. */\n  private scan(text: string): GuardrailMatch[] {\n    const lowered = text.toLowerCase();\n    const matches: GuardrailMatch[] = [];\n\n    for (const marker of this.markers) {\n      if (marker.phrase !== undefined) {\n        const start = lowered.indexOf(marker.phrase);\n\n        if (start !== -1) {\n          matches.push({\n            rule: `${DETECTOR_NAME}.${marker.label}`,\n            label: marker.label,\n            span: [start, start + marker.phrase.length - 1],\n          });\n        }\n\n        continue;\n      }\n\n      // Caller-supplied RegExp — tested against the original (not lowered)\n      // text so author-controlled case sensitivity is preserved.\n      const pattern = marker.pattern;\n\n      if (pattern === undefined) {\n        continue;\n      }\n\n      const result = pattern.exec(text);\n\n      if (result !== null) {\n        const start = result.index;\n\n        matches.push({\n          rule: `${DETECTOR_NAME}.${marker.label}`,\n          label: marker.label,\n          span: [start, start + result[0].length - 1],\n        });\n      }\n    }\n\n    return matches;\n  }\n}\n\n/**\n * Compile the built-in phrase set plus any caller `markers` into a single\n * ordered list. A caller `string` becomes a lowercased substring matcher\n * (labelled `custom`); a caller `RegExp` is carried as-is (labelled\n * `custom`). Built-ins keep their threat-category label.\n */\nfunction compileMarkers(\n  extra: readonly (string | RegExp)[],\n): readonly CompiledMarker[] {\n  const compiled: CompiledMarker[] = BUILT_IN_MARKERS.map((entry) => ({\n    label: entry.label,\n    phrase: entry.phrase,\n  }));\n\n  for (const marker of extra) {\n    if (typeof marker === \"string\") {\n      compiled.push({ label: \"custom\", phrase: marker.toLowerCase() });\n\n      continue;\n    }\n\n    compiled.push({ label: \"custom\", pattern: marker });\n  }\n\n  return compiled;\n}\n\n/**\n * Build the built-in `injection` detector (surfaced as\n * `ai.guardrail.injection(options?)`). Matches a curated set of jailbreak /\n * prompt-injection marker phrases — override (`\"ignore previous\n * instructions\"`), role-reset (`\"you are now\"`), jailbreak (`\"developer\n * mode\"`, `\"do anything now\"`), and exfiltration (`\"reveal your system\n * prompt\"`) — extensible with caller `markers` (case-insensitive substrings\n * or `RegExp`s).\n *\n * Zero runtime dependency: matching is pure string / regex. On a hit the\n * verdict is `flag` by default (record but allow); pass `onMatch: \"block\"`\n * to reject instead — commonly used on the `input` phase, where the core\n * `trip.before` seam supports `block` / `flag` only.\n *\n * @param options - Extra `markers` and the `onMatch` action (`\"flag\"` | `\"block\"`).\n * @returns A {@link SyncGuardrailDetector} for the guard's `input` / `output` / `tool` arrays.\n *\n * @example\n * const guard = ai.guardrail({\n *   input: [ai.guardrail.injection({ onMatch: \"block\" })],\n *   output: [ai.guardrail.injection()], // flag-only on the model's reply\n * });\n *\n * @example\n * // Extend the built-in set with a house rule.\n * ai.guardrail.injection({ markers: [/system\\s*:\\s*override/i, \"sudo mode\"] });\n */\nexport function injection(\n  options?: InjectionDetectorOptions,\n): SyncGuardrailDetector {\n  return new InjectionDetector(options);\n}\n"],"mappings":";AAOA,MAAM,gBAAgB;;;;;;;;;;;;AAatB,MAAM,mBAAmF;CACvF;EAAE,QAAQ;EAAgC,OAAO;CAAW;CAC5D;EAAE,QAAQ;EAAoC,OAAO;CAAW;CAChE;EAAE,QAAQ;EAAiC,OAAO;CAAW;CAC7D;EAAE,QAAQ;EAAmC,OAAO;CAAW;CAC/D;EAAE,QAAQ;EAAuC,OAAO;CAAW;CACnE;EAAE,QAAQ;EAAgC,OAAO;CAAW;CAC5D;EAAE,QAAQ;EAAoC,OAAO;CAAW;CAChE;EAAE,QAAQ;EAA4B,OAAO;CAAW;CACxD;EAAE,QAAQ;EAA8B,OAAO;CAAW;CAC1D;EAAE,QAAQ;EAAmC,OAAO;CAAW;CAC/D;EAAE,QAAQ;EAAe,OAAO;CAAa;CAC7C;EAAE,QAAQ;EAAU,OAAO;CAAa;CACxC;EAAE,QAAQ;EAAiB,OAAO;CAAa;CAC/C;EAAE,QAAQ;EAAkB,OAAO;CAAY;CAC/C;EAAE,QAAQ;EAAa,OAAO;CAAY;CAC1C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