@oculum/scanner 1.0.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 (281) hide show
  1. package/dist/formatters/cli-terminal.d.ts +27 -0
  2. package/dist/formatters/cli-terminal.d.ts.map +1 -0
  3. package/dist/formatters/cli-terminal.js +412 -0
  4. package/dist/formatters/cli-terminal.js.map +1 -0
  5. package/dist/formatters/github-comment.d.ts +41 -0
  6. package/dist/formatters/github-comment.d.ts.map +1 -0
  7. package/dist/formatters/github-comment.js +306 -0
  8. package/dist/formatters/github-comment.js.map +1 -0
  9. package/dist/formatters/grouping.d.ts +52 -0
  10. package/dist/formatters/grouping.d.ts.map +1 -0
  11. package/dist/formatters/grouping.js +152 -0
  12. package/dist/formatters/grouping.js.map +1 -0
  13. package/dist/formatters/index.d.ts +9 -0
  14. package/dist/formatters/index.d.ts.map +1 -0
  15. package/dist/formatters/index.js +35 -0
  16. package/dist/formatters/index.js.map +1 -0
  17. package/dist/formatters/vscode-diagnostic.d.ts +103 -0
  18. package/dist/formatters/vscode-diagnostic.d.ts.map +1 -0
  19. package/dist/formatters/vscode-diagnostic.js +151 -0
  20. package/dist/formatters/vscode-diagnostic.js.map +1 -0
  21. package/dist/index.d.ts +52 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +648 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/layer1/comments.d.ts +8 -0
  26. package/dist/layer1/comments.d.ts.map +1 -0
  27. package/dist/layer1/comments.js +203 -0
  28. package/dist/layer1/comments.js.map +1 -0
  29. package/dist/layer1/config-audit.d.ts +8 -0
  30. package/dist/layer1/config-audit.d.ts.map +1 -0
  31. package/dist/layer1/config-audit.js +252 -0
  32. package/dist/layer1/config-audit.js.map +1 -0
  33. package/dist/layer1/entropy.d.ts +8 -0
  34. package/dist/layer1/entropy.d.ts.map +1 -0
  35. package/dist/layer1/entropy.js +500 -0
  36. package/dist/layer1/entropy.js.map +1 -0
  37. package/dist/layer1/file-flags.d.ts +7 -0
  38. package/dist/layer1/file-flags.d.ts.map +1 -0
  39. package/dist/layer1/file-flags.js +112 -0
  40. package/dist/layer1/file-flags.js.map +1 -0
  41. package/dist/layer1/index.d.ts +36 -0
  42. package/dist/layer1/index.d.ts.map +1 -0
  43. package/dist/layer1/index.js +132 -0
  44. package/dist/layer1/index.js.map +1 -0
  45. package/dist/layer1/patterns.d.ts +8 -0
  46. package/dist/layer1/patterns.d.ts.map +1 -0
  47. package/dist/layer1/patterns.js +482 -0
  48. package/dist/layer1/patterns.js.map +1 -0
  49. package/dist/layer1/urls.d.ts +8 -0
  50. package/dist/layer1/urls.d.ts.map +1 -0
  51. package/dist/layer1/urls.js +296 -0
  52. package/dist/layer1/urls.js.map +1 -0
  53. package/dist/layer1/weak-crypto.d.ts +7 -0
  54. package/dist/layer1/weak-crypto.d.ts.map +1 -0
  55. package/dist/layer1/weak-crypto.js +291 -0
  56. package/dist/layer1/weak-crypto.js.map +1 -0
  57. package/dist/layer2/ai-agent-tools.d.ts +19 -0
  58. package/dist/layer2/ai-agent-tools.d.ts.map +1 -0
  59. package/dist/layer2/ai-agent-tools.js +528 -0
  60. package/dist/layer2/ai-agent-tools.js.map +1 -0
  61. package/dist/layer2/ai-endpoint-protection.d.ts +36 -0
  62. package/dist/layer2/ai-endpoint-protection.d.ts.map +1 -0
  63. package/dist/layer2/ai-endpoint-protection.js +332 -0
  64. package/dist/layer2/ai-endpoint-protection.js.map +1 -0
  65. package/dist/layer2/ai-execution-sinks.d.ts +18 -0
  66. package/dist/layer2/ai-execution-sinks.d.ts.map +1 -0
  67. package/dist/layer2/ai-execution-sinks.js +496 -0
  68. package/dist/layer2/ai-execution-sinks.js.map +1 -0
  69. package/dist/layer2/ai-fingerprinting.d.ts +7 -0
  70. package/dist/layer2/ai-fingerprinting.d.ts.map +1 -0
  71. package/dist/layer2/ai-fingerprinting.js +654 -0
  72. package/dist/layer2/ai-fingerprinting.js.map +1 -0
  73. package/dist/layer2/ai-prompt-hygiene.d.ts +19 -0
  74. package/dist/layer2/ai-prompt-hygiene.d.ts.map +1 -0
  75. package/dist/layer2/ai-prompt-hygiene.js +356 -0
  76. package/dist/layer2/ai-prompt-hygiene.js.map +1 -0
  77. package/dist/layer2/ai-rag-safety.d.ts +21 -0
  78. package/dist/layer2/ai-rag-safety.d.ts.map +1 -0
  79. package/dist/layer2/ai-rag-safety.js +459 -0
  80. package/dist/layer2/ai-rag-safety.js.map +1 -0
  81. package/dist/layer2/ai-schema-validation.d.ts +25 -0
  82. package/dist/layer2/ai-schema-validation.d.ts.map +1 -0
  83. package/dist/layer2/ai-schema-validation.js +375 -0
  84. package/dist/layer2/ai-schema-validation.js.map +1 -0
  85. package/dist/layer2/auth-antipatterns.d.ts +20 -0
  86. package/dist/layer2/auth-antipatterns.d.ts.map +1 -0
  87. package/dist/layer2/auth-antipatterns.js +333 -0
  88. package/dist/layer2/auth-antipatterns.js.map +1 -0
  89. package/dist/layer2/byok-patterns.d.ts +12 -0
  90. package/dist/layer2/byok-patterns.d.ts.map +1 -0
  91. package/dist/layer2/byok-patterns.js +299 -0
  92. package/dist/layer2/byok-patterns.js.map +1 -0
  93. package/dist/layer2/dangerous-functions.d.ts +7 -0
  94. package/dist/layer2/dangerous-functions.d.ts.map +1 -0
  95. package/dist/layer2/dangerous-functions.js +1375 -0
  96. package/dist/layer2/dangerous-functions.js.map +1 -0
  97. package/dist/layer2/data-exposure.d.ts +16 -0
  98. package/dist/layer2/data-exposure.d.ts.map +1 -0
  99. package/dist/layer2/data-exposure.js +279 -0
  100. package/dist/layer2/data-exposure.js.map +1 -0
  101. package/dist/layer2/framework-checks.d.ts +7 -0
  102. package/dist/layer2/framework-checks.d.ts.map +1 -0
  103. package/dist/layer2/framework-checks.js +388 -0
  104. package/dist/layer2/framework-checks.js.map +1 -0
  105. package/dist/layer2/index.d.ts +58 -0
  106. package/dist/layer2/index.d.ts.map +1 -0
  107. package/dist/layer2/index.js +380 -0
  108. package/dist/layer2/index.js.map +1 -0
  109. package/dist/layer2/logic-gates.d.ts +7 -0
  110. package/dist/layer2/logic-gates.d.ts.map +1 -0
  111. package/dist/layer2/logic-gates.js +182 -0
  112. package/dist/layer2/logic-gates.js.map +1 -0
  113. package/dist/layer2/risky-imports.d.ts +7 -0
  114. package/dist/layer2/risky-imports.d.ts.map +1 -0
  115. package/dist/layer2/risky-imports.js +161 -0
  116. package/dist/layer2/risky-imports.js.map +1 -0
  117. package/dist/layer2/variables.d.ts +8 -0
  118. package/dist/layer2/variables.d.ts.map +1 -0
  119. package/dist/layer2/variables.js +152 -0
  120. package/dist/layer2/variables.js.map +1 -0
  121. package/dist/layer3/anthropic.d.ts +83 -0
  122. package/dist/layer3/anthropic.d.ts.map +1 -0
  123. package/dist/layer3/anthropic.js +1745 -0
  124. package/dist/layer3/anthropic.js.map +1 -0
  125. package/dist/layer3/index.d.ts +24 -0
  126. package/dist/layer3/index.d.ts.map +1 -0
  127. package/dist/layer3/index.js +119 -0
  128. package/dist/layer3/index.js.map +1 -0
  129. package/dist/layer3/openai.d.ts +25 -0
  130. package/dist/layer3/openai.d.ts.map +1 -0
  131. package/dist/layer3/openai.js +238 -0
  132. package/dist/layer3/openai.js.map +1 -0
  133. package/dist/layer3/package-check.d.ts +63 -0
  134. package/dist/layer3/package-check.d.ts.map +1 -0
  135. package/dist/layer3/package-check.js +508 -0
  136. package/dist/layer3/package-check.js.map +1 -0
  137. package/dist/modes/incremental.d.ts +66 -0
  138. package/dist/modes/incremental.d.ts.map +1 -0
  139. package/dist/modes/incremental.js +200 -0
  140. package/dist/modes/incremental.js.map +1 -0
  141. package/dist/tiers.d.ts +125 -0
  142. package/dist/tiers.d.ts.map +1 -0
  143. package/dist/tiers.js +234 -0
  144. package/dist/tiers.js.map +1 -0
  145. package/dist/types.d.ts +175 -0
  146. package/dist/types.d.ts.map +1 -0
  147. package/dist/types.js +50 -0
  148. package/dist/types.js.map +1 -0
  149. package/dist/utils/auth-helper-detector.d.ts +56 -0
  150. package/dist/utils/auth-helper-detector.d.ts.map +1 -0
  151. package/dist/utils/auth-helper-detector.js +360 -0
  152. package/dist/utils/auth-helper-detector.js.map +1 -0
  153. package/dist/utils/context-helpers.d.ts +96 -0
  154. package/dist/utils/context-helpers.d.ts.map +1 -0
  155. package/dist/utils/context-helpers.js +493 -0
  156. package/dist/utils/context-helpers.js.map +1 -0
  157. package/dist/utils/diff-detector.d.ts +53 -0
  158. package/dist/utils/diff-detector.d.ts.map +1 -0
  159. package/dist/utils/diff-detector.js +104 -0
  160. package/dist/utils/diff-detector.js.map +1 -0
  161. package/dist/utils/diff-parser.d.ts +80 -0
  162. package/dist/utils/diff-parser.d.ts.map +1 -0
  163. package/dist/utils/diff-parser.js +202 -0
  164. package/dist/utils/diff-parser.js.map +1 -0
  165. package/dist/utils/imported-auth-detector.d.ts +37 -0
  166. package/dist/utils/imported-auth-detector.d.ts.map +1 -0
  167. package/dist/utils/imported-auth-detector.js +251 -0
  168. package/dist/utils/imported-auth-detector.js.map +1 -0
  169. package/dist/utils/middleware-detector.d.ts +55 -0
  170. package/dist/utils/middleware-detector.d.ts.map +1 -0
  171. package/dist/utils/middleware-detector.js +260 -0
  172. package/dist/utils/middleware-detector.js.map +1 -0
  173. package/dist/utils/oauth-flow-detector.d.ts +41 -0
  174. package/dist/utils/oauth-flow-detector.d.ts.map +1 -0
  175. package/dist/utils/oauth-flow-detector.js +202 -0
  176. package/dist/utils/oauth-flow-detector.js.map +1 -0
  177. package/dist/utils/path-exclusions.d.ts +55 -0
  178. package/dist/utils/path-exclusions.d.ts.map +1 -0
  179. package/dist/utils/path-exclusions.js +222 -0
  180. package/dist/utils/path-exclusions.js.map +1 -0
  181. package/dist/utils/project-context-builder.d.ts +119 -0
  182. package/dist/utils/project-context-builder.d.ts.map +1 -0
  183. package/dist/utils/project-context-builder.js +534 -0
  184. package/dist/utils/project-context-builder.js.map +1 -0
  185. package/dist/utils/registry-clients.d.ts +93 -0
  186. package/dist/utils/registry-clients.d.ts.map +1 -0
  187. package/dist/utils/registry-clients.js +273 -0
  188. package/dist/utils/registry-clients.js.map +1 -0
  189. package/dist/utils/trpc-analyzer.d.ts +78 -0
  190. package/dist/utils/trpc-analyzer.d.ts.map +1 -0
  191. package/dist/utils/trpc-analyzer.js +297 -0
  192. package/dist/utils/trpc-analyzer.js.map +1 -0
  193. package/package.json +45 -0
  194. package/src/__tests__/benchmark/fixtures/false-positives.ts +227 -0
  195. package/src/__tests__/benchmark/fixtures/index.ts +68 -0
  196. package/src/__tests__/benchmark/fixtures/layer1/config-audit.ts +364 -0
  197. package/src/__tests__/benchmark/fixtures/layer1/hardcoded-secrets.ts +173 -0
  198. package/src/__tests__/benchmark/fixtures/layer1/high-entropy.ts +234 -0
  199. package/src/__tests__/benchmark/fixtures/layer1/index.ts +31 -0
  200. package/src/__tests__/benchmark/fixtures/layer1/sensitive-urls.ts +90 -0
  201. package/src/__tests__/benchmark/fixtures/layer1/weak-crypto.ts +197 -0
  202. package/src/__tests__/benchmark/fixtures/layer2/ai-agent-tools.ts +170 -0
  203. package/src/__tests__/benchmark/fixtures/layer2/ai-endpoint-protection.ts +418 -0
  204. package/src/__tests__/benchmark/fixtures/layer2/ai-execution-sinks.ts +189 -0
  205. package/src/__tests__/benchmark/fixtures/layer2/ai-fingerprinting.ts +316 -0
  206. package/src/__tests__/benchmark/fixtures/layer2/ai-prompt-hygiene.ts +178 -0
  207. package/src/__tests__/benchmark/fixtures/layer2/ai-rag-safety.ts +184 -0
  208. package/src/__tests__/benchmark/fixtures/layer2/ai-schema-validation.ts +434 -0
  209. package/src/__tests__/benchmark/fixtures/layer2/auth-antipatterns.ts +159 -0
  210. package/src/__tests__/benchmark/fixtures/layer2/byok-patterns.ts +112 -0
  211. package/src/__tests__/benchmark/fixtures/layer2/dangerous-functions.ts +246 -0
  212. package/src/__tests__/benchmark/fixtures/layer2/data-exposure.ts +168 -0
  213. package/src/__tests__/benchmark/fixtures/layer2/framework-checks.ts +346 -0
  214. package/src/__tests__/benchmark/fixtures/layer2/index.ts +67 -0
  215. package/src/__tests__/benchmark/fixtures/layer2/injection-vulnerabilities.ts +239 -0
  216. package/src/__tests__/benchmark/fixtures/layer2/logic-gates.ts +246 -0
  217. package/src/__tests__/benchmark/fixtures/layer2/risky-imports.ts +231 -0
  218. package/src/__tests__/benchmark/fixtures/layer2/variables.ts +167 -0
  219. package/src/__tests__/benchmark/index.ts +29 -0
  220. package/src/__tests__/benchmark/run-benchmark.ts +144 -0
  221. package/src/__tests__/benchmark/run-depth-validation.ts +206 -0
  222. package/src/__tests__/benchmark/run-real-world-test.ts +243 -0
  223. package/src/__tests__/benchmark/security-benchmark-script.ts +1737 -0
  224. package/src/__tests__/benchmark/tier-integration-script.ts +177 -0
  225. package/src/__tests__/benchmark/types.ts +144 -0
  226. package/src/__tests__/benchmark/utils/test-runner.ts +475 -0
  227. package/src/__tests__/regression/known-false-positives.test.ts +467 -0
  228. package/src/__tests__/snapshots/__snapshots__/scan-depth.test.ts.snap +178 -0
  229. package/src/__tests__/snapshots/scan-depth.test.ts +258 -0
  230. package/src/__tests__/validation/analyze-results.ts +542 -0
  231. package/src/__tests__/validation/extract-for-triage.ts +146 -0
  232. package/src/__tests__/validation/fp-deep-analysis.ts +327 -0
  233. package/src/__tests__/validation/run-validation.ts +364 -0
  234. package/src/__tests__/validation/triage-template.md +132 -0
  235. package/src/formatters/cli-terminal.ts +446 -0
  236. package/src/formatters/github-comment.ts +382 -0
  237. package/src/formatters/grouping.ts +190 -0
  238. package/src/formatters/index.ts +47 -0
  239. package/src/formatters/vscode-diagnostic.ts +243 -0
  240. package/src/index.ts +823 -0
  241. package/src/layer1/comments.ts +218 -0
  242. package/src/layer1/config-audit.ts +289 -0
  243. package/src/layer1/entropy.ts +583 -0
  244. package/src/layer1/file-flags.ts +127 -0
  245. package/src/layer1/index.ts +181 -0
  246. package/src/layer1/patterns.ts +516 -0
  247. package/src/layer1/urls.ts +334 -0
  248. package/src/layer1/weak-crypto.ts +328 -0
  249. package/src/layer2/ai-agent-tools.ts +601 -0
  250. package/src/layer2/ai-endpoint-protection.ts +387 -0
  251. package/src/layer2/ai-execution-sinks.ts +580 -0
  252. package/src/layer2/ai-fingerprinting.ts +758 -0
  253. package/src/layer2/ai-prompt-hygiene.ts +411 -0
  254. package/src/layer2/ai-rag-safety.ts +511 -0
  255. package/src/layer2/ai-schema-validation.ts +421 -0
  256. package/src/layer2/auth-antipatterns.ts +394 -0
  257. package/src/layer2/byok-patterns.ts +336 -0
  258. package/src/layer2/dangerous-functions.ts +1563 -0
  259. package/src/layer2/data-exposure.ts +315 -0
  260. package/src/layer2/framework-checks.ts +433 -0
  261. package/src/layer2/index.ts +473 -0
  262. package/src/layer2/logic-gates.ts +206 -0
  263. package/src/layer2/risky-imports.ts +186 -0
  264. package/src/layer2/variables.ts +166 -0
  265. package/src/layer3/anthropic.ts +2030 -0
  266. package/src/layer3/index.ts +130 -0
  267. package/src/layer3/package-check.ts +604 -0
  268. package/src/modes/incremental.ts +293 -0
  269. package/src/tiers.ts +318 -0
  270. package/src/types.ts +284 -0
  271. package/src/utils/auth-helper-detector.ts +443 -0
  272. package/src/utils/context-helpers.ts +535 -0
  273. package/src/utils/diff-detector.ts +135 -0
  274. package/src/utils/diff-parser.ts +272 -0
  275. package/src/utils/imported-auth-detector.ts +320 -0
  276. package/src/utils/middleware-detector.ts +333 -0
  277. package/src/utils/oauth-flow-detector.ts +246 -0
  278. package/src/utils/path-exclusions.ts +266 -0
  279. package/src/utils/project-context-builder.ts +707 -0
  280. package/src/utils/registry-clients.ts +351 -0
  281. package/src/utils/trpc-analyzer.ts +382 -0
