@openai/codex-security 0.2.0 → 0.4.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 (484) hide show
  1. package/README.md +437 -2692
  2. package/_bundled_plugin/.app.json +5 -1
  3. package/_bundled_plugin/.codex-plugin/plugin.json +4 -0
  4. package/_bundled_plugin/.mcp.json +2 -0
  5. package/_bundled_plugin/examples/completed-scan/coverage.json +0 -1
  6. package/_bundled_plugin/examples/completed-scan/report.md +28 -26
  7. package/_bundled_plugin/examples/completed-scan/scan-manifest.json +1 -1
  8. package/_bundled_plugin/mcp/helpers.mjs +1 -0
  9. package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
  10. package/_bundled_plugin/mcp/native/THIRD_PARTY_NOTICES.txt +6 -213
  11. package/_bundled_plugin/mcp/native/darwin-arm64/unix.node +0 -0
  12. package/_bundled_plugin/mcp/native/darwin-x64/unix.node +0 -0
  13. package/_bundled_plugin/mcp/native/linux-arm64-gnu/unix.node +0 -0
  14. package/_bundled_plugin/mcp/native/linux-arm64-musl/unix.node +0 -0
  15. package/_bundled_plugin/mcp/native/linux-x64-gnu/unix.node +0 -0
  16. package/_bundled_plugin/mcp/native/linux-x64-musl/unix.node +0 -0
  17. package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
  18. package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
  19. package/_bundled_plugin/mcp/permission-profile-preflight.mjs +556 -0
  20. package/_bundled_plugin/mcp/server.mjs +1 -0
  21. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  22. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  23. package/_bundled_plugin/preflight/capability-profiles.toml +0 -13
  24. package/_bundled_plugin/references/artifact-storage.md +2 -2
  25. package/_bundled_plugin/references/config-preflight.md +0 -4
  26. package/_bundled_plugin/references/core-scan.md +21 -9
  27. package/_bundled_plugin/references/final-report.md +11 -13
  28. package/_bundled_plugin/references/finding-detail-fields.md +90 -80
  29. package/_bundled_plugin/references/sarif-adapter.md +1 -1
  30. package/_bundled_plugin/references/scan-artifacts.md +1 -2
  31. package/_bundled_plugin/references/scan-contract.md +9 -4
  32. package/_bundled_plugin/references/scan-prologue.md +12 -0
  33. package/_bundled_plugin/references/security-guidance.md +2 -2
  34. package/_bundled_plugin/references/standalone-validation.md +11 -0
  35. package/_bundled_plugin/references/threat-model.md +1 -1
  36. package/_bundled_plugin/schemas/coverage.schema.json +43 -170
  37. package/_bundled_plugin/schemas/definitions/artifact-common.schema.json +2 -24
  38. package/_bundled_plugin/schemas/definitions/discovery-candidate.schema.json +49 -25
  39. package/_bundled_plugin/schemas/findings.schema.json +159 -567
  40. package/_bundled_plugin/schemas/patch-risk-assessment.schema.json +106 -119
  41. package/_bundled_plugin/schemas/scan-manifest.schema.json +78 -220
  42. package/_bundled_plugin/schemas/tools/candidate-attack-paths.schema.json +91 -83
  43. package/_bundled_plugin/schemas/tools/candidate-validations.schema.json +35 -64
  44. package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +5 -2
  45. package/_bundled_plugin/schemas/tools/discovery-candidates.schema.json +6 -55
  46. package/_bundled_plugin/schemas/tools/review-items.schema.json +2 -38
  47. package/_bundled_plugin/schemas/tools/scan-draft.schema.json +169 -294
  48. package/_bundled_plugin/scripts/codex_profile.mjs +239 -0
  49. package/_bundled_plugin/scripts/codex_session.mjs +53 -0
  50. package/_bundled_plugin/scripts/config_preflight.py +26 -62
  51. package/_bundled_plugin/scripts/deep_scan_config.py +9 -11
  52. package/_bundled_plugin/scripts/deep_scan_workbench.py +129 -150
  53. package/_bundled_plugin/scripts/finalize_scan_contract.py +321 -237
  54. package/_bundled_plugin/scripts/finding_preview.py +39 -9
  55. package/_bundled_plugin/scripts/generate_in_scope_files.py +27 -77
  56. package/_bundled_plugin/scripts/generate_rank_input.py +115 -926
  57. package/_bundled_plugin/scripts/launch_codex_security_mcp +35 -10
  58. package/_bundled_plugin/scripts/launch_codex_security_mcp.cmd +5 -5
  59. package/_bundled_plugin/scripts/rank_preview.py +42 -918
  60. package/_bundled_plugin/scripts/report_projection.py +150 -183
  61. package/_bundled_plugin/scripts/snapshot_sqlite.py +6 -1
  62. package/_bundled_plugin/scripts/threat_model_projection.py +4 -7
  63. package/_bundled_plugin/scripts/validate_scan_contract.py +4 -34
  64. package/_bundled_plugin/scripts/validate_tracking_source.py +3 -13
  65. package/_bundled_plugin/scripts/windows_scan_local_files.py +48 -3
  66. package/_bundled_plugin/scripts/workbench/handoff.py +25 -50
  67. package/_bundled_plugin/scripts/workbench/runtime_toml.py +22 -0
  68. package/_bundled_plugin/scripts/workbench/storage.py +10 -6
  69. package/_bundled_plugin/scripts/workbench_cli.py +102 -184
  70. package/_bundled_plugin/scripts/workbench_db.py +463 -464
  71. package/_bundled_plugin/scripts/workbench_feedback.py +6 -3
  72. package/_bundled_plugin/scripts/workbench_finding_index.py +19 -1
  73. package/_bundled_plugin/scripts/workbench_finding_workflows.py +18 -4
  74. package/_bundled_plugin/scripts/workbench_local_dedupe.py +136 -0
  75. package/_bundled_plugin/scripts/workbench_native_indexes.py +27 -14
  76. package/_bundled_plugin/scripts/workbench_progress.py +14 -58
  77. package/_bundled_plugin/scripts/workbench_publication.py +234 -289
  78. package/_bundled_plugin/scripts/workbench_remediation.py +12 -0
  79. package/_bundled_plugin/scripts/workbench_saved_results.py +172 -49
  80. package/_bundled_plugin/scripts/workbench_scan_history.py +57 -57
  81. package/_bundled_plugin/scripts/workbench_scan_start.py +105 -41
  82. package/_bundled_plugin/scripts/workbench_scan_usage.py +194 -42
  83. package/_bundled_plugin/scripts/workbench_schema.py +37 -1151
  84. package/_bundled_plugin/scripts/workbench_source_excerpt.py +11 -1
  85. package/_bundled_plugin/scripts/workbench_target.py +430 -142
  86. package/_bundled_plugin/scripts/workbench_validation.py +31 -28
  87. package/_bundled_plugin/shared/workbench-history-repairs.json +218 -0
  88. package/_bundled_plugin/shared/workbench-migrations.json +414 -0
  89. package/_bundled_plugin/skills/assess-patch-risk/SKILL.md +15 -4
  90. package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +1 -0
  91. package/_bundled_plugin/skills/cloud-findings/SKILL.md +54 -0
  92. package/_bundled_plugin/skills/cloud-findings/agents/openai.yaml +4 -0
  93. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +3 -3
  94. package/_bundled_plugin/skills/finding-discovery/SKILL.md +3 -3
  95. package/_bundled_plugin/skills/propose-security-hardening/references/proposal-format.md +50 -20
  96. package/_bundled_plugin/skills/security-diff-scan/SKILL.md +8 -14
  97. package/_bundled_plugin/skills/security-scan/SKILL.md +22 -7
  98. package/_bundled_plugin/skills/security-scan/references/desktop-scan.md +2 -2
  99. package/_bundled_plugin/skills/security-scan/references/scan-artifacts-and-ledger.md +15 -1
  100. package/_bundled_plugin/skills/threat-model/SKILL.md +1 -1
  101. package/_bundled_plugin/skills/track-findings/SKILL.md +48 -176
  102. package/_bundled_plugin/skills/track-findings/references/github-security-advisories.md +16 -24
  103. package/_bundled_plugin/skills/track-findings/references/jira.md +15 -61
  104. package/_bundled_plugin/skills/triage-finding/SKILL.md +50 -289
  105. package/_bundled_plugin/skills/triage-finding/agents/openai.yaml +2 -2
  106. package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +23 -43
  107. package/_bundled_plugin/skills/triage-finding/references/triage-result-contract.md +7 -1
  108. package/bin/codex-security.mjs +9 -2
  109. package/dist/api.d.ts +63 -10
  110. package/dist/api.d.ts.map +1 -1
  111. package/dist/api.js +1015 -383
  112. package/dist/api.js.map +1 -1
  113. package/dist/artifact-export.d.ts +1 -1
  114. package/dist/artifact-export.d.ts.map +1 -1
  115. package/dist/artifact-export.js +28 -13
  116. package/dist/artifact-export.js.map +1 -1
  117. package/dist/auth.d.ts +2 -6
  118. package/dist/auth.d.ts.map +1 -1
  119. package/dist/auth.js +19 -9
  120. package/dist/auth.js.map +1 -1
  121. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  122. package/dist/bulk-scan-discovery.js +1 -0
  123. package/dist/bulk-scan-discovery.js.map +1 -1
  124. package/dist/classify-severity.d.ts +26 -9
  125. package/dist/classify-severity.d.ts.map +1 -1
  126. package/dist/classify-severity.js +13 -30
  127. package/dist/classify-severity.js.map +1 -1
  128. package/dist/cli-help.d.ts.map +1 -1
  129. package/dist/cli-help.js +17 -13
  130. package/dist/cli-help.js.map +1 -1
  131. package/dist/cli-signals.d.ts +8 -0
  132. package/dist/cli-signals.d.ts.map +1 -0
  133. package/dist/cli-signals.js +28 -0
  134. package/dist/cli-signals.js.map +1 -0
  135. package/dist/cli.d.ts +4 -3
  136. package/dist/cli.d.ts.map +1 -1
  137. package/dist/cli.js +1102 -905
  138. package/dist/cli.js.map +1 -1
  139. package/dist/cloud-publish.d.ts.map +1 -1
  140. package/dist/cloud-publish.js +10 -9
  141. package/dist/cloud-publish.js.map +1 -1
  142. package/dist/codeowners.d.ts +19 -0
  143. package/dist/codeowners.d.ts.map +1 -0
  144. package/dist/codeowners.js +138 -0
  145. package/dist/codeowners.js.map +1 -0
  146. package/dist/codex-sdk-environment.d.ts +1 -0
  147. package/dist/codex-sdk-environment.d.ts.map +1 -1
  148. package/dist/codex-sdk-environment.js +40 -2
  149. package/dist/codex-sdk-environment.js.map +1 -1
  150. package/dist/component-plan.d.ts +2 -5
  151. package/dist/component-plan.d.ts.map +1 -1
  152. package/dist/component-plan.js +39 -31
  153. package/dist/component-plan.js.map +1 -1
  154. package/dist/component-scan.d.ts +6 -5
  155. package/dist/component-scan.d.ts.map +1 -1
  156. package/dist/component-scan.js +25 -36
  157. package/dist/component-scan.js.map +1 -1
  158. package/dist/config.d.ts.map +1 -1
  159. package/dist/config.js +64 -74
  160. package/dist/config.js.map +1 -1
  161. package/dist/contract.d.ts +16 -3
  162. package/dist/contract.d.ts.map +1 -1
  163. package/dist/contract.js +91 -255
  164. package/dist/contract.js.map +1 -1
  165. package/dist/cost-model.d.ts +13 -6
  166. package/dist/cost-model.d.ts.map +1 -1
  167. package/dist/cost-model.js +14 -4
  168. package/dist/cost-model.js.map +1 -1
  169. package/dist/cost.d.ts +10 -2
  170. package/dist/cost.d.ts.map +1 -1
  171. package/dist/cost.js +102 -104
  172. package/dist/cost.js.map +1 -1
  173. package/dist/custom-validation-prompt.d.ts.map +1 -1
  174. package/dist/custom-validation-prompt.js +6 -5
  175. package/dist/custom-validation-prompt.js.map +1 -1
  176. package/dist/custom-validation.d.ts +1 -2
  177. package/dist/custom-validation.d.ts.map +1 -1
  178. package/dist/custom-validation.js +9 -3
  179. package/dist/custom-validation.js.map +1 -1
  180. package/dist/deduplication/checkpointed-review.d.ts +4 -2
  181. package/dist/deduplication/checkpointed-review.d.ts.map +1 -1
  182. package/dist/deduplication/checkpointed-review.js +23 -9
  183. package/dist/deduplication/checkpointed-review.js.map +1 -1
  184. package/dist/deduplication/codex-review.d.ts +8 -6
  185. package/dist/deduplication/codex-review.d.ts.map +1 -1
  186. package/dist/deduplication/codex-review.js +128 -19
  187. package/dist/deduplication/codex-review.js.map +1 -1
  188. package/dist/deduplication/deduplication-prompts.d.ts +5 -2
  189. package/dist/deduplication/deduplication-prompts.d.ts.map +1 -1
  190. package/dist/deduplication/deduplication-prompts.js +18 -4
  191. package/dist/deduplication/deduplication-prompts.js.map +1 -1
  192. package/dist/deduplication/deduplication-reviewer.d.ts +27 -5
  193. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
  194. package/dist/deduplication/deduplication-reviewer.js +58 -48
  195. package/dist/deduplication/deduplication-reviewer.js.map +1 -1
  196. package/dist/deduplication/deduplication.d.ts.map +1 -1
  197. package/dist/deduplication/deduplication.js +19 -51
  198. package/dist/deduplication/deduplication.js.map +1 -1
  199. package/dist/deduplication/diagnostics.d.ts +17 -0
  200. package/dist/deduplication/diagnostics.d.ts.map +1 -0
  201. package/dist/deduplication/diagnostics.js +5 -0
  202. package/dist/deduplication/diagnostics.js.map +1 -0
  203. package/dist/deduplication/finding-schema.d.ts.map +1 -1
  204. package/dist/deduplication/finding-schema.js +7 -1
  205. package/dist/deduplication/finding-schema.js.map +1 -1
  206. package/dist/deduplication/local.d.ts +31 -0
  207. package/dist/deduplication/local.d.ts.map +1 -0
  208. package/dist/deduplication/local.js +166 -0
  209. package/dist/deduplication/local.js.map +1 -0
  210. package/dist/deduplication/records-protocol.d.ts +3 -8
  211. package/dist/deduplication/records-protocol.d.ts.map +1 -1
  212. package/dist/deduplication/records-protocol.js +11 -184
  213. package/dist/deduplication/records-protocol.js.map +1 -1
  214. package/dist/deduplication/records.d.ts +186 -11
  215. package/dist/deduplication/records.d.ts.map +1 -1
  216. package/dist/deduplication/records.js +10 -18
  217. package/dist/deduplication/records.js.map +1 -1
  218. package/dist/deduplication/scan.d.ts +9 -3
  219. package/dist/deduplication/scan.d.ts.map +1 -1
  220. package/dist/deduplication/scan.js +158 -39
  221. package/dist/deduplication/scan.js.map +1 -1
  222. package/dist/deep-config.d.ts +2 -1
  223. package/dist/deep-config.d.ts.map +1 -1
  224. package/dist/deep-config.js +4 -4
  225. package/dist/deep-config.js.map +1 -1
  226. package/dist/deep-progress.d.ts +17 -1
  227. package/dist/deep-progress.d.ts.map +1 -1
  228. package/dist/deep-progress.js +19 -17
  229. package/dist/deep-progress.js.map +1 -1
  230. package/dist/deep-scan.d.ts +20 -0
  231. package/dist/deep-scan.d.ts.map +1 -0
  232. package/dist/deep-scan.js +113 -0
  233. package/dist/deep-scan.js.map +1 -0
  234. package/dist/errors.d.ts +2 -0
  235. package/dist/errors.d.ts.map +1 -1
  236. package/dist/errors.js +2 -0
  237. package/dist/errors.js.map +1 -1
  238. package/dist/executable-path.d.ts +2 -0
  239. package/dist/executable-path.d.ts.map +1 -0
  240. package/dist/executable-path.js +12 -0
  241. package/dist/executable-path.js.map +1 -0
  242. package/dist/feedback.d.ts +1 -4
  243. package/dist/feedback.d.ts.map +1 -1
  244. package/dist/feedback.js +54 -22
  245. package/dist/feedback.js.map +1 -1
  246. package/dist/finding-catalogue.d.ts.map +1 -1
  247. package/dist/finding-catalogue.js +4 -8
  248. package/dist/finding-catalogue.js.map +1 -1
  249. package/dist/finding-normalization.d.ts +5 -0
  250. package/dist/finding-normalization.d.ts.map +1 -0
  251. package/dist/finding-normalization.js +120 -0
  252. package/dist/finding-normalization.js.map +1 -0
  253. package/dist/finding-retrieval.d.ts +9 -0
  254. package/dist/finding-retrieval.d.ts.map +1 -1
  255. package/dist/finding-retrieval.js.map +1 -1
  256. package/dist/finding-workflow.d.ts +13 -8
  257. package/dist/finding-workflow.d.ts.map +1 -1
  258. package/dist/finding-workflow.js +14 -6
  259. package/dist/finding-workflow.js.map +1 -1
  260. package/dist/findings-client.d.ts +2 -1
  261. package/dist/findings-client.d.ts.map +1 -1
  262. package/dist/findings-client.js +36 -9
  263. package/dist/findings-client.js.map +1 -1
  264. package/dist/findings-errors.d.ts +16 -0
  265. package/dist/findings-errors.d.ts.map +1 -0
  266. package/dist/findings-errors.js +24 -0
  267. package/dist/findings-errors.js.map +1 -0
  268. package/dist/findings-import.d.ts +1 -1
  269. package/dist/findings-import.d.ts.map +1 -1
  270. package/dist/findings-import.js +12 -30
  271. package/dist/findings-import.js.map +1 -1
  272. package/dist/github.d.ts.map +1 -1
  273. package/dist/github.js +7 -1
  274. package/dist/github.js.map +1 -1
  275. package/dist/host-protocol.d.ts +19 -0
  276. package/dist/host-protocol.d.ts.map +1 -0
  277. package/dist/host-protocol.js +270 -0
  278. package/dist/host-protocol.js.map +1 -0
  279. package/dist/hosted-scan-protocol.d.ts +4 -0
  280. package/dist/hosted-scan-protocol.d.ts.map +1 -0
  281. package/dist/hosted-scan-protocol.js +25 -0
  282. package/dist/hosted-scan-protocol.js.map +1 -0
  283. package/dist/hosted-scan.d.ts +117 -0
  284. package/dist/hosted-scan.d.ts.map +1 -0
  285. package/dist/hosted-scan.js +260 -0
  286. package/dist/hosted-scan.js.map +1 -0
  287. package/dist/import-scan.d.ts.map +1 -1
  288. package/dist/import-scan.js +48 -38
  289. package/dist/import-scan.js.map +1 -1
  290. package/dist/index.d.ts +11 -4
  291. package/dist/index.d.ts.map +1 -1
  292. package/dist/index.js +2 -1
  293. package/dist/index.js.map +1 -1
  294. package/dist/knowledge-base.d.ts +7 -1
  295. package/dist/knowledge-base.d.ts.map +1 -1
  296. package/dist/knowledge-base.js +114 -46
  297. package/dist/knowledge-base.js.map +1 -1
  298. package/dist/linear.d.ts.map +1 -1
  299. package/dist/linear.js +2 -7
  300. package/dist/linear.js.map +1 -1
  301. package/dist/mock-scan.d.ts.map +1 -1
  302. package/dist/mock-scan.js +0 -1
  303. package/dist/mock-scan.js.map +1 -1
  304. package/dist/models.d.ts +0 -2
  305. package/dist/models.d.ts.map +1 -1
  306. package/dist/models.js.map +1 -1
  307. package/dist/multiscan.d.ts +9 -3
  308. package/dist/multiscan.d.ts.map +1 -1
  309. package/dist/multiscan.js +240 -135
  310. package/dist/multiscan.js.map +1 -1
  311. package/dist/owner-evidence.d.ts +10 -2
  312. package/dist/owner-evidence.d.ts.map +1 -1
  313. package/dist/owner-evidence.js +67 -8
  314. package/dist/owner-evidence.js.map +1 -1
  315. package/dist/patch-tui.d.ts.map +1 -1
  316. package/dist/patch-tui.js +3 -2
  317. package/dist/patch-tui.js.map +1 -1
  318. package/dist/project-config.js +3 -3
  319. package/dist/project-config.js.map +1 -1
  320. package/dist/provider-profile.d.ts +21 -0
  321. package/dist/provider-profile.d.ts.map +1 -0
  322. package/dist/provider-profile.js +133 -0
  323. package/dist/provider-profile.js.map +1 -0
  324. package/dist/publication-events.d.ts +6 -8
  325. package/dist/publication-events.d.ts.map +1 -1
  326. package/dist/publication-events.js +25 -50
  327. package/dist/publication-events.js.map +1 -1
  328. package/dist/publication-store.d.ts.map +1 -1
  329. package/dist/publication-store.js +95 -109
  330. package/dist/publication-store.js.map +1 -1
  331. package/dist/publication.d.ts +1 -4
  332. package/dist/publication.d.ts.map +1 -1
  333. package/dist/publication.js +2 -13
  334. package/dist/publication.js.map +1 -1
  335. package/dist/publish.d.ts +1 -5
  336. package/dist/publish.d.ts.map +1 -1
  337. package/dist/publish.js +109 -160
  338. package/dist/publish.js.map +1 -1
  339. package/dist/request-metadata.d.ts +9 -0
  340. package/dist/request-metadata.d.ts.map +1 -0
  341. package/dist/request-metadata.js +13 -0
  342. package/dist/request-metadata.js.map +1 -0
  343. package/dist/runtime.d.ts +20 -6
  344. package/dist/runtime.d.ts.map +1 -1
  345. package/dist/runtime.js +407 -148
  346. package/dist/runtime.js.map +1 -1
  347. package/dist/saved-scan-bootstrap.d.ts +14 -0
  348. package/dist/saved-scan-bootstrap.d.ts.map +1 -0
  349. package/dist/saved-scan-bootstrap.js +261 -0
  350. package/dist/saved-scan-bootstrap.js.map +1 -0
  351. package/dist/saved-scan.d.ts +1 -1
  352. package/dist/saved-scan.d.ts.map +1 -1
  353. package/dist/saved-scan.js +6 -12
  354. package/dist/saved-scan.js.map +1 -1
  355. package/dist/scan-activity.js +11 -8
  356. package/dist/scan-activity.js.map +1 -1
  357. package/dist/scan-comparison.d.ts +28 -15
  358. package/dist/scan-comparison.d.ts.map +1 -1
  359. package/dist/scan-comparison.js +276 -281
  360. package/dist/scan-comparison.js.map +1 -1
  361. package/dist/scan-dashboard.d.ts +3 -0
  362. package/dist/scan-dashboard.d.ts.map +1 -1
  363. package/dist/scan-dashboard.js +345 -77
  364. package/dist/scan-dashboard.js.map +1 -1
  365. package/dist/scan-executor.d.ts +104 -0
  366. package/dist/scan-executor.d.ts.map +1 -0
  367. package/dist/scan-executor.js +62 -0
  368. package/dist/scan-executor.js.map +1 -0
  369. package/dist/scan-history-renderer.d.ts.map +1 -1
  370. package/dist/scan-history-renderer.js +54 -13
  371. package/dist/scan-history-renderer.js.map +1 -1
  372. package/dist/scan-inputs.d.ts +6 -0
  373. package/dist/scan-inputs.d.ts.map +1 -0
  374. package/dist/scan-inputs.js +71 -0
  375. package/dist/scan-inputs.js.map +1 -0
  376. package/dist/scan-logs.d.ts +2 -3
  377. package/dist/scan-logs.d.ts.map +1 -1
  378. package/dist/scan-logs.js +12 -6
  379. package/dist/scan-logs.js.map +1 -1
  380. package/dist/scan-sessions.d.ts +4 -0
  381. package/dist/scan-sessions.d.ts.map +1 -1
  382. package/dist/scan-sessions.js +17 -0
  383. package/dist/scan-sessions.js.map +1 -1
  384. package/dist/scan-skill-paths.d.ts +8 -0
  385. package/dist/scan-skill-paths.d.ts.map +1 -0
  386. package/dist/scan-skill-paths.js +47 -0
  387. package/dist/scan-skill-paths.js.map +1 -0
  388. package/dist/security-policy-cli.d.ts.map +1 -1
  389. package/dist/security-policy-cli.js +7 -27
  390. package/dist/security-policy-cli.js.map +1 -1
  391. package/dist/security-policy.d.ts +9 -5
  392. package/dist/security-policy.d.ts.map +1 -1
  393. package/dist/security-policy.js +33 -14
  394. package/dist/security-policy.js.map +1 -1
  395. package/dist/server/api.d.ts +0 -1
  396. package/dist/server/api.d.ts.map +1 -1
  397. package/dist/server/api.js +0 -1
  398. package/dist/server/api.js.map +1 -1
  399. package/dist/server/embeddings.d.ts +3 -1
  400. package/dist/server/embeddings.d.ts.map +1 -1
  401. package/dist/server/embeddings.js +10 -3
  402. package/dist/server/embeddings.js.map +1 -1
  403. package/dist/server/errors.d.ts +5 -2
  404. package/dist/server/errors.d.ts.map +1 -1
  405. package/dist/server/errors.js +7 -0
  406. package/dist/server/errors.js.map +1 -1
  407. package/dist/server/sqlite-store.d.ts +0 -2
  408. package/dist/server/sqlite-store.d.ts.map +1 -1
  409. package/dist/server/sqlite-store.js +54 -44
  410. package/dist/server/sqlite-store.js.map +1 -1
  411. package/dist/server/storage.d.ts +0 -2
  412. package/dist/server/storage.d.ts.map +1 -1
  413. package/dist/server/storage.js.map +1 -1
  414. package/dist/severity-store.js +19 -24
  415. package/dist/severity-store.js.map +1 -1
  416. package/dist/standalone-validation-prompt.d.ts +2 -0
  417. package/dist/standalone-validation-prompt.d.ts.map +1 -0
  418. package/dist/standalone-validation-prompt.js +10 -0
  419. package/dist/standalone-validation-prompt.js.map +1 -0
  420. package/dist/suggest-owners.d.ts +4 -2
  421. package/dist/suggest-owners.d.ts.map +1 -1
  422. package/dist/suggest-owners.js +11 -5
  423. package/dist/suggest-owners.js.map +1 -1
  424. package/dist/targets.d.ts +11 -0
  425. package/dist/targets.d.ts.map +1 -1
  426. package/dist/targets.js +123 -72
  427. package/dist/targets.js.map +1 -1
  428. package/dist/trusted-executable.d.ts +2 -2
  429. package/dist/trusted-executable.d.ts.map +1 -1
  430. package/dist/trusted-executable.js +8 -6
  431. package/dist/trusted-executable.js.map +1 -1
  432. package/dist/value.js +7 -0
  433. package/dist/value.js.map +1 -1
  434. package/dist/version.js.map +1 -1
  435. package/dist/workbench-arguments.d.ts +3 -0
  436. package/dist/workbench-arguments.d.ts.map +1 -0
  437. package/dist/workbench-arguments.js +15 -0
  438. package/dist/workbench-arguments.js.map +1 -0
  439. package/dist/worker-progress.d.ts +44 -11
  440. package/dist/worker-progress.d.ts.map +1 -1
  441. package/dist/worker-progress.js +48 -49
  442. package/dist/worker-progress.js.map +1 -1
  443. package/docs/cli.md +1788 -0
  444. package/docs/findings-service.md +402 -0
  445. package/docs/hosted-scans.md +242 -0
  446. package/package.json +23 -22
  447. package/schemas/hosted-scan-v2.fixture.json +145 -0
  448. package/_bundled_plugin/schemas/tools/worker-threat-model.schema.json +0 -18
  449. package/_bundled_plugin/scripts/workbench_dashboard.py +0 -159
  450. package/_bundled_plugin/scripts/workbench_findings.py +0 -221
  451. package/_bundled_plugin/scripts/workbench_severity.py +0 -122
  452. package/_bundled_plugin/skills/assess-patch-risk/scripts/validate_patch_risk_assessment.py +0 -163
  453. package/dist/server/dashboard/THIRD_PARTY_NOTICES.txt +0 -276
  454. package/dist/server/dashboard/app.css +0 -2
  455. package/dist/server/dashboard/app.js +0 -66
  456. package/dist/server/dashboard/index.html +0 -18
  457. package/dist/server/dashboard-types.d.ts +0 -48
  458. package/dist/server/dashboard-types.d.ts.map +0 -1
  459. package/dist/server/dashboard-types.js +0 -2
  460. package/dist/server/dashboard-types.js.map +0 -1
  461. package/dist/server/dashboard.d.ts +0 -6
  462. package/dist/server/dashboard.d.ts.map +0 -1
  463. package/dist/server/dashboard.js +0 -59
  464. package/dist/server/dashboard.js.map +0 -1
  465. package/dist/server/index.d.ts +0 -2
  466. package/dist/server/index.d.ts.map +0 -1
  467. package/dist/server/index.js +0 -6
  468. package/dist/server/index.js.map +0 -1
  469. package/dist/server/routes.d.ts +0 -7
  470. package/dist/server/routes.d.ts.map +0 -1
  471. package/dist/server/routes.js +0 -98
  472. package/dist/server/routes.js.map +0 -1
  473. package/dist/server/serve.d.ts +0 -2
  474. package/dist/server/serve.d.ts.map +0 -1
  475. package/dist/server/serve.js +0 -28
  476. package/dist/server/serve.js.map +0 -1
  477. package/dist/server/server.d.ts +0 -10
  478. package/dist/server/server.d.ts.map +0 -1
  479. package/dist/server/server.js +0 -16
  480. package/dist/server/server.js.map +0 -1
  481. package/dist/server/validation.d.ts +0 -17
  482. package/dist/server/validation.d.ts.map +0 -1
  483. package/dist/server/validation.js +0 -53
  484. package/dist/server/validation.js.map +0 -1
