hullproof 0.0.0-stage → 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 (363) hide show
  1. package/LICENSE +222 -0
  2. package/NOTICE +32 -0
  3. package/README.md +313 -2
  4. package/checks/db.firebase-rules-not-open.json +24 -0
  5. package/checks/db.rls-every-table.json +25 -0
  6. package/checks/log.no-credential-in-logs.json +30 -0
  7. package/checks/secrets.no-env-file-committed.json +31 -0
  8. package/checks/secrets.no-secret-in-public-env.json +37 -0
  9. package/checks/storage.firebase-rules-not-open.json +22 -0
  10. package/checks/supply.no-untrusted-event-in-run.json +23 -0
  11. package/checks/web.cors-no-reflected-origin.json +31 -0
  12. package/detectors/ai.json +25 -0
  13. package/detectors/auth.json +27 -0
  14. package/detectors/ci.json +19 -0
  15. package/detectors/containers.json +8 -0
  16. package/detectors/data_stores.json +38 -0
  17. package/detectors/frameworks.json +94 -0
  18. package/detectors/hosting.json +44 -0
  19. package/detectors/iac.json +43 -0
  20. package/detectors/languages.json +48 -0
  21. package/detectors/mobile.json +22 -0
  22. package/detectors/package_managers.json +39 -0
  23. package/detectors/payments.json +19 -0
  24. package/detectors/runtimes.json +17 -0
  25. package/dist/checks/code/admin-role.js +46 -0
  26. package/dist/checks/code/owner-filter.js +91 -0
  27. package/dist/checks/code/shared.js +84 -0
  28. package/dist/checks/code/webhook-verify.js +77 -0
  29. package/dist/checks/context.js +64 -0
  30. package/dist/checks/map/config-checks.js +74 -0
  31. package/dist/checks/map/entry-checks.js +97 -0
  32. package/dist/checks/map/shared.js +44 -0
  33. package/dist/checks/ops.js +307 -0
  34. package/dist/checks/pro-map.js +1 -0
  35. package/dist/checks/run.js +92 -0
  36. package/dist/checks/types.js +1 -0
  37. package/dist/ci/comment.js +44 -0
  38. package/dist/ci/sarif-check.js +157 -0
  39. package/dist/ci/sarif.js +61 -0
  40. package/dist/cli.js +358 -0
  41. package/dist/commands/ci.js +115 -0
  42. package/dist/commands/git.js +58 -0
  43. package/dist/commands/hooks.js +30 -0
  44. package/dist/commands/hunt-pro.js +1 -0
  45. package/dist/commands/hunt.js +86 -0
  46. package/dist/commands/map.js +33 -0
  47. package/dist/commands/mcp.js +17 -0
  48. package/dist/commands/pack.js +48 -0
  49. package/dist/commands/prove-pro.js +1 -0
  50. package/dist/commands/prove.js +173 -0
  51. package/dist/commands/report-gate.js +92 -0
  52. package/dist/commands/scan.js +158 -0
  53. package/dist/confirm/agreement.js +18 -0
  54. package/dist/confirm/chain.js +84 -0
  55. package/dist/confirm/confirm.js +112 -0
  56. package/dist/confirm/honesty.js +75 -0
  57. package/dist/confirm/lead.js +92 -0
  58. package/dist/confirm/merge.js +88 -0
  59. package/dist/confirm/query.js +145 -0
  60. package/dist/confirm/rule.js +86 -0
  61. package/dist/confirm/store.js +42 -0
  62. package/dist/confirm/summary.js +6 -0
  63. package/dist/confirm/synthesis.js +28 -0
  64. package/dist/discover/detectors.js +50 -0
  65. package/dist/discover/discover.js +270 -0
  66. package/dist/discover/extract.js +282 -0
  67. package/dist/discover/git.js +40 -0
  68. package/dist/discover/kit-files.js +47 -0
  69. package/dist/discover/walk.js +110 -0
  70. package/dist/env.js +57 -0
  71. package/dist/gate/gate.js +173 -0
  72. package/dist/hooks/install.js +153 -0
  73. package/dist/hooks/run.js +112 -0
  74. package/dist/map/analyze.js +201 -0
  75. package/dist/map/build.js +253 -0
  76. package/dist/map/candidates.js +49 -0
  77. package/dist/map/cblocks.js +77 -0
  78. package/dist/map/config.js +291 -0
  79. package/dist/map/dotnet.js +242 -0
  80. package/dist/map/entry.js +78 -0
  81. package/dist/map/expo.js +190 -0
  82. package/dist/map/go.js +292 -0
  83. package/dist/map/js-express.js +143 -0
  84. package/dist/map/js-misc.js +295 -0
  85. package/dist/map/js-next.js +292 -0
  86. package/dist/map/laravel.js +389 -0
  87. package/dist/map/lex-more.js +305 -0
  88. package/dist/map/lex.js +425 -0
  89. package/dist/map/light-index.js +191 -0
  90. package/dist/map/light-profiles.js +324 -0
  91. package/dist/map/light.js +574 -0
  92. package/dist/map/local-vocab.js +27 -0
  93. package/dist/map/mobile.js +326 -0
  94. package/dist/map/py-django.js +281 -0
  95. package/dist/map/py.js +391 -0
  96. package/dist/map/rails.js +391 -0
  97. package/dist/map/repo.js +178 -0
  98. package/dist/map/rust.js +312 -0
  99. package/dist/map/spring.js +207 -0
  100. package/dist/map/taint.js +96 -0
  101. package/dist/map/touches.js +355 -0
  102. package/dist/map/vocab.js +88 -0
  103. package/dist/mapping/controls.js +34 -0
  104. package/dist/mapping/dependency-policy.js +44 -0
  105. package/dist/mapping/map.js +44 -0
  106. package/dist/mapping/maps/gitleaks.json +12 -0
  107. package/dist/mapping/maps/osv-scanner.json +7 -0
  108. package/dist/mapping/maps/trivy.json +13 -0
  109. package/dist/mcp/confine.js +59 -0
  110. package/dist/mcp/protocol.js +140 -0
  111. package/dist/mcp/schema.js +43 -0
  112. package/dist/mcp/server.js +37 -0
  113. package/dist/mcp/tools.js +238 -0
  114. package/dist/packs/coverage.js +79 -0
  115. package/dist/packs/manifest.js +36 -0
  116. package/dist/packs/validate.js +271 -0
  117. package/dist/prove/container.js +276 -0
  118. package/dist/prove/exec-main.js +20 -0
  119. package/dist/prove/executor.js +149 -0
  120. package/dist/prove/local.js +131 -0
  121. package/dist/prove/probes.js +65 -0
  122. package/dist/prove/recipe.js +278 -0
  123. package/dist/prove/run.js +136 -0
  124. package/dist/prove/runner.js +1 -0
  125. package/dist/prove/safety.js +126 -0
  126. package/dist/prove/spec.js +357 -0
  127. package/dist/prove/store.js +65 -0
  128. package/dist/report/build.js +297 -0
  129. package/dist/report/entry-points.js +75 -0
  130. package/dist/report/hunt-section.js +71 -0
  131. package/dist/report/kit.js +41 -0
  132. package/dist/report/load.js +35 -0
  133. package/dist/report/messages.js +273 -0
  134. package/dist/report/prove-section.js +65 -0
  135. package/dist/report/report.js +156 -0
  136. package/dist/report/surface.js +21 -0
  137. package/dist/report/test-code.js +51 -0
  138. package/dist/report/tiers.js +44 -0
  139. package/dist/scanners/common.js +162 -0
  140. package/dist/scanners/gitleaks.js +90 -0
  141. package/dist/scanners/index.js +113 -0
  142. package/dist/scanners/osv-scanner.js +77 -0
  143. package/dist/scanners/run-targets.js +34 -0
  144. package/dist/scanners/secret-noise.js +89 -0
  145. package/dist/scanners/semgrep.js +142 -0
  146. package/dist/scanners/trivy.js +65 -0
  147. package/dist/scanners/types.js +1 -0
  148. package/dist/types/check.js +1 -0
  149. package/dist/types/codemap.js +1 -0
  150. package/dist/types/findings.js +5 -0
  151. package/dist/types/hunt.js +1 -0
  152. package/dist/types/index.js +1 -0
  153. package/dist/types/prove.js +1 -0
  154. package/dist/types/results.js +2 -0
  155. package/dist/types/scan.js +1 -0
  156. package/dist/types/stack.js +5 -0
  157. package/dist/util.js +71 -0
  158. package/dist/version.js +2 -0
  159. package/package.json +46 -4
  160. package/packs/android/fixture-result.json +43 -0
  161. package/packs/android/pack.json +56 -0
  162. package/packs/dotnet/fixture-result.json +62 -0
  163. package/packs/dotnet/pack.json +88 -0
  164. package/packs/expo/answer-key.json +51 -0
  165. package/packs/expo/fixture-result.json +62 -0
  166. package/packs/expo/pack.json +79 -0
  167. package/packs/firebase/answer-key.json +27 -0
  168. package/packs/firebase/fixture-result.json +43 -0
  169. package/packs/firebase/pack.json +58 -0
  170. package/packs/go/answer-key.json +101 -0
  171. package/packs/go/fixture-result.json +64 -0
  172. package/packs/go/pack.json +99 -0
  173. package/packs/ios/fixture-result.json +43 -0
  174. package/packs/ios/pack.json +55 -0
  175. package/packs/laravel/answer-key.json +84 -0
  176. package/packs/laravel/fixture-result.json +60 -0
  177. package/packs/laravel/pack.json +91 -0
  178. package/packs/node-ts/answer-key.json +220 -0
  179. package/packs/node-ts/fixture-result.json +114 -0
  180. package/packs/node-ts/pack.json +134 -0
  181. package/packs/python/answer-key.json +195 -0
  182. package/packs/python/fixture-result.json +119 -0
  183. package/packs/python/pack.json +121 -0
  184. package/packs/rails/answer-key.json +84 -0
  185. package/packs/rails/fixture-result.json +60 -0
  186. package/packs/rails/pack.json +78 -0
  187. package/packs/rust/fixture-result.json +43 -0
  188. package/packs/rust/pack.json +69 -0
  189. package/packs/spring/fixture-result.json +63 -0
  190. package/packs/spring/pack.json +103 -0
  191. package/packs/supabase/answer-key.json +52 -0
  192. package/packs/supabase/fixture-result.json +58 -0
  193. package/packs/supabase/pack.json +60 -0
  194. package/rules/gitleaks.toml +256 -0
  195. package/rules/semgrep/api-route-no-auth.yaml +31 -0
  196. package/rules/semgrep/cors-reflect-origin.yaml +48 -0
  197. package/rules/semgrep/eval-or-new-function.yaml +185 -0
  198. package/rules/semgrep/log-session-token.yaml +48 -0
  199. package/rules/semgrep/math-random-tokens.yaml +24 -0
  200. package/rules/semgrep/next-public-secret-name.yaml +32 -0
  201. package/rules/semgrep/postgrest-filter-injection.yaml +78 -0
  202. package/rules/semgrep/raw-sql-concat.yaml +42 -0
  203. package/rules/semgrep/read-by-id-no-owner.yaml +49 -0
  204. package/rules/semgrep/server-action-no-auth.yaml +26 -0
  205. package/rules/semgrep/sql-variants.yaml +99 -0
  206. package/rules/semgrep/ssrf-unguarded-url.yaml +137 -0
  207. package/rules/semgrep/subprocess-injection.yaml +163 -0
  208. package/rules/semgrep/taint-go.yaml +333 -0
  209. package/rules/semgrep/taint-java.yaml +317 -0
  210. package/rules/semgrep/taint-javascript.yaml +412 -0
  211. package/rules/semgrep/taint-php.yaml +361 -0
  212. package/rules/semgrep/taint-python.yaml +414 -0
  213. package/rules/semgrep/tool-endpoint-admin-client.yaml +86 -0
  214. package/rules/semgrep/user-metadata-authz.yaml +22 -0
  215. package/rules/semgrep/verify-result-unused.yaml +62 -0
  216. package/rules/semgrep/webhook-parse-before-verify.yaml +29 -0
  217. package/rules/test-artifact-ids.json +4 -0
  218. package/rules/tiers.json +427 -0
  219. package/schemas/check-results.schema.json +55 -0
  220. package/schemas/check.schema.json +41 -0
  221. package/schemas/codemap.schema.json +216 -0
  222. package/schemas/findings.schema.json +1048 -0
  223. package/schemas/hunt-output.schema.json +46 -0
  224. package/schemas/hunt-result.schema.json +58 -0
  225. package/schemas/results.schema.json +17 -0
  226. package/schemas/scan-hits.schema.json +32 -0
  227. package/schemas/scan-meta.schema.json +25 -0
  228. package/schemas/stack.schema.json +93 -0
  229. package/src/checks/README.md +32 -0
  230. package/src/checks/code/admin-role.ts +49 -0
  231. package/src/checks/code/owner-filter.ts +95 -0
  232. package/src/checks/code/shared.ts +66 -0
  233. package/src/checks/code/webhook-verify.ts +75 -0
  234. package/src/checks/context.ts +72 -0
  235. package/src/checks/map/config-checks.ts +79 -0
  236. package/src/checks/map/entry-checks.ts +116 -0
  237. package/src/checks/map/shared.ts +63 -0
  238. package/src/checks/ops.ts +280 -0
  239. package/src/checks/pro-map.ts +4 -0
  240. package/src/checks/run.ts +80 -0
  241. package/src/checks/types.ts +12 -0
  242. package/src/ci/comment.ts +52 -0
  243. package/src/ci/sarif-check.ts +100 -0
  244. package/src/ci/sarif.ts +72 -0
  245. package/src/cli.ts +273 -0
  246. package/src/commands/ci.ts +104 -0
  247. package/src/commands/git.ts +59 -0
  248. package/src/commands/hooks.ts +27 -0
  249. package/src/commands/hunt-pro.ts +4 -0
  250. package/src/commands/hunt.ts +68 -0
  251. package/src/commands/map.ts +27 -0
  252. package/src/commands/mcp.ts +18 -0
  253. package/src/commands/pack.ts +34 -0
  254. package/src/commands/prove-pro.ts +4 -0
  255. package/src/commands/prove.ts +126 -0
  256. package/src/commands/report-gate.ts +75 -0
  257. package/src/commands/scan.ts +156 -0
  258. package/src/confirm/agreement.ts +23 -0
  259. package/src/confirm/chain.ts +67 -0
  260. package/src/confirm/confirm.ts +107 -0
  261. package/src/confirm/honesty.ts +65 -0
  262. package/src/confirm/lead.ts +76 -0
  263. package/src/confirm/merge.ts +89 -0
  264. package/src/confirm/query.ts +126 -0
  265. package/src/confirm/rule.ts +76 -0
  266. package/src/confirm/store.ts +42 -0
  267. package/src/confirm/summary.ts +9 -0
  268. package/src/confirm/synthesis.ts +33 -0
  269. package/src/discover/detectors.ts +71 -0
  270. package/src/discover/discover.ts +253 -0
  271. package/src/discover/extract.ts +235 -0
  272. package/src/discover/git.ts +31 -0
  273. package/src/discover/kit-files.ts +37 -0
  274. package/src/discover/walk.ts +88 -0
  275. package/src/env.ts +51 -0
  276. package/src/gate/gate.ts +169 -0
  277. package/src/hooks/install.ts +142 -0
  278. package/src/hooks/run.ts +121 -0
  279. package/src/map/analyze.ts +219 -0
  280. package/src/map/build.ts +206 -0
  281. package/src/map/candidates.ts +47 -0
  282. package/src/map/cblocks.ts +55 -0
  283. package/src/map/config.ts +207 -0
  284. package/src/map/dotnet.ts +205 -0
  285. package/src/map/entry.ts +80 -0
  286. package/src/map/expo.ts +171 -0
  287. package/src/map/go.ts +271 -0
  288. package/src/map/js-express.ts +135 -0
  289. package/src/map/js-misc.ts +221 -0
  290. package/src/map/js-next.ts +282 -0
  291. package/src/map/laravel.ts +327 -0
  292. package/src/map/lex-more.ts +152 -0
  293. package/src/map/lex.ts +274 -0
  294. package/src/map/light-index.ts +134 -0
  295. package/src/map/light-profiles.ts +363 -0
  296. package/src/map/light.ts +484 -0
  297. package/src/map/local-vocab.ts +30 -0
  298. package/src/map/mobile.ts +263 -0
  299. package/src/map/py-django.ts +239 -0
  300. package/src/map/py.ts +329 -0
  301. package/src/map/rails.ts +291 -0
  302. package/src/map/repo.ts +150 -0
  303. package/src/map/rust.ts +256 -0
  304. package/src/map/spring.ts +185 -0
  305. package/src/map/taint.ts +86 -0
  306. package/src/map/touches.ts +296 -0
  307. package/src/map/vocab.ts +106 -0
  308. package/src/mapping/controls.ts +47 -0
  309. package/src/mapping/dependency-policy.ts +55 -0
  310. package/src/mapping/map.ts +59 -0
  311. package/src/mapping/maps/gitleaks.json +12 -0
  312. package/src/mapping/maps/osv-scanner.json +7 -0
  313. package/src/mapping/maps/trivy.json +13 -0
  314. package/src/mcp/confine.ts +44 -0
  315. package/src/mcp/protocol.ts +144 -0
  316. package/src/mcp/schema.ts +35 -0
  317. package/src/mcp/server.ts +32 -0
  318. package/src/mcp/tools.ts +235 -0
  319. package/src/packs/coverage.ts +98 -0
  320. package/src/packs/manifest.ts +97 -0
  321. package/src/packs/validate.ts +204 -0
  322. package/src/prove/container.ts +237 -0
  323. package/src/prove/exec-main.ts +16 -0
  324. package/src/prove/executor.ts +130 -0
  325. package/src/prove/local.ts +100 -0
  326. package/src/prove/probes.ts +68 -0
  327. package/src/prove/recipe.ts +215 -0
  328. package/src/prove/run.ts +124 -0
  329. package/src/prove/runner.ts +28 -0
  330. package/src/prove/safety.ts +98 -0
  331. package/src/prove/spec.ts +256 -0
  332. package/src/prove/store.ts +48 -0
  333. package/src/report/build.ts +304 -0
  334. package/src/report/entry-points.ts +78 -0
  335. package/src/report/hunt-section.ts +67 -0
  336. package/src/report/kit.ts +59 -0
  337. package/src/report/load.ts +49 -0
  338. package/src/report/messages.ts +270 -0
  339. package/src/report/prove-section.ts +61 -0
  340. package/src/report/report.ts +160 -0
  341. package/src/report/surface.ts +32 -0
  342. package/src/report/test-code.ts +56 -0
  343. package/src/report/tiers.ts +50 -0
  344. package/src/scanners/common.ts +149 -0
  345. package/src/scanners/gitleaks.ts +89 -0
  346. package/src/scanners/index.ts +103 -0
  347. package/src/scanners/osv-scanner.ts +90 -0
  348. package/src/scanners/run-targets.ts +35 -0
  349. package/src/scanners/secret-noise.ts +80 -0
  350. package/src/scanners/semgrep.ts +139 -0
  351. package/src/scanners/trivy.ts +66 -0
  352. package/src/scanners/types.ts +98 -0
  353. package/src/types/check.ts +52 -0
  354. package/src/types/codemap.ts +142 -0
  355. package/src/types/findings.ts +107 -0
  356. package/src/types/hunt.ts +97 -0
  357. package/src/types/index.ts +10 -0
  358. package/src/types/prove.ts +83 -0
  359. package/src/types/results.ts +15 -0
  360. package/src/types/scan.ts +63 -0
  361. package/src/types/stack.ts +61 -0
  362. package/src/util.ts +59 -0
  363. package/src/version.ts +2 -0