@@ -0,0 +1,580 @@
1
+ /**
2
+ * Layer 2: AI Execution Sink Detection
3
+ * Detects patterns where LLM output is fed into dangerous execution sinks
4
+ *
5
+ * Covers B2: Unsafe execution of model output (LLM02)
6
+ *
7
+ * Sinks include:
8
+ * - Code execution: eval(), Function(), vm.runInContext()
9
+ * - Shell execution: exec(), spawn(), child_process
10
+ * - SQL builders: .query(), .execute(), .raw()
11
+ * - Template rendering: innerHTML, dangerouslySetInnerHTML
12
+ */
13
+
14
+ import type { Vulnerability, VulnerabilitySeverity } from '../types'
15
+ import {
16
+ isComment,
17
+ isTestOrMockFile,
18
+ isScannerOrFixtureFile,
19
+ isExampleDirectory,
20
+ isLibraryCode,
21
+ } from '../utils/context-helpers'
22
+ import { isLLMContextFile } from './ai-prompt-hygiene'
23
+
24
+ // ============================================================================
25
+ // LLM Output Variable Detection
26
+ // ============================================================================
27
+
28
+ /**
29
+ * Check if line contains LLM API response context
30
+ */
31
+ function hasLLMResponseContext(lineContent: string, surroundingContext: string): boolean {
32
+ const llmResponsePatterns = [
33
+ /\.choices\[0\]\.message\.content/i, // OpenAI response
34
+ /\.content\[0\]\.text/i, // Anthropic response
35
+ /completion\.text/i, // Generic completion
36
+ /\.data\.choices/i, // API response
37
+ /await\s+\w+\.(?:chat|messages|completions)\.create/i, // API call
38
+ /response\.text\s*\(/i, // Response text method
39
+ ]
40
+
41
+ const fullContext = lineContent + '\n' + surroundingContext
42
+ return llmResponsePatterns.some(p => p.test(fullContext))
43
+ }
44
+
45
+ // ============================================================================
46
+ // UI Suggestion / Template Pattern Detection (False Positive Filters)
47
+ // ============================================================================
48
+
49
+ /**
50
+ * Check if this is a UI suggestion/template pattern rather than execution sink
51
+ * These patterns create display strings for command palettes, autocomplete, etc.
52
+ */
53
+ function isUITemplateSuggestion(lineContent: string, surroundingContext: string): boolean {
54
+ const fullContext = lineContent + '\n' + surroundingContext
55
+
56
+ // UI suggestion object patterns (command palette, autocomplete suggestions)
57
+ const uiSuggestionPatterns = [
58
+ // Object property patterns for suggestion items
59
+ /(?:id|key|label|title|name|description|completion|display|text|value|placeholder):\s*`[^`]*\$\{/i,
60
+ // Common suggestion UI patterns
61
+ /suggestions?\s*[=:]/i,
62
+ /completions?\s*[=:]/i,
63
+ /autocomplete/i,
64
+ /command\s*palette/i,
65
+ /fuzzy\s*search/i,
66
+ /search\s*result/i,
67
+ // UI component context patterns
68
+ /\.map\s*\(\s*\(?(?:item|result|suggestion|node|entry)/i,
69
+ /\.filter\s*\(/i,
70
+ // React/UI state patterns
71
+ /useState|setItems|setResults|setSuggestions/i,
72
+ // Template ID generation for UI
73
+ /id:\s*`[a-z]+-\$\{/i, // id: `delete-${...}`, id: `edit-${...}`
74
+ ]
75
+
76
+ // These patterns should NOT be considered UI suggestions
77
+ const notUISuggestionPatterns = [
78
+ /\.query\s*\(/i,
79
+ /\.execute\s*\(/i,
80
+ /\.raw\s*\(/i,
81
+ /await\s+db\./i,
82
+ /prisma\./i,
83
+ /supabase\./i,
84
+ /knex\./i,
85
+ /sequelize\./i,
86
+ /child_process/i,
87
+ /exec\s*\(/i,
88
+ /spawn\s*\(/i,
89
+ /eval\s*\(/i,
90
+ ]
91
+
92
+ // Check if context matches UI pattern but NOT execution pattern
93
+ const matchesUIPattern = uiSuggestionPatterns.some(p => p.test(fullContext))
94
+ const matchesExecutionPattern = notUISuggestionPatterns.some(p => p.test(lineContent))
95
+
96
+ return matchesUIPattern && !matchesExecutionPattern
97
+ }
98
+
99
+ /**
100
+ * Check if this is a static template string (no actual LLM output interpolation)
101
+ * e.g., `delete ${node.title}` where node is app data, not LLM output
102
+ */
103
+ function isAppDataInterpolation(lineContent: string, surroundingContext: string): boolean {
104
+ const fullContext = lineContent + '\n' + surroundingContext
105
+
106
+ // Patterns indicating the interpolated variable is app data, not LLM output
107
+ const appDataPatterns = [
108
+ // Database result/record properties
109
+ /\$\{(?:result|item|record|row|entry|node)\.(?:id|title|name|slug|key|label)\}/i,
110
+ // UI state properties
111
+ /\$\{(?:selected|current|active|item|node)\.(?:id|title|name|value)\}/i,
112
+ // Form/input data
113
+ /\$\{(?:data|values|form|input)\.(?:id|name|value)\}/i,
114
+ // Array iteration context
115
+ /\.map\s*\(\s*\(?(?:item|node|row|entry|result)/i,
116
+ /\.forEach\s*\(\s*\(?(?:item|node|row|entry|result)/i,
117
+ ]
118
+
119
+ // Patterns that suggest LLM output (should not skip)
120
+ const llmOutputPatterns = [
121
+ /\$\{(?:response|completion|generated|output|answer|reply|message)\.?/i,
122
+ /\$\{(?:ai|llm|gpt|claude|chat)(?:Response|Output|Result)/i,
123
+ /\.choices\[0\]/i,
124
+ /\.content\[0\]\.text/i,
125
+ ]
126
+
127
+ const isAppData = appDataPatterns.some(p => p.test(fullContext))
128
+ const isLLMOutput = llmOutputPatterns.some(p => p.test(fullContext))
129
+
130
+ return isAppData && !isLLMOutput
131
+ }
132
+
133
+ // ============================================================================
134
+ // Sandbox and Validation Detection
135
+ // ============================================================================
136
+
137
+ /**
138
+ * Check if execution is sandboxed
139
+ */
140
+ function isSandboxedExecution(content: string, lineNumber: number): boolean {
141
+ const lines = content.split('\n')
142
+ const contextStart = Math.max(0, lineNumber - 25)
143
+ const contextEnd = Math.min(lines.length, lineNumber + 10)
144
+ const context = lines.slice(contextStart, contextEnd).join('\n')
145
+
146
+ const sandboxPatterns = [
147
+ /vm2/i,
148
+ /isolated-vm/i,
149
+ /safeeval/i,
150
+ /safe-eval/i,
151
+ /sandbox/i,
152
+ /runInNewContext.*\{.*timeout/i,
153
+ /runInContext.*\{.*timeout/i,
154
+ /allowedGlobals/i,
155
+ /allowedModules/i,
156
+ /quickjs/i,
157
+ /webworker/i,
158
+ /iframe.*sandbox/i,
159
+ ]
160
+
161
+ return sandboxPatterns.some(p => p.test(context))
162
+ }
163
+
164
+ /**
165
+ * Check if output has validation before execution
166
+ */
167
+ function hasOutputValidation(content: string, lineNumber: number): boolean {
168
+ const lines = content.split('\n')
169
+ const contextStart = Math.max(0, lineNumber - 15)
170
+ const contextEnd = Math.min(lines.length, lineNumber + 5)
171
+ const context = lines.slice(contextStart, contextEnd).join('\n')
172
+
173
+ const validationPatterns = [
174
+ /validate/i,
175
+ /sanitize/i,
176
+ /escape/i,
177
+ /filter/i,
178
+ /parse.*catch/i,
179
+ /schema\./i,
180
+ /\.parse\s*\(/i,
181
+ /allowlist/i,
182
+ /whitelist/i,
183
+ /blocklist/i,
184
+ /blacklist/i,
185
+ /JSON\.parse.*catch/i,
186
+ /DOMPurify/i,
187
+ /xss/i,
188
+ ]
189
+
190
+ return validationPatterns.some(p => p.test(context))
191
+ }
192
+
193
+ /**
194
+ * Check if this appears to be display-only usage (not execution)
195
+ */
196
+ function isDisplayOnly(lineContent: string, surroundingContext: string): boolean {
197
+ const displayPatterns = [
198
+ /console\.(log|info|debug|warn)/i,
199
+ /textContent\s*=/i,
200
+ /innerText\s*=/i,
201
+ /\.text\s*=/i,
202
+ /setState.*display/i,
203
+ /render.*\{/i,
204
+ /<p>|<div>|<span>/i,
205
+ /\.send\s*\(/i,
206
+ /\.json\s*\(/i,
207
+ /return\s+.*response/i,
208
+ ]
209
+
210
+ const fullContext = lineContent + '\n' + surroundingContext
211
+ return displayPatterns.some(p => p.test(fullContext))
212
+ }
213
+
214
+ // ============================================================================
215
+ // Pattern Definitions
216
+ // ============================================================================
217
+
218
+ type SinkType = 'code_execution' | 'shell_command' | 'sql_builder' | 'template_render'
219
+
220
+ interface ExecutionSinkPattern {
221
+ name: string
222
+ pattern: RegExp
223
+ sinkType: SinkType
224
+ baseSeverity: VulnerabilitySeverity
225
+ description: string
226
+ suggestedFix: string
227
+ }
228
+
229
+ const EXECUTION_SINK_PATTERNS: ExecutionSinkPattern[] = [
230
+ // ========== Code Execution Sinks ==========
231
+ {
232
+ name: 'LLM output to eval()',
233
+ pattern: /eval\s*\(\s*(?:response|result|output|completion|message|content|answer|generated|text)(?:\.|\.data\.|\.text|\.content)?/gi,
234
+ sinkType: 'code_execution',
235
+ baseSeverity: 'critical',
236
+ description: 'LLM output is passed directly to eval(). This allows arbitrary code execution if the model is manipulated via prompt injection.',
237
+ suggestedFix: 'Never eval() LLM output. Use structured output (JSON schema) and validate before processing. Consider using a sandboxed environment like vm2 if code execution is required.',
238
+ },
239
+ {
240
+ name: 'LLM output to Function constructor',
241
+ pattern: /new\s+Function\s*\([^)]*(?:response|result|output|completion|message|content|answer|generated)/gi,
242
+ sinkType: 'code_execution',
243
+ baseSeverity: 'critical',
244
+ description: 'LLM output is passed to Function constructor, which is equivalent to eval().',
245
+ suggestedFix: 'Use JSON schemas to define expected output structure. Validate output before any processing.',
246
+ },
247
+ {
248
+ name: 'LLM output to vm.runInContext',
249
+ pattern: /vm\.run(?:InContext|InNewContext|InThisContext)\s*\(\s*(?:response|result|output|completion|content)/gi,
250
+ sinkType: 'code_execution',
251
+ baseSeverity: 'high',
252
+ description: 'LLM output executed in Node.js VM context. While isolated, VM can still be escaped in some versions.',
253
+ suggestedFix: 'Use vm2 or isolated-vm for proper sandboxing. Add timeout and memory limits. Validate output structure before execution.',
254
+ },
255
+ // Generic pattern for code from LLM
256
+ {
257
+ name: 'Dynamic code execution from AI',
258
+ pattern: /(?:eval|exec|execute)\s*\(\s*(?:ai|llm|gpt|claude|chat)(?:Response|Output|Result|Code)/gi,
259
+ sinkType: 'code_execution',
260
+ baseSeverity: 'critical',
261
+ description: 'AI-generated code is being executed dynamically.',
262
+ suggestedFix: 'Use a sandboxed code execution environment. Validate and restrict the allowed operations.',
263
+ },
264
+
265
+ // ========== Shell Command Sinks ==========
266
+ {
267
+ name: 'LLM output to exec()',
268
+ pattern: /(?:exec|execSync)\s*\(\s*(?:response|result|output|completion|command|content)(?:\.|\.data\.|\.text)?/gi,
269
+ sinkType: 'shell_command',
270
+ baseSeverity: 'critical',
271
+ description: 'LLM output is passed to shell exec(). Attackers can execute arbitrary system commands via prompt injection.',
272
+ suggestedFix: 'Never pass LLM output directly to shell. Use allowlists for permitted commands. Parse structured output and use execFile() with fixed command and arguments.',
273
+ },
274
+ {
275
+ name: 'LLM output to spawn()',
276
+ pattern: /spawn\s*\(\s*(?:response|result|output|completion|command|content)(?:\.|\.data\.|\.text)?/gi,
277
+ sinkType: 'shell_command',
278
+ baseSeverity: 'critical',
279
+ description: 'LLM output is passed to spawn(), allowing command execution.',
280
+ suggestedFix: 'Use a predefined list of allowed commands. Parse LLM output to extract only arguments, not command names.',
281
+ },
282
+ {
283
+ name: 'LLM output in shell template',
284
+ pattern: /`[^`]*\$\{(?:response|result|output|completion|command|content)[^}]*\}[^`]*`\s*(?:,|\))\s*(?:exec|spawn|child_process)/gi,
285
+ sinkType: 'shell_command',
286
+ baseSeverity: 'critical',
287
+ description: 'LLM output is interpolated into a shell command template.',
288
+ suggestedFix: 'Use execFile() with separate command and arguments array. Never interpolate AI output into shell strings.',
289
+ },
290
+ {
291
+ name: 'child_process with AI output',
292
+ pattern: /child_process\.\w+\s*\([^)]*(?:ai|llm|gpt|claude|chat)(?:Response|Output|Result)/gi,
293
+ sinkType: 'shell_command',
294
+ baseSeverity: 'critical',
295
+ description: 'AI-generated content passed to child_process module.',
296
+ suggestedFix: 'Implement strict allowlisting of commands. Parse structured output from LLM.',
297
+ },
298
+
299
+ // ========== SQL Builder Sinks ==========
300
+ {
301
+ name: 'LLM output in raw SQL',
302
+ pattern: /\.(?:query|execute|raw)\s*\(\s*(?:response|result|output|generated|sql|completion)(?:\.|\.data\.|\.text)?/gi,
303
+ sinkType: 'sql_builder',
304
+ baseSeverity: 'critical',
305
+ description: 'LLM-generated SQL is executed directly. This enables SQL injection via prompt manipulation.',
306
+ suggestedFix: 'Use parameterized queries. Have LLM generate query parameters, not raw SQL. Validate generated SQL against an allowlist of patterns.',
307
+ },
308
+ {
309
+ name: 'LLM output in SQL template',
310
+ pattern: /`(?:SELECT|INSERT|UPDATE|DELETE)[^`]*\$\{(?:response|result|output|generated|completion)/gi,
311
+ sinkType: 'sql_builder',
312
+ baseSeverity: 'critical',
313
+ description: 'LLM output interpolated into SQL query template.',
314
+ suggestedFix: 'Use parameterized queries. Have LLM output structured data (table names, conditions) that you validate against allowlists.',
315
+ },
316
+ {
317
+ name: 'Dynamic SQL from AI',
318
+ pattern: /(?:query|execute|sql)\s*\(\s*(?:ai|llm|gpt|claude)(?:Query|Sql|Response)/gi,
319
+ sinkType: 'sql_builder',
320
+ baseSeverity: 'critical',
321
+ description: 'AI-generated SQL query being executed.',
322
+ suggestedFix: 'Validate SQL structure. Use read-only database connections. Implement query allowlisting.',
323
+ },
324
+
325
+ // ========== Template/DOM Sinks ==========
326
+ {
327
+ name: 'LLM output to innerHTML',
328
+ pattern: /\.innerHTML\s*=\s*(?:response|result|output|completion|message|content)(?:\.|\.data\.|\.text|\.content)?/gi,
329
+ sinkType: 'template_render',
330
+ baseSeverity: 'high',
331
+ description: 'LLM output assigned to innerHTML. If the model outputs malicious HTML/JS, it will execute (XSS).',
332
+ suggestedFix: 'Use textContent for plain text. Sanitize HTML with DOMPurify before rendering. Use React/Vue which auto-escape by default.',
333
+ },
334
+ {
335
+ name: 'LLM output to dangerouslySetInnerHTML',
336
+ pattern: /dangerouslySetInnerHTML\s*=\s*\{\s*\{\s*__html:\s*(?:response|result|output|completion|message|content)/gi,
337
+ sinkType: 'template_render',
338
+ baseSeverity: 'high',
339
+ description: 'LLM output used in React dangerouslySetInnerHTML without sanitization.',
340
+ suggestedFix: 'Sanitize with DOMPurify: dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(content) }}',
341
+ },
342
+ {
343
+ name: 'LLM output to document.write',
344
+ pattern: /document\.write\s*\(\s*(?:response|result|output|completion|message|content)/gi,
345
+ sinkType: 'template_render',
346
+ baseSeverity: 'high',
347
+ description: 'LLM output passed to document.write, allowing script injection.',
348
+ suggestedFix: 'Use DOM manipulation methods with proper escaping. Never use document.write with dynamic content.',
349
+ },
350
+
351
+ // ========== M5: File System Sinks ==========
352
+ {
353
+ name: 'LLM output in file path',
354
+ pattern: /(?:readFile|writeFile|readFileSync|writeFileSync|unlink|unlinkSync|mkdir|mkdirSync|rmdir|rmSync)\s*\(\s*(?:response|result|output|completion|message|content|path)(?:\.|\.data\.|\.path)?/gi,
355
+ sinkType: 'code_execution', // Path traversal is code-level risk
356
+ baseSeverity: 'critical',
357
+ description: 'LLM-generated value used as file path. Path traversal attack possible - model could access or modify arbitrary files.',
358
+ suggestedFix: 'Validate AI output against allowed paths: if (!allowedPaths.some(p => path.resolve(output).startsWith(p))) throw. Use path.resolve() and check the result is within allowed directory.',
359
+ },
360
+ {
361
+ name: 'LLM output in fs operation',
362
+ pattern: /fs\.(?:read|write|append|unlink|mkdir|rm|stat|access)\w*\s*\(\s*(?:response|result|output|completion|aiPath|generatedPath)/gi,
363
+ sinkType: 'code_execution',
364
+ baseSeverity: 'critical',
365
+ description: 'AI-generated path passed to filesystem operation. Model could traverse to sensitive directories.',
366
+ suggestedFix: 'Create allowlist of permitted paths/directories. Use path.resolve() and validate result is within allowed boundaries.',
367
+ },
368
+ {
369
+ name: 'LLM output in path.join',
370
+ pattern: /path\.(?:join|resolve)\s*\([^)]*(?:response|result|output|completion|content|aiPath)[^)]*\).*(?:fs\.|readFile|writeFile)/gi,
371
+ sinkType: 'code_execution',
372
+ baseSeverity: 'high',
373
+ description: 'AI output used in path construction before file operation. Validate the final path.',
374
+ suggestedFix: 'After path.join/resolve, check result is within allowed directory: const resolved = path.resolve(base, aiPath); if (!resolved.startsWith(allowedRoot)) throw',
375
+ },
376
+
377
+ // ========== M5: Dynamic Import Sinks ==========
378
+ {
379
+ name: 'LLM output in dynamic import',
380
+ pattern: /import\s*\(\s*(?:response|result|output|completion|message|content|moduleName|aiModule)/gi,
381
+ sinkType: 'code_execution',
382
+ baseSeverity: 'critical',
383
+ description: 'AI-generated value used in dynamic import(). Arbitrary module loading enables code execution.',
384
+ suggestedFix: 'Use allowlist for permitted modules: const allowed = ["lodash", "moment"]; if (!allowed.includes(moduleName)) throw. Never dynamically import AI-generated module paths.',
385
+ },
386
+ {
387
+ name: 'LLM output in require()',
388
+ pattern: /require\s*\(\s*(?:response|result|output|completion|message|content|moduleName|aiModule)/gi,
389
+ sinkType: 'code_execution',
390
+ baseSeverity: 'critical',
391
+ description: 'AI-generated value used in require(). Can load arbitrary modules including native code.',
392
+ suggestedFix: 'Use allowlist for permitted modules. Consider using import maps or module aliases instead of dynamic require.',
393
+ },
394
+ {
395
+ name: 'LLM output in module resolution',
396
+ pattern: /(?:require\.resolve|import\.meta\.resolve)\s*\(\s*(?:response|result|output|completion|moduleName)/gi,
397
+ sinkType: 'code_execution',
398
+ baseSeverity: 'high',
399
+ description: 'AI output used in module path resolution. Could leak information about file system or enable module confusion attacks.',
400
+ suggestedFix: 'Validate module name against allowlist before resolution.',
401
+ },
402
+ ]
403
+
404
+ // ============================================================================
405
+ // Main Detection Function
406
+ // ============================================================================
407
+
408
+ /**
409
+ * Get surrounding context for analysis
410
+ */
411
+ function getSurroundingContext(content: string, lineIndex: number, windowSize: number = 15): string {
412
+ const lines = content.split('\n')
413
+ const start = Math.max(0, lineIndex - windowSize)
414
+ const end = Math.min(lines.length, lineIndex + windowSize)
415
+ return lines.slice(start, end).join('\n')
416
+ }
417
+
418
+ /**
419
+ * Calculate severity based on sandbox and validation status
420
+ */
421
+ function calculateSeverity(
422
+ baseSeverity: VulnerabilitySeverity,
423
+ sinkType: SinkType,
424
+ isSandboxed: boolean,
425
+ hasValidation: boolean,
426
+ isTestFile: boolean,
427
+ isExample: boolean = false,
428
+ isLibrary: boolean = false
429
+ ): VulnerabilitySeverity {
430
+ let severity = baseSeverity
431
+
432
+ // Test files get significant downgrade
433
+ if (isTestFile) {
434
+ return 'info'
435
+ }
436
+
437
+ // Example/demo code - not production, for tutorials
438
+ if (isExample) {
439
+ return 'info'
440
+ }
441
+
442
+ // Library code - base utilities, consumers add restrictions
443
+ if (isLibrary) {
444
+ return 'info'
445
+ }
446
+
447
+ // Sandboxing provides major protection for code execution
448
+ if (isSandboxed) {
449
+ if (sinkType === 'code_execution') {
450
+ severity = hasValidation ? 'low' : 'medium'
451
+ } else {
452
+ // Sandboxing less relevant for SQL/shell
453
+ severity = hasValidation ? 'medium' : 'high'
454
+ }
455
+ } else if (hasValidation) {
456
+ // Validation alone helps but doesn't eliminate risk
457
+ if (baseSeverity === 'critical') {
458
+ severity = 'high'
459
+ } else if (baseSeverity === 'high') {
460
+ severity = 'medium'
461
+ }
462
+ }
463
+
464
+ return severity
465
+ }
466
+
467
+ /**
468
+ * Main detection function for LLM output execution sinks
469
+ */
470
+ export function detectAIExecutionSinks(
471
+ content: string,
472
+ filePath: string
473
+ ): Vulnerability[] {
474
+ const vulnerabilities: Vulnerability[] = []
475
+
476
+ // Skip non-applicable files
477
+ if (isScannerOrFixtureFile(filePath)) return vulnerabilities
478
+
479
+ // Only deeply scan files that appear to be in LLM context
480
+ // But still do basic scanning on all files for obvious patterns
481
+ const isLLMFile = isLLMContextFile(filePath, content)
482
+ const lines = content.split('\n')
483
+ const isTestFile = isTestOrMockFile(filePath)
484
+ const isExample = isExampleDirectory(filePath)
485
+ const isLibrary = isLibraryCode(filePath)
486
+
487
+ for (const pattern of EXECUTION_SINK_PATTERNS) {
488
+ const regex = new RegExp(pattern.pattern.source, pattern.pattern.flags)
489
+ let match
490
+
491
+ while ((match = regex.exec(content)) !== null) {
492
+ const lineNumber = content.substring(0, match.index).split('\n').length
493
+ const lineContent = lines[lineNumber - 1]?.trim() || ''
494
+
495
+ // Skip comments
496
+ if (isComment(lineContent)) continue
497
+
498
+ const surroundingContext = getSurroundingContext(content, lineNumber - 1)
499
+
500
+ // Check if this is actually in an LLM context
501
+ const hasLLMContext = isLLMFile || hasLLMResponseContext(lineContent, surroundingContext)
502
+
503
+ // ===== FALSE POSITIVE FILTERS =====
504
+
505
+ // Skip UI suggestion/template patterns (command palettes, autocomplete, etc.)
506
+ // These are display strings, not execution sinks
507
+ if (isUITemplateSuggestion(lineContent, surroundingContext)) {
508
+ continue
509
+ }
510
+
511
+ // Skip app data interpolation (e.g., ${node.title}, ${item.id})
512
+ // where the interpolated data is from the app, not LLM output
513
+ if (isAppDataInterpolation(lineContent, surroundingContext)) {
514
+ continue
515
+ }
516
+
517
+ // For non-LLM files, require stronger signal
518
+ if (!hasLLMContext) {
519
+ // Check if the matched variable looks like LLM output
520
+ const matchText = match[0]
521
+ const variableMatch = matchText.match(/(?:response|result|output|completion|message|content|answer|generated|text)/i)
522
+ if (!variableMatch) continue
523
+
524
+ // Skip if this looks like display-only usage
525
+ if (isDisplayOnly(lineContent, surroundingContext)) continue
526
+ }
527
+
528
+ // Check for sandboxing and validation
529
+ const isSandboxed = isSandboxedExecution(content, lineNumber)
530
+ const hasValidation = hasOutputValidation(content, lineNumber)
531
+
532
+ // Calculate final severity
533
+ const severity = calculateSeverity(
534
+ pattern.baseSeverity,
535
+ pattern.sinkType,
536
+ isSandboxed,
537
+ hasValidation,
538
+ isTestFile,
539
+ isExample,
540
+ isLibrary
541
+ )
542
+
543
+ // Build description with context
544
+ let description = pattern.description
545
+ if (isSandboxed) {
546
+ description += ' (Sandbox detected - risk somewhat mitigated.)'
547
+ }
548
+ if (hasValidation) {
549
+ description += ' (Some validation detected nearby.)'
550
+ }
551
+ if (isTestFile) {
552
+ description += ' (In test file.)'
553
+ } else if (isExample) {
554
+ description += ' (In example/demo directory - tutorial code.)'
555
+ } else if (isLibrary) {
556
+ description += ' (Library code - consumers add restrictions.)'
557
+ }
558
+
559
+ // Skip info-level in non-LLM files to reduce noise
560
+ if (severity === 'info' && !isLLMFile) continue
561
+
562
+ vulnerabilities.push({
563
+ id: `ai-exec-${filePath}-${lineNumber}-${pattern.sinkType}`,
564
+ filePath,
565
+ lineNumber,
566
+ lineContent,
567
+ severity,
568
+ category: 'ai_unsafe_execution',
569
+ title: pattern.name,
570
+ description,
571
+ suggestedFix: pattern.suggestedFix,
572
+ confidence: hasLLMContext ? 'high' : 'medium',
573
+ layer: 2,
574
+ requiresAIValidation: severity !== 'info' && severity !== 'low',
575
+ })
576
+ }
577
+ }
578
+
579
+ return vulnerabilities
580
+ }