@@ -1,11 +1,9 @@
1
- """Build deterministic structural or sampled source previews for rank inputs."""
1
+ """Build bounded source previews for rank inputs regardless of language."""
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
5
  import argparse
6
- import ast
7
- import json
8
- import re
6
+ from bisect import bisect_right
9
7
  from pathlib import Path
10
8
 
11
9
  DEFAULT_PREVIEW_BYTES = 1024
@@ -14,103 +12,9 @@ PREVIEW_HEAD_LINES = 12
14
12
  PREVIEW_SAMPLE_LINES = 10
15
13
  _UTF16_BOMS = (b"\xff\xfe", b"\xfe\xff")
16
14
 
17
- TEXT_CODE_EXTENSIONS = {
18
- ".c",
19
- ".cc",
20
- ".cfg",
21
- ".cjs",
22
- ".clj",
23
- ".cpp",
24
- ".cs",
25
- ".css",
26
- ".cts",
27
- ".cue",
28
- ".cxx",
29
- ".dart",
30
- ".ejs",
31
- ".erb",
32
- ".ex",
33
- ".exs",
34
- ".go",
35
- ".graphql",
36
- ".h",
37
- ".hh",
38
- ".hpp",
39
- ".hs",
40
- ".html",
41
- ".hxx",
42
- ".java",
43
- ".js",
44
- ".json",
45
- ".jsx",
46
- ".kt",
47
- ".kts",
48
- ".lua",
49
- ".m",
50
- ".mjs",
51
- ".mm",
52
- ".mts",
53
- ".php",
54
- ".phtml",
55
- ".proto",
56
- ".ps1",
57
- ".psd1",
58
- ".psm1",
59
- ".py",
60
- ".rb",
61
- ".rs",
62
- ".scala",
63
- ".sh",
64
- ".sol",
65
- ".sql",
66
- ".svelte",
67
- ".swift",
68
- ".tf",
69
- ".toml",
70
- ".ts",
71
- ".tsx",
72
- ".vue",
73
- ".vy",
74
- ".xml",
75
- ".yaml",
76
- ".yml",
77
- }
78
-
79
- JAVASCRIPT_EXTENSIONS = {".cjs", ".cts", ".js", ".jsx", ".mjs", ".mts", ".ts", ".tsx", ".vue"}
80
- JAVA_LIKE_EXTENSIONS = {
81
- ".c",
82
- ".cc",
83
- ".cpp",
84
- ".cs",
85
- ".cxx",
86
- ".h",
87
- ".hh",
88
- ".hpp",
89
- ".hxx",
90
- ".java",
91
- ".mm",
92
- }
93
- BRACE_LANGUAGE_EXTENSIONS = {
94
- *JAVASCRIPT_EXTENSIONS,
95
- *JAVA_LIKE_EXTENSIONS,
96
- ".dart",
97
- ".go",
98
- ".kt",
99
- ".kts",
100
- ".php",
101
- ".rs",
102
- ".scala",
103
- ".swift",
104
- }
105
- NESTED_BLOCK_COMMENT_EXTENSIONS = {".kt", ".kts", ".rs", ".scala", ".swift"}
106
- CPP_RAW_STRING_RE = re.compile(r'(?<![\w\x80-\U0010ffff])(?:u8|u|U|L)?R"([^\s()\\]{0,16})\(')
107
- RUST_RAW_STRING_RE = re.compile(r'(?:br|r)(#{0,16})"')
108
- RUST_LIFETIME_RE = re.compile(r"'[A-Za-z_][A-Za-z0-9_]*")
109
- PHP_HEREDOC_RE = re.compile(r"<<<\s*['\"]?([A-Za-z_]\w*)['\"]?")
110
-
111
15
 
