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
@@ -0,0 +1,326 @@
1
+ import { empty, makeEntry } from './entry.js';
2
+ import { Lines } from './lex.js';
3
+ // Names that say a value is a secret. A key that is public by design (a maps key, a Firebase web key, an analytics id) is not one.
4
+ const SECRET_NAME = /(?:secret|private[_.\- ]?key|passw(?:or)?d|passwd|signing[_.\- ]?key|service[_.\- ]?role|auth[_.\- ]?token|access[_.\- ]?token|refresh[_.\- ]?token)/i;
5
+ const PUBLIC_NAME = /(?:public|publishable|client[_.\- ]?id|maps|geo|analytics|sentry[_.\- ]?dsn)/i;
6
+ const PLACEHOLDER = /^(?:@|\$\(|\$\{|%|<|your[_\- ]|changeme|change[_\- ]me|todo|xxx|null|none|true|false|\s*$)/i;
7
+ export const isSecretValue = (name, value) => SECRET_NAME.test(name) && !PUBLIC_NAME.test(name) && value.length >= 8 && !PLACEHOLDER.test(value.trim());
8
+ const blankComments = (t) => t.replace(/<!--[\s\S]*?-->/g, (c) => c.replace(/[^\n]/g, ' '));
9
+ const view = (file, text) => ({ file, text, masked: text, lang: 'kt', lines: new Lines(text), imports: '' });
10
+ const bool = (v) => (v === 'true' ? true : v === 'false' ? false : null);
11
+ function tokens(text) {
12
+ const out = [];
13
+ for (const m of text.matchAll(/<(\/?)([A-Za-z_][\w.:-]*)((?:[^>"']|"[^"]*"|'[^']*')*?)(\/?)>/g)) {
14
+ const attrs = {};
15
+ for (const a of m[3].matchAll(/([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g))
16
+ attrs[a[1]] = a[2] ?? a[3] ?? '';
17
+ out.push({ name: m[2], close: m[1] === '/', self: m[4] === '/', attrs, at: m.index, end: m.index + m[0].length });
18
+ }
19
+ return out;
20
+ }
21
+ const COMPONENTS = new Set(['activity', 'activity-alias', 'service', 'receiver', 'provider']);
22
+ export function readManifest(file, text, gradleTarget) {
23
+ const clean = blankComments(text);
24
+ const lines = new Lines(text);
25
+ const toks = tokens(clean);
26
+ const secrets = [];
27
+ const bounds = new Map();
28
+ const pkg = toks.find((t) => t.name === 'manifest')?.attrs.package ?? null;
29
+ const sdk = toks.find((t) => t.name === 'uses-sdk');
30
+ const targetRaw = sdk?.attrs['android:targetSdkVersion'];
31
+ const target = targetRaw && /^\d+$/.test(targetRaw) ? Number(targetRaw) : gradleTarget;
32
+ const app = toks.find((t) => t.name === 'application' && !t.close);
33
+ const manifest = {
34
+ file, package: pkg, line: lines.at(app?.at ?? 0), debuggable: bool(app?.attrs['android:debuggable']), allow_backup: bool(app?.attrs['android:allowBackup']),
35
+ uses_cleartext_traffic: bool(app?.attrs['android:usesCleartextTraffic']), network_security_config: app?.attrs['android:networkSecurityConfig'] ?? null,
36
+ target_sdk: target, permissions: toks.filter((t) => t.name === 'uses-permission' && !t.close).map((t) => t.attrs['android:name']).filter(Boolean).sort(), components: [],
37
+ };
38
+ let cur = null;
39
+ let curStart = 0;
40
+ let filter = null;
41
+ let hasFilter = false;
42
+ const finish = (endAt) => {
43
+ if (!cur)
44
+ return;
45
+ // A component with an intent filter and no explicit flag is exported before Android 12 (target SDK 31).
46
+ if (!cur.exported_explicit)
47
+ cur.exported = hasFilter && (target === null || target < 31);
48
+ bounds.set(cur, [curStart, endAt]);
49
+ manifest.components.push(cur);
50
+ cur = null;
51
+ };
52
+ for (const t of toks) {
53
+ if (t.name === 'meta-data' && !t.close) {
54
+ const n = t.attrs['android:name'] ?? '';
55
+ const v = t.attrs['android:value'] ?? '';
56
+ if (isSecretValue(n, v))
57
+ secrets.push({ file, line: lines.at(t.at), key: n, where: 'android manifest' });
58
+ continue;
59
+ }
60
+ if (COMPONENTS.has(t.name)) {
61
+ if (t.close) {
62
+ finish(t.end);
63
+ continue;
64
+ }
65
+ finish(t.at);
66
+ const nm = t.attrs['android:name'] ?? '';
67
+ const full = nm.startsWith('.') ? `${pkg ?? ''}${nm}` : nm.includes('.') ? nm : `${pkg ?? ''}.${nm}`;
68
+ const perm = t.attrs['android:permission'] ?? t.attrs['android:readPermission'] ?? t.attrs['android:writePermission'] ?? null;
69
+ const ex = t.attrs['android:exported'];
70
+ cur = { kind: t.name === 'activity-alias' ? 'activity' : t.name, name: full, line: lines.at(t.at), exported: ex === 'true', exported_explicit: ex !== undefined, permission: perm, launcher: false, filters: [] };
71
+ curStart = t.at;
72
+ hasFilter = false;
73
+ if (t.self)
74
+ finish(t.end);
75
+ continue;
76
+ }
77
+ if (!cur)
78
+ continue;
79
+ if (t.name === 'intent-filter') {
80
+ if (t.close) {
81
+ if (filter)
82
+ cur.filters.push(filter);
83
+ filter = null;
84
+ }
85
+ else {
86
+ filter = { actions: [], categories: [], schemes: [], hosts: [], paths: [] };
87
+ hasFilter = true;
88
+ }
89
+ continue;
90
+ }
91
+ if (!filter || t.close)
92
+ continue;
93
+ if (t.name === 'action')
94
+ filter.actions.push(t.attrs['android:name'] ?? '');
95
+ else if (t.name === 'category')
96
+ filter.categories.push(t.attrs['android:name'] ?? '');
97
+ else if (t.name === 'data') {
98
+ const a = t.attrs;
99
+ if (a['android:scheme'])
100
+ filter.schemes.push(a['android:scheme']);
101
+ if (a['android:host'])
102
+ filter.hosts.push(a['android:host']);
103
+ const p = a['android:path'] ?? a['android:pathPrefix'] ?? a['android:pathPattern'] ?? a['android:pathAdvancedPattern'];
104
+ if (p)
105
+ filter.paths.push(p);
106
+ }
107
+ }
108
+ finish(clean.length);
109
+ for (const c of manifest.components)
110
+ c.launcher = c.filters.length > 0 && c.filters.every((f) => f.actions.every((a) => a === 'android.intent.action.MAIN') && f.categories.includes('android.intent.category.LAUNCHER'));
111
+ for (const c of manifest.components) {
112
+ c.filters.forEach((f) => { f.actions.sort(); f.categories.sort(); f.schemes.sort(); f.hosts.sort(); f.paths.sort(); });
113
+ }
114
+ manifest.components.sort((a, b) => a.line - b.line || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
115
+ return { manifest, secrets, bounds };
116
+ }
117
+ export function readNetworkSecurity(file, text) {
118
+ const clean = blankComments(text);
119
+ const lines = new Lines(text);
120
+ const toks = tokens(clean);
121
+ const base = toks.find((t) => t.name === 'base-config' && !t.close);
122
+ const domains = [];
123
+ let pins = 0;
124
+ let inDomainConfig = null;
125
+ let userCerts = false;
126
+ let cleartextDomains = [];
127
+ const pending = [];
128
+ for (const t of toks) {
129
+ if (t.name === 'domain-config') {
130
+ if (t.close) {
131
+ if (inDomainConfig?.attrs.cleartextTrafficPermitted === 'true')
132
+ cleartextDomains = cleartextDomains.concat(pending);
133
+ pending.length = 0;
134
+ inDomainConfig = null;
135
+ }
136
+ else {
137
+ inDomainConfig = t;
138
+ pending.length = 0;
139
+ }
140
+ }
141
+ else if (t.name === 'domain' && !t.close && inDomainConfig) {
142
+ const d = clean.slice(t.end, clean.indexOf('<', t.end)).trim();
143
+ if (d) {
144
+ domains.push(d);
145
+ pending.push(d);
146
+ }
147
+ }
148
+ else if (t.name === 'pin' && !t.close)
149
+ pins++;
150
+ else if (t.name === 'certificates' && t.attrs.src === 'user')
151
+ userCerts = true;
152
+ }
153
+ return { file, line: lines.at(base?.at ?? 0), cleartext_base: bool(base?.attrs.cleartextTrafficPermitted), cleartext_domains: cleartextDomains.sort(), trusts_user_certs: userCerts, pinned_domains: pins };
154
+ }
155
+ function readPlist(text) {
156
+ const lines = new Lines(text);
157
+ const toks = [...blankComments(text).matchAll(/<(\/?)(dict|array|key|string|integer|real|true|false|data|date)\b[^>]*?(\/?)>/g)];
158
+ let i = 0;
159
+ const keyLines = new Map();
160
+ const textAfter = (m) => text.slice(m.index + m[0].length, text.indexOf('<', m.index + m[0].length));
161
+ const value = () => {
162
+ const t = toks[i++];
163
+ if (!t)
164
+ return '';
165
+ if (t[2] === 'true')
166
+ return true;
167
+ if (t[2] === 'false')
168
+ return false;
169
+ if (t[2] === 'string' || t[2] === 'data' || t[2] === 'date') {
170
+ const v = t[3] === '/' ? '' : textAfter(t);
171
+ if (t[3] !== '/')
172
+ i++;
173
+ return v;
174
+ }
175
+ if (t[2] === 'integer' || t[2] === 'real') {
176
+ const v = Number(textAfter(t));
177
+ i++;
178
+ return v;
179
+ }
180
+ if (t[2] === 'array') {
181
+ const a = [];
182
+ while (i < toks.length && !(toks[i][1] === '/' && toks[i][2] === 'array'))
183
+ a.push(value());
184
+ i++;
185
+ return a;
186
+ }
187
+ if (t[2] === 'dict') {
188
+ const d = {};
189
+ while (i < toks.length && !(toks[i][1] === '/' && toks[i][2] === 'dict')) {
190
+ const k = toks[i];
191
+ if (k[2] !== 'key' || k[1] === '/') {
192
+ i++;
193
+ continue;
194
+ }
195
+ const name = textAfter(k);
196
+ keyLines.set(name, lines.at(k.index));
197
+ i += 2;
198
+ d[name] = value();
199
+ }
200
+ i++;
201
+ return d;
202
+ }
203
+ return '';
204
+ };
205
+ const start = toks.findIndex((t) => t[2] === 'dict' && t[1] !== '/');
206
+ if (start < 0)
207
+ return null;
208
+ i = start;
209
+ const v = value();
210
+ return typeof v === 'object' && !Array.isArray(v) ? { value: v, keyLines } : null;
211
+ }
212
+ const strList = (v) => (Array.isArray(v) ? v.filter((x) => typeof x === 'string') : []);
213
+ export function readPlistFile(file, text) {
214
+ const r = readPlist(text);
215
+ if (!r)
216
+ return null;
217
+ const d = r.value;
218
+ const ats = typeof d.NSAppTransportSecurity === 'object' && !Array.isArray(d.NSAppTransportSecurity) ? d.NSAppTransportSecurity : null;
219
+ const secrets = [];
220
+ for (const [k, v] of Object.entries(d))
221
+ if (typeof v === 'string' && isSecretValue(k, v))
222
+ secrets.push({ file, line: r.keyLines.get(k) ?? 1, key: k, where: 'ios plist' });
223
+ const types = Array.isArray(d.CFBundleURLTypes) ? d.CFBundleURLTypes : [];
224
+ const schemes = types.flatMap((t) => (typeof t === 'object' && !Array.isArray(t) ? strList(t.CFBundleURLSchemes) : []));
225
+ const line = r.keyLines.get('CFBundleURLTypes') ?? 1;
226
+ const assoc = strList(d['com.apple.developer.associated-domains']).filter((x) => x.startsWith('applinks:')).map((x) => x.slice('applinks:'.length).replace(/\?.*$/, ''));
227
+ return {
228
+ plist: {
229
+ file, ats_arbitrary_loads: ats && typeof ats.NSAllowsArbitraryLoads === 'boolean' ? ats.NSAllowsArbitraryLoads : null, ats_line: r.keyLines.get('NSAllowsArbitraryLoads') ?? null,
230
+ url_schemes: schemes.slice().sort(), associated_domains: assoc.slice().sort(), usage_descriptions: Object.keys(d).filter((k) => /UsageDescription$/.test(k)).sort(),
231
+ },
232
+ secrets, urlSchemes: schemes.map((scheme) => ({ scheme, line })), domains: assoc.map((domain) => ({ domain, line: r.keyLines.get('com.apple.developer.associated-domains') ?? 1 })),
233
+ };
234
+ }
235
+ const SKIP = /(?:^|\/)(?:Pods|Carthage|\.build|DerivedData|node_modules|build|intermediates)\//;
236
+ export function mobileFiles(repo) {
237
+ const f = repo.ctx.files.filter((x) => !SKIP.test(x));
238
+ return {
239
+ manifests: f.filter((x) => /(?:^|\/)AndroidManifest\.xml$/.test(x) && !/\/build\//.test(x)),
240
+ netsec: f.filter((x) => /(?:^|\/)res\/xml\/[\w-]*network_security_config\w*\.xml$/.test(x)),
241
+ strings: f.filter((x) => /(?:^|\/)res\/values[\w-]*\/strings\.xml$/.test(x)),
242
+ plists: f.filter((x) => /(?:^|\/)Info\.plist$|\.entitlements$/.test(x)),
243
+ };
244
+ }
245
+ const gradleTarget = (repo) => {
246
+ for (const f of repo.ctx.files.filter((x) => /(?:^|\/)build\.gradle(?:\.kts)?$/.test(x))) {
247
+ const m = /\btargetSdk(?:Version)?\s*[= ]\s*(\d+)/.exec(repo.ctx.text(f));
248
+ if (m)
249
+ return Number(m[1]);
250
+ }
251
+ return null;
252
+ };
253
+ export function mobileFacts(repo) {
254
+ const files = mobileFiles(repo);
255
+ const secrets = [];
256
+ let android = null;
257
+ if (files.manifests.length || files.netsec.length) {
258
+ const target = gradleTarget(repo);
259
+ android = { manifests: [], network_security: files.netsec.map((f) => readNetworkSecurity(f, repo.ctx.text(f))) };
260
+ for (const f of files.manifests) {
261
+ const r = readManifest(f, repo.ctx.text(f), target);
262
+ android.manifests.push(r.manifest);
263
+ secrets.push(...r.secrets);
264
+ }
265
+ for (const f of files.strings) {
266
+ const t = repo.ctx.text(f);
267
+ const lines = new Lines(t);
268
+ for (const m of blankComments(t).matchAll(/<string\s+name="([^"]+)"[^>]*>([^<]*)<\/string>/g))
269
+ if (isSecretValue(m[1], m[2]))
270
+ secrets.push({ file: f, line: lines.at(m.index), key: m[1], where: 'android strings' });
271
+ }
272
+ }
273
+ let ios = null;
274
+ const plists = files.plists.map((f) => ({ f, r: readPlistFile(f, repo.ctx.text(f)) })).filter((x) => x.r !== null);
275
+ if (plists.length) {
276
+ ios = { plists: plists.map((x) => x.r.plist) };
277
+ for (const x of plists)
278
+ secrets.push(...x.r.secrets);
279
+ }
280
+ const sorted = secrets.sort((a, b) => (a.file < b.file ? -1 : a.file > b.file ? 1 : 0) || a.line - b.line || (a.key < b.key ? -1 : 1));
281
+ return { android, ios, secrets: sorted };
282
+ }
283
+ // ---- entry points -----------------------------------------------------------------------------------------------------------------
284
+ export function extractMobile(repo) {
285
+ const out = empty();
286
+ const files = mobileFiles(repo);
287
+ const target = gradleTarget(repo);
288
+ for (const f of files.manifests) {
289
+ const text = repo.ctx.text(f);
290
+ const v = view(f, text);
291
+ const { manifest, bounds } = readManifest(f, text, target);
292
+ for (const c of manifest.components) {
293
+ if (!c.exported || c.launcher)
294
+ continue;
295
+ const [s, e] = bounds.get(c) ?? [0, 0];
296
+ const prot = c.permission ? [{ kind: 'role_check', scope: 'handler', file: f, line: c.line, via: `android:permission ${c.permission}`, enforced: true, confidence: 'high' }] : [];
297
+ const analysis = { protections: prot, reads: [], data: [], outbound: [], taint: { names: new Set(), validated: new Set(), trusted: new Set() } };
298
+ const links = c.filters.filter((x) => x.actions.includes('android.intent.action.VIEW') && x.categories.includes('android.intent.category.BROWSABLE') && x.schemes.length);
299
+ if (c.kind === 'activity' && links.length) {
300
+ const names = new Set();
301
+ for (const fl of links)
302
+ for (const sc of fl.schemes)
303
+ for (const h of fl.hosts.length ? fl.hosts : [''])
304
+ for (const p of fl.paths.length ? fl.paths : [''])
305
+ names.add(`${sc}://${h}${p}`);
306
+ for (const n of [...names].sort())
307
+ out.entries.push(makeEntry(repo, { kind: 'deep_link', framework: 'android', file: f, line: c.line, method: null, path: null, name: n, confidence: 'high', handlerView: v, spec: { start: s, end: e, seeds: [] }, analysis, tags: ['public'] }));
308
+ continue;
309
+ }
310
+ out.entries.push(makeEntry(repo, { kind: 'ipc', framework: 'android', file: f, line: c.line, method: null, path: null, name: c.name, confidence: c.exported_explicit ? 'high' : 'medium', handlerView: v, spec: { start: s, end: e, seeds: [] }, analysis }));
311
+ }
312
+ }
313
+ for (const f of files.plists) {
314
+ const text = repo.ctx.text(f);
315
+ const r = readPlistFile(f, text);
316
+ if (!r)
317
+ continue;
318
+ const v = view(f, text);
319
+ const analysis = { protections: [], reads: [], data: [], outbound: [], taint: { names: new Set(), validated: new Set(), trusted: new Set() } };
320
+ for (const u of r.urlSchemes)
321
+ out.entries.push(makeEntry(repo, { kind: 'deep_link', framework: 'ios', file: f, line: u.line, method: null, path: null, name: `${u.scheme}://`, confidence: 'high', handlerView: v, spec: { start: 0, end: 0, seeds: [] }, analysis, tags: ['public'] }));
322
+ for (const d of r.domains)
323
+ out.entries.push(makeEntry(repo, { kind: 'deep_link', framework: 'ios', file: f, line: d.line, method: null, path: null, name: `https://${d.domain}/`, confidence: 'high', handlerView: v, spec: { start: 0, end: 0, seeds: [] }, analysis, tags: ['public'] }));
324
+ }
325
+ return out;
326
+ }
@@ -0,0 +1,281 @@
1
+ import { empty, joinPath, makeEntry, normPath } from './entry.js';
2
+ import { closeOf, pyBlock, splitArgs } from './lex.js';
3
+ import { classDecos, decoratorGroups, pyParams, pyClassMethods, resolvePyModule } from './py.js';
4
+ const GENERIC = {
5
+ ListAPIView: ['GET'], CreateAPIView: ['POST'], RetrieveAPIView: ['GET'], UpdateAPIView: ['PUT', 'PATCH'], DestroyAPIView: ['DELETE'],
6
+ ListCreateAPIView: ['GET', 'POST'], RetrieveUpdateAPIView: ['GET', 'PUT', 'PATCH'], RetrieveDestroyAPIView: ['GET', 'DELETE'],
7
+ RetrieveUpdateDestroyAPIView: ['GET', 'PUT', 'PATCH', 'DELETE'], ModelViewSet: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
8
+ ReadOnlyModelViewSet: ['GET'], ListModelMixin: ['GET'], CreateModelMixin: ['POST'], DestroyModelMixin: ['DELETE'],
9
+ };
10
+ const routeText = (t) => /^\s*[rRuU]?(['"])((?:\\.|(?!\1).)*)\1/.exec(t)?.[2] ?? null;
11
+ const cleanRoute = (r) => normPath(r.replace(/^\^/, '').replace(/\$$/, '').replace(/\(\?P<(\w+)>[^)]*\)/g, ':$1').replace(/\\\//g, '/'));
12
+ function parseUrlConf(repo, v) {
13
+ if (!/\burlpatterns\b/.test(v.masked))
14
+ return null;
15
+ const conf = { file: v.file, view: v, patterns: [], routers: new Map() };
16
+ // Lists of patterns defined in this file (api_urls = [...]): a pattern belongs to the innermost list that holds it.
17
+ const lists = [];
18
+ for (const l of v.masked.matchAll(/^(\w+)\s*(?::[^=\n]+)?=\s*\[/gm)) {
19
+ const open = l.index + l[0].length - 1;
20
+ const close = closeOf(v.masked, open);
21
+ if (close > 0)
22
+ lists.push({ name: l[1], from: open, to: close });
23
+ }
24
+ for (const m of v.masked.matchAll(/(?<![\w.])(path|re_path|url)\s*\(/g)) {
25
+ const open = m.index + m[0].length - 1;
26
+ const args = splitArgs(v.masked, open);
27
+ if (args.length < 2)
28
+ continue;
29
+ const route = routeText(v.text.slice(args[0].start, args[0].end));
30
+ if (route === null)
31
+ continue;
32
+ const viewText = v.text.slice(args[1].start, args[1].end);
33
+ const inc = /^include\s*\(\s*([\s\S]*)\)\s*$/.exec(viewText);
34
+ let includeFile = null;
35
+ let routerName = null;
36
+ let includeList = null;
37
+ if (inc) {
38
+ const lit = /^\(?\s*['"]([\w.]+)['"]/.exec(inc[1])?.[1];
39
+ const rn = /^(\w+)\.urls/.exec(inc[1])?.[1];
40
+ const own = /^\(?\s*(\w+)\s*\)?\s*$/.exec(inc[1])?.[1];
41
+ if (own && lists.some((x) => x.name === own))
42
+ includeList = own;
43
+ else if (lit)
44
+ includeFile = resolvePyModule(repo, v.file, lit);
45
+ else if (rn)
46
+ routerName = rn;
47
+ else {
48
+ const id = /^\(?\s*(\w+)/.exec(inc[1])?.[1];
49
+ if (id) {
50
+ const imp = [...v.text.matchAll(new RegExp(`from\\s+([\\w.]+)\\s+import\\s+[^\\n]*\\b${id}\\b`, 'g'))][0];
51
+ includeFile = resolvePyModule(repo, v.file, imp ? `${imp[1]}.${id}` : id) ?? (imp ? resolvePyModule(repo, v.file, imp[1]) : null);
52
+ }
53
+ }
54
+ }
55
+ const holder = lists.filter((x) => m.index > x.from && m.index < x.to).sort((a, b) => (a.to - a.from) - (b.to - b.from))[0];
56
+ conf.patterns.push({ route: cleanRoute(route), viewText: inc ? '' : viewText, line: v.lines.at(m.index), includeFile, routerName, list: holder?.name ?? null, includeList });
57
+ }
58
+ for (const m of v.masked.matchAll(/\b(\w+)\.register\s*\(/g)) {
59
+ const open = m.index + m[0].length - 1;
60
+ const args = splitArgs(v.masked, open);
61
+ if (args.length < 2)
62
+ continue;
63
+ const prefix = routeText(v.text.slice(args[0].start, args[0].end));
64
+ const vs = /^[A-Za-z_][\w.]*/.exec(v.text.slice(args[1].start, args[1].end))?.[0];
65
+ if (prefix === null || !vs)
66
+ continue;
67
+ const list = conf.routers.get(m[1]) ?? [];
68
+ list.push({ prefix: cleanRoute(prefix), viewset: vs.split('.').pop(), line: v.lines.at(m.index) });
69
+ conf.routers.set(m[1], list);
70
+ }
71
+ return conf;
72
+ }
73
+ // The function a url pattern names. A dotted reference (views.add) is followed through the import in the url file, a bare name through
74
+ // "from x import name", and otherwise a def of that name in a views module wins over one in a model, an admin class or a command.
75
+ function pickView(repo, urls, expr, fnName) {
76
+ const defs = repo.defs('py', fnName);
77
+ if (defs.length <= 1)
78
+ return defs[0];
79
+ const ref = /^([\w.]+)/.exec(expr)?.[1] ?? fnName;
80
+ const parts = ref.split('.');
81
+ let file = null;
82
+ if (parts.length > 1) {
83
+ const mod = parts[parts.length - 2];
84
+ const imp = new RegExp(`from\\s+([.\\w]+)\\s+import\\s+(?:[^\\n]*,\\s*)?${mod}\\b|import\\s+([.\\w]+)\\s+as\\s+${mod}\\b`).exec(urls.text);
85
+ if (imp)
86
+ file = imp[2] ? resolvePyModule(repo, urls.file, imp[2]) : (resolvePyModule(repo, urls.file, `${imp[1].replace(/\.$/, '')}.${mod}`) ?? resolvePyModule(repo, urls.file, imp[1]));
87
+ }
88
+ else {
89
+ const imp = new RegExp(`from\\s+([.\\w]+)\\s+import\\s+[^\\n]*\\b${fnName}\\b`).exec(urls.text);
90
+ if (imp)
91
+ file = resolvePyModule(repo, urls.file, imp[1]);
92
+ }
93
+ const hit = file ? defs.filter((x) => x.view.file === file) : [];
94
+ if (hit.length)
95
+ return hit[0];
96
+ const dir = urls.file.includes('/') ? urls.file.slice(0, urls.file.lastIndexOf('/') + 1) : '';
97
+ const rank = (f) => (/(?:^|\/)views?(?:\/|\.py$|_)/.test(f) ? 0 : f.startsWith(dir) ? 1 : 2) + (/(?:admin|models?|management|tests?)(?:\/|\.py$)/.test(f) ? 3 : 0);
98
+ return defs.slice().sort((a, b) => rank(a.view.file) - rank(b.view.file))[0];
99
+ }
100
+ function findClass(repo, name, near) {
101
+ const files = [near, ...repo.filesOf('py').filter((f) => f !== near)];
102
+ for (const f of files) {
103
+ const view = repo.view(f);
104
+ if (!view)
105
+ continue;
106
+ const m = new RegExp(`^class\\s+${name}\\b[^\\n]*:`, 'm').exec(view.masked);
107
+ if (m)
108
+ return { view, at: m.index };
109
+ }
110
+ return null;
111
+ }
112
+ export function extractDjango(repo) {
113
+ const out = empty();
114
+ const confs = new Map();
115
+ for (const f of repo.filesOf('py')) {
116
+ if (!/urls?\.py$|urlconf|routing|routes/.test(f) && !/urlpatterns/.test(repo.ctx.text(f)))
117
+ continue;
118
+ const v = repo.view(f);
119
+ const c = v ? parseUrlConf(repo, v) : null;
120
+ if (c)
121
+ confs.set(f, c);
122
+ }
123
+ if (!confs.size)
124
+ return out;
125
+ const included = new Set();
126
+ for (const c of confs.values())
127
+ for (const p of c.patterns)
128
+ if (p.includeFile)
129
+ included.add(p.includeFile);
130
+ const roots = [...confs.keys()].filter((f) => !included.has(f)).sort();
131
+ const emitted = new Set();
132
+ const emitView = (c, prefix, p, viewExpr, route, depth) => {
133
+ void depth;
134
+ const full = joinPath(prefix, route);
135
+ const v = c.view;
136
+ if (/admin\.site\.urls/.test(viewExpr)) {
137
+ const e = makeEntry(repo, { kind: 'http_route', framework: 'django-admin', file: v.file, line: p.line, method: 'ANY', path: joinPath(full, '*'), name: 'django admin', confidence: 'medium', handlerView: v, spec: { start: 0, end: 0, seeds: [], decorators: [], returnsJson: false }, tags: ['admin'] });
138
+ e.protections.push({ kind: 'session_read', scope: 'global', file: v.file, line: p.line, via: 'django admin site login', enforced: true, confidence: 'high' }, { kind: 'role_check', scope: 'global', file: v.file, line: p.line, via: 'django admin is_staff', enforced: true, confidence: 'high' });
139
+ out.entries.push(e);
140
+ return;
141
+ }
142
+ const asView = /^([\w.]+?)\.as_view\s*\(/.exec(viewExpr);
143
+ if (asView) {
144
+ const cls = asView[1].split('.').pop();
145
+ const found = findClass(repo, cls, v.file);
146
+ if (!found)
147
+ return;
148
+ emitClass(repo, found.view, found.at, cls, full, out);
149
+ return;
150
+ }
151
+ const fnName = /^([\w.]+)\s*(?:,|$)/.exec(viewExpr.trim())?.[1]?.split('.').pop();
152
+ if (!fnName)
153
+ return;
154
+ const d = pickView(repo, v, viewExpr.trim(), fnName);
155
+ if (!d)
156
+ return;
157
+ const b = pyBlock(d.view.masked, d.view.lines, d.start);
158
+ const g = decoratorGroups(d.view).find((x) => x.name === fnName && x.kind === 'def');
159
+ const decoText = g ? g.decos.map((x) => d.view.text.slice(x.start, x.end)).join('\n') : '';
160
+ const apiView = /@api_view\s*\(\s*\[([^\]]*)\]/.exec(decoText)?.[1];
161
+ const reqMethods = /@require_http_methods\s*\(\s*[\[(]([^\])]*)[\])]/.exec(decoText)?.[1];
162
+ const single = /@require_(GET|POST|safe)\b/.exec(decoText)?.[1];
163
+ const ms = apiView || reqMethods ? [...(apiView ?? reqMethods).matchAll(/['"](\w+)['"]/g)].map((x) => x[1].toUpperCase()) : single ? (single === 'safe' ? ['GET', 'HEAD'] : [single]) : ['ANY'];
164
+ const deco = g ? g.decos.map((x) => ({ view: d.view, start: x.start, end: x.end, scope: 'router' })) : [];
165
+ for (const method of ms.length ? ms : ['ANY']) {
166
+ out.entries.push(makeEntry(repo, { kind: 'http_route', framework: 'django', file: d.view.file, line: d.view.lines.at(d.start), method, path: full, name: fnName, confidence: 'medium', handlerView: d.view, spec: { start: d.start, end: b.end, seeds: [...pyParams(d.view.masked, d.start), 'request'], decorators: deco, returnsJson: true } }));
167
+ }
168
+ };
169
+ const walk = (file, prefix, seen, list = null) => {
170
+ const c = confs.get(file);
171
+ if (!c || (list === null && seen.includes(file)))
172
+ return;
173
+ // The top level is urlpatterns, or every pattern outside a named helper list; a helper list is read where an include() names it.
174
+ const named = new Set(c.patterns.filter((x) => x.includeList).map((x) => x.includeList));
175
+ for (const p of c.patterns) {
176
+ if (list === null ? (p.list !== null && p.list !== 'urlpatterns' && named.has(p.list)) : p.list !== list)
177
+ continue;
178
+ const joined = joinPath(prefix, p.route);
179
+ if (p.includeList) {
180
+ if (!seen.includes(`${file}#${p.includeList}`))
181
+ walk(file, joined, [...seen, `${file}#${p.includeList}`], p.includeList);
182
+ continue;
183
+ }
184
+ if (p.includeFile) {
185
+ walk(p.includeFile, joined, [...seen, file]);
186
+ continue;
187
+ }
188
+ if (p.routerName) {
189
+ for (const r of c.routers.get(p.routerName) ?? []) {
190
+ const found = findClass(repo, r.viewset, file);
191
+ if (!found)
192
+ continue;
193
+ const base = joinPath(joined, r.prefix);
194
+ emitClass(repo, found.view, found.at, r.viewset, base, out, true);
195
+ }
196
+ continue;
197
+ }
198
+ if (!p.viewText)
199
+ continue;
200
+ const key = `${file}:${p.line}:${prefix}:${p.route}:${list ?? ''}`;
201
+ if (emitted.has(key))
202
+ continue;
203
+ emitted.add(key);
204
+ emitView(c, prefix, p, p.viewText, p.route, seen.length);
205
+ }
206
+ };
207
+ for (const r of roots.length ? roots : [...confs.keys()].sort())
208
+ walk(r, '', []);
209
+ // REST framework settings that apply to every view.
210
+ for (const f of repo.filesOf('py')) {
211
+ if (!/settings/.test(f))
212
+ continue;
213
+ const v = repo.view(f);
214
+ const m = v && /REST_FRAMEWORK\s*=\s*\{/.exec(v.masked);
215
+ if (!v || !m)
216
+ continue;
217
+ const open = v.masked.indexOf('{', m.index);
218
+ const close = closeOf(v.masked, open);
219
+ const body = v.text.slice(open, close);
220
+ const line = v.lines.at(m.index);
221
+ const protections = [];
222
+ if (/DEFAULT_PERMISSION_CLASSES[^\]]*IsAuthenticated(?!OrReadOnly)/.test(body))
223
+ protections.push({ kind: 'session_read', scope: 'global', file: f, line, via: 'DEFAULT_PERMISSION_CLASSES IsAuthenticated', enforced: true, confidence: 'high' });
224
+ if (/DEFAULT_PERMISSION_CLASSES[^\]]*IsAdminUser/.test(body))
225
+ protections.push({ kind: 'role_check', scope: 'global', file: f, line, via: 'DEFAULT_PERMISSION_CLASSES IsAdminUser', enforced: true, confidence: 'high' });
226
+ if (/DEFAULT_THROTTLE_CLASSES/.test(body))
227
+ protections.push({ kind: 'rate_limit', scope: 'global', file: f, line, via: 'DEFAULT_THROTTLE_CLASSES', enforced: true, confidence: 'high' });
228
+ if (protections.length)
229
+ out.gates.push({ id: `${f}:${line}:REST_FRAMEWORK`, kind: 'guard', framework: 'django', file: f, line, paths: ['*'], methods: [], protections, confidence: 'high' });
230
+ }
231
+ return out;
232
+ }
233
+ function emitClass(repo, view, at, cls, full, out, viewset = false) {
234
+ const b = pyBlock(view.masked, view.lines, at);
235
+ const header = view.masked.slice(at, b.bodyStart);
236
+ const bases = (/\(([^)]*)\)/.exec(header)?.[1] ?? '').split(',').map((x) => x.trim().split('.').pop()).filter(Boolean);
237
+ const decos = classDecos(view, cls);
238
+ const explicit = pyClassMethods(repo, view, cls);
239
+ const implied = bases.flatMap((x) => GENERIC[x] ?? []);
240
+ const seen = new Set();
241
+ const detail = joinPath(full, ':pk');
242
+ for (const cm of explicit) {
243
+ const path = viewset && /^(GET|PUT|PATCH|DELETE)$/.test(cm.method) && !/list/i.test(cm.method) ? full : full;
244
+ out.entries.push(makeEntry(repo, { kind: 'http_route', framework: 'django', file: cm.view.file, line: cm.view.lines.at(cm.start), method: cm.method, path, name: `${cls}.${cm.method.toLowerCase()}`, confidence: 'medium', handlerView: cm.view, spec: { start: cm.start, end: cm.end, seeds: [...pyParams(cm.view.masked, cm.start), 'request'], decorators: decos, returnsJson: true } }));
245
+ seen.add(cm.method);
246
+ }
247
+ // ViewSet actions (list, create, retrieve, update, partial_update, destroy) defined as methods.
248
+ if (viewset) {
249
+ const actions = [['list', 'GET', false], ['create', 'POST', false], ['retrieve', 'GET', true], ['update', 'PUT', true], ['partial_update', 'PATCH', true], ['destroy', 'DELETE', true]];
250
+ for (const x of view.masked.slice(b.bodyStart, b.end).matchAll(/^[ \t]+(?:async\s+)?def\s+(list|create|retrieve|update|partial_update|destroy)\s*\(/gm)) {
251
+ const a = actions.find((y) => y[0] === x[1]);
252
+ const ls = view.masked.lastIndexOf('\n', b.bodyStart + x.index + 1) + 1;
253
+ const mb = pyBlock(view.masked, view.lines, ls);
254
+ out.entries.push(makeEntry(repo, { kind: 'http_route', framework: 'django', file: view.file, line: view.lines.at(ls), method: a[1], path: a[2] ? detail : full, name: `${cls}.${x[1]}`, confidence: 'medium', handlerView: view, spec: { start: ls, end: mb.end, seeds: [...pyParams(view.masked, ls), 'request'], decorators: decos, returnsJson: true } }));
255
+ seen.add(`${a[1]}:${a[2]}`);
256
+ }
257
+ for (const x of view.masked.slice(b.bodyStart, b.end).matchAll(/@action\s*\(([^)]*)\)\s*\n[ \t]*(?:async\s+)?def\s+(\w+)\s*\(/g)) {
258
+ const ms = /methods\s*=\s*[\[(]([^\])]*)[\])]/.exec(view.text.slice(b.bodyStart + x.index, b.bodyStart + x.index + x[0].length))?.[1];
259
+ const methods = ms ? [...ms.matchAll(/['"](\w+)['"]/g)].map((y) => y[1].toUpperCase()) : ['GET'];
260
+ const ls = view.masked.lastIndexOf('\n', b.bodyStart + x.index + x[0].length - 2) + 1;
261
+ const mb = pyBlock(view.masked, view.lines, ls);
262
+ const det = /detail\s*=\s*True/.test(view.text.slice(b.bodyStart + x.index, b.bodyStart + x.index + x[0].length));
263
+ for (const method of methods)
264
+ out.entries.push(makeEntry(repo, { kind: 'http_route', framework: 'django', file: view.file, line: view.lines.at(ls), method, path: joinPath(det ? detail : full, x[2]), name: `${cls}.${x[2]}`, confidence: 'medium', handlerView: view, spec: { start: ls, end: mb.end, seeds: [...pyParams(view.masked, ls), 'request'], decorators: decos, returnsJson: true } }));
265
+ }
266
+ }
267
+ // Methods implied by a generic base class (ListCreateAPIView, ModelViewSet ...) that the class does not override.
268
+ if (viewset && bases.some((x) => GENERIC[x])) {
269
+ const have = (m, d) => seen.has(`${m}:${d}`);
270
+ const all = bases.includes('ReadOnlyModelViewSet') ? [['GET', false], ['GET', true]] : [['GET', false], ['POST', false], ['GET', true], ['PUT', true], ['PATCH', true], ['DELETE', true]];
271
+ const action = (method, det) => (method === 'GET' ? (det ? 'retrieve' : 'list') : method === 'POST' ? 'create' : method === 'PUT' ? 'update' : method === 'PATCH' ? 'partial_update' : 'destroy');
272
+ for (const [method, det] of all)
273
+ if (!have(method, det))
274
+ out.entries.push(makeEntry(repo, { kind: 'http_route', framework: 'django', file: view.file, line: view.lines.at(at), method, path: det ? detail : full, name: `${cls}.${action(method, det)}`, confidence: 'medium', handlerView: view, spec: { start: at, end: b.end, seeds: ['request'], decorators: decos, returnsJson: true } }));
275
+ }
276
+ else if (!viewset) {
277
+ for (const method of implied)
278
+ if (!seen.has(method))
279
+ out.entries.push(makeEntry(repo, { kind: 'http_route', framework: 'django', file: view.file, line: view.lines.at(at), method, path: full, name: `${cls}.${method.toLowerCase()}`, confidence: 'medium', handlerView: view, spec: { start: at, end: b.end, seeds: ['request'], decorators: decos, returnsJson: true } }));
280
+ }
281
+ }