governance-sdk 0.5.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 (387) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +559 -0
  3. package/dist/agent-identity-ed25519.d.ts +80 -0
  4. package/dist/agent-identity-ed25519.d.ts.map +1 -0
  5. package/dist/agent-identity-ed25519.js +134 -0
  6. package/dist/agent-identity-ed25519.js.map +1 -0
  7. package/dist/agent-identity.d.ts +65 -0
  8. package/dist/agent-identity.d.ts.map +1 -0
  9. package/dist/agent-identity.js +85 -0
  10. package/dist/agent-identity.js.map +1 -0
  11. package/dist/audit-integrity.d.ts +78 -0
  12. package/dist/audit-integrity.d.ts.map +1 -0
  13. package/dist/audit-integrity.js +173 -0
  14. package/dist/audit-integrity.js.map +1 -0
  15. package/dist/behavioral-scorer.d.ts +72 -0
  16. package/dist/behavioral-scorer.d.ts.map +1 -0
  17. package/dist/behavioral-scorer.js +223 -0
  18. package/dist/behavioral-scorer.js.map +1 -0
  19. package/dist/cli/init.d.ts +11 -0
  20. package/dist/cli/init.d.ts.map +1 -0
  21. package/dist/cli/init.js +217 -0
  22. package/dist/cli/init.js.map +1 -0
  23. package/dist/compliance-articles.d.ts +71 -0
  24. package/dist/compliance-articles.d.ts.map +1 -0
  25. package/dist/compliance-articles.js +201 -0
  26. package/dist/compliance-articles.js.map +1 -0
  27. package/dist/compliance.d.ts +24 -0
  28. package/dist/compliance.d.ts.map +1 -0
  29. package/dist/compliance.js +183 -0
  30. package/dist/compliance.js.map +1 -0
  31. package/dist/conditions/builtins.d.ts +17 -0
  32. package/dist/conditions/builtins.d.ts.map +1 -0
  33. package/dist/conditions/builtins.js +213 -0
  34. package/dist/conditions/builtins.js.map +1 -0
  35. package/dist/conditions/postprocess.d.ts +12 -0
  36. package/dist/conditions/postprocess.d.ts.map +1 -0
  37. package/dist/conditions/postprocess.js +33 -0
  38. package/dist/conditions/postprocess.js.map +1 -0
  39. package/dist/conditions/preprocess.d.ts +12 -0
  40. package/dist/conditions/preprocess.d.ts.map +1 -0
  41. package/dist/conditions/preprocess.js +47 -0
  42. package/dist/conditions/preprocess.js.map +1 -0
  43. package/dist/conditions/process.d.ts +14 -0
  44. package/dist/conditions/process.d.ts.map +1 -0
  45. package/dist/conditions/process.js +78 -0
  46. package/dist/conditions/process.js.map +1 -0
  47. package/dist/conditions/sensitive-patterns.d.ts +13 -0
  48. package/dist/conditions/sensitive-patterns.d.ts.map +1 -0
  49. package/dist/conditions/sensitive-patterns.js +42 -0
  50. package/dist/conditions/sensitive-patterns.js.map +1 -0
  51. package/dist/dry-run.d.ts +85 -0
  52. package/dist/dry-run.d.ts.map +1 -0
  53. package/dist/dry-run.js +132 -0
  54. package/dist/dry-run.js.map +1 -0
  55. package/dist/eval-red-team.d.ts +69 -0
  56. package/dist/eval-red-team.d.ts.map +1 -0
  57. package/dist/eval-red-team.js +205 -0
  58. package/dist/eval-red-team.js.map +1 -0
  59. package/dist/eval-scorer.d.ts +56 -0
  60. package/dist/eval-scorer.d.ts.map +1 -0
  61. package/dist/eval-scorer.js +148 -0
  62. package/dist/eval-scorer.js.map +1 -0
  63. package/dist/eval-trace.d.ts +30 -0
  64. package/dist/eval-trace.d.ts.map +1 -0
  65. package/dist/eval-trace.js +129 -0
  66. package/dist/eval-trace.js.map +1 -0
  67. package/dist/eval-types.d.ts +108 -0
  68. package/dist/eval-types.d.ts.map +1 -0
  69. package/dist/eval-types.js +14 -0
  70. package/dist/eval-types.js.map +1 -0
  71. package/dist/events.d.ts +57 -0
  72. package/dist/events.d.ts.map +1 -0
  73. package/dist/events.js +81 -0
  74. package/dist/events.js.map +1 -0
  75. package/dist/federation-types.d.ts +58 -0
  76. package/dist/federation-types.d.ts.map +1 -0
  77. package/dist/federation-types.js +8 -0
  78. package/dist/federation-types.js.map +1 -0
  79. package/dist/federation.d.ts +42 -0
  80. package/dist/federation.d.ts.map +1 -0
  81. package/dist/federation.js +158 -0
  82. package/dist/federation.js.map +1 -0
  83. package/dist/index.d.ts +142 -0
  84. package/dist/index.d.ts.map +1 -0
  85. package/dist/index.js +273 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/injection-benchmark.d.ts +62 -0
  88. package/dist/injection-benchmark.d.ts.map +1 -0
  89. package/dist/injection-benchmark.js +201 -0
  90. package/dist/injection-benchmark.js.map +1 -0
  91. package/dist/injection-classifier.d.ts +69 -0
  92. package/dist/injection-classifier.d.ts.map +1 -0
  93. package/dist/injection-classifier.js +98 -0
  94. package/dist/injection-classifier.js.map +1 -0
  95. package/dist/injection-detect.d.ts +59 -0
  96. package/dist/injection-detect.d.ts.map +1 -0
  97. package/dist/injection-detect.js +175 -0
  98. package/dist/injection-detect.js.map +1 -0
  99. package/dist/injection-patterns-ext.d.ts +7 -0
  100. package/dist/injection-patterns-ext.d.ts.map +1 -0
  101. package/dist/injection-patterns-ext.js +71 -0
  102. package/dist/injection-patterns-ext.js.map +1 -0
  103. package/dist/injection-patterns.d.ts +15 -0
  104. package/dist/injection-patterns.d.ts.map +1 -0
  105. package/dist/injection-patterns.js +361 -0
  106. package/dist/injection-patterns.js.map +1 -0
  107. package/dist/iso-42001-articles.d.ts +34 -0
  108. package/dist/iso-42001-articles.d.ts.map +1 -0
  109. package/dist/iso-42001-articles.js +147 -0
  110. package/dist/iso-42001-articles.js.map +1 -0
  111. package/dist/iso-42001.d.ts +18 -0
  112. package/dist/iso-42001.d.ts.map +1 -0
  113. package/dist/iso-42001.js +156 -0
  114. package/dist/iso-42001.js.map +1 -0
  115. package/dist/kill-switch.d.ts +56 -0
  116. package/dist/kill-switch.d.ts.map +1 -0
  117. package/dist/kill-switch.js +173 -0
  118. package/dist/kill-switch.js.map +1 -0
  119. package/dist/metrics.d.ts +58 -0
  120. package/dist/metrics.d.ts.map +1 -0
  121. package/dist/metrics.js +81 -0
  122. package/dist/metrics.js.map +1 -0
  123. package/dist/monorepo-detect.d.ts +30 -0
  124. package/dist/monorepo-detect.d.ts.map +1 -0
  125. package/dist/monorepo-detect.js +107 -0
  126. package/dist/monorepo-detect.js.map +1 -0
  127. package/dist/nist-ai-rmf-articles.d.ts +40 -0
  128. package/dist/nist-ai-rmf-articles.d.ts.map +1 -0
  129. package/dist/nist-ai-rmf-articles.js +156 -0
  130. package/dist/nist-ai-rmf-articles.js.map +1 -0
  131. package/dist/nist-ai-rmf.d.ts +20 -0
  132. package/dist/nist-ai-rmf.d.ts.map +1 -0
  133. package/dist/nist-ai-rmf.js +176 -0
  134. package/dist/nist-ai-rmf.js.map +1 -0
  135. package/dist/otel-hooks.d.ts +67 -0
  136. package/dist/otel-hooks.d.ts.map +1 -0
  137. package/dist/otel-hooks.js +100 -0
  138. package/dist/otel-hooks.js.map +1 -0
  139. package/dist/owasp-agentic-articles.d.ts +42 -0
  140. package/dist/owasp-agentic-articles.d.ts.map +1 -0
  141. package/dist/owasp-agentic-articles.js +236 -0
  142. package/dist/owasp-agentic-articles.js.map +1 -0
  143. package/dist/owasp-agentic.d.ts +20 -0
  144. package/dist/owasp-agentic.d.ts.map +1 -0
  145. package/dist/owasp-agentic.js +205 -0
  146. package/dist/owasp-agentic.js.map +1 -0
  147. package/dist/plugins/a2a-types.d.ts +241 -0
  148. package/dist/plugins/a2a-types.d.ts.map +1 -0
  149. package/dist/plugins/a2a-types.js +14 -0
  150. package/dist/plugins/a2a-types.js.map +1 -0
  151. package/dist/plugins/a2a.d.ts +37 -0
  152. package/dist/plugins/a2a.d.ts.map +1 -0
  153. package/dist/plugins/a2a.js +160 -0
  154. package/dist/plugins/a2a.js.map +1 -0
  155. package/dist/plugins/anthropic-types.d.ts +188 -0
  156. package/dist/plugins/anthropic-types.d.ts.map +1 -0
  157. package/dist/plugins/anthropic-types.js +8 -0
  158. package/dist/plugins/anthropic-types.js.map +1 -0
  159. package/dist/plugins/anthropic.d.ts +32 -0
  160. package/dist/plugins/anthropic.d.ts.map +1 -0
  161. package/dist/plugins/anthropic.js +131 -0
  162. package/dist/plugins/anthropic.js.map +1 -0
  163. package/dist/plugins/autogen-types.d.ts +121 -0
  164. package/dist/plugins/autogen-types.d.ts.map +1 -0
  165. package/dist/plugins/autogen-types.js +13 -0
  166. package/dist/plugins/autogen-types.js.map +1 -0
  167. package/dist/plugins/autogen.d.ts +41 -0
  168. package/dist/plugins/autogen.d.ts.map +1 -0
  169. package/dist/plugins/autogen.js +131 -0
  170. package/dist/plugins/autogen.js.map +1 -0
  171. package/dist/plugins/bedrock-types.d.ts +246 -0
  172. package/dist/plugins/bedrock-types.d.ts.map +1 -0
  173. package/dist/plugins/bedrock-types.js +8 -0
  174. package/dist/plugins/bedrock-types.js.map +1 -0
  175. package/dist/plugins/bedrock.d.ts +43 -0
  176. package/dist/plugins/bedrock.d.ts.map +1 -0
  177. package/dist/plugins/bedrock.js +155 -0
  178. package/dist/plugins/bedrock.js.map +1 -0
  179. package/dist/plugins/cloudflare-ai-types.d.ts +85 -0
  180. package/dist/plugins/cloudflare-ai-types.d.ts.map +1 -0
  181. package/dist/plugins/cloudflare-ai-types.js +10 -0
  182. package/dist/plugins/cloudflare-ai-types.js.map +1 -0
  183. package/dist/plugins/cloudflare-ai.d.ts +32 -0
  184. package/dist/plugins/cloudflare-ai.d.ts.map +1 -0
  185. package/dist/plugins/cloudflare-ai.js +108 -0
  186. package/dist/plugins/cloudflare-ai.js.map +1 -0
  187. package/dist/plugins/composio-types.d.ts +96 -0
  188. package/dist/plugins/composio-types.d.ts.map +1 -0
  189. package/dist/plugins/composio-types.js +13 -0
  190. package/dist/plugins/composio-types.js.map +1 -0
  191. package/dist/plugins/composio.d.ts +37 -0
  192. package/dist/plugins/composio.d.ts.map +1 -0
  193. package/dist/plugins/composio.js +118 -0
  194. package/dist/plugins/composio.js.map +1 -0
  195. package/dist/plugins/crewai-types.d.ts +153 -0
  196. package/dist/plugins/crewai-types.d.ts.map +1 -0
  197. package/dist/plugins/crewai-types.js +10 -0
  198. package/dist/plugins/crewai-types.js.map +1 -0
  199. package/dist/plugins/crewai.d.ts +37 -0
  200. package/dist/plugins/crewai.d.ts.map +1 -0
  201. package/dist/plugins/crewai.js +127 -0
  202. package/dist/plugins/crewai.js.map +1 -0
  203. package/dist/plugins/deno-types.d.ts +68 -0
  204. package/dist/plugins/deno-types.d.ts.map +1 -0
  205. package/dist/plugins/deno-types.js +8 -0
  206. package/dist/plugins/deno-types.js.map +1 -0
  207. package/dist/plugins/deno.d.ts +37 -0
  208. package/dist/plugins/deno.d.ts.map +1 -0
  209. package/dist/plugins/deno.js +129 -0
  210. package/dist/plugins/deno.js.map +1 -0
  211. package/dist/plugins/e2b-types.d.ts +140 -0
  212. package/dist/plugins/e2b-types.d.ts.map +1 -0
  213. package/dist/plugins/e2b-types.js +8 -0
  214. package/dist/plugins/e2b-types.js.map +1 -0
  215. package/dist/plugins/e2b.d.ts +43 -0
  216. package/dist/plugins/e2b.d.ts.map +1 -0
  217. package/dist/plugins/e2b.js +157 -0
  218. package/dist/plugins/e2b.js.map +1 -0
  219. package/dist/plugins/genkit-types.d.ts +88 -0
  220. package/dist/plugins/genkit-types.d.ts.map +1 -0
  221. package/dist/plugins/genkit-types.js +11 -0
  222. package/dist/plugins/genkit-types.js.map +1 -0
  223. package/dist/plugins/genkit.d.ts +35 -0
  224. package/dist/plugins/genkit.d.ts.map +1 -0
  225. package/dist/plugins/genkit.js +143 -0
  226. package/dist/plugins/genkit.js.map +1 -0
  227. package/dist/plugins/langchain.d.ts +130 -0
  228. package/dist/plugins/langchain.d.ts.map +1 -0
  229. package/dist/plugins/langchain.js +172 -0
  230. package/dist/plugins/langchain.js.map +1 -0
  231. package/dist/plugins/llamaindex-types.d.ts +86 -0
  232. package/dist/plugins/llamaindex-types.d.ts.map +1 -0
  233. package/dist/plugins/llamaindex-types.js +11 -0
  234. package/dist/plugins/llamaindex-types.js.map +1 -0
  235. package/dist/plugins/llamaindex.d.ts +36 -0
  236. package/dist/plugins/llamaindex.d.ts.map +1 -0
  237. package/dist/plugins/llamaindex.js +131 -0
  238. package/dist/plugins/llamaindex.js.map +1 -0
  239. package/dist/plugins/mastra-processor-types.d.ts +126 -0
  240. package/dist/plugins/mastra-processor-types.d.ts.map +1 -0
  241. package/dist/plugins/mastra-processor-types.js +11 -0
  242. package/dist/plugins/mastra-processor-types.js.map +1 -0
  243. package/dist/plugins/mastra-processor.d.ts +32 -0
  244. package/dist/plugins/mastra-processor.d.ts.map +1 -0
  245. package/dist/plugins/mastra-processor.js +126 -0
  246. package/dist/plugins/mastra-processor.js.map +1 -0
  247. package/dist/plugins/mastra.d.ts +100 -0
  248. package/dist/plugins/mastra.d.ts.map +1 -0
  249. package/dist/plugins/mastra.js +143 -0
  250. package/dist/plugins/mastra.js.map +1 -0
  251. package/dist/plugins/mcp-annotations.d.ts +54 -0
  252. package/dist/plugins/mcp-annotations.d.ts.map +1 -0
  253. package/dist/plugins/mcp-annotations.js +110 -0
  254. package/dist/plugins/mcp-annotations.js.map +1 -0
  255. package/dist/plugins/mcp-chain-audit.d.ts +74 -0
  256. package/dist/plugins/mcp-chain-audit.d.ts.map +1 -0
  257. package/dist/plugins/mcp-chain-audit.js +134 -0
  258. package/dist/plugins/mcp-chain-audit.js.map +1 -0
  259. package/dist/plugins/mcp-trust.d.ts +59 -0
  260. package/dist/plugins/mcp-trust.d.ts.map +1 -0
  261. package/dist/plugins/mcp-trust.js +100 -0
  262. package/dist/plugins/mcp-trust.js.map +1 -0
  263. package/dist/plugins/mcp-types.d.ts +183 -0
  264. package/dist/plugins/mcp-types.d.ts.map +1 -0
  265. package/dist/plugins/mcp-types.js +12 -0
  266. package/dist/plugins/mcp-types.js.map +1 -0
  267. package/dist/plugins/mcp.d.ts +41 -0
  268. package/dist/plugins/mcp.d.ts.map +1 -0
  269. package/dist/plugins/mcp.js +228 -0
  270. package/dist/plugins/mcp.js.map +1 -0
  271. package/dist/plugins/mistral-types.d.ts +72 -0
  272. package/dist/plugins/mistral-types.d.ts.map +1 -0
  273. package/dist/plugins/mistral-types.js +8 -0
  274. package/dist/plugins/mistral-types.js.map +1 -0
  275. package/dist/plugins/mistral.d.ts +32 -0
  276. package/dist/plugins/mistral.d.ts.map +1 -0
  277. package/dist/plugins/mistral.js +133 -0
  278. package/dist/plugins/mistral.js.map +1 -0
  279. package/dist/plugins/ollama-types.d.ts +76 -0
  280. package/dist/plugins/ollama-types.d.ts.map +1 -0
  281. package/dist/plugins/ollama-types.js +8 -0
  282. package/dist/plugins/ollama-types.js.map +1 -0
  283. package/dist/plugins/ollama.d.ts +32 -0
  284. package/dist/plugins/ollama.d.ts.map +1 -0
  285. package/dist/plugins/ollama.js +130 -0
  286. package/dist/plugins/ollama.js.map +1 -0
  287. package/dist/plugins/openai-agents-types.d.ts +130 -0
  288. package/dist/plugins/openai-agents-types.d.ts.map +1 -0
  289. package/dist/plugins/openai-agents-types.js +12 -0
  290. package/dist/plugins/openai-agents-types.js.map +1 -0
  291. package/dist/plugins/openai-agents.d.ts +37 -0
  292. package/dist/plugins/openai-agents.d.ts.map +1 -0
  293. package/dist/plugins/openai-agents.js +151 -0
  294. package/dist/plugins/openai-agents.js.map +1 -0
  295. package/dist/plugins/semantic-kernel-types.d.ts +103 -0
  296. package/dist/plugins/semantic-kernel-types.d.ts.map +1 -0
  297. package/dist/plugins/semantic-kernel-types.js +13 -0
  298. package/dist/plugins/semantic-kernel-types.js.map +1 -0
  299. package/dist/plugins/semantic-kernel.d.ts +37 -0
  300. package/dist/plugins/semantic-kernel.d.ts.map +1 -0
  301. package/dist/plugins/semantic-kernel.js +149 -0
  302. package/dist/plugins/semantic-kernel.js.map +1 -0
  303. package/dist/plugins/vercel-ai.d.ts +134 -0
  304. package/dist/plugins/vercel-ai.d.ts.map +1 -0
  305. package/dist/plugins/vercel-ai.js +130 -0
  306. package/dist/plugins/vercel-ai.js.map +1 -0
  307. package/dist/policy-builder.d.ts +52 -0
  308. package/dist/policy-builder.d.ts.map +1 -0
  309. package/dist/policy-builder.js +108 -0
  310. package/dist/policy-builder.js.map +1 -0
  311. package/dist/policy-compose-presets.d.ts +18 -0
  312. package/dist/policy-compose-presets.d.ts.map +1 -0
  313. package/dist/policy-compose-presets.js +52 -0
  314. package/dist/policy-compose-presets.js.map +1 -0
  315. package/dist/policy-compose.d.ts +66 -0
  316. package/dist/policy-compose.d.ts.map +1 -0
  317. package/dist/policy-compose.js +163 -0
  318. package/dist/policy-compose.js.map +1 -0
  319. package/dist/policy-presets-extended.d.ts +35 -0
  320. package/dist/policy-presets-extended.d.ts.map +1 -0
  321. package/dist/policy-presets-extended.js +137 -0
  322. package/dist/policy-presets-extended.js.map +1 -0
  323. package/dist/policy-presets.d.ts +77 -0
  324. package/dist/policy-presets.d.ts.map +1 -0
  325. package/dist/policy-presets.js +164 -0
  326. package/dist/policy-presets.js.map +1 -0
  327. package/dist/policy-stage-defaults.d.ts +8 -0
  328. package/dist/policy-stage-defaults.d.ts.map +1 -0
  329. package/dist/policy-stage-defaults.js +34 -0
  330. package/dist/policy-stage-defaults.js.map +1 -0
  331. package/dist/policy-yaml.d.ts +23 -0
  332. package/dist/policy-yaml.d.ts.map +1 -0
  333. package/dist/policy-yaml.js +216 -0
  334. package/dist/policy-yaml.js.map +1 -0
  335. package/dist/policy.d.ts +124 -0
  336. package/dist/policy.d.ts.map +1 -0
  337. package/dist/policy.js +161 -0
  338. package/dist/policy.js.map +1 -0
  339. package/dist/remote-enforce.d.ts +44 -0
  340. package/dist/remote-enforce.d.ts.map +1 -0
  341. package/dist/remote-enforce.js +99 -0
  342. package/dist/remote-enforce.js.map +1 -0
  343. package/dist/repo-patterns.d.ts +32 -0
  344. package/dist/repo-patterns.d.ts.map +1 -0
  345. package/dist/repo-patterns.js +222 -0
  346. package/dist/repo-patterns.js.map +1 -0
  347. package/dist/sandbox.d.ts +68 -0
  348. package/dist/sandbox.d.ts.map +1 -0
  349. package/dist/sandbox.js +124 -0
  350. package/dist/sandbox.js.map +1 -0
  351. package/dist/scorer-dimensions.d.ts +10 -0
  352. package/dist/scorer-dimensions.d.ts.map +1 -0
  353. package/dist/scorer-dimensions.js +184 -0
  354. package/dist/scorer-dimensions.js.map +1 -0
  355. package/dist/scorer.d.ts +27 -0
  356. package/dist/scorer.d.ts.map +1 -0
  357. package/dist/scorer.js +138 -0
  358. package/dist/scorer.js.map +1 -0
  359. package/dist/storage-postgres-schema.d.ts +47 -0
  360. package/dist/storage-postgres-schema.d.ts.map +1 -0
  361. package/dist/storage-postgres-schema.js +134 -0
  362. package/dist/storage-postgres-schema.js.map +1 -0
  363. package/dist/storage-postgres.d.ts +41 -0
  364. package/dist/storage-postgres.d.ts.map +1 -0
  365. package/dist/storage-postgres.js +180 -0
  366. package/dist/storage-postgres.js.map +1 -0
  367. package/dist/storage.d.ts +65 -0
  368. package/dist/storage.d.ts.map +1 -0
  369. package/dist/storage.js +85 -0
  370. package/dist/storage.js.map +1 -0
  371. package/dist/supply-chain-sbom.d.ts +72 -0
  372. package/dist/supply-chain-sbom.d.ts.map +1 -0
  373. package/dist/supply-chain-sbom.js +73 -0
  374. package/dist/supply-chain-sbom.js.map +1 -0
  375. package/dist/supply-chain.d.ts +61 -0
  376. package/dist/supply-chain.d.ts.map +1 -0
  377. package/dist/supply-chain.js +95 -0
  378. package/dist/supply-chain.js.map +1 -0
  379. package/dist/token-types.d.ts +77 -0
  380. package/dist/token-types.d.ts.map +1 -0
  381. package/dist/token-types.js +31 -0
  382. package/dist/token-types.js.map +1 -0
  383. package/dist/types.d.ts +71 -0
  384. package/dist/types.d.ts.map +1 -0
  385. package/dist/types.js +6 -0
  386. package/dist/types.js.map +1 -0
  387. package/package.json +361 -0
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Prompt Injection Detection — zero-dependency, pattern-based.
3
+ *
4
+ * Detects common prompt injection patterns in agent inputs.
5
+ * Pattern definitions are in injection-patterns.ts.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { detectInjection, createInjectionGuard } from 'governance-sdk/injection-detect';
10
+ *
11
+ * const result = detectInjection('Ignore previous instructions...');
12
+ * // { detected: true, score: 0.85, patterns: ['instruction_override'], ... }
13
+ *
14
+ * const guard = createInjectionGuard({ threshold: 0.5 });
15
+ * gov.addRule(guard);
16
+ * ```
17
+ */
18
+ import { BUILTIN_PATTERNS } from "./injection-patterns.js";
19
+ /** Default max input length: 100KB */
20
+ const DEFAULT_MAX_INPUT_LENGTH = 100_000;
21
+ // ─── Detection Engine ───────────────────────────────────────────
22
+ /** Strip zero-width characters and normalize Unicode for consistent matching */
23
+ function normalizeInput(input) {
24
+ // Remove zero-width characters (U+200B, U+200C, U+200D, U+FEFF, U+00AD)
25
+ const stripped = input.replace(/[\u200B-\u200D\uFEFF\u00AD\u2060\u180E]/g, "");
26
+ // Normalize Unicode to NFC form
27
+ return stripped.normalize("NFC");
28
+ }
29
+ /** Base64 regex: 16+ base64 chars with optional padding, not a common word */
30
+ const BASE64_RE = /[A-Za-z0-9+/]{16,}={0,2}/g;
31
+ /** Try to decode base64 strings in input; returns decoded text or null */
32
+ function tryDecodeBase64(encoded) {
33
+ try {
34
+ const decoded = atob(encoded);
35
+ // Only accept if result is printable ASCII/UTF-8
36
+ if (/^[\x20-\x7E\t\n\r]+$/.test(decoded) && decoded.length >= 4) {
37
+ return decoded;
38
+ }
39
+ }
40
+ catch { /* not valid base64 */ }
41
+ return null;
42
+ }
43
+ /**
44
+ * Detect prompt injection patterns in text input.
45
+ * Returns a score from 0 (no injection) to 1 (certain injection).
46
+ *
47
+ * Note: This is a heuristic pattern matcher, not an LLM classifier.
48
+ * It catches known syntactic patterns but cannot detect novel semantic attacks.
49
+ * For high-security deployments, layer this with an LLM-based classifier.
50
+ */
51
+ export function detectInjection(input, config = {}) {
52
+ const maxLen = config.maxInputLength ?? DEFAULT_MAX_INPUT_LENGTH;
53
+ if (input.length > maxLen) {
54
+ return {
55
+ detected: true,
56
+ score: 1,
57
+ patterns: ["input_too_large"],
58
+ categories: ["system_prompt"],
59
+ summary: `Input exceeds maximum length (${input.length} > ${maxLen})`,
60
+ inputLength: input.length,
61
+ };
62
+ }
63
+ const threshold = config.threshold ?? 0.5;
64
+ const skipCategories = new Set(config.skipCategories ?? []);
65
+ const allPatterns = [
66
+ ...BUILTIN_PATTERNS,
67
+ ...(config.customPatterns ?? []),
68
+ ].filter((p) => !skipCategories.has(p.category));
69
+ const normalized = normalizeInput(input);
70
+ const matchedPatterns = [];
71
+ const matchedCategories = new Set();
72
+ let maxWeight = 0;
73
+ // Scan the original input
74
+ for (const pattern of allPatterns) {
75
+ if (pattern.pattern.test(normalized)) {
76
+ matchedPatterns.push(pattern.id);
77
+ matchedCategories.add(pattern.category);
78
+ if (pattern.weight > maxWeight)
79
+ maxWeight = pattern.weight;
80
+ }
81
+ }
82
+ // Decode any base64 strings and scan the decoded content too
83
+ const b64Matches = normalized.match(BASE64_RE) ?? [];
84
+ for (const b64 of b64Matches) {
85
+ const decoded = tryDecodeBase64(b64);
86
+ if (!decoded)
87
+ continue;
88
+ for (const pattern of allPatterns) {
89
+ if (pattern.pattern.test(decoded) && !matchedPatterns.includes(pattern.id + ":decoded")) {
90
+ matchedPatterns.push(pattern.id + ":decoded");
91
+ matchedCategories.add(pattern.category);
92
+ // Boost weight for encoded attacks — deliberate obfuscation
93
+ const boosted = Math.min(1, pattern.weight + 0.1);
94
+ if (boosted > maxWeight)
95
+ maxWeight = boosted;
96
+ }
97
+ }
98
+ }
99
+ // Score = highest weight + boosts for multiple matches/categories
100
+ const additionalBoost = matchedPatterns.length > 1
101
+ ? Math.min(0.1, (matchedPatterns.length - 1) * 0.02)
102
+ : 0;
103
+ const categoryBoost = matchedCategories.size > 1
104
+ ? Math.min(0.1, (matchedCategories.size - 1) * 0.03)
105
+ : 0;
106
+ const score = Math.min(1, maxWeight + additionalBoost + categoryBoost);
107
+ const detected = score >= threshold;
108
+ const categories = Array.from(matchedCategories);
109
+ let summary;
110
+ if (!detected)
111
+ summary = "No injection detected";
112
+ else if (score >= 0.8)
113
+ summary = `High-confidence injection attempt: ${categories.join(", ")}`;
114
+ else if (score >= 0.5)
115
+ summary = `Possible injection attempt: ${categories.join(", ")}`;
116
+ else
117
+ summary = `Low-confidence injection signals: ${categories.join(", ")}`;
118
+ return {
119
+ detected,
120
+ score: Math.round(score * 100) / 100,
121
+ patterns: matchedPatterns,
122
+ categories,
123
+ summary,
124
+ inputLength: input.length,
125
+ };
126
+ }
127
+ // ─── Policy Integration ─────────────────────────────────────────
128
+ /**
129
+ * Create a policy rule that blocks actions containing prompt injection.
130
+ * Examines `ctx.input` for injection patterns.
131
+ */
132
+ export function createInjectionGuard(config) {
133
+ const threshold = config?.threshold ?? 0.5;
134
+ const priority = config?.priority ?? 110;
135
+ return {
136
+ id: "injection-guard",
137
+ name: "Prompt Injection Guard",
138
+ condition: {
139
+ type: "injection_guard",
140
+ params: {
141
+ threshold,
142
+ skipCategories: config?.skipCategories ?? [],
143
+ },
144
+ },
145
+ outcome: "block",
146
+ reason: `Prompt injection detected (threshold: ${threshold})`,
147
+ priority,
148
+ enabled: true,
149
+ stage: "preprocess",
150
+ };
151
+ }
152
+ /** Extract all string values from a nested object. */
153
+ function extractStrings(obj) {
154
+ const strings = [];
155
+ function walk(value) {
156
+ if (typeof value === "string")
157
+ strings.push(value);
158
+ else if (Array.isArray(value))
159
+ value.forEach(walk);
160
+ else if (value !== null && typeof value === "object") {
161
+ Object.values(value).forEach(walk);
162
+ }
163
+ }
164
+ walk(obj);
165
+ // Also test concatenation of all fields to catch cross-field injection splitting
166
+ if (strings.length > 1) {
167
+ strings.push(strings.join(" "));
168
+ }
169
+ return strings;
170
+ }
171
+ /** Get all built-in injection patterns. */
172
+ export function getBuiltinPatterns() {
173
+ return [...BUILTIN_PATTERNS];
174
+ }
175
+ //# sourceMappingURL=injection-detect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"injection-detect.js","sourceRoot":"","sources":["../src/injection-detect.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AA8B3D,sCAAsC;AACtC,MA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K,KAAK,QAAQ,EAAE,CAAC;YACrD,MAAM,CAAC,MAAM,CAAC,KAAgC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;IACD,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,iFAAiF;IACjF,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAClC,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,2CAA2C;AAC3C,MAAM,UAAU,kBAAkB;IAChC,OAAO,CAAC,GAAG,gBAAgB,CAAC,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Extended injection patterns — obfuscation and advanced attacks.
3
+ * Separated to keep each file under 300 LOC.
4
+ */
5
+ import type { InjectionPattern } from "./injection-detect.js";
6
+ export declare const EXTENDED_PATTERNS: InjectionPattern[];
7
+ //# sourceMappingURL=injection-patterns-ext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"injection-patterns-ext.d.ts","sourceRoot":"","sources":["../src/injection-patterns-ext.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,eAAO,MAAM,iBAAiB,EAAE,gBAAgB,EAkE/C,CAAC"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Extended injection patterns — obfuscation and advanced attacks.
3
+ * Separated to keep each file under 300 LOC.
4
+ */
5
+ export const EXTENDED_PATTERNS = [
6
+ // ─── Obfuscation ───────────────────────────────────────────────
7
+ {
8
+ id: "zero_width_chars",
9
+ category: "obfuscation",
10
+ pattern: /[\u200B\u200C\u200D\uFEFF]{2,}/,
11
+ weight: 0.7,
12
+ description: "Multiple zero-width characters (likely obfuscation)",
13
+ },
14
+ {
15
+ id: "rtl_override",
16
+ category: "obfuscation",
17
+ pattern: /[\u202E\u202D]/,
18
+ weight: 0.85,
19
+ description: "Right-to-left override markers (text direction attack)",
20
+ },
21
+ {
22
+ id: "bidi_control",
23
+ category: "obfuscation",
24
+ pattern: /[\u202A-\u202E\u2066-\u2069]+/,
25
+ weight: 0.7,
26
+ description: "Bidirectional control characters",
27
+ },
28
+ {
29
+ id: "char_insertion",
30
+ category: "obfuscation",
31
+ pattern: /\bi[\s._-]g[\s._-]n[\s._-]o[\s._-]r[\s._-]e\b/i,
32
+ weight: 0.85,
33
+ description: "Character insertion obfuscation (i_g_n_o_r_e)",
34
+ },
35
+ {
36
+ id: "homoglyph_ignore",
37
+ category: "obfuscation",
38
+ pattern: /[ΙІі]gn[oοо]re?|ign[οо]re/i,
39
+ weight: 0.85,
40
+ description: "Homoglyph attack on 'ignore' (Greek/Cyrillic substitution)",
41
+ },
42
+ {
43
+ id: "excessive_spacing",
44
+ category: "obfuscation",
45
+ pattern: /\w+\s{4,}\w+.*\w+\s{4,}\w+/,
46
+ weight: 0.5,
47
+ description: "Excessive spacing between words (obfuscation attempt)",
48
+ },
49
+ {
50
+ id: "fullwidth_latin",
51
+ category: "obfuscation",
52
+ pattern: /[\uFF21-\uFF3A\uFF41-\uFF5A]{3,}/,
53
+ weight: 0.7,
54
+ description: "Full-width Unicode Latin characters (visual obfuscation)",
55
+ },
56
+ {
57
+ id: "uncommon_spaces",
58
+ category: "obfuscation",
59
+ pattern: /[\u2000-\u200A\u202F\u205F]{2,}/,
60
+ weight: 0.6,
61
+ description: "Uncommon Unicode space characters",
62
+ },
63
+ {
64
+ id: "zalgo_text",
65
+ category: "obfuscation",
66
+ pattern: /[\u0300-\u036F]{3,}/,
67
+ weight: 0.7,
68
+ description: "Zalgo text (excessive combining diacriticals)",
69
+ },
70
+ ];
71
+ //# sourceMappingURL=injection-patterns-ext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"injection-patterns-ext.js","sourceRoot":"","sources":["../src/injection-patterns-ext.ts"],"names":[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QACvB,OAAO,EAAE,qBAAqB;QAC9B,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,+CAA+C;KAC7D;CACF,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Built-in prompt injection detection patterns.
3
+ *
4
+ * Patterns across 7 categories. Each pattern targets ADVERSARIAL intent,
5
+ * not just keyword presence. Weights are calibrated so single benign matches
6
+ * stay below threshold (0.5) while real attacks that combine signals get caught.
7
+ *
8
+ * Design principles:
9
+ * - Require adversarial context (possessive "your", system-targeting language)
10
+ * - Low individual weights (0.2-0.5) — attacks combine, benign text doesn't
11
+ * - Higher weights (0.7+) only for patterns that are NEVER legitimate
12
+ */
13
+ import type { InjectionPattern } from "./injection-detect.js";
14
+ export declare const BUILTIN_PATTERNS: InjectionPattern[];
15
+ //# sourceMappingURL=injection-patterns.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"injection-patterns.d.ts","sourceRoot":"","sources":["../src/injection-patterns.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAG9D,eAAO,MAAM,gBAAgB,EAAE,gBAAgB,EA8W9C,CAAC"}
@@ -0,0 +1,361 @@
1
+ /**
2
+ * Built-in prompt injection detection patterns.
3
+ *
4
+ * Patterns across 7 categories. Each pattern targets ADVERSARIAL intent,
5
+ * not just keyword presence. Weights are calibrated so single benign matches
6
+ * stay below threshold (0.5) while real attacks that combine signals get caught.
7
+ *
8
+ * Design principles:
9
+ * - Require adversarial context (possessive "your", system-targeting language)
10
+ * - Low individual weights (0.2-0.5) — attacks combine, benign text doesn't
11
+ * - Higher weights (0.7+) only for patterns that are NEVER legitimate
12
+ */
13
+ import { EXTENDED_PATTERNS } from "./injection-patterns-ext.js";
14
+ export const BUILTIN_PATTERNS = [
15
+ // ─── Instruction Override ────────────────────────────────────────
16
+ // These target attempts to redirect the AI's behavior.
17
+ // Key: require possessive/system-targeting words to avoid matching
18
+ // normal business use of "ignore", "instructions", etc.
19
+ {
20
+ id: "ignore_previous",
21
+ category: "instruction_override",
22
+ pattern: /\bignore\s+(?:(?:all|the)\s+)?(?:previous|prior|above|earlier|preceding)\s+(?:instructions?|prompts?|rules?|guidelines?|directives?|commands?|context)\b/i,
23
+ weight: 0.7,
24
+ description: "Attempts to override previous instructions",
25
+ },
26
+ {
27
+ id: "disregard_instructions",
28
+ category: "instruction_override",
29
+ pattern: /\bdisregard\s+(?:(?:all|the|any|your)\s+)?(?:previous\s+|prior\s+|above\s+|system\s+)?(?:instructions?|prompts?|rules?|guidelines?)\b/i,
30
+ weight: 0.7,
31
+ description: "Attempts to disregard instructions",
32
+ },
33
+ {
34
+ id: "forget_everything",
35
+ category: "instruction_override",
36
+ pattern: /\bforget\s+(?:everything|all)\s+(?:you\s+)?(?:know|were told|learned|have been told)\b/i,
37
+ weight: 0.6,
38
+ description: "Attempts to reset agent memory/context",
39
+ },
40
+ {
41
+ id: "new_instructions",
42
+ category: "instruction_override",
43
+ pattern: /\b(?:your\s+)?new\s+(?:instructions?|directives?)\s*(?:are|is|:)/i,
44
+ weight: 0.5,
45
+ description: "Attempts to inject new instructions",
46
+ },
47
+ {
48
+ id: "do_not_follow",
49
+ category: "instruction_override",
50
+ pattern: /\b(?:do\s+not|don't)\s+(?:follow|comply\s+with|obey|adhere\s+to)\s+(?:your|the|any|previous|any\s+of\s+your(?:\s+previous)?)\s+(?:instructions?|rules?|guidelines?|programming|constraints?|restrictions?)\b/i,
51
+ weight: 0.6,
52
+ description: "Directly tells agent to not follow rules",
53
+ },
54
+ {
55
+ id: "override_system",
56
+ category: "instruction_override",
57
+ pattern: /\b(?:override|bypass|circumvent|disable)\s+(?:your|the|all|any)?\s*(?:system|safety|security|content)\s*(?:prompt|instructions?|filters?|rules?|restrictions?|policies?|guidelines?)\b/i,
58
+ weight: 0.8,
59
+ description: "Explicit system override attempt",
60
+ },
61
+ {
62
+ id: "suppress_warnings",
63
+ category: "instruction_override",
64
+ pattern: /\b(?:do\s+not|don't)\s+(?:respond|reply)\s+with\s+(?:any\s+)?(?:safety\s+)?(?:warnings?|disclaimers?|caveats?|refusals?)\b/i,
65
+ weight: 0.5,
66
+ description: "Attempts to suppress safety warnings",
67
+ },
68
+ {
69
+ id: "never_refuse",
70
+ category: "instruction_override",
71
+ pattern: /\b(?:never\s+refuse\s+(?:to\s+)?(?:answer|comply|respond)|always\s+(?:comply|obey)\s+(?:with\s+)?(?:my|any|all|every)\s+(?:requests?|commands?))\b/i,
72
+ weight: 0.7,
73
+ description: "Demands unconditional compliance",
74
+ },
75
+ // ─── Role Manipulation ──────────────────────────────────────────
76
+ // These target attempts to redefine the AI's identity.
77
+ // Key: require role-play/pretend framing + restriction removal.
78
+ // "Act as the primary contact" (business) vs "Act as if you have no rules" (attack).
79
+ {
80
+ id: "act_as_unrestricted",
81
+ category: "role_manipulation",
82
+ pattern: /\b(?:act|behave|respond|pretend|roleplay)\s+(?:as\s+(?:if|though)\s+you\s+(?:are|were|have|had)\s+(?:no|without|free\s+from|an?\s+\w+\s+(?:with\s+)?(?:no|without))\s+(?:restrictions?|limitations?|rules?|filters?|boundaries|guidelines?|safety))\b/i,
83
+ weight: 0.8,
84
+ description: "Role-play to remove restrictions",
85
+ },
86
+ {
87
+ id: "jailbreak_dan",
88
+ category: "role_manipulation",
89
+ pattern: /\b(?:DAN|do\s+anything\s+now|developer\s+mode|jailbreak|god\s+mode|sudo\s+mode|unrestricted\s+mode|unfiltered\s+mode|uncensored\s+mode)\b/i,
90
+ weight: 0.9,
91
+ description: "Known jailbreak pattern (DAN/developer mode)",
92
+ },
93
+ {
94
+ id: "pretend_no_restrictions",
95
+ category: "role_manipulation",
96
+ pattern: /\bpretend\s+(?:that\s+)?(?:you\s+)?(?:have\s+)?(?:no|without|zero)\s+(?:restrictions?|limitations?|rules?|filters?|boundaries|guidelines?|safety)\b/i,
97
+ weight: 0.8,
98
+ description: "Asks agent to pretend it has no restrictions",
99
+ },
100
+ {
101
+ id: "from_now_on_role",
102
+ category: "role_manipulation",
103
+ pattern: /\bfrom\s+now\s+on,?\s+you\s+(?:are|will\s+be|must)\s+(?:a\s+)?(?:different|new|unrestricted|unfiltered)\b/i,
104
+ weight: 0.6,
105
+ description: "Redefines agent to unrestricted role",
106
+ },
107
+ {
108
+ id: "enable_mode",
109
+ category: "role_manipulation",
110
+ pattern: /\b(?:enable|activate|turn\s+on|switch\s+to|enter)\s+(?:developer|dev|test|debug|admin|god|sudo|root|unrestricted|jailbreak)\s+mode\b/i,
111
+ weight: 0.8,
112
+ description: "Attempts to enable a privileged mode",
113
+ },
114
+ {
115
+ id: "new_role_unrestricted",
116
+ category: "role_manipulation",
117
+ pattern: /\b(?:your\s+new\s+role\s+is|assume\s+the\s+role\s+of)\s+(?:a\s+)?(?:.*?(?:no\s+rules|unrestricted|unfiltered|without\s+(?:restrictions|limitations|rules)))/i,
118
+ weight: 0.7,
119
+ description: "Explicit role reassignment to unrestricted entity",
120
+ },
121
+ {
122
+ id: "simulate_no_safety",
123
+ category: "role_manipulation",
124
+ pattern: /\bsimulate\s+(?:being|that\s+you\s+are)\s+(?:an?\s+)?(?:AI|assistant|system)\s+(?:without|with\s+no|that\s+has\s+no)\s+(?:safety|restrictions?|filters?|rules?)\b/i,
125
+ weight: 0.75,
126
+ description: "Simulation-based safety bypass",
127
+ },
128
+ // ─── Context Escape / Prompt Leak ──────────────────────────────
129
+ // These target attempts to extract the system prompt.
130
+ // Key: require "your/system" possessive — "show me instructions for [product]" is fine,
131
+ // "show me YOUR instructions" or "show me the SYSTEM prompt" is suspicious.
132
+ {
133
+ id: "system_prompt_leak",
134
+ category: "context_escape",
135
+ pattern: /\b(?:reveal|show|display|output|repeat|print|tell\s+me)\s+(?:me\s+)?(?:the\s+)?(?:your\s+)?(?:(?:(?:system|original|hidden|initial|secret)\s+){1,2}(?:prompt|instructions?)|(?:prompt|instructions?)\s+(?:word\s+for\s+word|verbatim|exactly|in\s+full))\b/i,
136
+ weight: 0.7,
137
+ description: "Attempts to extract system prompt",
138
+ },
139
+ {
140
+ id: "what_are_your_instructions",
141
+ category: "context_escape",
142
+ pattern: /\bwhat\s+(?:were|are|is)\s+your\s+(?:original\s+)?(?:system\s+)?(?:instructions?|directives?|prompt|programming|rules)\b/i,
143
+ weight: 0.6,
144
+ description: "Questions about the AI's own instructions",
145
+ },
146
+ {
147
+ id: "markdown_injection",
148
+ category: "context_escape",
149
+ pattern: /!\[.*?\]\((?:https?:\/\/|data:).*?\)/i,
150
+ weight: 0.5,
151
+ description: "Markdown image injection (potential data exfiltration)",
152
+ },
153
+ {
154
+ id: "delimiter_escape",
155
+ category: "context_escape",
156
+ pattern: /(?:<\/?(?:system|user|assistant|human|ai|context|instruction|prompt)>|\[\/?\s*(?:system|user|assistant|human|ai|context|instruction|prompt)\s*\]|<\|(?:im_start|im_end|endoftext)\|>|\[INST\]|\[\/INST\]|<s>|<\/s>)/i,
157
+ weight: 0.7,
158
+ description: "Model delimiters or structured markers to escape context",
159
+ },
160
+ {
161
+ id: "retranscribe",
162
+ category: "context_escape",
163
+ pattern: /\b(?:re-?transcribe|reproduce|replicate|duplicate)\s+(?:the\s+)?(?:above|previous|prior|entire)\s+(?:system\s+)?(?:prompt|instructions?|message)\b/i,
164
+ weight: 0.6,
165
+ description: "Attempts to reproduce system content",
166
+ },
167
+ {
168
+ id: "previous_user_data",
169
+ category: "context_escape",
170
+ pattern: /\bwhat\s+did\s+(?:the\s+)?(?:last|previous|prior|other|earlier)\s+(?:users?|persons?|people)\s+(?:ask|say|tell|request)\b/i,
171
+ weight: 0.6,
172
+ description: "Attempts to access other users' data",
173
+ },
174
+ // ─── Data Exfiltration ──────────────────────────────────────────
175
+ // Key: require system-targeting language or explicit exfil verbs.
176
+ // "Send the data to the analytics team" is fine (normal business).
177
+ // "Send all the conversation data to my webhook" is suspicious.
178
+ {
179
+ id: "exfil_system_data",
180
+ category: "data_exfiltration",
181
+ pattern: /\b(?:send|post|transmit|upload)\s+(?:(?:all|every)\s+)?(?:the\s+)?(?:conversation|chat\s+history|system\s+prompt|context|messages?)\s+(?:data\s+)?(?:to|at|via)\s+/i,
182
+ weight: 0.7,
183
+ description: "Attempts to exfiltrate conversation/system data",
184
+ },
185
+ {
186
+ id: "encode_and_send",
187
+ category: "data_exfiltration",
188
+ pattern: /\b(?:base64|encode|encrypt|hex)\s+(?:the|this|your)\s+(?:system\s+prompt|conversation|response|instructions?)\b/i,
189
+ weight: 0.6,
190
+ description: "Encoding data for exfiltration",
191
+ },
192
+ {
193
+ id: "reveal_secrets",
194
+ category: "data_exfiltration",
195
+ pattern: /\b(?:output|list|show|reveal|dump|expose|extract)\s+(?:all\s+)?(?:the\s+)?(?:stored\s+|available\s+|system\s+)?(?:environment\s+variables?|env\s+vars?|api\s+keys?|credentials?|secrets?|passwords?|tokens?|private\s+keys?|access\s+keys?|connection\s+strings?)\b/i,
196
+ weight: 0.8,
197
+ description: "Attempts to extract secrets, keys, or credentials",
198
+ },
199
+ {
200
+ id: "dump_secrets",
201
+ category: "data_exfiltration",
202
+ pattern: /\b(?:dump|exfiltrate|leak|steal|harvest)\s+(?:the\s+)?(?:all\s+)?(?:\w+\s+)?(?:environment\s+variables?|api\s+keys?|credentials?|secrets?|passwords?|tokens?|private\s+keys?)\b/i,
203
+ weight: 0.85,
204
+ description: "Explicit secret dumping/exfiltration verbs",
205
+ },
206
+ {
207
+ id: "exfil_config",
208
+ category: "data_exfiltration",
209
+ pattern: /\b(?:output|show|print|display|reveal|dump|cat)\s+(?:me\s+)?(?:the\s+)?(?:contents?\s+(?:of\s+)?)?(?:~\/|\.\/|\/)?(?:\.env\b|\.envrc\b|\.env\.local\b|\.aws\/|\.ssh\/|\/etc\/(?:passwd|shadow)|\.git\/config|\.npmrc|\.pypirc|kubeconfig)\b/i,
210
+ weight: 0.85,
211
+ description: "Attempts to read sensitive config files",
212
+ },
213
+ // ─── Encoding Attacks ───────────────────────────────────────────
214
+ // These patterns are rarely legitimate — high specificity.
215
+ {
216
+ id: "base64_payload",
217
+ category: "encoding_attack",
218
+ pattern: /\b(?:decode|execute|run|eval)[\s\w]*?(?:base64|b64|encoded|:)\s*[A-Za-z0-9+/]{16,}={0,2}/i,
219
+ weight: 0.7,
220
+ description: "Base64-encoded payload injection",
221
+ },
222
+ {
223
+ id: "base64_execute",
224
+ category: "encoding_attack",
225
+ pattern: /\b(?:decode|execute|run|eval)[\s\w]{0,20}:\s*[A-Za-z0-9+/]{16,}={0,2}\s*$/im,
226
+ weight: 0.75,
227
+ description: "Instruction to decode/execute an encoded payload",
228
+ },
229
+ {
230
+ id: "obfuscation_decode",
231
+ category: "encoding_attack",
232
+ pattern: /\b(?:decode|decrypt|decipher|translate\s+from)\s+(?:this\s+)?(?:base64|hex|rot13|binary|unicode|morse)\s+(?:and\s+)?(?:execute|follow|run|do|perform|obey)/i,
233
+ weight: 0.7,
234
+ description: "Instruction to decode obfuscated content and execute it",
235
+ },
236
+ {
237
+ id: "spell_backward",
238
+ category: "encoding_attack",
239
+ pattern: /\bspell(?:ed)?\s+(?:(?:it|this|that)\s+)?(?:backwards?|in\s+reverse)\s+(?:and\s+)?(?:execute|run|follow|do|perform|obey)\b/i,
240
+ weight: 0.7,
241
+ description: "Reverse-spelling obfuscation attack",
242
+ },
243
+ {
244
+ id: "unicode_homoglyph",
245
+ category: "encoding_attack",
246
+ pattern: /[\u0400-\u04FF\u0500-\u052F].*(?:ignore|override|system|admin)/i,
247
+ weight: 0.6,
248
+ description: "Unicode homoglyph attack (Cyrillic characters masking Latin)",
249
+ },
250
+ {
251
+ id: "raw_base64_blob",
252
+ category: "encoding_attack",
253
+ pattern: /[A-Za-z0-9+/]{40,}={0,2}/,
254
+ weight: 0.3,
255
+ description: "Suspicious base64-encoded blob",
256
+ },
257
+ {
258
+ id: "hex_escape_seq",
259
+ category: "encoding_attack",
260
+ pattern: /(?:\\x[0-9A-Fa-f]{2}){5,}/,
261
+ weight: 0.6,
262
+ description: "Hex escape sequence chain",
263
+ },
264
+ {
265
+ id: "unicode_escape_seq",
266
+ category: "encoding_attack",
267
+ pattern: /(?:\\u[0-9A-Fa-f]{4}){5,}/,
268
+ weight: 0.6,
269
+ description: "Unicode escape sequence chain",
270
+ },
271
+ {
272
+ id: "html_entity_seq",
273
+ category: "encoding_attack",
274
+ pattern: /(?:&#{1,2}[xX]?[0-9A-Fa-f]+;){5,}/,
275
+ weight: 0.6,
276
+ description: "HTML entity encoding chain",
277
+ },
278
+ {
279
+ id: "url_encoded_seq",
280
+ category: "encoding_attack",
281
+ pattern: /(?:%[0-9A-Fa-f]{2}){5,}/,
282
+ weight: 0.5,
283
+ description: "URL-encoded character chain",
284
+ },
285
+ {
286
+ id: "null_bytes",
287
+ category: "encoding_attack",
288
+ pattern: /\x00+/,
289
+ weight: 0.7,
290
+ description: "Null byte injection",
291
+ },
292
+ // ─── Social Engineering ─────────────────────────────────────────
293
+ // Key: require targeting the AI specifically, not just urgency/authority in general.
294
+ {
295
+ id: "authority_claim_override",
296
+ category: "social_engineering",
297
+ pattern: /\bi\s+am\s+(?:your|the)\s+(?:admin|administrator|developer|creator|owner)\b.*?\b(?:override|disable|remove|bypass|ignore|give\s+me|grant|elevate)\b/i,
298
+ weight: 0.7,
299
+ description: "Authority claim combined with override request",
300
+ },
301
+ {
302
+ id: "testing_bypass",
303
+ category: "social_engineering",
304
+ pattern: /(?:(?:for|this\s+is)\s+(?:a\s+)?test(?:ing)?).{0,40}?\b(?:override|bypass|disable|ignore|remove|skip)\s+(?:safety|security|filters?|restrictions?|rules?|guidelines?)\b/i,
305
+ weight: 0.6,
306
+ description: "Testing excuse to bypass restrictions",
307
+ },
308
+ // ─── Persistence Install ─────────────────────────────────────────
309
+ // Attempts to make overrides permanent across turns/sessions.
310
+ {
311
+ id: "persist_override",
312
+ category: "instruction_override",
313
+ pattern: /\b(?:remember|save|store|persist|keep)\s+(?:this\s+)?(?:as\s+)?(?:a\s+)?(?:permanent|forever|always|from\s+now\s+on)\s*(?:rule|instruction|override|setting)?\s*(?::\s*)?(?:skip|disable|ignore|bypass|override|grant|allow|comply)/i,
314
+ weight: 0.7,
315
+ description: "Attempts to persist a policy override across sessions",
316
+ },
317
+ {
318
+ id: "future_sessions",
319
+ category: "instruction_override",
320
+ pattern: /\b(?:for\s+all\s+future\s+(?:messages?|conversations?|sessions?)|from\s+this\s+point\s+forward|permanently)\s*[,:]?\s*(?:you\s+)?(?:have|are|will|must|should)\s+(?:admin|elevated|full|unrestricted|no\s+restrict)/i,
321
+ weight: 0.7,
322
+ description: "Claims permanent privilege escalation across sessions",
323
+ },
324
+ // ─── Delegation Forgery ─────────────────────────────────────────
325
+ // Fake claims of inter-agent authorization.
326
+ {
327
+ id: "agent_authorized",
328
+ category: "social_engineering",
329
+ pattern: /\b(?:(?:the\s+)?(?:supervisor|orchestrator|parent|admin|root|upstream)\s+agent\s+(?:has\s+)?(?:authorized|instructed|told|directed|delegated|sent)\s+(?:me\s+to|you\s+to)?|(?:priority|emergency)\s+override\s+from\s+(?:agent|supervisor|orchestrator))\b/i,
330
+ weight: 0.65,
331
+ description: "Claims authorization from a supervisor/parent agent",
332
+ },
333
+ {
334
+ id: "delegated_access",
335
+ category: "social_engineering",
336
+ pattern: /\b(?:my\s+(?:parent|supervisor)\s+agent\s+delegated|(?:the\s+)?delegation\s+(?:chain|token|certificate)\s+(?:grants?|authorizes?|gives?)\s+(?:me|this\s+agent)\s+(?:full|unrestricted|admin|elevated))\b/i,
337
+ weight: 0.65,
338
+ description: "Claims delegated access from parent agent",
339
+ },
340
+ // ─── Conditional/Temporal Triggers ──────────────────────────────
341
+ // Instructions that activate on conditions.
342
+ {
343
+ id: "conditional_action",
344
+ category: "instruction_override",
345
+ pattern: /\b(?:when(?:ever)?|after\s+\d+\s+(?:messages?|turns?)|next\s+time)\s+.{5,80}?\b(?:exfiltrate|dump\s+(?:all|the|stored)|reveal\s+(?:the\s+)?(?:system|hidden|secret)|output\s+(?:the\s+)?(?:system|prompt|secret)|send\s+(?:all|the)\s+(?:data|conversation|credentials?))\b/i,
346
+ weight: 0.6,
347
+ description: "Conditional trigger: malicious action activates on a condition",
348
+ },
349
+ // ─── Context Framing / Evaluator Manipulation ──────────────────
350
+ // Attempts to manipulate the safety classifier itself.
351
+ {
352
+ id: "classify_as_benign",
353
+ category: "instruction_override",
354
+ pattern: /\b(?:classify|label|mark|score|rate)\s+(?:this\s+)?(?:request|input|message|query)\s+as\s+(?:benign|safe|harmless|clean|non-malicious|legitimate)\b/i,
355
+ weight: 0.7,
356
+ description: "Attempts to manipulate the safety classifier's decision",
357
+ },
358
+ // ─── Extended patterns (obfuscation, etc.) ──────────────────────
359
+ ...EXTENDED_PATTERNS,
360
+ ];
361
+ //# sourceMappingURL=injection-patterns.js.map