112
16
  def _decode_source(data: bytes) -> str:
113
- encoding = "utf-16" if data.startswith(_UTF16_BOMS) else "utf-8"
17
+ encoding = "utf-16" if data.startswith(_UTF16_BOMS) else "utf-8-sig"
114
18
  return data.decode(encoding, errors="ignore")
115
19
 
116
20
 
@@ -120,8 +24,21 @@ def is_binary_sample(data: bytes) -> bool:
120
24
  return b"\0" in data
121
25
 
122
26
 
123
- def compact_preview_line(line: str) -> str:
124
- return " ".join(line.split())
27
+ def is_binary_file(path: Path) -> bool:
28
+ """Classify the whole file in bounded chunks."""
29
+ try:
30
+ with path.open("rb") as source:
31
+ sample = source.read(DEFAULT_PREVIEW_READ_BYTES)
32
+ if is_binary_sample(sample):
33
+ return True
34
+ bom = sample[:2] if sample.startswith(_UTF16_BOMS) else b""
35
+ while chunk := source.read(DEFAULT_PREVIEW_READ_BYTES):
36
+ # Even-sized reads preserve UTF-16 code-unit alignment and byte order.
37
+ if is_binary_sample(bom + chunk) if bom else b"\0" in chunk:
38
+ return True
39
+ except OSError:
40
+ return True
41
+ return False
125
42
 