package/src/map/go.ts ADDED
@@ -0,0 +1,271 @@
1
+ // Go entry points: net/http (ServeMux, including the Go 1.22 "METHOD /path" patterns), chi, gin, echo, fiber and gorilla/mux.
2
+ // Router variables are followed in order inside a function (Group, Route, With, Use, PathPrefix), routers handed in as
3
+ // parameters are read at the prefix the file can see, and a Mount or a router returned by a function is followed to its
4
+ // definition. Pattern based, like the other extractors; every fact carries a confidence.
5
+ import type { EntryPoint, Scope } from '../types/index.ts';
6
+ import type { FileView } from './analyze.ts';
7
+ import { empty, joinPath, makeEntry, type Extracted } from './entry.ts';
8
+ import { goFunc, type FnIndex } from './light-index.ts';
9
+ import { analyzeLight, type LightDeco } from './light.ts';
10
+ import { closeOf, splitArgs } from './lex.ts';
11
+ import type { LocalVocab } from './local-vocab.ts';
12
+ import type { Repo } from './repo.ts';
13
+
14
+ interface Mw { view: FileView; start: number; end: number; scope: Scope; only: string | null }
15
+ interface Env { prefix: string; mws: Mw[]; fw: string; param: boolean }
16
+ interface FnRef { file: string; view: FileView; start: number; end: number; bodyStart: number; params: string; name: string }
17
+ interface MountCall { prefix: string; mws: Mw[]; fw: string; targets: string[]; from: string }
18
+
19
+ const ROUTER_TYPE = /\*?(?:gin\.(?:Engine|RouterGroup|IRouter|IRoutes)|chi\.(?:Router|Mux)|echo\.(?:Echo|Group)|fiber\.(?:App|Router)|mux\.Router|http\.ServeMux)\b/;
20
+ const FW_BY_IMPORT: [RegExp, string][] = [[/gin-gonic\/gin/, 'gin'], [/go-chi\/chi/, 'chi'], [/labstack\/echo/, 'echo'], [/gofiber\/fiber/, 'fiber'], [/gorilla\/mux/, 'gorilla-mux']];
21
+ const METHODS = new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE']);
22
+ const CREATE = /(?<![\w.])([A-Za-z_]\w*)\s*(?::=|=)\s*(?:chi\.NewRouter|chi\.NewMux|gin\.Default|gin\.New|echo\.New|fiber\.New|mux\.NewRouter|http\.NewServeMux|NewRouter|NewServeMux)\s*\(/g;
23
+ const ROUTE_CALLS = new Set(['Get', 'Post', 'Put', 'Patch', 'Delete', 'Head', 'Options', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS', 'Any', 'All', 'Handle', 'HandleFunc', 'Method', 'MethodFunc', 'Match', 'Add', 'Connect', 'Trace', 'Mount', 'Use', 'Group', 'Route', 'With', 'PathPrefix', 'Subrouter', 'Static']);
24
+
25
+ const unq = (s: string): string | null => /^\s*(?:"((?:\\.|[^"\\])*)"|`([^`]*)`)\s*$/.exec(s)?.slice(1).find((x) => x !== undefined) ?? null;
26
+
27
+ export function goFiles(repo: Repo): string[] {
28
+ return repo.filesOf('go').filter((f) => { const v = repo.view(f); return !!v && /net\/http|gin-gonic|go-chi|labstack\/echo|gofiber|gorilla\/mux/.test(v.imports); });
29
+ }
30
+
31
+ export function extractGoAll(repo: Repo, fn: FnIndex, local: LocalVocab | null): Extracted {
32
+ const out = empty();
33
+ const keyOf = (f: FnRef): string => `${f.file}#${f.start}`;
34
+ const funcs: FnRef[] = [];
35
+ for (const file of goFiles(repo)) {
36
+ const v = repo.view(file)!;
37
+ for (const x of v.masked.matchAll(/^func\s+(?:\(\s*\w*\s*\*?\s*[\w.[\]]+\s*\)\s*)?(\w+)\s*[([]/gm)) {
38
+ const f = goFunc(v.masked, x.index!);
39
+ if (f) funcs.push({ file, view: v, start: f.start, end: f.end, bodyStart: f.bodyStart, params: f.params, name: x[1] });
40
+ }
41
+ // Package level routing, outside any function: var mux = http.NewServeMux(); func init() is a function and is covered above.
42
+ }
43
+ const scanned = new Map<string, { entries: EntryPoint[]; mounts: MountCall[] }>();
44
+ const mounted = new Map<string, Env>();
45
+ const scan = (f: FnRef, base: Env | null): { entries: EntryPoint[]; mounts: MountCall[] } => {
46
+ const r = scanFunction(repo, fn, local, f, base);
47
+ return r;
48
+ };
49
+ for (const f of funcs) scanned.set(keyOf(f), scan(f, null));
50
+ // Mounted routers: scan the function that builds them again at the mount prefix, and drop its standalone result.
51
+ const queue: MountCall[] = [...scanned.values()].flatMap((s) => s.mounts);
52
+ for (let depth = 0; depth < 4 && queue.length; depth++) {
53
+ const next: MountCall[] = [];
54
+ for (const m of queue.splice(0)) {
55
+ for (const name of m.targets) {
56
+ const t = fn.defs(name).filter((d) => d.view.lang === 'go' && funcs.some((f) => f.file === d.view.file && f.start === d.start))[0];
57
+ if (!t) continue;
58
+ const ref = funcs.find((f) => f.file === t.view.file && f.start === t.start)!;
59
+ const key = keyOf(ref);
60
+ if (mounted.has(key)) continue;
61
+ const env: Env = { prefix: m.prefix, mws: m.mws, fw: m.fw, param: true };
62
+ mounted.set(key, env);
63
+ const res = scan(ref, env);
64
+ scanned.set(key, res);
65
+ next.push(...res.mounts);
66
+ }
67
+ }
68
+ queue.push(...next);
69
+ }
70
+ for (const [, s] of [...scanned.entries()].sort(([a], [b]) => (a < b ? -1 : 1))) out.entries.push(...s.entries);
71
+ return out;
72
+ }
73
+
74
+ function scanFunction(repo: Repo, fn: FnIndex, local: LocalVocab | null, f: FnRef, base: Env | null): { entries: EntryPoint[]; mounts: MountCall[] } {
75
+ const v = f.view;
76
+ const fw0 = FW_BY_IMPORT.find(([re]) => re.test(v.imports))?.[1] ?? 'net-http';
77
+ const entries: EntryPoint[] = [];
78
+ const mounts: MountCall[] = [];
79
+ const env = new Map<string, Env>();
80
+ let baseUsed = false;
81
+ const fresh = (): Env => { if (base && !baseUsed) { baseUsed = true; return { ...base, mws: base.mws.slice() }; } return { prefix: '', mws: [], fw: fw0, param: false }; };
82
+ // Routers handed in as parameters.
83
+ for (const part of f.params.split(',')) {
84
+ const m = /^\s*(\w+)\s+(.+)$/.exec(part);
85
+ if (m && ROUTER_TYPE.test(m[2])) env.set(m[1], base ? { ...base, mws: base.mws.slice() } : { prefix: '', mws: [], fw: fw0, param: true });
86
+ }
87
+ if (base && env.size) baseUsed = true;
88
+ if (/"net\/http"/.test(v.imports)) env.set('http', { prefix: '', mws: [], fw: 'net-http', param: false });
89
+
90
+ const text = v.text;
91
+ const masked = v.masked;
92
+ const mwOf = (a: { start: number; end: number }, scope: Scope, only: string | null = null): Mw => ({ view: v, start: a.start, end: a.end, scope, only });
93
+
94
+ const route = (e: Env, recv: string, method: string, open: number, chained: Mw[], at: number, forced: string | null): void => {
95
+ const args = splitArgs(masked, open);
96
+ if (!args.length) return;
97
+ let verb: string | null = forced;
98
+ let pathIdx = 0;
99
+ if (method === 'Handle' && args.length >= 3 && unq(text.slice(args[0].start, args[0].end)) && METHODS.has(unq(text.slice(args[0].start, args[0].end))!)) { verb = unq(text.slice(args[0].start, args[0].end))!; pathIdx = 1; }
100
+ if (method === 'Add' && args.length >= 3) { verb = unq(text.slice(args[0].start, args[0].end)); pathIdx = 1; }
101
+ if ((method === 'Method' || method === 'MethodFunc') && args.length >= 3) { verb = unq(text.slice(args[0].start, args[0].end))?.toUpperCase() ?? null; pathIdx = 1; }
102
+ if (method === 'Match' && args.length >= 3) { verb = 'ANY'; pathIdx = 1; }
103
+ if (!verb) verb = ROUTE_CALLS.has(method) && METHODS.has(method.toUpperCase()) ? method.toUpperCase() : 'ANY';
104
+ let path = unq(text.slice(args[pathIdx].start, args[pathIdx].end));
105
+ if (path === null) return;
106
+ // Go 1.22 patterns: "GET /notes/{id}", and gorilla/chi style braces.
107
+ const pm = /^([A-Z]+)\s+(\S.*)$/.exec(path);
108
+ if (pm && METHODS.has(pm[1])) { verb = pm[1]; path = pm[2]; }
109
+ path = path.replace(/\{(\w+)(?::[^}]*)?\}/g, ':$1').replace(/:(\w+)(?=\/|$)/g, ':$1').replace(/\*(\w*)/g, (_m, n) => `*${n || 'rest'}`).replace(/\{(\w+)\.\.\.\}/g, '*$1');
110
+ const rest = args.slice(pathIdx + 1);
111
+ if (!rest.length) return;
112
+ const echo = e.fw === 'echo';
113
+ const handlerArg = echo ? rest[0] : rest[rest.length - 1];
114
+ const perRoute = (echo ? rest.slice(1) : rest.slice(0, -1)).map((a) => mwOf(a, 'wrapper'));
115
+ const full = joinPath(e.prefix, path);
116
+ const handlerText = text.slice(handlerArg.start, handlerArg.end);
117
+ if (/\bFileServer\b|\bStripPrefix\b|\bStatic\b/.test(handlerText) && !/func\s*\(/.test(handlerText)) return;
118
+ const resolved = resolveHandler(repo, fn, v, handlerArg);
119
+ const decorators: LightDeco[] = [
120
+ ...e.mws.filter((m) => m.only === null || full.startsWith(m.only)).map((m) => ({ view: m.view, start: m.start, end: m.end, scope: m.scope })),
121
+ ...chained.map((m) => ({ view: m.view, start: m.start, end: m.end, scope: m.scope })),
122
+ ...perRoute.map((m) => ({ view: m.view, start: m.start, end: m.end, scope: m.scope })),
123
+ ...resolved.wrappers.map((w) => ({ view: v, start: w.start, end: w.end, scope: 'wrapper' as Scope })),
124
+ ];
125
+ const hv = resolved.view;
126
+ const a = analyzeLight(hv, { start: resolved.start, end: resolved.end, seeds: [], decorators, lookup: fn.find, local });
127
+ const line = v.lines.at(at);
128
+ entries.push(makeEntry(repo, {
129
+ kind: 'http_route', framework: e.fw, file: v.file, line, method: verb, path: full, name: resolved.name,
130
+ confidence: e.param || !resolved.found ? 'medium' : 'high', handlerView: hv, spec: { start: resolved.start, end: resolved.end, seeds: [] }, analysis: a,
131
+ }));
132
+ };
133
+
134
+ // Events in order: router creation, groups, middleware, routes, mounts.
135
+ type Ev = { at: number; kind: 'create' | 'call'; m: RegExpExecArray };
136
+ const evs: Ev[] = [];
137
+ const body = [f.bodyStart, f.end] as const;
138
+ CREATE.lastIndex = 0;
139
+ for (const m of masked.slice(body[0], body[1]).matchAll(CREATE)) evs.push({ at: body[0] + m.index!, kind: 'create', m: m as RegExpExecArray });
140
+ const callRe = /(?<![\w.])([A-Za-z_]\w*)\s*\.\s*([A-Za-z_]\w*)\s*\(/g;
141
+ for (const m of masked.slice(body[0], body[1]).matchAll(callRe)) evs.push({ at: body[0] + m.index!, kind: 'call', m: m as RegExpExecArray });
142
+ evs.sort((a, b) => a.at - b.at || (a.kind === 'create' ? -1 : 1));
143
+ let skipTo = -1;
144
+ const handleCall = (recv: string, method: string, at: number, open: number, e: Env, assignTo: string | null, within: Map<string, Env>): number => {
145
+ const args = splitArgs(masked, open);
146
+ const closeAt = closeOf(masked, open);
147
+ if (method === 'Use') {
148
+ let only: string | null = null;
149
+ let list = args;
150
+ if (e.fw === 'fiber' && args.length > 1 && unq(text.slice(args[0].start, args[0].end)) !== null) { only = unq(text.slice(args[0].start, args[0].end)); list = args.slice(1); }
151
+ for (const a of list) e.mws.push(mwOf(a, 'router', only === null ? null : joinPath(e.prefix, only)));
152
+ return closeAt;
153
+ }
154
+ if ((method === 'Group' || method === 'Route') && args.length) {
155
+ // chi: r.Route("/p", func(r chi.Router) { ... }) and r.Group(func(r chi.Router) { ... })
156
+ const lit = args.find((a) => /^func\s*\(/.test(masked.slice(a.start, a.end)));
157
+ if (lit && /\bchi\b|Router/.test(masked.slice(lit.start, lit.start + 80))) {
158
+ const g = goFunc(masked, lit.start);
159
+ const pm = /^func\s*\(\s*(\w+)/.exec(masked.slice(lit.start));
160
+ if (g && pm) {
161
+ const p = method === 'Route' ? unq(text.slice(args[0].start, args[0].end)) ?? '' : '';
162
+ const child: Env = { prefix: joinPath(e.prefix, p || '/'), mws: e.mws.slice(), fw: e.fw, param: e.param };
163
+ const inner = new Map(within);
164
+ inner.set(pm[1], child);
165
+ walkRange(g.bodyStart + 1, g.end - 1, inner);
166
+ return g.end;
167
+ }
168
+ }
169
+ if (assignTo) {
170
+ const p = unq(text.slice(args[0].start, args[0].end)) ?? '';
171
+ const child: Env = { prefix: joinPath(e.prefix, p || '/'), mws: [...e.mws, ...args.slice(1).map((a) => mwOf(a, 'router'))], fw: e.fw, param: e.param };
172
+ within.set(assignTo, child);
173
+ return closeAt;
174
+ }
175
+ return closeAt;
176
+ }
177
+ if (method === 'PathPrefix' && assignTo) {
178
+ const p = unq(text.slice(args[0]?.start ?? 0, args[0]?.end ?? 0)) ?? '';
179
+ within.set(assignTo, { prefix: joinPath(e.prefix, p || '/'), mws: e.mws.slice(), fw: e.fw, param: e.param });
180
+ return closeAt;
181
+ }
182
+ if (method === 'With') {
183
+ const tail = /^\s*\.\s*([A-Za-z_]\w*)\s*\(/.exec(masked.slice(closeAt, closeAt + 60));
184
+ if (tail) {
185
+ const chained = args.map((a) => mwOf(a, 'wrapper'));
186
+ const open2 = closeAt + tail[0].length - 1;
187
+ route(e, recv, tail[1], open2, chained, at, null);
188
+ return closeOf(masked, open2);
189
+ }
190
+ return closeAt;
191
+ }
192
+ if (method === 'Mount') {
193
+ const p = unq(text.slice(args[0]?.start ?? 0, args[0]?.end ?? 0));
194
+ const target = args[1] ? text.slice(args[1].start, args[1].end) : '';
195
+ if (p !== null) {
196
+ const names = [...target.matchAll(/(?<![\w.])(\w+)\s*\(/g)].map((x) => x[1]).concat(/^\s*\w+\s*$/.test(target) ? [target.trim()] : []);
197
+ mounts.push({ prefix: joinPath(e.prefix, p), mws: e.mws.slice(), fw: e.fw, targets: names, from: v.file });
198
+ }
199
+ return closeAt;
200
+ }
201
+ if (method === 'Subrouter' || method === 'Static') return closeAt;
202
+ if (ROUTE_CALLS.has(method) || METHODS.has(method.toUpperCase())) {
203
+ // gorilla/mux: r.HandleFunc("/x", h).Methods("POST")
204
+ let forced: string | null = null;
205
+ const meth = /^\s*\.\s*Methods\s*\(([^)]*)\)/.exec(masked.slice(closeAt, closeAt + 80));
206
+ if (meth) forced = [...text.slice(closeAt, closeAt + 120).matchAll(/"(\w+)"/g)].map((x) => x[1].toUpperCase())[0] ?? null;
207
+ route(e, recv, method, open, [], at, forced);
208
+ return meth ? closeAt + meth[0].length : closeAt;
209
+ }
210
+ return closeAt;
211
+ };
212
+ const walkRange = (from: number, to: number, scope: Map<string, Env>): void => {
213
+ for (const ev of evs) {
214
+ if (ev.at < from || ev.at >= to || ev.at < skipTo) continue;
215
+ if (ev.kind === 'create') {
216
+ scope.set(ev.m[1], fresh());
217
+ continue;
218
+ }
219
+ const recv = ev.m[1];
220
+ const method = ev.m[2];
221
+ const e = scope.get(recv);
222
+ if (!e) continue;
223
+ const open = ev.at + ev.m[0].length - 1;
224
+ const pre = masked.slice(Math.max(0, ev.at - 80), ev.at);
225
+ const assign = /(?:^|\n)\s*(\w+)\s*:?=\s*$/.exec(pre)?.[1] ?? null;
226
+ const end = handleCall(recv, method, ev.at, open, e, assign, scope);
227
+ if (end > skipTo) skipTo = end;
228
+ }
229
+ };
230
+ walkRange(body[0], body[1], env);
231
+ return { entries, mounts };
232
+ }
233
+
234
+ interface Resolved { view: FileView; start: number; end: number; name: string | null; found: boolean; wrappers: { start: number; end: number }[] }
235
+
236
+ // A handler argument: a function literal, a name, a method value (h.CreateNote), or a call that wraps or builds a handler.
237
+ function resolveHandler(repo: Repo, fn: FnIndex, v: FileView, a: { start: number; end: number }, depth = 0): Resolved {
238
+ const m = v.masked;
239
+ const t = m.slice(a.start, a.end).trim();
240
+ const miss: Resolved = { view: v, start: a.start, end: a.end, name: /^(?:\w+\.)*(\w+)$/.exec(t)?.[1] ?? null, found: false, wrappers: [] };
241
+ if (depth > 4) return miss;
242
+ if (/^func\s*\(/.test(t)) {
243
+ const g = goFunc(m, a.start + (m.slice(a.start, a.end).length - m.slice(a.start, a.end).trimStart().length));
244
+ if (g) return { view: v, start: g.start, end: g.end, name: null, found: true, wrappers: [] };
245
+ }
246
+ const call = /^((?:\w+\.)*\w+)\s*\(/.exec(t);
247
+ if (call) {
248
+ const at = a.start + (m.slice(a.start, a.end).length - m.slice(a.start, a.end).trimStart().length);
249
+ const open = at + call[0].length - 1;
250
+ const args = splitArgs(m, open);
251
+ const head = call[1];
252
+ const last = head.split('.').pop()!;
253
+ if (/^HandlerFunc$/.test(last) && args.length === 1) return resolveHandler(repo, fn, v, args[0], depth + 1);
254
+ for (const x of args) {
255
+ const r = resolveHandler(repo, fn, v, x, depth + 1);
256
+ if (r.found) return { ...r, wrappers: [{ start: at, end: x.start }, ...r.wrappers] };
257
+ }
258
+ // A handler factory: s.handleCreate() returns the handler, so read its body.
259
+ const def = fn.find(last, v.file) ?? fn.defs(last)[0];
260
+ if (def) return { view: def.view, start: def.start, end: def.end, name: last, found: true, wrappers: [] };
261
+ return miss;
262
+ }
263
+ const nm = /^(?:\w+\.)*(\w+)$/.exec(t)?.[1];
264
+ if (nm) {
265
+ const defs = fn.defs(nm);
266
+ const def = fn.find(nm, v.file) ?? defs.find((d) => d.view.file === v.file) ?? defs[0];
267
+ if (def) return { view: def.view, start: def.start, end: def.end, name: nm, found: true, wrappers: [] };
268
+ }
269
+ return miss;
270
+ }
271
+
@@ -0,0 +1,135 @@
1
+ // Entry points registered with a call, in the Express family: Express, Koa router, Fastify, Hono, restify and similar
2
+ // (app.get('/path', ...middleware, handler)), plus the app.use gates and router mounts around them.
3
+ import type { Confidence, Gate, Protection } from '../types/index.ts';
4
+ import { analyze, type FileView, type Spec } from './analyze.ts';
5
+ import { empty, joinPath, makeEntry, type Extracted } from './entry.ts';
6
+ import { closeOf, jsFunction, splitArgs } from './lex.ts';
7
+ import { paramsOf, resolveFunction } from './js-next.ts';
8
+ import type { Repo } from './repo.ts';
9
+
10
+ const FRAMEWORK = /\b(?:express|fastify|koa|hono|restify|polka|@hapi\/hapi|itty-router|elysia|@koa\/router|koa-router|connect|h3|@fastify\/\w+)\b/;
11
+ const CREATORS = /\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=\n]+)?=\s*(?:await\s+)?(?:new\s+)?(?:express\s*\.\s*Router|express|Router|fastify|Fastify|Hono|Koa|Elysia|polka|createRouter|KoaRouter|restify\s*\.\s*createServer|\w*[rR]outer)\s*[(<]/g;
12
+ const VERBS = new Set(['get', 'post', 'put', 'patch', 'delete', 'all', 'options', 'head']);
13
+
14
+ const confMin = (cs: Confidence[]): Confidence => (cs.includes('low') ? 'low' : cs.includes('medium') ? 'medium' : 'high');
15
+
16
+ function literalPaths(v: FileView, a: { start: number; end: number } | undefined): string[] | null {
17
+ if (!a) return null;
18
+ const c = v.masked[a.start];
19
+ if (c === '"' || c === "'" || c === '`') {
20
+ const p = v.text.slice(a.start + 1, a.end - 1);
21
+ return p.startsWith('/') || p === '*' || p === '' ? [p] : null;
22
+ }
23
+ if (c === '[') {
24
+ const ps = [...v.text.slice(a.start, a.end).matchAll(/['"`]([^'"`]*)['"`]/g)].map((x) => x[1]).filter((p) => p.startsWith('/') || p === '*');
25
+ return ps.length ? ps : null;
26
+ }
27
+ return null;
28
+ }
29
+
30
+ export function extractExpress(repo: Repo, v: FileView): Extracted {
31
+ const out = empty();
32
+ if (!FRAMEWORK.test(v.imports) && !/\bexpress\s*\(\s*\)|\bRouter\s*\(\s*\)/.test(v.masked)) return out;
33
+ const names = new Set(['app', 'router', 'server', 'api', 'route', 'routes', 'fastify', 'instance']);
34
+ for (const m of v.masked.matchAll(CREATORS)) names.add(m[1]);
35
+ const recv = [...names].map((n) => n.replace(/\$/g, '\\$')).join('|');
36
+ const call = new RegExp(`(?<![.\\w$])(?:${recv})\\s*\\.\\s*(get|post|put|patch|delete|all|options|head|use|route)\\s*\\(`, 'g');
37
+ const imports = repo.importMap(v);
38
+ const helpers = repo.helpers('js');
39
+
40
+ const middlewareDeco = (args: { start: number; end: number }[]): NonNullable<Spec['decorators']> =>
41
+ args.map((a) => ({ view: v, start: a.start, end: a.end, scope: 'wrapper' as const }));
42
+ const calledNames = (args: { start: number; end: number }[]): string[] => {
43
+ const set = new Set<string>();
44
+ for (const a of args) for (const m of v.masked.slice(a.start, a.end).matchAll(/(?<![\w$])([A-Za-z_$][\w$]*)\s*(?:\(|,|$)/g)) set.add(m[1]);
45
+ for (const a of args) { const t = v.masked.slice(a.start, a.end); const id = /^[A-Za-z_$][\w$.]*$/.exec(t); if (id) set.add(id[0].split('.').pop()!); }
46
+ return [...set].sort();
47
+ };
48
+
49
+ const register = (verb: string, paths: string[], args: { start: number; end: number }[], at: number): void => {
50
+ if (!args.length) return;
51
+ const last = args[args.length - 1];
52
+ const mws = args.slice(0, -1);
53
+ const r = resolveFunction(repo, v, last.start, last.end);
54
+ const line = v.lines.at(at);
55
+ for (const p of paths) {
56
+ const method = verb === 'all' ? 'ANY' : verb.toUpperCase();
57
+ const handlerView = r?.view ?? v;
58
+ const start = r?.start ?? last.start;
59
+ const end = r?.end ?? last.end;
60
+ const seeds = r ? (r.params.length ? [r.params[0]] : ['req']) : [];
61
+ const deco = middlewareDeco(mws);
62
+ if (r?.wrapper) deco.push({ view: v, start: r.wrapper.from, end: r.wrapper.to, scope: 'wrapper' });
63
+ const extra = calledNames(mws).concat(r?.wrapper ? [r.wrapper.name.split('.').pop()!] : []);
64
+ const e = makeEntry(repo, {
65
+ kind: 'http_route', framework: 'express', file: v.file, line, method, path: joinPath('', p === '' ? '/' : p), name: r?.defName ?? nameOf(v, last),
66
+ confidence: 'high', handlerView, spec: { start, end, seeds, decorators: deco, extraCalls: extra, returnsJson: true },
67
+ });
68
+ out.entries.push(e);
69
+ }
70
+ };
71
+
72
+ for (const m of v.masked.matchAll(call)) {
73
+ const verb = m[1];
74
+ const open = m.index! + m[0].length - 1;
75
+ const args = splitArgs(v.masked, open);
76
+ if (verb === 'route') {
77
+ const paths = literalPaths(v, args[0]);
78
+ if (!paths) continue;
79
+ // .route('/x').get(h).post(h): read the chain after the call.
80
+ let i = closeOf(v.masked, open);
81
+ if (i < 0) continue;
82
+ for (let g = 0; g < 8; g++) {
83
+ const nxt = /^\s*\.\s*(get|post|put|patch|delete|all|options|head)\s*\(/.exec(v.masked.slice(i, i + 60));
84
+ if (!nxt) break;
85
+ const o = i + nxt[0].length - 1;
86
+ register(nxt[1], paths, splitArgs(v.masked, o), i + nxt[0].indexOf(nxt[1]));
87
+ i = closeOf(v.masked, o);
88
+ if (i < 0) break;
89
+ }
90
+ continue;
91
+ }
92
+ if (VERBS.has(verb)) {
93
+ const paths = literalPaths(v, args[0]);
94
+ if (!paths || args.length < 2) continue;
95
+ register(verb, paths, args.slice(1), m.index!);
96
+ continue;
97
+ }
98
+ // use(): a gate on a path, or a router mount.
99
+ const paths = literalPaths(v, args[0]);
100
+ const rest = paths ? args.slice(1) : args;
101
+ const prefix = paths?.[0] ?? '*';
102
+ const line = v.lines.at(m.index!);
103
+ let target: string | null = null;
104
+ for (const a of rest) {
105
+ const id = /^[A-Za-z_$][\w$]*$/.exec(v.masked.slice(a.start, a.end));
106
+ const spec = id ? imports.get(id[0]) : undefined;
107
+ const file = spec ? repo.resolveImport(v.file, spec) : null;
108
+ if (file && /\bRouter\s*\(|\bexpress\.Router\b|\bnew\s+Hono\b|\bfastify\b/.test(repo.ctx.text(file))) target = file;
109
+ }
110
+ const gateArgs = rest.filter((a) => { const id = /^[A-Za-z_$][\w$]*$/.exec(v.masked.slice(a.start, a.end)); return !(id && target && imports.get(id[0]) && repo.resolveImport(v.file, imports.get(id[0])!) === target); });
111
+ const protections: Protection[] = [];
112
+ for (const a of gateArgs) {
113
+ const spec: Spec = { start: a.start, end: a.end, seeds: [], decorators: [], extraCalls: calledNames([a]), returnsJson: false, gate: true };
114
+ const an = analyze(v, spec, helpers);
115
+ for (const p of an.protections) if (p.kind !== 'owner_filter' && p.kind !== 'audit_log') protections.push({ ...p, scope: 'middleware' });
116
+ }
117
+ const joined = protections.length ? protections : [];
118
+ if (target) out.mounts.push({ from: v.file, line, prefix: joinPath('', prefix === '*' ? '/' : prefix), target, protections: joined });
119
+ else if (joined.length) {
120
+ const gate: Gate = { id: `${v.file}:${line}:use:${prefix}`, kind: 'middleware', framework: 'express', file: v.file, line, paths: [prefix === '*' ? '*' : joinPath('', prefix)], methods: [], protections: joined, confidence: confMin(joined.map((p) => p.confidence)) === 'high' ? 'medium' : confMin(joined.map((p) => p.confidence)) };
121
+ out.gates.push(gate);
122
+ }
123
+ }
124
+ return out;
125
+ }
126
+
127
+ // A readable name for the handler argument: the function name, or the called factory, or null for an inline function.
128
+ function nameOf(v: FileView, a: { start: number; end: number }): string | null {
129
+ const t = v.masked.slice(a.start, a.end);
130
+ if (/^(?:async\b|function\b|\(|[A-Za-z_$][\w$]*\s*=>)/.test(t)) return null;
131
+ const m = /^([A-Za-z_$][\w$.]*)/.exec(t);
132
+ return m ? m[1] : null;
133
+ }
134
+
135
+ export { paramsOf };
@@ -0,0 +1,221 @@
1
+ // NestJS controllers, resolvers and consumers; scheduled and queued jobs; functions handed to a model as tools.
2
+ import type { Gate, InputSource, Protection } from '../types/index.ts';
3
+ import type { FileView, Spec } from './analyze.ts';
4
+ import { empty, joinPath, makeEntry, paramNames, type Extracted } from './entry.ts';
5
+ import { closeOf, jsFunction, splitArgs } from './lex.ts';
6
+ import { paramsOf, resolveFunction } from './js-next.ts';
7
+ import type { Repo } from './repo.ts';
8
+
9
+ const NEST_VERBS = /@(Get|Post|Put|Patch|Delete|All|Options|Head)\s*\(/g;
10
+ const NEST_OTHER = /@(Query|Mutation|Subscription|ResolveField|MessagePattern|EventPattern|SubscribeMessage|Cron|Interval|Timeout|Process)\s*\(/g;
11
+ const PUBLIC_MARK = /@(?:Public|AllowAnonymous|SkipAuth|IsPublic|NoAuth|AllowUnauthenticated)\s*\(/;
12
+
13
+ const dtoCache = new WeakMap<Repo, Map<string, boolean>>();
14
+ // Whether a class of this name in the repository carries class-validator decorators.
15
+ function dtoValidated(repo: Repo, name: string): boolean {
16
+ let cache = dtoCache.get(repo);
17
+ if (!cache) { cache = new Map(); dtoCache.set(repo, cache); }
18
+ const hit = cache.get(name);
19
+ if (hit !== undefined) return hit;
20
+ let ok = false;
21
+ for (const f of repo.filesOf('js')) {
22
+ const t = repo.ctx.text(f);
23
+ const m = new RegExp(`class\\s+${name}\\b[^{]*\\{`).exec(t);
24
+ if (!m) continue;
25
+ const body = t.slice(m.index, m.index + 4000);
26
+ if (/@(?:Is[A-Z]\w*|Min|Max|Length|MinLength|MaxLength|Matches|Validate\w*|Allow|ArrayMaxSize|ArrayMinSize|Contains|NotEquals|Equals|IsOptional)\s*\(/.test(body)) { ok = true; break; }
27
+ }
28
+ cache.set(name, ok);
29
+ return ok;
30
+ }
31
+
32
+ export function extractNest(repo: Repo, v: FileView): Extracted {
33
+ const out = empty();
34
+ if (!/@(?:Controller|Resolver|Cron|Interval|Timeout|MessagePattern|EventPattern|SubscribeMessage|Process|Processor)\s*\(|APP_GUARD|useGlobalPipes|consumer\s*\.\s*apply\s*\(/.test(v.text)) return out;
35
+ const { masked, text } = v;
36
+
37
+ // Global guards and pipes become gates.
38
+ for (const m of masked.matchAll(/provide\s*:\s*APP_GUARD/g)) {
39
+ const seg = text.slice(m.index!, m.index! + 160);
40
+ const cls = /useClass\s*:\s*([A-Za-z_$][\w$]*)/.exec(seg)?.[1] ?? 'APP_GUARD';
41
+ const protections: Protection[] = [];
42
+ const line = v.lines.at(m.index!);
43
+ if (/auth|jwt|session|firebase|supabase/i.test(cls)) protections.push({ kind: 'session_read', scope: 'global', file: v.file, line, via: `APP_GUARD ${cls}`, enforced: true, confidence: 'medium' });
44
+ if (/role|permission|admin|abac|casl/i.test(cls)) protections.push({ kind: 'role_check', scope: 'global', file: v.file, line, via: `APP_GUARD ${cls}`, enforced: true, confidence: 'medium' });
45
+ if (/throttl|rate/i.test(cls)) protections.push({ kind: 'rate_limit', scope: 'global', file: v.file, line, via: `APP_GUARD ${cls}`, enforced: true, confidence: 'high' });
46
+ if (protections.length) out.gates.push({ id: `${v.file}:${line}:APP_GUARD:${cls}`, kind: 'guard', framework: 'nest', file: v.file, line, paths: ['*'], methods: [], protections, confidence: 'medium' });
47
+ }
48
+ // Module middleware: consumer.apply(AuthMiddleware).forRoutes('path', { path: 'x', method: RequestMethod.GET }). One gate per route spec.
49
+ for (const m of masked.matchAll(/\.\s*apply\s*\(([^)]*)\)/g)) {
50
+ const statementEnd = masked.indexOf(';', m.index!);
51
+ const chain = text.slice(m.index!, statementEnd < 0 ? m.index! + 600 : statementEnd);
52
+ const fr = /\.\s*forRoutes\s*\(/.exec(chain);
53
+ if (!fr) continue;
54
+ const names = [...m[1].matchAll(/[A-Za-z_$][\w$]*/g)].map((x) => x[0]).filter((x) => !/^(?:new|function|req|res|next)$/.test(x));
55
+ const line = v.lines.at(m.index!);
56
+ const protections: Protection[] = [];
57
+ for (const n of names) {
58
+ if (/auth|jwt|session|firebase|supabase|login|token/i.test(n)) protections.push({ kind: 'session_read', scope: 'middleware', file: v.file, line, via: `module middleware ${n}`, enforced: null, confidence: 'medium' });
59
+ if (/role|permission|admin|abac|casl/i.test(n)) protections.push({ kind: 'role_check', scope: 'middleware', file: v.file, line, via: `module middleware ${n}`, enforced: null, confidence: 'medium' });
60
+ if (/throttl|rate/i.test(n)) protections.push({ kind: 'rate_limit', scope: 'middleware', file: v.file, line, via: `module middleware ${n}`, enforced: null, confidence: 'medium' });
61
+ }
62
+ if (!protections.length) continue;
63
+ const open = m.index! + chain.indexOf('forRoutes') + 'forRoutes'.length;
64
+ const args = text.slice(text.indexOf('(', open - 1) + 1, text.indexOf(')', open - 1 + chain.slice(chain.indexOf('forRoutes')).length)).trim();
65
+ const specs: { path: string; methods: string[] }[] = [];
66
+ for (const o of args.matchAll(/\{\s*path\s*:\s*['"`]([^'"`]*)['"`]\s*,\s*method\s*:\s*RequestMethod\.(\w+)\s*\}/g)) specs.push({ path: o[1], methods: o[2] === 'ALL' ? [] : [o[2]] });
67
+ const rest = args.replace(/\{[^}]*\}/g, ' ');
68
+ for (const sx of rest.matchAll(/['"`]([^'"`]*)['"`]/g)) specs.push({ path: sx[1], methods: [] });
69
+ if (!specs.length && /^\s*\*\s*$|['"`]\*['"`]/.test(args)) specs.push({ path: '*', methods: [] });
70
+ specs.forEach((sp, i) => {
71
+ const star = sp.path.replace(/\/?\*.*$/, '');
72
+ const gp = sp.path === '*' ? '*' : joinPath('/', star);
73
+ out.gates.push({ id: `${v.file}:${line}:forRoutes:${i}`, kind: 'middleware', framework: 'nest', file: v.file, line, paths: [gp], methods: sp.methods, protections, confidence: 'medium' });
74
+ });
75
+ }
76
+ for (const m of masked.matchAll(/useGlobalPipes\s*\(\s*new\s+ValidationPipe/g)) {
77
+ const line = v.lines.at(m.index!);
78
+ const g: Gate = { id: `${v.file}:${line}:ValidationPipe`, kind: 'global_pipe', framework: 'nest', file: v.file, line, paths: ['*'], methods: [], protections: [{ kind: 'input_validation', scope: 'global', file: v.file, line, via: 'global ValidationPipe', enforced: true, confidence: 'medium' }], confidence: 'medium' };
79
+ out.gates.push(g);
80
+ }
81
+
82
+ for (const cm of masked.matchAll(/(?<![\w$])class\s+([A-Za-z_$][\w$]*)\b/g)) {
83
+ const classKw = cm.index!;
84
+ let ds = classKw;
85
+ for (let k = classKw - 1; k >= 0 && classKw - k < 900; k--) { const c = masked[k]; if (c === '}' || c === ';') break; ds = k; }
86
+ const classDeco = text.slice(ds, classKw);
87
+ const bodyOpen = masked.indexOf('{', classKw);
88
+ if (bodyOpen < 0) continue;
89
+ const bodyClose = closeOf(masked, bodyOpen);
90
+ if (bodyClose < 0) continue;
91
+ const isController = /@Controller\s*\(/.test(classDeco);
92
+ const isResolver = /@Resolver\s*\(/.test(classDeco);
93
+ const prefix = isController ? (/@Controller\s*\(\s*(?:['"`]([^'"`]*)['"`]|\{[^}]*?path\s*:\s*['"`]([^'"`]*)['"`])/.exec(classDeco)?.slice(1).find((x) => x !== undefined) ?? '') : '';
94
+ const body = masked.slice(bodyOpen, bodyClose);
95
+ const verbRe = isController ? NEST_VERBS : NEST_OTHER;
96
+ verbRe.lastIndex = 0;
97
+ const hits = [...body.matchAll(isController ? NEST_VERBS : new RegExp(NEST_OTHER.source, 'g'))];
98
+ if (!hits.length) continue;
99
+ for (const h of hits) {
100
+ const at = bodyOpen + h.index!;
101
+ const decoOpen = at + h[0].length - 1;
102
+ const decoClose = closeOf(masked, decoOpen);
103
+ if (decoClose < 0) continue;
104
+ // Skip further decorators, then read the method name and signature.
105
+ let i = decoClose;
106
+ for (;;) {
107
+ const nxt = /^\s*@[\w$.]+\s*(?:\()?/.exec(masked.slice(i, i + 80));
108
+ if (!nxt) break;
109
+ const o = i + nxt[0].length - 1;
110
+ if (masked[o] === '(') { const c = closeOf(masked, o); if (c < 0) break; i = c; } else i += nxt[0].length;
111
+ }
112
+ const sig = /^\s*(?:(?:public|private|protected|async|static|override)\s+)*([A-Za-z_$][\w$]*)\s*(?:<[^>(]*>)?\s*\(/.exec(masked.slice(i, i + 200));
113
+ if (!sig) continue;
114
+ const parenIdx = i + sig[0].length - 1;
115
+ const f = jsFunction(masked, parenIdx, true);
116
+ if (!f) continue;
117
+ let ms = at;
118
+ for (let k = at - 1; k > bodyOpen && at - k < 700; k--) { const c = masked[k]; if (c === '}' || c === ';' || c === '{') break; ms = k; }
119
+ const methodDeco = text.slice(ms, at + (decoClose - at));
120
+ const argText = text.slice(decoOpen + 1, decoClose - 1);
121
+ const litPath = /^\s*['"`]([^'"`]*)['"`]/.exec(argText)?.[1] ?? '';
122
+ const deco: NonNullable<Spec['decorators']> = [{ view: v, start: ds, end: classKw, scope: 'router' }, { view: v, start: ms, end: i, scope: 'handler' }];
123
+ const name = sig[1];
124
+ const params = paramNames(masked, parenIdx);
125
+ let kind: 'http_route' | 'graphql_resolver' | 'message_consumer' | 'websocket' | 'job' = 'http_route';
126
+ let method: string | null = h[1].toUpperCase();
127
+ let path: string | null = null;
128
+ if (isController) { method = h[1] === 'All' ? 'ANY' : h[1].toUpperCase(); path = joinPath('/' + prefix, litPath); if (path === '') path = '/'; }
129
+ else if (/^(?:Query|Mutation|Subscription|ResolveField)$/.test(h[1])) { kind = 'graphql_resolver'; method = h[1] === 'ResolveField' ? 'FIELD' : h[1].toUpperCase(); path = name; }
130
+ else if (/^(?:MessagePattern|EventPattern)$/.test(h[1])) { kind = 'message_consumer'; method = null; path = litPath || argText.trim().slice(0, 60) || name; }
131
+ else if (h[1] === 'SubscribeMessage') { kind = 'websocket'; method = null; path = litPath || name; }
132
+ else { kind = 'job'; method = null; path = litPath || (argText.trim().slice(0, 40) || name); }
133
+ if (!isController && !isResolver && kind === 'graphql_resolver') continue;
134
+ const e = makeEntry(repo, { kind, framework: 'nest', file: v.file, line: v.lines.at(at), method, path, name, confidence: 'high', handlerView: v, spec: { start: f.start, end: f.end, seeds: params, decorators: deco, extraCalls: [], returnsJson: true }, tags: PUBLIC_MARK.test(classDeco) || PUBLIC_MARK.test(methodDeco) ? ['public'] : [] });
135
+ // Parameter decorators are request reads; a typed @Body() is checked against class-validator decorators on the DTO.
136
+ const sigText = text.slice(parenIdx, f.bodyStart);
137
+ const reads: [InputSource, RegExp][] = [['body', /@Body\s*\(/], ['query', /@Query\s*\(/], ['params', /@Param\s*\(/], ['headers', /@Headers?\s*\(/], ['file', /@(?:UploadedFiles?|UploadedFile)\s*\(/]];
138
+ for (const [source, re] of reads) { const rm = re.exec(sigText); if (rm && !e.reads.some((r) => r.source === source)) e.reads.push({ source, line: v.lines.at(parenIdx + rm.index) }); }
139
+ if (e.reads.some((r) => r.source === 'file') && !e.tags.includes('upload')) e.tags = [...e.tags, 'upload'].sort();
140
+ if (/@(?:UseInterceptors\s*\(\s*(?:FileInterceptor|FilesInterceptor))/.test(methodDeco) && !e.reads.some((r) => r.source === 'file')) e.reads.push({ source: 'file', line: e.line });
141
+ for (const dm of sigText.matchAll(/@Body\s*\([^)]*\)\s*[A-Za-z_$][\w$]*\s*:\s*([A-Z][\w$]*)/g)) {
142
+ if (dtoValidated(repo, dm[1])) e.protections.push({ kind: 'input_validation', scope: 'handler', file: v.file, line: v.lines.at(parenIdx + dm.index!), via: `DTO ${dm[1]}`, enforced: null, confidence: 'medium' });
143
+ }
144
+ e.protections.sort((a, b) => a.line - b.line || (a.kind < b.kind ? -1 : a.kind > b.kind ? 1 : 0) || (a.via < b.via ? -1 : a.via > b.via ? 1 : 0));
145
+ e.reads.sort((a, b) => (a.source < b.source ? -1 : 1));
146
+ out.entries.push(e);
147
+ }
148
+ }
149
+ return out;
150
+ }
151
+
152
+ const JOB_CALL = /(?<![\w$])(cron\.schedule|schedule\.scheduleJob|scheduleJob|new\s+CronJob|agenda\.define|new\s+Worker|\w*[qQ]ueue\w*\.process|boss\.work|inngest\.createFunction|\w+\.addRepeatableJob|setInterval)\s*\(/g;
153
+
154
+ export function extractJobs(repo: Repo, v: FileView): Extracted {
155
+ const out = empty();
156
+ if (!/cron|schedule|agenda|Worker|[qQ]ueue|boss|inngest/.test(v.text)) return out;
157
+ for (const m of v.masked.matchAll(JOB_CALL)) {
158
+ if (m[1] === 'setInterval') continue;
159
+ const open = m.index! + m[0].length - 1;
160
+ const args = splitArgs(v.masked, open);
161
+ const first = args[0];
162
+ const lit = first && /^['"`]/.test(v.masked[first.start]) ? v.text.slice(first.start + 1, first.end - 1) : null;
163
+ const fa = args.map((a) => ({ a, r: resolveFunction(repo, v, a.start, a.end) })).find((x) => x.r);
164
+ if (!fa?.r) continue;
165
+ const framework = /cron|schedule/i.test(m[1]) ? 'node-cron' : /Worker|process|work/.test(m[1]) ? 'queue' : m[1];
166
+ out.entries.push(makeEntry(repo, { kind: 'job', framework, file: v.file, line: v.lines.at(m.index!), method: null, path: lit, name: lit && !/[\s*]/.test(lit) ? lit : null, confidence: 'high', handlerView: fa.r.view, spec: { start: fa.r.start, end: fa.r.end, seeds: fa.r.params, decorators: [], returnsJson: false } }));
167
+ }
168
+ return out;
169
+ }
170
+
171
+ const TOOL_OBJECT = /\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=\n]+)?=\s*\{/g;
172
+ const TOOLISH = /tool|function|handler|action|capabilit/i;
173
+
174
+ export function extractAiTools(repo: Repo, v: FileView): Extracted {
175
+ const out = empty();
176
+ if (!/tool|function_call|mcp|[aA]gent|llm|openai|anthropic|generateText|assistant/.test(v.text)) return out;
177
+ const seen = new Set<number>();
178
+ const push = (name: string | null, f: { start: number; end: number; bodyStart: number }, line: number, framework: string): void => {
179
+ if (seen.has(f.start)) return;
180
+ seen.add(f.start);
181
+ out.entries.push(makeEntry(repo, { kind: 'ai_tool', framework, file: v.file, line, method: null, path: null, name, confidence: 'medium', handlerView: v, spec: { start: f.start, end: f.end, seeds: paramsOf(v.masked, f).slice(0, 1), decorators: [], returnsJson: false } }));
182
+ };
183
+ // 1. An object of functions named like tools: { read_note: (args) => ..., send: async (args) => ... }
184
+ for (const m of v.masked.matchAll(TOOL_OBJECT)) {
185
+ if (!TOOLISH.test(m[1]) || !/\b(?:tool|function|openai|anthropic|llm|assistant|agent|model)/i.test(v.text)) continue;
186
+ const open = m.index! + m[0].length - 1;
187
+ const close = closeOf(v.masked, open);
188
+ if (close < 0) continue;
189
+ for (const seg of splitArgs(v.masked, open)) {
190
+ const head = /^(?:async\s+)?\[?['"]?([\w$-]+)['"]?\]?\s*(:|\()/.exec(v.text.slice(seg.start, seg.end).replace(/^async\s+/, 'async '));
191
+ if (!head) continue;
192
+ const name = head[1];
193
+ let at = seg.start + head[0].length - 1;
194
+ if (head[2] === ':') at = seg.start + head[0].length;
195
+ const f = jsFunction(v.masked, head[2] === '(' ? at : at);
196
+ if (f && f.end <= seg.end + 1) push(name, f, v.lines.at(seg.start), 'ai-tools');
197
+ }
198
+ }
199
+ // 2. tool({ description, parameters, execute })
200
+ for (const m of v.masked.matchAll(/(?<![\w$])tool\s*\(\s*\{/g)) {
201
+ const open = v.masked.indexOf('{', m.index!);
202
+ for (const seg of splitArgs(v.masked, open)) {
203
+ const ex = /^execute\s*(:|\()/.exec(v.masked.slice(seg.start, seg.end));
204
+ if (!ex) continue;
205
+ const f = jsFunction(v.masked, seg.start + (ex[1] === ':' ? ex[0].length : ex[0].length - 1));
206
+ if (!f) continue;
207
+ const before = v.text.slice(Math.max(0, m.index! - 80), m.index!);
208
+ const nm = /([A-Za-z_$][\w$]*)\s*(?::|=)\s*$/.exec(before)?.[1] ?? null;
209
+ push(nm, f, v.lines.at(m.index!), 'ai-sdk');
210
+ }
211
+ }
212
+ // 3. MCP: server.tool('name', schema, handler)
213
+ for (const m of v.masked.matchAll(/\.\s*(?:tool|registerTool)\s*\(\s*(['"])/g)) {
214
+ const open = v.masked.indexOf('(', m.index!);
215
+ const args = splitArgs(v.masked, open);
216
+ const nm = v.text.slice(args[0].start + 1, args[0].end - 1);
217
+ const fa = args.map((a) => jsFunction(v.masked, a.start)).filter((x) => x).pop();
218
+ if (fa) push(nm, fa, v.lines.at(m.index!), 'mcp');
219
+ }
220
+ return out;
221
+ }