126
43
 
127
44
  def truncate_utf8(text: str, max_bytes: int) -> str:
@@ -134,9 +51,9 @@ def truncate_utf8(text: str, max_bytes: int) -> str:
134
51
 
135
52
 
136
53
  def select_preview_lines(lines: list[str]) -> list[str]:
137
- compact = [value for line in lines if (value := compact_preview_line(line))]
138
- head = compact[:PREVIEW_HEAD_LINES]
139
- remainder = compact[PREVIEW_HEAD_LINES:]
54
+ nonblank = [line for line in lines if line.strip()]
55
+ head = nonblank[:PREVIEW_HEAD_LINES]
56
+ remainder = nonblank[PREVIEW_HEAD_LINES:]
140
57
  if len(remainder) <= PREVIEW_SAMPLE_LINES:
141
58
  return [*head, *remainder]
142
59
 
@@ -158,819 +75,19 @@ def fit_preview_lines(lines: list[str], max_bytes: int) -> str:
158
75
 
159
76
  def render(line_bytes: int) -> str:
160
77
  return "\n".join(
161
- line if line == "..." else truncate_utf8(line, line_bytes).rstrip() for line in lines
78
+ line if line == "..." else truncate_utf8(line, line_bytes) for line in lines
162
79
  )
163
80
 
164
- content_lines = [line for line in lines if line != "..."]
165
- if not content_lines:
166
- return truncate_utf8(full_preview, max_bytes)
167
- low = 0
168
- high = max(len(line.encode("utf-8")) for line in content_lines)
169
- best = ""
170
- while low <= high:
171
- middle = (low + high) // 2
172
- candidate = render(middle)
173
- if len(candidate.encode("utf-8")) <= max_bytes:
174
- best = candidate
175
- low = middle + 1
176
- else:
177
- high = middle - 1
178
- if best:
81
+ high = max(len(line.encode("utf-8")) for line in lines)
82
+ limit = bisect_right(
83
+ range(high + 1), max_bytes, key=lambda size: len(render(size).encode("utf-8"))
84
+ )
85
+ best = render(limit - 1) if limit else ""
86
+ if best.strip() not in {"", "..."}:
179
87
  return best
180
88
  return truncate_utf8(full_preview, max_bytes)
181
89
 
182
90
 
183
- def python_decorators(node: ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef) -> str:
184
- values: list[str] = []
185
- for decorator in node.decorator_list:
186
- try:
187
- rendered = compact_preview_line(ast.unparse(decorator))
188
- except (AttributeError, RecursionError, ValueError):
189
- continue
190
- if rendered:
191
- values.append(f"@{rendered}")
192
- return " ".join(values)
193
-
194
-
195
- def python_arguments(node: ast.FunctionDef | ast.AsyncFunctionDef) -> str:
196
- arguments = [argument.arg for argument in (*node.args.posonlyargs, *node.args.args)]
197
- if node.args.vararg is not None:
198
- arguments.append(f"*{node.args.vararg.arg}")
199
- arguments.extend(argument.arg for argument in node.args.kwonlyargs)
200
- if node.args.kwarg is not None:
201
- arguments.append(f"**{node.args.kwarg.arg}")
202
- return ", ".join(arguments)
203
-
204
-
205
- def python_outline(text: str) -> list[str]:
206
- try:
207
- tree = ast.parse(text)
208
- except (SyntaxError, ValueError, TypeError, MemoryError, RecursionError):
209
- return []
210
-
211
- outline: list[str] = []
212
- for node in tree.body:
213
- if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
214
- prefix = python_decorators(node)
215
- kind = "async function" if isinstance(node, ast.AsyncFunctionDef) else "function"
216
- declaration = f"{kind} {node.name}({python_arguments(node)})"
217
- outline.append(f"{prefix} {declaration}".strip())
218
- elif isinstance(node, ast.ClassDef):
219
- prefix = python_decorators(node)
220
- bases: list[str] = []
221
- for base in node.bases:
222
- try:
223
- bases.append(compact_preview_line(ast.unparse(base)))
224
- except (AttributeError, RecursionError, ValueError):
225
- continue
226
- suffix = f"({', '.join(bases)})" if bases else ""
227
- outline.append(f"{prefix} class {node.name}{suffix}".strip())
228
- for member in node.body:
229
- if not isinstance(member, (ast.FunctionDef, ast.AsyncFunctionDef)):
230
- continue
231
- member_prefix = python_decorators(member)
232
- kind = "async method" if isinstance(member, ast.AsyncFunctionDef) else "method"
233
- declaration = f"{kind} {node.name}.{member.name}"
234
- outline.append(f"{member_prefix} {declaration}".strip())
235
- return outline
236
-
237
-
238
- def javascript_regex_end(text: str, start: int) -> int | None:
239
- if start + 1 >= len(text) or text[start + 1] in {"/", "*"}:
240
- return None
241
- previous = start - 1
242
- while previous >= 0 and text[previous] in " \t\r":
243
- previous -= 1
244
- if previous >= 0 and text[previous] not in "=(:,[!&|?{};\n":
245
- prefix = text[max(0, previous - 8) : previous + 1]
246
- if not re.search(r"\b(?:case|return|throw)$", prefix):
247
- return None
248
-
249
- index = start + 1
250
- in_character_class = False
251
- while index < len(text):
252
- char = text[index]
253
- if char == "\n":
254
- return None
255
- if char == "\\" and index + 1 < len(text):
256
- index += 2
257
- continue
258
- if char == "[":
259
- in_character_class = True
260
- elif char == "]":
261
- in_character_class = False
262
- elif char == "/" and not in_character_class:
263
- index += 1
264
- while index < len(text) and text[index].isalpha():
265
- index += 1
266
- return index
267
- index += 1
268
- return None
269
-
270
-
271
- def mask_c_style_source(text: str, suffix: str) -> str:
272
- masked: list[str] = []
273
- index = 0
274
- block_comment_depth = 0
275
- in_line_comment = False
276
- quote = ""
277
- raw_terminator = ""
278
- heredoc_terminator = ""
279
- while index < len(text):
280
- char = text[index]
281
- next_char = text[index + 1] if index + 1 < len(text) else ""
282
- if char == "\n":
283
- masked.append(char)
284
- in_line_comment = False
285
- if quote and quote not in {"`", '"""', "'''", '@"'}:
286
- quote = ""
287
- index += 1
288
- continue
289
- if in_line_comment:
290
- masked.append(" ")
291
- index += 1
292
- continue
293
- if block_comment_depth:
294
- if char == "/" and next_char == "*" and suffix in NESTED_BLOCK_COMMENT_EXTENSIONS:
295
- masked.extend((" ", " "))
296
- block_comment_depth += 1
297
- index += 2
298
- elif char == "*" and next_char == "/":
299
- masked.extend((" ", " "))
300
- block_comment_depth -= 1
301
- index += 2
302
- else:
303
- masked.append(" ")
304
- index += 1
305
- continue
306
- if heredoc_terminator:
307
- if index == 0 or text[index - 1] == "\n":
308
- # PHP identifiers allow every non-ASCII byte, including non-word characters.
309
- terminator_match = re.compile(
310
- rf"[ \t]*{heredoc_terminator}(?![A-Za-z0-9_\x80-\U0010ffff])"
311
- ).match(text, index)
312
- if terminator_match:
313
- masked.extend(" " * (terminator_match.end() - index))
314
- index = terminator_match.end()
315
- heredoc_terminator = ""
316
- continue
317
- masked.append(" ")
318
- index += 1
319
- continue
320
- if raw_terminator:
321
- if text.startswith(raw_terminator, index):
322
- masked.extend(" " * len(raw_terminator))
323
- index += len(raw_terminator)
324
- raw_terminator = ""
325
- else:
326
- masked.append(" ")
327
- index += 1
328
- continue
329
- if quote in {'"""', "'''"}:
330
- if text.startswith(quote, index):
331
- masked.extend(" " * len(quote))
332
- index += len(quote)
333
- quote = ""
334
- else:
335
- masked.append(" ")
336
- index += 1
337
- continue
338
- if quote == '@"':
339
- masked.append(" ")
340
- if char == '"' and next_char == '"':
341
- masked.append(" ")
342
- index += 2
343
- else:
344
- if char == '"':
345
- quote = ""
346
- index += 1
347
- continue
348
- if quote:
349
- if char == "\\" and next_char:
350
- masked.append(" ")
351
- if next_char == "\n":
352
- index += 1
353
- else:
354
- masked.append(" ")
355
- index += 2
356
- else:
357
- masked.append(" ")
358
- if char == quote:
359
- quote = ""
360
- index += 1
361
- continue
362
- if suffix in {".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".hxx", ".mm"}:
363
- raw_match = CPP_RAW_STRING_RE.match(text, index)
364
- if raw_match:
365
- token = raw_match.group(0)
366
- masked.extend(" " * len(token))
367
- raw_terminator = f'){raw_match.group(1)}"'
368
- index += len(token)
369
- continue
370
- if suffix == ".rs":
371
- raw_match = RUST_RAW_STRING_RE.match(text, index)
372
- if raw_match:
373
- token = raw_match.group(0)
374
- masked.extend(" " * len(token))
375
- raw_terminator = f'"{raw_match.group(1)}'
376
- index += len(token)
377
- continue
378
- if char == "'":
379
- lifetime_match = RUST_LIFETIME_RE.match(text, index)
380
- if lifetime_match:
381
- token = lifetime_match.group(0)
382
- following = text[index + len(token) : index + len(token) + 1]
383
- if following != "'":
384
- masked.extend(token)
385
- index += len(token)
386
- continue
387
- if suffix == ".cs" and char == "@" and next_char == '"':
388
- masked.extend((" ", " "))
389
- quote = '@"'
390
- index += 2
391
- continue
392
- if suffix == ".go" and char == "`":
393
- masked.append(" ")
394
- raw_terminator = "`"
395
- index += 1
396
- continue
397
- triple_quote = text[index : index + 3]
398
- if triple_quote in {'"""', "'''"}:
399
- masked.extend((" ", " ", " "))
400
- quote = triple_quote
401
- index += 3
402
- continue
403
- if suffix == ".php" and text.startswith("<<<", index):
404
- heredoc_match = PHP_HEREDOC_RE.match(text, index)
405
- if heredoc_match:
406
- token = heredoc_match.group(0)
407
- masked.extend(" " * len(token))
408
- heredoc_terminator = heredoc_match.group(1)
409
- index += len(token)
410
- continue
411
- if suffix in JAVASCRIPT_EXTENSIONS and char == "/":
412
- regex_end = javascript_regex_end(text, index)
413
- if regex_end is not None:
414
- masked.extend(" " * (regex_end - index))
415
- index = regex_end
416
- continue
417
- if char == "/" and next_char == "/":
418
- masked.extend((" ", " "))
419
- in_line_comment = True
420
- index += 2
421
- continue
422
- if char == "/" and next_char == "*":
423
- masked.extend((" ", " "))
424
- block_comment_depth = 1
425
- index += 2
426
- continue
427
- if char == "#" and suffix == ".php" and next_char != "[":
428
- masked.append(" ")
429
- in_line_comment = True
430
- index += 1
431
- continue
432
- if char in {'"', "'", "`"}:
433
- quote = char
434
- masked.append(" ")
435
- index += 1
436
- continue
437
- masked.append(char)
438
- index += 1
439
- return "".join(masked)
440
-
441
-
442
- def match_type_declaration(line: str, suffix: str) -> tuple[str, str] | None:
443
- match: re.Match[str] | None = None
444
- if suffix in JAVASCRIPT_EXTENSIONS:
445
- match = re.match(
446
- r"^(?:(?:export|default|declare|abstract)\s+)*(class|interface|enum|namespace)\s+"
447
- r"([A-Za-z_$][\w$]*)",
448
- line,
449
- )
450
- elif suffix == ".java":
451
- match = re.search(r"\b(class|interface|enum|record|@interface)\s+([A-Za-z_$][\w$]*)", line)
452
- elif suffix == ".cs":
453
- match = re.search(
454
- r"\b(class|interface|struct|enum|record(?:\s+(?:class|struct))?)\s+"
455
- r"(@?[A-Za-z_]\w*)",
456
- line,
457
- )
458
- elif suffix == ".php":
459
- match = re.match(
460
- r"^(?:(?:abstract|final|readonly)\s+)*(class|interface|trait|enum)\s+"
461
- r"([A-Za-z_]\w*)",
462
- line,
463
- )
464
- elif suffix == ".go":
465
- match = re.match(r"^type\s+([A-Za-z_]\w*)\s+(struct|interface)\b", line)
466
- if match:
467
- return match.group(2), match.group(1)
468
- elif suffix in {".kt", ".kts"}:
469
- companion_match = re.search(r"\bcompanion\s+object(?:\s+([A-Za-z_]\w*))?", line)
470
- if companion_match:
471
- return "object", companion_match.group(1) or "Companion"
472
- match = re.search(
473
- r"\b((?:(?:data|enum|sealed|annotation|value)\s+)?(?:class|interface|object))\s+"
474
- r"([A-Za-z_]\w*)",
475
- line,
476
- )
477
- elif suffix == ".scala":
478
- match = re.match(
479
- r"^(?:(?:case|sealed|abstract)\s+)*(class|trait|object|enum)\s+([A-Za-z_]\w*)", line
480
- )
481
- elif suffix == ".rs":
482
- match = re.search(r"\b(struct|enum|trait|union)\s+([A-Za-z_]\w*)", line)
483
- if not match:
484
- impl_match = re.search(
485
- r"\bimpl(?:\s*<[^>{}]*>)?\s+(?:[\w:<>]+\s+for\s+)?([A-Za-z_]\w*)",
486
- line,
487
- )
488
- if impl_match:
489
- return "impl", impl_match.group(1)
490
- elif suffix == ".swift":
491
- match = re.search(r"\b(class|struct|enum|protocol|actor|extension)\s+([A-Za-z_]\w*)", line)
492
- elif suffix == ".dart":
493
- match = re.search(r"\b(class|mixin|enum|extension)\s+([A-Za-z_]\w*)", line)
494
- elif suffix in {".c", ".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".hxx", ".mm"}:
495
- match = re.search(
496
- r"\b(class|struct|union|enum(?:\s+class)?)\s+([A-Za-z_]\w*)",
497
- line,
498
- )
499
- if not match:
500
- return None
501
- return match.group(1), match.group(2)
502
-
503
-
504
- CONTROL_NAMES = {
505
- "assert",
506
- "catch",
507
- "for",
508
- "foreach",
509
- "if",
510
- "lock",
511
- "new",
512
- "return",
513
- "sizeof",
514
- "static_assert",
515
- "switch",
516
- "synchronized",
517
- "throw",
518
- "typeof",
519
- "while",
520
- }
521
-
522
-
523
- def match_java_like_function(
524
- line: str, suffix: str, type_name: str | None
525
- ) -> tuple[str, str] | None:
526
- paren = line.find("(")
527
- if paren < 0:
528
- return None
529
- before = line[:paren].rstrip()
530
- if not before or "=" in before or before.startswith("#"):
531
- return None
532
- before = re.sub(r"<[^<>]*>$", "", before).rstrip()
533
- name_match = re.search(
534
- r"(~?[A-Za-z_$@][\w$@]*(?:(?:::|\.)~?[A-Za-z_$@][\w$@]*)?)$",
535
- before,
536
- )
537
- if not name_match:
538
- return None
539
- qualified_name = name_match.group(1)
540
- name = re.split(r"::|\.", qualified_name)[-1].lstrip("~")
541
- if name in CONTROL_NAMES:
542
- return None
543
- prefix = before[: name_match.start()].strip()
544
- if not prefix and name != (type_name or ""):
545
- return None
546
- if suffix in {
547
- ".c",
548
- ".cc",
549
- ".cpp",
550
- ".cxx",
551
- ".h",
552
- ".hh",
553
- ".hpp",
554
- ".hxx",
555
- ".mm",
556
- } and prefix.startswith(("typedef", "using")):
557
- return None
558
- if (
559
- suffix in {".c", ".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".hxx", ".mm"}
560
- and type_name is None
561
- and line.endswith(";")
562
- and not re.search(
563
- r"(?:\b(?:auto|bool|char|consteval|constexpr|double|extern|float|inline|int|long|"
564
- r"short|signed|static|unsigned|void)\b|[*&:]|<)",
565
- prefix,
566
- )
567
- ):
568
- return None
569
- return ("method" if type_name else "function"), qualified_name
570
-
571
-
572
- def match_function_declaration(
573
- line: str, suffix: str, type_name: str | None
574
- ) -> tuple[str, str] | None:
575
- if suffix in JAVASCRIPT_EXTENSIONS:
576
- function_match = re.match(
577
- r"^(?:(?:export|default|declare)\s+)*(async\s+)?function\s*\*?\s*"
578
- r"([A-Za-z_$][\w$]*)\s*(?:<[^>{}]*>)?\s*\(",
579
- line,
580
- )
581
- if function_match:
582
- kind = "async function" if function_match.group(1) else "function"
583
- return kind, function_match.group(2)
584
- arrow_match = re.match(
585
- r"^(?:(?:export|declare)\s+)*(?:const|let|var)\s+([A-Za-z_$][\w$]*)"
586
- r"\s*(?::[^=]+)?=\s*(?:async\s+)?(?:function\b|\([^)]*\)\s*(?::[^=]+)?=>|"
587
- r"[A-Za-z_$][\w$]*\s*=>)",
588
- line,
589
- )
590
- if arrow_match:
591
- return "function", arrow_match.group(1)
592
- if type_name:
593
- field_arrow_match = re.match(
594
- r"^(?:(?:public|private|protected|static|abstract|override|readonly|declare|"
595
- r"accessor)\s+)*(#?[A-Za-z_$][\w$]*)(?:[?!])?\s*(?::[^=]+)?=\s*"
596
- r"(?:async\s+)?(?:\([^)]*\)\s*(?::[^=]+)?=>|[A-Za-z_$][\w$]*\s*=>)",
597
- line,
598
- )
599
- if field_arrow_match:
600
- return "method", field_arrow_match.group(1)
601
- method_match = re.match(
602
- r"^(?:(?:public|private|protected|static|abstract|async|override|readonly|"
603
- r"declare|get|set)\s+)*(#?[A-Za-z_$][\w$]*|constructor)\s*"
604
- r"(?:<[^>{}]*>)?\s*\(",
605
- line,
606
- )
607
- if method_match and method_match.group(1) not in CONTROL_NAMES:
608
- return "method", method_match.group(1)
609
- return None
610
- if suffix == ".php":
611
- match = re.match(
612
- r"^(?:(?:public|protected|private|static|final|abstract|readonly)\s+)*"
613
- r"function\s*&?\s*([A-Za-z_]\w*)\s*\(",
614
- line,
615
- )
616
- if match:
617
- return ("method" if type_name else "function"), match.group(1)
618
- return None
619
- if suffix == ".go":
620
- match = re.match(
621
- r"^func\s+(?:\(([^)]*)\)\s*)?([A-Za-z_]\w*)\s*(?:\[[^]]+\]\s*)?\(",
622
- line,
623
- )
624
- if match:
625
- receiver = match.group(1)
626
- name = match.group(2)
627
- if receiver:
628
- receiver_match = re.search(r"\*?([A-Za-z_]\w*)(?:\[[^]]*])?\s*$", receiver)
629
- if receiver_match:
630
- return "method", f"{receiver_match.group(1)}.{name}"
631
- return "method", name
632
- return ("method" if type_name else "function"), name
633
- return None
634
- if suffix in {".kt", ".kts"}:
635
- match = re.search(r"\bfun\s+(?:<[^>{}]*>\s*)?(?:[\w.<>?]+\.)?([A-Za-z_]\w*)\s*\(", line)
636
- if match:
637
- return ("method" if type_name else "function"), match.group(1)
638
- return None
639
- if suffix == ".scala":
640
- match = re.search(r"\bdef\s+([A-Za-z_]\w*)\b", line)
641
- if match:
642
- return ("method" if type_name else "function"), match.group(1)
643
- return None
644
- if suffix == ".rs":
645
- match = re.search(
646
- r"\b(?:async\s+)?(?:unsafe\s+)?(?:extern\s+\"[^\"]+\"\s+)?(?:const\s+)?"
647
- r"fn\s+([A-Za-z_]\w*)",
648
- line,
649
- )
650
- if match:
651
- return ("method" if type_name else "function"), match.group(1)
652
- return None
653
- if suffix == ".swift":
654
- match = re.search(r"\bfunc\s+([A-Za-z_]\w*)\s*(?:<[^>{}]*>)?\s*\(", line)
655
- if match:
656
- return ("method" if type_name else "function"), match.group(1)
657
- if type_name and re.search(r"\binit\s*\(", line):
658
- return "method", "init"
659
- return None
660
- if suffix == ".dart":
661
- return match_java_like_function(line, suffix, type_name)
662
- if suffix in JAVA_LIKE_EXTENSIONS:
663
- return match_java_like_function(line, suffix, type_name)
664
- return None
665
-
666
-
667
- def is_annotation_line(line: str) -> bool:
668
- return bool(
669
- re.match(r"^@[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*(?:\(.*\))?$", line)
670
- or re.match(r"^\[[A-Za-z_][^]]*]$", line)
671
- or re.match(r"^#\[[A-Za-z_][^]]*]$", line)
672
- )
673
-
674
-
675
- LEADING_ANNOTATION_RE = re.compile(
676
- r"^(?:@[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*(?:\([^)]*\))?|"
677
- r"\[[A-Za-z_][^]]*]|#\[[A-Za-z_][^]]*])\s*"
678
- )
679
-
680
-
681
- def strip_leading_annotations(original: str, masked: str) -> tuple[str, str, list[str]]:
682
- annotations: list[str] = []
683
- if masked.startswith("@interface "):
684
- return original, masked, annotations
685
- while masked_match := LEADING_ANNOTATION_RE.match(masked):
686
- original_match = LEADING_ANNOTATION_RE.match(original)
687
- if original_match:
688
- annotations.append(compact_preview_line(original_match.group(0)))
689
- original = original[original_match.end() :].lstrip()
690
- masked = masked[masked_match.end() :].lstrip()
691
- return original, masked, annotations
692
-
693
-
694
- def brace_language_outline(text: str, suffix: str) -> list[str]:
695
- original_lines = text.splitlines()
696
- masked_lines = mask_c_style_source(text, suffix).splitlines()
697
- outline: dict[str, None] = {}
698
- type_stack: list[tuple[str, int]] = []
699
- function_depths: list[int] = []
700
- blocked_depths: list[int] = []
701
- pending_type: str | None = None
702
- pending_function = False
703
- pending_annotations: list[str] = []
704
- depth = 0
705
-
706
- def add(value: str) -> None:
707
- decorated = f"{' '.join(pending_annotations[-2:])} {value}".strip()
708
- outline[decorated] = None
709
-
710
- def next_lines_open_body(line_index: int) -> bool:
711
- inspected = 0
712
- for future_index in range(line_index + 1, len(masked_lines)):
713
- future = masked_lines[future_index]
714
- future_line = compact_preview_line(future)
715
- if not future_line:
716
- continue
717
- inspected += 1
718
- if "{" in future_line:
719
- return True
720
- if "}" in future_line or future_line.endswith(";") or inspected >= 6:
721
- return False
722
- return False
723
-
724
- for line_index, (original, masked) in enumerate(
725
- zip(original_lines, masked_lines, strict=False)
726
- ):
727
- while function_depths and depth < function_depths[-1]:
728
- function_depths.pop()
729
- while type_stack and depth < type_stack[-1][1]:
730
- type_stack.pop()
731
- while blocked_depths and depth < blocked_depths[-1]:
732
- blocked_depths.pop()
733
-
734
- masked_line = compact_preview_line(masked)
735
- original_line = compact_preview_line(original)
736
- opens = masked.count("{")
737
- closes = masked.count("}")
738
-
739
- opened_pending_scope = False
740
- if pending_type and "{" in masked:
741
- type_stack.append((pending_type, depth + 1))
742
- pending_type = None
743
- opened_pending_scope = True
744
- if pending_function and "{" in masked:
745
- function_depths.append(depth + 1)
746
- pending_function = False
747
- opened_pending_scope = True
748
-
749
- if (
750
- masked_line
751
- and original_line
752
- and is_annotation_line(original_line)
753
- and not function_depths
754
- and not blocked_depths
755
- ):
756
- pending_annotations.append(original_line)
757
- elif masked_line and not function_depths and not blocked_depths:
758
- _, masked_line, inline_annotations = strip_leading_annotations(
759
- original_line, masked_line
760
- )
761
- pending_annotations.extend(inline_annotations)
762
- current_type = type_stack[-1][0] if type_stack else None
763
- direct_type_body = not type_stack or depth == type_stack[-1][1]
764
- type_match = match_type_declaration(masked_line, suffix) if direct_type_body else None
765
- if type_match:
766
- kind, name = type_match
767
- if (
768
- current_type
769
- and suffix in {".kt", ".kts"}
770
- and kind == "object"
771
- and name == "Companion"
772
- ):
773
- name = f"{current_type}.{name}"
774
- add(f"{kind} {name}")
775
- pending_annotations.clear()
776
- if "{" in masked:
777
- type_stack.append((name, depth + 1))
778
- elif not masked_line.endswith(";") and next_lines_open_body(line_index):
779
- pending_type = name
780
- elif direct_type_body:
781
- function_match = match_function_declaration(masked_line, suffix, current_type)
782
- if function_match:
783
- kind, name = function_match
784
- qualified_name = (
785
- f"{current_type}.{name}"
786
- if current_type and "." not in name and "::" not in name
787
- else name
788
- )
789
- add(f"{kind} {qualified_name}")
790
- pending_annotations.clear()
791
- if "{" in masked:
792
- function_depths.append(depth + 1)
793
- elif (
794
- not masked_line.endswith(";")
795
- and "=>" not in masked_line
796
- and "=" not in masked_line
797
- and next_lines_open_body(line_index)
798
- ):
799
- pending_function = True
800
- else:
801
- pending_annotations.clear()
802
- is_transparent_container = bool(
803
- re.match(
804
- r"^(?:(?:export|inline)\s+)?namespace\b|"
805
- r"^(?:pub\s+)?mod\b|^(?:declare\s+)?module\b|"
806
- r"^(?:unsafe\s+)?extern\b",
807
- masked_line,
808
- )
809
- )
810
- if opens and not opened_pending_scope and not is_transparent_container:
811
- blocked_depths.append(depth + 1)
812
-
813
- depth = max(0, depth + opens - closes)
814
- while function_depths and depth < function_depths[-1]:
815
- function_depths.pop()
816
- while type_stack and depth < type_stack[-1][1]:
817
- type_stack.pop()
818
- while blocked_depths and depth < blocked_depths[-1]:
819
- blocked_depths.pop()
820
- return list(outline)
821
-
822
-
823
- def ruby_outline(text: str) -> list[str]:
824
- outline: dict[str, None] = {}
825
- type_stack: list[tuple[int, str]] = []
826
- function_indents: list[int] = []
827
-
828
- for raw_line in text.splitlines():
829
- stripped = raw_line.strip()
830
- if not stripped or stripped.startswith("#"):
831
- continue
832
- indent = len(raw_line.expandtabs(4)) - len(raw_line.lstrip().expandtabs(4))
833
- while function_indents and indent <= function_indents[-1]:
834
- function_indents.pop()
835
- while type_stack and indent <= type_stack[-1][0]:
836
- type_stack.pop()
837
-
838
- type_match = re.match(r"^(class|module)\s+([A-Z]\w*(?:::[A-Z]\w*)*)", stripped)
839
- if type_match and not function_indents:
840
- kind, name = type_match.groups()
841
- outline[f"{kind} {name}"] = None
842
- type_stack.append((indent, name))
843
- continue
844
-
845
- function_match = re.match(r"^def\s+(?:self\.)?([^\s(]+)", stripped)
846
- if function_match and not function_indents:
847
- name = function_match.group(1)
848
- if type_stack:
849
- outline[f"method {type_stack[-1][1]}.{name}"] = None
850
- else:
851
- outline[f"function {name}"] = None
852
- function_indents.append(indent)
853
- return list(outline)
854
-
855
-
856
- def simple_language_outline(text: str, suffix: str) -> list[str]:
857
- outline: dict[str, None] = {}
858
-
859
- for raw_line in text.splitlines():
860
- line = compact_preview_line(raw_line)
861
- if not line:
862
- continue
863
- match: re.Match[str] | None = None
864
- if suffix == ".py":
865
- match = re.match(r"^(async\s+)?def\s+([A-Za-z_]\w*)\s*\(", line)
866
- if match:
867
- outline[
868
- f"{'async function' if match.group(1) else 'function'} {match.group(2)}"
869
- ] = None
870
- continue
871
- match = re.match(r"^class\s+([A-Za-z_]\w*)", line)
872
- if match:
873
- outline[f"class {match.group(1)}"] = None
874
- elif suffix in {".ex", ".exs"}:
875
- match = re.match(r"^defmodule\s+([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*)", line)
876
- if match:
877
- outline[f"module {match.group(1)}"] = None
878
- continue
879
- match = re.match(r"^(?:def|defp|defmacro)\s+([A-Za-z_]\w*[!?]?)", line)
880
- if match:
881
- outline[f"function {match.group(1)}"] = None
882
- elif suffix == ".clj":
883
- match = re.match(
884
- r"^\((defn|defmacro|defprotocol|defrecord|deftype|defmulti|defmethod)\s+([^\s)]+)",
885
- line,
886
- )
887
- if match:
888
- outline[f"{match.group(1)} {match.group(2)}"] = None
889
- elif suffix == ".lua":
890
- match = re.match(r"^(?:local\s+)?function\s+([A-Za-z_]\w*(?:[.:][A-Za-z_]\w*)*)", line)
891
- if match:
892
- outline[f"function {match.group(1)}"] = None
893
- elif suffix == ".sh":
894
- match = re.match(r"^(?:function\s+)?([A-Za-z_]\w*)\s*\(\s*\)\s*\{?", line)
895
- if match:
896
- outline[f"function {match.group(1)}"] = None
897
- elif suffix == ".hs":
898
- match = re.match(r"^(data|newtype|type|class)\s+([A-Z]\w*)", line)
899
- if match:
900
- outline[f"{match.group(1)} {match.group(2)}"] = None
901
- continue
902
- match = re.match(r"^([a-z_]\w*)\s*::", line)
903
- if match:
904
- outline[f"function {match.group(1)}"] = None
905
- elif suffix == ".proto":
906
- match = re.match(r"^(message|service|enum)\s+([A-Za-z_]\w*)", line)
907
- if match:
908
- outline[f"{match.group(1)} {match.group(2)}"] = None
909
- continue
910
- match = re.match(r"^rpc\s+([A-Za-z_]\w*)\s*\(", line)
911
- if match:
912
- outline[f"rpc {match.group(1)}"] = None
913
- elif suffix == ".graphql":
914
- match = re.match(
915
- r"^(type|interface|input|enum|union|scalar|directive)\s+([A-Za-z_]\w*)",
916
- line,
917
- )
918
- if match:
919
- outline[f"{match.group(1)} {match.group(2)}"] = None
920
- continue
921
- match = re.match(r"^(query|mutation|subscription)\s+([A-Za-z_]\w*)", line)
922
- if match:
923
- outline[f"{match.group(1)} {match.group(2)}"] = None
924
- elif suffix == ".sql":
925
- match = re.match(
926
- r"^CREATE\s+(?:OR\s+REPLACE\s+)?(FUNCTION|PROCEDURE|TABLE|VIEW|TRIGGER)\s+"
927
- r"([^\s(;]+)",
928
- line,
929
- flags=re.IGNORECASE,
930
- )
931
- if match:
932
- outline[f"{match.group(1).lower()} {match.group(2)}"] = None
933
- elif suffix in {".yaml", ".yml"}:
934
- match = re.match(r"^([A-Za-z0-9_.-]+):(?:\s|$)", raw_line)
935
- if match:
936
- outline[f"key {match.group(1)}"] = None
937
- elif suffix in {".toml", ".cfg"}:
938
- match = re.match(r"^\[([^]]+)]$", line)
939
- if match:
940
- outline[f"section {match.group(1)}"] = None
941
- return list(outline)
942
-
943
-
944
- def json_outline(text: str) -> list[str]:
945
- try:
946
- parsed = json.loads(text)
947
- except (json.JSONDecodeError, RecursionError, MemoryError):
948
- return []
949
- if not isinstance(parsed, dict):
950
- return []
951
- outline: list[str] = []
952
- for key, value in parsed.items():
953
- if isinstance(value, dict) and value:
954
- children = ", ".join(str(child) for child in value)
955
- outline.append(f"key {key} [{children}]")
956
- else:
957
- outline.append(f"key {key}")
958
- return outline
959
-
960
-
961
- def structural_outline(path: Path, text: str) -> list[str]:
962
- suffix = path.suffix.lower()
963
- if suffix == ".py":
964
- return python_outline(text) or simple_language_outline(text, suffix)
965
- if suffix == ".rb":
966
- return ruby_outline(text)
967
- if suffix in BRACE_LANGUAGE_EXTENSIONS:
968
- return brace_language_outline(text, suffix)
969
- if suffix == ".json":
970
- return json_outline(text)
971
- return simple_language_outline(text, suffix)
972
-
973
-
974
91
  def preview_for(
975
92
  path: Path, preview_bytes: int, *, max_read_bytes: int = DEFAULT_PREVIEW_READ_BYTES
976
93
  ) -> tuple[str, bool]:
@@ -986,20 +103,27 @@ def preview_for(
986
103
  data = sample + remaining
987
104
  except OSError:
988
105
  return "", True
989
- return preview_for_bytes(path, data, preview_bytes)
106
+ return preview_for_bytes(data, preview_bytes)
990
107
 
991
108
 
992
- def preview_for_bytes(path: Path, data: bytes, preview_bytes: int) -> tuple[str, bool]:
109
+ def preview_for_bytes(data: bytes, preview_bytes: int) -> tuple[str, bool]:
993
110
  if is_binary_sample(data):
994
111
  return "", True
995
- text = _decode_source(data)
996
- outline = structural_outline(path, text)
997
- preview_lines = select_preview_lines(outline or text.splitlines())
998
- return fit_preview_lines(preview_lines, preview_bytes), False
112
+ lines = _decode_source(data).replace("\r\n", "\n").replace("\r", "\n").split("\n")
113
+ start, end = 0, len(lines)
114
+ while start < end and not lines[start].strip():
115
+ start += 1
116
+ while end > start and not lines[end - 1].strip():
117
+ end -= 1
118
+ lines = lines[start:end]
119
+ text = "\n".join(lines)
120
+ if len(text.encode("utf-8")) <= preview_bytes:
121
+ return text, False
122
+ return fit_preview_lines(select_preview_lines(lines), preview_bytes), False
999
123
 
1000
124
 
1001
125
  if __name__ == "__main__":
1002
126
  parser = argparse.ArgumentParser(
1003
- description="Build structural or sampled previews for Codex Security rank inputs."
127
+ description="Build bounded source previews for Codex Security rank inputs."
1004
128
  )
1005
129
  parser.parse_args()