@openai/codex-security 0.8.2 → 0.9.1

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 (649) hide show
  1. package/README.md +201 -178
  2. package/_bundled_plugin/.app.json +4 -0
  3. package/_bundled_plugin/.mcp.json +6 -1
  4. package/_bundled_plugin/examples/completed-scan/findings.json +16 -4
  5. package/_bundled_plugin/examples/completed-scan/report.md +11 -8
  6. package/_bundled_plugin/examples/completed-scan/scan-manifest.json +4 -2
  7. package/_bundled_plugin/mcp/fzstd-LICENSE.txt +21 -0
  8. package/_bundled_plugin/mcp/helpers.mjs +2 -11
  9. package/_bundled_plugin/mcp/helpers.mjs.br +0 -0
  10. package/_bundled_plugin/mcp/native/THIRD_PARTY_NOTICES.txt +3 -210
  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/package.json +1 -0
  20. package/_bundled_plugin/mcp/permission-profile-preflight.mjs +51 -14
  21. package/_bundled_plugin/mcp/server.mjs +2 -11
  22. package/_bundled_plugin/mcp/server.mjs.br +0 -0
  23. package/_bundled_plugin/mcp/session-decompression.mjs +712 -0
  24. package/_bundled_plugin/references/artifact-storage.md +1 -1
  25. package/_bundled_plugin/references/core-scan.md +14 -5
  26. package/_bundled_plugin/references/default-severity-rubric.md +33 -0
  27. package/_bundled_plugin/references/final-report.md +5 -3
  28. package/_bundled_plugin/references/finding-detail-fields.md +19 -18
  29. package/_bundled_plugin/references/sarif-adapter.md +8 -2
  30. package/_bundled_plugin/references/scan-artifacts.md +21 -12
  31. package/_bundled_plugin/references/scan-contract.md +27 -33
  32. package/_bundled_plugin/references/threat-model.md +1 -0
  33. package/_bundled_plugin/schemas/tools/scan-draft.schema.json +2 -2
  34. package/_bundled_plugin/scripts/check_deep_scan_target.py +97 -0
  35. package/_bundled_plugin/scripts/codex_profile.mjs +7 -1
  36. package/_bundled_plugin/scripts/deep_scan_config.py +20 -1
  37. package/_bundled_plugin/scripts/deep_scan_workbench.py +188 -454
  38. package/_bundled_plugin/scripts/filesystem_identity.py +1 -0
  39. package/_bundled_plugin/scripts/finalize_scan_contract.py +317 -377
  40. package/_bundled_plugin/scripts/finding_preview.py +25 -29
  41. package/_bundled_plugin/scripts/launch_codex_security_mcp +1 -0
  42. package/_bundled_plugin/scripts/legacy_findings.py +148 -0
  43. package/_bundled_plugin/scripts/posix_scan_local_files.py +198 -0
  44. package/_bundled_plugin/scripts/read_codex_session.mjs +21 -0
  45. package/_bundled_plugin/scripts/report_projection.py +167 -178
  46. package/_bundled_plugin/scripts/review_state.py +8 -2
  47. package/_bundled_plugin/scripts/servicenow_avit.py +20 -0
  48. package/_bundled_plugin/scripts/snapshot_sqlite.py +1 -0
  49. package/_bundled_plugin/scripts/threat_model_projection.py +22 -26
  50. package/_bundled_plugin/scripts/validate_scan_contract.py +9 -7
  51. package/_bundled_plugin/scripts/windows_scan_local_files.py +600 -72
  52. package/_bundled_plugin/scripts/workbench/__init__.py +1 -1
  53. package/_bundled_plugin/scripts/workbench/handoff.py +47 -0
  54. package/_bundled_plugin/scripts/workbench/retained_coverage.py +38 -75
  55. package/_bundled_plugin/scripts/workbench/retained_records.py +18 -30
  56. package/_bundled_plugin/scripts/workbench/runtime_toml.py +4 -1
  57. package/_bundled_plugin/scripts/workbench/saved_coverage.py +152 -248
  58. package/_bundled_plugin/scripts/workbench/saved_outputs.py +9 -0
  59. package/_bundled_plugin/scripts/workbench/saved_sources.py +47 -40
  60. package/_bundled_plugin/scripts/workbench/storage.py +71 -7
  61. package/_bundled_plugin/scripts/workbench_cli.py +121 -14
  62. package/_bundled_plugin/scripts/workbench_constants.py +19 -0
  63. package/_bundled_plugin/scripts/workbench_coverage_receipts.py +3 -3
  64. package/_bundled_plugin/scripts/workbench_db.py +550 -492
  65. package/_bundled_plugin/scripts/workbench_feedback.py +8 -7
  66. package/_bundled_plugin/scripts/workbench_finding_index.py +1 -0
  67. package/_bundled_plugin/scripts/workbench_finding_workflows.py +90 -4
  68. package/_bundled_plugin/scripts/workbench_native_indexes.py +29 -33
  69. package/_bundled_plugin/scripts/workbench_progress.py +10 -1
  70. package/_bundled_plugin/scripts/workbench_publication.py +14 -10
  71. package/_bundled_plugin/scripts/workbench_remediation.py +508 -11
  72. package/_bundled_plugin/scripts/workbench_saved_results.py +132 -77
  73. package/_bundled_plugin/scripts/workbench_scan_history.py +139 -80
  74. package/_bundled_plugin/scripts/workbench_scan_start.py +84 -25
  75. package/_bundled_plugin/scripts/workbench_scan_usage.py +666 -117
  76. package/_bundled_plugin/scripts/workbench_schema.py +44 -55
  77. package/_bundled_plugin/scripts/workbench_setup.py +251 -0
  78. package/_bundled_plugin/scripts/workbench_source_excerpt.py +13 -0
  79. package/_bundled_plugin/scripts/workbench_source_scopes.py +33 -23
  80. package/_bundled_plugin/scripts/workbench_target.py +330 -66
  81. package/_bundled_plugin/scripts/workbench_target_state.py +3 -0
  82. package/_bundled_plugin/scripts/workbench_validation.py +44 -12
  83. package/_bundled_plugin/shared/workbench-history-repairs.json +19 -16
  84. package/_bundled_plugin/shared/workbench-migrations.json +53 -0
  85. package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +1 -1
  86. package/_bundled_plugin/skills/attack-path-analysis/references/severity-policy.md +4 -68
  87. package/_bundled_plugin/skills/check-skills/SKILL.md +43 -0
  88. package/_bundled_plugin/skills/check-skills/agents/openai.yaml +4 -0
  89. package/_bundled_plugin/skills/cloud-findings/SKILL.md +6 -4
  90. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +7 -50
  91. package/_bundled_plugin/skills/fix-finding/SKILL.md +19 -2
  92. package/_bundled_plugin/skills/publish-findings/SKILL.md +81 -45
  93. package/_bundled_plugin/skills/publish-findings/references/checkmarx-import.md +37 -0
  94. package/_bundled_plugin/skills/publish-findings/references/wiz-import.md +5 -24
  95. package/_bundled_plugin/skills/publish-findings/scripts/collect_checkmarx.py +464 -0
  96. package/_bundled_plugin/skills/threat-model-visualize/scripts/build_explorer.py +36 -9
  97. package/_bundled_plugin/skills/track-findings/SKILL.md +5 -3
  98. package/_bundled_plugin/skills/triage-finding/references/github-rest-intake.md +22 -68
  99. package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +12 -6
  100. package/dist/api.d.ts +45 -5
  101. package/dist/api.d.ts.map +1 -1
  102. package/dist/api.js +1144 -411
  103. package/dist/api.js.map +1 -1
  104. package/dist/auth.d.ts.map +1 -1
  105. package/dist/auth.js +10 -7
  106. package/dist/auth.js.map +1 -1
  107. package/dist/bedrock-oss-pricing.d.ts +6 -0
  108. package/dist/bedrock-oss-pricing.d.ts.map +1 -0
  109. package/dist/bedrock-oss-pricing.js +58 -0
  110. package/dist/bedrock-oss-pricing.js.map +1 -0
  111. package/dist/bedrock-pricing.d.ts +15 -0
  112. package/dist/bedrock-pricing.d.ts.map +1 -0
  113. package/dist/bedrock-pricing.js +117 -0
  114. package/dist/bedrock-pricing.js.map +1 -0
  115. package/dist/blackduck-findings.d.ts +8 -0
  116. package/dist/blackduck-findings.d.ts.map +1 -0
  117. package/dist/blackduck-findings.js +320 -0
  118. package/dist/blackduck-findings.js.map +1 -0
  119. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  120. package/dist/bulk-scan-discovery.js +51 -36
  121. package/dist/bulk-scan-discovery.js.map +1 -1
  122. package/dist/checkmarx-evidence.d.ts +19 -0
  123. package/dist/checkmarx-evidence.d.ts.map +1 -0
  124. package/dist/checkmarx-evidence.js +36 -0
  125. package/dist/checkmarx-evidence.js.map +1 -0
  126. package/dist/checkmarx-findings.d.ts +4 -0
  127. package/dist/checkmarx-findings.d.ts.map +1 -0
  128. package/dist/checkmarx-findings.js +27 -0
  129. package/dist/checkmarx-findings.js.map +1 -0
  130. package/dist/checkmarx-one.d.ts +4 -0
  131. package/dist/checkmarx-one.d.ts.map +1 -0
  132. package/dist/checkmarx-one.js +53 -0
  133. package/dist/checkmarx-one.js.map +1 -0
  134. package/dist/checkmarx-repository.d.ts +12 -0
  135. package/dist/checkmarx-repository.d.ts.map +1 -0
  136. package/dist/checkmarx-repository.js +181 -0
  137. package/dist/checkmarx-repository.js.map +1 -0
  138. package/dist/checkmarx-xml.d.ts +12 -0
  139. package/dist/checkmarx-xml.d.ts.map +1 -0
  140. package/dist/checkmarx-xml.js +196 -0
  141. package/dist/checkmarx-xml.js.map +1 -0
  142. package/dist/classify-severity-records.d.ts +6 -9
  143. package/dist/classify-severity-records.d.ts.map +1 -1
  144. package/dist/classify-severity-records.js +9 -21
  145. package/dist/classify-severity-records.js.map +1 -1
  146. package/dist/classify-severity.d.ts +4 -1
  147. package/dist/classify-severity.d.ts.map +1 -1
  148. package/dist/classify-severity.js +30 -13
  149. package/dist/classify-severity.js.map +1 -1
  150. package/dist/cli.d.ts +14 -5
  151. package/dist/cli.d.ts.map +1 -1
  152. package/dist/cli.js +2112 -1266
  153. package/dist/cli.js.map +1 -1
  154. package/dist/cloud-publish.js +2 -2
  155. package/dist/cloud-publish.js.map +1 -1
  156. package/dist/codex-prompt.d.ts +2 -0
  157. package/dist/codex-prompt.d.ts.map +1 -1
  158. package/dist/codex-prompt.js +6 -0
  159. package/dist/codex-prompt.js.map +1 -1
  160. package/dist/component-scan.d.ts +16 -3
  161. package/dist/component-scan.d.ts.map +1 -1
  162. package/dist/component-scan.js +32 -6
  163. package/dist/component-scan.js.map +1 -1
  164. package/dist/config.d.ts +2 -1
  165. package/dist/config.d.ts.map +1 -1
  166. package/dist/config.js +221 -65
  167. package/dist/config.js.map +1 -1
  168. package/dist/continuum-findings.d.ts +7 -0
  169. package/dist/continuum-findings.d.ts.map +1 -0
  170. package/dist/continuum-findings.js +225 -0
  171. package/dist/continuum-findings.js.map +1 -0
  172. package/dist/contract-values.d.ts +30 -0
  173. package/dist/contract-values.d.ts.map +1 -0
  174. package/dist/contract-values.js +78 -0
  175. package/dist/contract-values.js.map +1 -0
  176. package/dist/contract.d.ts +2 -20
  177. package/dist/contract.d.ts.map +1 -1
  178. package/dist/contract.js +58 -669
  179. package/dist/contract.js.map +1 -1
  180. package/dist/cost-model.d.ts +13 -1
  181. package/dist/cost-model.d.ts.map +1 -1
  182. package/dist/cost-model.js +66 -115
  183. package/dist/cost-model.js.map +1 -1
  184. package/dist/cost.d.ts +61 -4
  185. package/dist/cost.d.ts.map +1 -1
  186. package/dist/cost.js +2560 -100
  187. package/dist/cost.js.map +1 -1
  188. package/dist/custom-publish.d.ts +4 -0
  189. package/dist/custom-publish.d.ts.map +1 -1
  190. package/dist/custom-publish.js +40 -3
  191. package/dist/custom-publish.js.map +1 -1
  192. package/dist/custom-validation-prompt.d.ts +1 -1
  193. package/dist/custom-validation-prompt.d.ts.map +1 -1
  194. package/dist/custom-validation-prompt.js +5 -0
  195. package/dist/custom-validation-prompt.js.map +1 -1
  196. package/dist/custom-validation.d.ts +2 -0
  197. package/dist/custom-validation.d.ts.map +1 -1
  198. package/dist/custom-validation.js +4 -0
  199. package/dist/custom-validation.js.map +1 -1
  200. package/dist/deduplication/deduplication-prompts.d.ts +4 -4
  201. package/dist/deduplication/deduplication-prompts.d.ts.map +1 -1
  202. package/dist/deduplication/deduplication-prompts.js +2 -2
  203. package/dist/deduplication/deduplication-prompts.js.map +1 -1
  204. package/dist/deduplication/deduplication-reviewer.d.ts +14 -11
  205. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
  206. package/dist/deduplication/deduplication-reviewer.js +11 -4
  207. package/dist/deduplication/deduplication-reviewer.js.map +1 -1
  208. package/dist/deduplication/deduplication.js.map +1 -1
  209. package/dist/deduplication/finding-schema.d.ts +19 -1
  210. package/dist/deduplication/finding-schema.d.ts.map +1 -1
  211. package/dist/deduplication/finding-schema.js +25 -0
  212. package/dist/deduplication/finding-schema.js.map +1 -1
  213. package/dist/deduplication/local.d.ts +2 -2
  214. package/dist/deduplication/local.d.ts.map +1 -1
  215. package/dist/deduplication/local.js +5 -10
  216. package/dist/deduplication/local.js.map +1 -1
  217. package/dist/deduplication/records.d.ts +8 -2
  218. package/dist/deduplication/records.d.ts.map +1 -1
  219. package/dist/deduplication/records.js +36 -8
  220. package/dist/deduplication/records.js.map +1 -1
  221. package/dist/deduplication/refusal.d.ts.map +1 -1
  222. package/dist/deduplication/refusal.js +25 -5
  223. package/dist/deduplication/refusal.js.map +1 -1
  224. package/dist/deduplication/scan.d.ts +3 -0
  225. package/dist/deduplication/scan.d.ts.map +1 -1
  226. package/dist/deduplication/scan.js +1 -1
  227. package/dist/deduplication/scan.js.map +1 -1
  228. package/dist/deep-config.d.ts +0 -1
  229. package/dist/deep-config.d.ts.map +1 -1
  230. package/dist/deep-config.js +22 -33
  231. package/dist/deep-config.js.map +1 -1
  232. package/dist/deep-scan-defaults.d.ts +2 -0
  233. package/dist/deep-scan-defaults.d.ts.map +1 -1
  234. package/dist/deep-scan-defaults.js +28 -1
  235. package/dist/deep-scan-defaults.js.map +1 -1
  236. package/dist/deep-scan.d.ts +13 -0
  237. package/dist/deep-scan.d.ts.map +1 -1
  238. package/dist/deep-scan.js +262 -7
  239. package/dist/deep-scan.js.map +1 -1
  240. package/dist/default-severity-rubric.d.ts +1 -2
  241. package/dist/default-severity-rubric.d.ts.map +1 -1
  242. package/dist/default-severity-rubric.js +2 -66
  243. package/dist/default-severity-rubric.js.map +1 -1
  244. package/dist/endor-findings.d.ts +3 -1
  245. package/dist/endor-findings.d.ts.map +1 -1
  246. package/dist/endor-findings.js +108 -80
  247. package/dist/endor-findings.js.map +1 -1
  248. package/dist/errors.d.ts +1 -1
  249. package/dist/errors.d.ts.map +1 -1
  250. package/dist/errors.js +9 -2
  251. package/dist/errors.js.map +1 -1
  252. package/dist/external-findings-import.d.ts +5 -3
  253. package/dist/external-findings-import.d.ts.map +1 -1
  254. package/dist/external-findings-import.js +11 -16
  255. package/dist/external-findings-import.js.map +1 -1
  256. package/dist/external-findings-local.d.ts +15 -4
  257. package/dist/external-findings-local.d.ts.map +1 -1
  258. package/dist/external-findings-local.js +619 -225
  259. package/dist/external-findings-local.js.map +1 -1
  260. package/dist/external-findings-publish.d.ts +20 -6
  261. package/dist/external-findings-publish.d.ts.map +1 -1
  262. package/dist/external-findings-publish.js +2356 -308
  263. package/dist/external-findings-publish.js.map +1 -1
  264. package/dist/external-findings-read.d.ts +28 -2
  265. package/dist/external-findings-read.d.ts.map +1 -1
  266. package/dist/external-findings-read.js +174 -81
  267. package/dist/external-findings-read.js.map +1 -1
  268. package/dist/external-findings.d.ts +17 -3
  269. package/dist/external-findings.d.ts.map +1 -1
  270. package/dist/external-findings.js +153 -1
  271. package/dist/external-findings.js.map +1 -1
  272. package/dist/external-import-contract.d.ts +33 -1
  273. package/dist/external-import-contract.d.ts.map +1 -1
  274. package/dist/external-import-contract.js +335 -51
  275. package/dist/external-import-contract.js.map +1 -1
  276. package/dist/external-import-models.d.ts +1 -1
  277. package/dist/external-import-models.d.ts.map +1 -1
  278. package/dist/external-import-models.js.map +1 -1
  279. package/dist/feedback.d.ts.map +1 -1
  280. package/dist/feedback.js +5 -4
  281. package/dist/feedback.js.map +1 -1
  282. package/dist/finding-prompt.d.ts +5 -0
  283. package/dist/finding-prompt.d.ts.map +1 -0
  284. package/dist/finding-prompt.js +32 -0
  285. package/dist/finding-prompt.js.map +1 -0
  286. package/dist/finding-retrieval.d.ts +3 -3
  287. package/dist/finding-retrieval.d.ts.map +1 -1
  288. package/dist/finding-retrieval.js.map +1 -1
  289. package/dist/finding-workflow.d.ts +6 -1
  290. package/dist/finding-workflow.d.ts.map +1 -1
  291. package/dist/finding-workflow.js +2 -2
  292. package/dist/finding-workflow.js.map +1 -1
  293. package/dist/findings-client.d.ts +1 -0
  294. package/dist/findings-client.d.ts.map +1 -1
  295. package/dist/findings-client.js +27 -3
  296. package/dist/findings-client.js.map +1 -1
  297. package/dist/git-remote.d.ts +4 -0
  298. package/dist/git-remote.d.ts.map +1 -1
  299. package/dist/git-remote.js +20 -0
  300. package/dist/git-remote.js.map +1 -1
  301. package/dist/github-advisories.d.ts +3 -12
  302. package/dist/github-advisories.d.ts.map +1 -1
  303. package/dist/github-advisories.js +9 -31
  304. package/dist/github-advisories.js.map +1 -1
  305. package/dist/github-dependabot-findings.d.ts.map +1 -1
  306. package/dist/github-dependabot-findings.js +22 -20
  307. package/dist/github-dependabot-findings.js.map +1 -1
  308. package/dist/github-findings.d.ts +4 -1
  309. package/dist/github-findings.d.ts.map +1 -1
  310. package/dist/github-findings.js +392 -79
  311. package/dist/github-findings.js.map +1 -1
  312. package/dist/github.d.ts +17 -7
  313. package/dist/github.d.ts.map +1 -1
  314. package/dist/github.js +43 -30
  315. package/dist/github.js.map +1 -1
  316. package/dist/host-protocol.d.ts +2 -2
  317. package/dist/host-protocol.d.ts.map +1 -1
  318. package/dist/host-protocol.js.map +1 -1
  319. package/dist/host-records-protocol.d.ts +10 -11
  320. package/dist/host-records-protocol.d.ts.map +1 -1
  321. package/dist/host-records-protocol.js +69 -9
  322. package/dist/host-records-protocol.js.map +1 -1
  323. package/dist/hosted-git-remotes.d.ts +5 -0
  324. package/dist/hosted-git-remotes.d.ts.map +1 -0
  325. package/dist/hosted-git-remotes.js +285 -0
  326. package/dist/hosted-git-remotes.js.map +1 -0
  327. package/dist/hosted-git-repository.d.ts +28 -0
  328. package/dist/hosted-git-repository.d.ts.map +1 -0
  329. package/dist/hosted-git-repository.js +222 -0
  330. package/dist/hosted-git-repository.js.map +1 -0
  331. package/dist/hosted-scan-protocol.js +2 -2
  332. package/dist/hosted-scan-protocol.js.map +1 -1
  333. package/dist/hosted-scan.d.ts +15 -0
  334. package/dist/hosted-scan.d.ts.map +1 -1
  335. package/dist/hosted-scan.js +71 -44
  336. package/dist/hosted-scan.js.map +1 -1
  337. package/dist/hosted-scanner-http.d.ts +21 -0
  338. package/dist/hosted-scanner-http.d.ts.map +1 -0
  339. package/dist/hosted-scanner-http.js +204 -0
  340. package/dist/hosted-scanner-http.js.map +1 -0
  341. package/dist/hosted-scanner-v1.d.ts +6 -0
  342. package/dist/hosted-scanner-v1.d.ts.map +1 -0
  343. package/dist/hosted-scanner-v1.js +60 -0
  344. package/dist/hosted-scanner-v1.js.map +1 -0
  345. package/dist/import-scan.d.ts.map +1 -1
  346. package/dist/import-scan.js +3 -0
  347. package/dist/import-scan.js.map +1 -1
  348. package/dist/index.d.ts +24 -6
  349. package/dist/index.d.ts.map +1 -1
  350. package/dist/index.js +15 -1
  351. package/dist/index.js.map +1 -1
  352. package/dist/inspector-findings.d.ts.map +1 -1
  353. package/dist/inspector-findings.js +58 -81
  354. package/dist/inspector-findings.js.map +1 -1
  355. package/dist/knowledge-base.d.ts.map +1 -1
  356. package/dist/knowledge-base.js +86 -10
  357. package/dist/knowledge-base.js.map +1 -1
  358. package/dist/local-git-metadata.d.ts +5 -0
  359. package/dist/local-git-metadata.d.ts.map +1 -0
  360. package/dist/local-git-metadata.js +130 -0
  361. package/dist/local-git-metadata.js.map +1 -0
  362. package/dist/multiscan.d.ts +6 -4
  363. package/dist/multiscan.d.ts.map +1 -1
  364. package/dist/multiscan.js +160 -77
  365. package/dist/multiscan.js.map +1 -1
  366. package/dist/native-json-findings.d.ts +2 -1
  367. package/dist/native-json-findings.d.ts.map +1 -1
  368. package/dist/native-json-findings.js +12 -2
  369. package/dist/native-json-findings.js.map +1 -1
  370. package/dist/opengrep-findings.d.ts +1 -0
  371. package/dist/opengrep-findings.d.ts.map +1 -1
  372. package/dist/opengrep-findings.js +49 -31
  373. package/dist/opengrep-findings.js.map +1 -1
  374. package/dist/operation-telemetry.d.ts +104 -0
  375. package/dist/operation-telemetry.d.ts.map +1 -0
  376. package/dist/operation-telemetry.js +293 -0
  377. package/dist/operation-telemetry.js.map +1 -0
  378. package/dist/patch-tui.d.ts.map +1 -1
  379. package/dist/patch-tui.js +90 -35
  380. package/dist/patch-tui.js.map +1 -1
  381. package/dist/plugin-python-probe.d.ts +4 -0
  382. package/dist/plugin-python-probe.d.ts.map +1 -0
  383. package/dist/plugin-python-probe.js +10 -0
  384. package/dist/plugin-python-probe.js.map +1 -0
  385. package/dist/private-finding-import.d.ts +341 -0
  386. package/dist/private-finding-import.d.ts.map +1 -0
  387. package/dist/private-finding-import.js +411 -0
  388. package/dist/private-finding-import.js.map +1 -0
  389. package/dist/project-config.d.ts.map +1 -1
  390. package/dist/project-config.js +2 -2
  391. package/dist/project-config.js.map +1 -1
  392. package/dist/provider-profile.d.ts +11 -3
  393. package/dist/provider-profile.d.ts.map +1 -1
  394. package/dist/provider-profile.js +80 -11
  395. package/dist/provider-profile.js.map +1 -1
  396. package/dist/publication-telemetry.d.ts +45 -0
  397. package/dist/publication-telemetry.d.ts.map +1 -0
  398. package/dist/publication-telemetry.js +50 -0
  399. package/dist/publication-telemetry.js.map +1 -0
  400. package/dist/publication.js +7 -1
  401. package/dist/publication.js.map +1 -1
  402. package/dist/publish.d.ts +4 -0
  403. package/dist/publish.d.ts.map +1 -1
  404. package/dist/publish.js +42 -2
  405. package/dist/publish.js.map +1 -1
  406. package/dist/reader-selection.d.ts +15 -0
  407. package/dist/reader-selection.d.ts.map +1 -0
  408. package/dist/reader-selection.js +89 -0
  409. package/dist/reader-selection.js.map +1 -0
  410. package/dist/record.d.ts +6 -1
  411. package/dist/record.d.ts.map +1 -1
  412. package/dist/record.js +53 -2
  413. package/dist/record.js.map +1 -1
  414. package/dist/remediation.d.ts +3 -1
  415. package/dist/remediation.d.ts.map +1 -1
  416. package/dist/remediation.js +28 -2
  417. package/dist/remediation.js.map +1 -1
  418. package/dist/request-metadata.d.ts +3 -0
  419. package/dist/request-metadata.d.ts.map +1 -1
  420. package/dist/request-metadata.js +9 -0
  421. package/dist/request-metadata.js.map +1 -1
  422. package/dist/result.d.ts +6 -0
  423. package/dist/result.d.ts.map +1 -1
  424. package/dist/result.js +3 -1
  425. package/dist/result.js.map +1 -1
  426. package/dist/retry-windows-rename.d.ts +8 -0
  427. package/dist/retry-windows-rename.d.ts.map +1 -0
  428. package/dist/retry-windows-rename.js +24 -0
  429. package/dist/retry-windows-rename.js.map +1 -0
  430. package/dist/runtime.d.ts +22 -1
  431. package/dist/runtime.d.ts.map +1 -1
  432. package/dist/runtime.js +408 -162
  433. package/dist/runtime.js.map +1 -1
  434. package/dist/saved-scan-bootstrap.d.ts +1 -0
  435. package/dist/saved-scan-bootstrap.d.ts.map +1 -1
  436. package/dist/saved-scan-bootstrap.js +18 -15
  437. package/dist/saved-scan-bootstrap.js.map +1 -1
  438. package/dist/scan-activity.d.ts.map +1 -1
  439. package/dist/scan-activity.js +172 -64
  440. package/dist/scan-activity.js.map +1 -1
  441. package/dist/scan-comparison.d.ts +10 -5
  442. package/dist/scan-comparison.d.ts.map +1 -1
  443. package/dist/scan-comparison.js +68 -25
  444. package/dist/scan-comparison.js.map +1 -1
  445. package/dist/scan-dashboard.d.ts +7 -6
  446. package/dist/scan-dashboard.d.ts.map +1 -1
  447. package/dist/scan-dashboard.js +91 -79
  448. package/dist/scan-dashboard.js.map +1 -1
  449. package/dist/scan-executor.d.ts +12 -1
  450. package/dist/scan-executor.d.ts.map +1 -1
  451. package/dist/scan-executor.js +22 -2
  452. package/dist/scan-executor.js.map +1 -1
  453. package/dist/scan-history-renderer.d.ts.map +1 -1
  454. package/dist/scan-history-renderer.js +31 -28
  455. package/dist/scan-history-renderer.js.map +1 -1
  456. package/dist/scan-logs.d.ts +12 -0
  457. package/dist/scan-logs.d.ts.map +1 -1
  458. package/dist/scan-logs.js +106 -77
  459. package/dist/scan-logs.js.map +1 -1
  460. package/dist/scan-measurement.d.ts +26 -0
  461. package/dist/scan-measurement.d.ts.map +1 -0
  462. package/dist/scan-measurement.js +77 -0
  463. package/dist/scan-measurement.js.map +1 -0
  464. package/dist/scan-progress.d.ts +1 -0
  465. package/dist/scan-progress.d.ts.map +1 -1
  466. package/dist/scan-progress.js +3 -0
  467. package/dist/scan-progress.js.map +1 -1
  468. package/dist/scan-sessions.d.ts.map +1 -1
  469. package/dist/scan-sessions.js +4 -1
  470. package/dist/scan-sessions.js.map +1 -1
  471. package/dist/scan-settings.d.ts +1 -1
  472. package/dist/scan-settings.d.ts.map +1 -1
  473. package/dist/scan-settings.js +1 -24
  474. package/dist/scan-settings.js.map +1 -1
  475. package/dist/scanner-import.d.ts +64 -6
  476. package/dist/scanner-import.d.ts.map +1 -1
  477. package/dist/scanner-import.js +406 -48
  478. package/dist/scanner-import.js.map +1 -1
  479. package/dist/scanner-repository-url.d.ts +17 -0
  480. package/dist/scanner-repository-url.d.ts.map +1 -0
  481. package/dist/scanner-repository-url.js +71 -0
  482. package/dist/scanner-repository-url.js.map +1 -0
  483. package/dist/security-policy.d.ts +1 -1
  484. package/dist/security-policy.d.ts.map +1 -1
  485. package/dist/security-policy.js.map +1 -1
  486. package/dist/semgrep-findings.d.ts +6 -3
  487. package/dist/semgrep-findings.d.ts.map +1 -1
  488. package/dist/semgrep-findings.js +124 -88
  489. package/dist/semgrep-findings.js.map +1 -1
  490. package/dist/semgrep-guardian-findings.d.ts +38 -0
  491. package/dist/semgrep-guardian-findings.d.ts.map +1 -0
  492. package/dist/semgrep-guardian-findings.js +165 -0
  493. package/dist/semgrep-guardian-findings.js.map +1 -0
  494. package/dist/semgrep-platform-collector.d.ts +26 -0
  495. package/dist/semgrep-platform-collector.d.ts.map +1 -0
  496. package/dist/semgrep-platform-collector.js +108 -0
  497. package/dist/semgrep-platform-collector.js.map +1 -0
  498. package/dist/semgrep-platform-findings.d.ts +213 -0
  499. package/dist/semgrep-platform-findings.d.ts.map +1 -0
  500. package/dist/semgrep-platform-findings.js +1078 -0
  501. package/dist/semgrep-platform-findings.js.map +1 -0
  502. package/dist/semgrep-platform-v1.d.ts +6 -0
  503. package/dist/semgrep-platform-v1.d.ts.map +1 -0
  504. package/dist/semgrep-platform-v1.js +83 -0
  505. package/dist/semgrep-platform-v1.js.map +1 -0
  506. package/dist/server/api.d.ts +1 -1
  507. package/dist/server/api.d.ts.map +1 -1
  508. package/dist/server/api.js.map +1 -1
  509. package/dist/server/embedding-tokenizer.cdx.json +33 -0
  510. package/dist/server/embedding-tokenizer.d.ts +2 -0
  511. package/dist/server/embedding-tokenizer.d.ts.map +1 -0
  512. package/dist/server/embedding-tokenizer.js +322 -0
  513. package/dist/server/embedding-tokenizer.js.map +6 -0
  514. package/dist/server/embeddings.d.ts +16 -2
  515. package/dist/server/embeddings.d.ts.map +1 -1
  516. package/dist/server/embeddings.js +35 -5
  517. package/dist/server/embeddings.js.map +1 -1
  518. package/dist/session-lines.d.ts +5 -0
  519. package/dist/session-lines.d.ts.map +1 -0
  520. package/dist/session-lines.js +60 -0
  521. package/dist/session-lines.js.map +1 -0
  522. package/dist/severity-review.d.ts +3 -1
  523. package/dist/severity-review.d.ts.map +1 -1
  524. package/dist/severity-review.js +3 -2
  525. package/dist/severity-review.js.map +1 -1
  526. package/dist/severity-rubric.d.ts +20 -0
  527. package/dist/severity-rubric.d.ts.map +1 -0
  528. package/dist/severity-rubric.js +73 -0
  529. package/dist/severity-rubric.js.map +1 -0
  530. package/dist/severity-store.js +15 -5
  531. package/dist/severity-store.js.map +1 -1
  532. package/dist/skill-authentication.d.ts +1 -0
  533. package/dist/skill-authentication.d.ts.map +1 -1
  534. package/dist/skill-authentication.js +1 -0
  535. package/dist/skill-authentication.js.map +1 -1
  536. package/dist/snyk-findings.d.ts +7 -2
  537. package/dist/snyk-findings.d.ts.map +1 -1
  538. package/dist/snyk-findings.js +108 -84
  539. package/dist/snyk-findings.js.map +1 -1
  540. package/dist/snyk-rest-findings.d.ts +76 -0
  541. package/dist/snyk-rest-findings.d.ts.map +1 -0
  542. package/dist/snyk-rest-findings.js +1051 -0
  543. package/dist/snyk-rest-findings.js.map +1 -0
  544. package/dist/snyk-rest-v1.d.ts +28 -0
  545. package/dist/snyk-rest-v1.d.ts.map +1 -0
  546. package/dist/snyk-rest-v1.js +132 -0
  547. package/dist/snyk-rest-v1.js.map +1 -0
  548. package/dist/snyk-token-origin.d.ts +4 -0
  549. package/dist/snyk-token-origin.d.ts.map +1 -0
  550. package/dist/snyk-token-origin.js +82 -0
  551. package/dist/snyk-token-origin.js.map +1 -0
  552. package/dist/sonar-collector.d.ts +16 -0
  553. package/dist/sonar-collector.d.ts.map +1 -0
  554. package/dist/sonar-collector.js +380 -0
  555. package/dist/sonar-collector.js.map +1 -0
  556. package/dist/sonar-findings.d.ts +49 -0
  557. package/dist/sonar-findings.d.ts.map +1 -0
  558. package/dist/sonar-findings.js +288 -0
  559. package/dist/sonar-findings.js.map +1 -0
  560. package/dist/sonar-import.d.ts +111 -0
  561. package/dist/sonar-import.d.ts.map +1 -0
  562. package/dist/sonar-import.js +277 -0
  563. package/dist/sonar-import.js.map +1 -0
  564. package/dist/suggest-owners.d.ts.map +1 -1
  565. package/dist/suggest-owners.js +2 -1
  566. package/dist/suggest-owners.js.map +1 -1
  567. package/dist/target-drift.d.ts +9 -0
  568. package/dist/target-drift.d.ts.map +1 -0
  569. package/dist/target-drift.js +27 -0
  570. package/dist/target-drift.js.map +1 -0
  571. package/dist/targets.d.ts +5 -0
  572. package/dist/targets.d.ts.map +1 -1
  573. package/dist/targets.js +73 -31
  574. package/dist/targets.js.map +1 -1
  575. package/dist/tenable-findings.d.ts +9 -0
  576. package/dist/tenable-findings.d.ts.map +1 -0
  577. package/dist/tenable-findings.js +122 -0
  578. package/dist/tenable-findings.js.map +1 -0
  579. package/dist/terminal-columns.d.ts +4 -0
  580. package/dist/terminal-columns.d.ts.map +1 -0
  581. package/dist/terminal-columns.js +19 -0
  582. package/dist/terminal-columns.js.map +1 -0
  583. package/dist/terminal-stream.d.ts +13 -0
  584. package/dist/terminal-stream.d.ts.map +1 -0
  585. package/dist/terminal-stream.js +35 -0
  586. package/dist/terminal-stream.js.map +1 -0
  587. package/dist/trivy-findings.d.ts +19 -0
  588. package/dist/trivy-findings.d.ts.map +1 -0
  589. package/dist/trivy-findings.js +435 -0
  590. package/dist/trivy-findings.js.map +1 -0
  591. package/dist/trivy-import.d.ts +19 -0
  592. package/dist/trivy-import.d.ts.map +1 -0
  593. package/dist/trivy-import.js +151 -0
  594. package/dist/trivy-import.js.map +1 -0
  595. package/dist/validation-records.d.ts +0 -3
  596. package/dist/validation-records.d.ts.map +1 -1
  597. package/dist/validation-records.js +1 -17
  598. package/dist/validation-records.js.map +1 -1
  599. package/dist/veracode-findings.d.ts +9 -0
  600. package/dist/veracode-findings.d.ts.map +1 -0
  601. package/dist/veracode-findings.js +409 -0
  602. package/dist/veracode-findings.js.map +1 -0
  603. package/dist/vertex-pricing.d.ts +8 -0
  604. package/dist/vertex-pricing.d.ts.map +1 -0
  605. package/dist/vertex-pricing.js +20 -0
  606. package/dist/vertex-pricing.js.map +1 -0
  607. package/dist/windows-path.d.ts +1 -0
  608. package/dist/windows-path.d.ts.map +1 -1
  609. package/dist/windows-path.js +27 -1
  610. package/dist/windows-path.js.map +1 -1
  611. package/dist/wiz-csv.d.ts +2 -2
  612. package/dist/wiz-csv.d.ts.map +1 -1
  613. package/dist/wiz-csv.js +1 -3
  614. package/dist/wiz-csv.js.map +1 -1
  615. package/dist/wiz-findings.d.ts +14 -2
  616. package/dist/wiz-findings.d.ts.map +1 -1
  617. package/dist/wiz-findings.js +143 -50
  618. package/dist/wiz-findings.js.map +1 -1
  619. package/docs/classify-severity-records.md +28 -13
  620. package/docs/cli.md +623 -843
  621. package/docs/dedupe-records.md +54 -24
  622. package/docs/findings-service.md +87 -47
  623. package/docs/hosted-remediation.md +14 -7
  624. package/docs/hosted-scans.md +64 -41
  625. package/docs/telemetry.md +236 -0
  626. package/docs/validate-records.md +78 -44
  627. package/package.json +19 -17
  628. package/schemas/external-findings.schema.json +14 -1
  629. package/schemas/hosted-scan-v2.fixture.json +16 -1
  630. package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
  631. package/_bundled_plugin/mcp/helpers.mjs.br.part-001 +0 -0
  632. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  633. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  634. package/_bundled_plugin/mcp/server.mjs.br.part-002 +0 -0
  635. package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +0 -96
  636. package/_bundled_plugin/scripts/validate_tracking_source.py +0 -82
  637. package/_bundled_plugin/scripts/workbench_local_dedupe.py +0 -136
  638. package/dist/deduplication/records-protocol.d.ts +0 -4
  639. package/dist/deduplication/records-protocol.d.ts.map +0 -1
  640. package/dist/deduplication/records-protocol.js +0 -49
  641. package/dist/deduplication/records-protocol.js.map +0 -1
  642. package/dist/external-imports-feature.d.ts +0 -6
  643. package/dist/external-imports-feature.d.ts.map +0 -1
  644. package/dist/external-imports-feature.js +0 -51
  645. package/dist/external-imports-feature.js.map +0 -1
  646. package/dist/finding-normalization.d.ts +0 -5
  647. package/dist/finding-normalization.d.ts.map +0 -1
  648. package/dist/finding-normalization.js +0 -120
  649. package/dist/finding-normalization.js.map +0 -1
@@ -1,23 +1,45 @@
1
1
  import { hash, randomUUID } from "node:crypto";
2
2
  import { chmodSync } from "node:fs";
3
3
  import { lstat, mkdir, open, readFile, readdir, rename, rm, } from "node:fs/promises";
4
- import { createRequire } from "node:module";
5
4
  import { join } from "node:path";
5
+ import { nullIfMissingFile } from "./targets.js";
6
6
  import { setTimeout as delay } from "node:timers/promises";
7
+ import { isBoxedPrimitive, isNativeError, isProxy, isStringObject, } from "node:util/types";
8
+ import { privateImportClient, privateJobVersions, selectPrivateReport, readPrivateCheckpoint, parsePrivateCheckpoint, unsuccessfulJob, } from "./private-finding-import.js";
7
9
  import { readCloudCredentials } from "./cloud-publish.js";
10
+ import { declaredScannerRepositoryUrl } from "./scanner-repository-url.js";
11
+ import { tenableReportFormat } from "./tenable-findings.js";
12
+ import { guardianFindingId, guardianFindingRepository, guardianSelectionFormat, } from "./semgrep-guardian-findings.js";
8
13
  import { cloudBaseUrl, cloudFindingsUrl, DEFAULT_CLOUD_BASE_URL, } from "./cloud-endpoint.js";
9
- import { CodexSecurityError } from "./errors.js";
10
- import { requireExternalImports } from "./external-imports-feature.js";
11
- import { codexSecurityStateDirectory, createWindowsOutputDirectory, validatePreparedOutputDir, } from "./runtime.js";
12
- import { EXTERNAL_FINDINGS_PROVIDERS, readExternalFindings, } from "./external-findings-read.js";
13
- import { canonicalJson } from "./record.js";
14
+ import { CodexSecurityError, errorMessage } from "./errors.js";
15
+ import { codexSecurityStateDirectory, createWindowsOutputDirectory, validatePreparedOutputDir, sqliteDatabaseOpener, } from "./runtime.js";
16
+ import { EXTERNAL_FINDINGS_PROVIDERS, readExternalFindings, requireCloudReader, semgrepSarifRepositories, snykFileRepositories, } from "./external-findings-read.js";
17
+ import { canSnapshotWithoutGetters, canonicalJson, isRecord, } from "./record.js";
18
+ import { notify } from "./value.js";
14
19
  import { wizCsvRepositoryUrl } from "./wiz-csv.js";
20
+ import { isWizPackageFinding, wizFindingRepository } from "./wiz-findings.js";
21
+ import { getOpenGrepSourcePositions } from "./opengrep-findings.js";
22
+ import { previousReaderSelection, hasPreviousCloudProjection, } from "./reader-selection.js";
15
23
  import { dependabotRepository } from "./github-dependabot-findings.js";
16
- import { snykRepositoryUrls } from "./snyk-findings.js";
17
- import { endorRepositoryUrls } from "./endor-findings.js";
24
+ import { retainedGitHubRepository } from "./github-findings.js";
25
+ import { normalizePreviousSemgrepFindings, normalizeSemgrepFindings, readSemgrepReport, } from "./semgrep-findings.js";
26
+ import { normalizeSnykFile, readSnykReport, snykRepositoryUrls, } from "./snyk-findings.js";
27
+ import { isSnykRestFinding, isSnykRestFormat, snykRestCheckpointFindingId, snykRestImportRepository, snykRestRepositoryMatches, } from "./snyk-rest-findings.js";
28
+ import { hasRetainedSemgrepScope, isSemgrepPlatformEvidence, isSemgrepPlatformFormat, semgrepPlatformFormat, parseSemgrepResponse, semgrepLegacyRepositoryWitnesses, semgrepNativeProjectUrl, semgrepPlatformFindingBinding, semgrepPlatformRepositoryReference, semgrepPlatformReportScope, } from "./semgrep-platform-findings.js";
29
+ import { semgrepV1Id } from "./semgrep-platform-v1.js";
30
+ import { hostedRepositoryUrlKey, normalizeHostedGitRepositoryUrl, } from "./hosted-git-repository.js";
31
+ import { trivyRemoteKey, trivyRepositoryUrl } from "./trivy-findings.js";
32
+ import { hostedRepositoryUrl } from "./hosted-scanner-http.js";
33
+ import { endorRepositoryUrls, requiresStructuredEndorReport, } from "./endor-findings.js";
34
+ import { blackDuckReportRepositories, projectedBlackDuckRepositoryUrl, } from "./blackduck-findings.js";
35
+ import { checkmarxSelection } from "./checkmarx-repository.js";
36
+ import { continuumReportRepositories } from "./continuum-findings.js";
37
+ import { veracodeReportRepositories } from "./veracode-findings.js";
18
38
  import { inspectorReportRepository } from "./inspector-findings.js";
19
- import { snapshotImportOptions, } from "./external-findings.js";
20
- import { cloudRepositoryUrlKey, validateImportRequest, validateExternalEvidence, validateExternalReport, validateImportReceipt, validateRepositories, validateSourceReports, validateSourceReport, } from "./external-import-contract.js";
39
+ import { sonarRepositoryUrls } from "./sonar-findings.js";
40
+ import { checkRetainedTrivyProvider, checkTrivyReportProvider, isTrivyReportHeader, retainedTrivyReport, trivyBatchSourceUrls, validateTrivyRepositoryUrl, } from "./trivy-import.js";
41
+ import { NoPublishableFindingsError, reportForJson, snapshotSourceRepositories, snapshotImportOptions, cloneImportPreview, nativeErrorStackGetter, snapshotNormalizedFindings, } from "./external-findings.js";
42
+ import { canonicalNumber, cloudRepositoryUrlKey, CloudEvidenceDepthError, validateImportRequest, validateExternalEvidence, validatePreviousCloudEvidence, validateExternalReport, validateImportReceipt, validateRepositories, validateSourceReports, validateSourceReport, } from "./external-import-contract.js";
21
43
  const MAX_REQUEST_BYTES = 5 * 1024 * 1024;
22
44
  /** Preserve accepted work in CLI JSON output when publication cannot finish. */
23
45
  export class ExternalPublicationError extends CodexSecurityError {
@@ -41,36 +63,147 @@ async function readInBatches(items, read) {
41
63
  }
42
64
  return output;
43
65
  }
66
+ // JSON.stringify serializes boxed primitives after the replacer. Let it
67
+ // produce their actual wire values, including false and strings.
68
+ function wireJson(value) {
69
+ return canonicalJson(value, undefined, isBoxedPrimitive);
70
+ }
71
+ function jsonDigest(value, serialize = wireJson) {
72
+ return hash("sha256", serialize(value));
73
+ }
74
+ function matchesRepositoryEvidence(expected, stored) {
75
+ const storedWire = wireJson(stored);
76
+ if (wireJson(expected) === storedWire)
77
+ return true;
78
+ // A repository can store a literal first-component colon with an explicit
79
+ // relative prefix. Match only that spelling in typed locations and their manifests.
80
+ const qualified = new Map();
81
+ const locations = expected.locations?.map((location, index) => {
82
+ const { path } = location;
83
+ const actual = stored.locations?.[index]?.path;
84
+ if (actual !== `./${path}` || !/^[^/:\\@]+:[^/\\]/u.test(path))
85
+ return location;
86
+ qualified.set(path, actual);
87
+ return { ...location, path: actual };
88
+ });
89
+ if (!qualified.size)
90
+ return false;
91
+ const packages = expected.packages?.map((item) => {
92
+ const path = item.manifest_path && qualified.get(item.manifest_path);
93
+ return path ? { ...item, manifest_path: path } : item;
94
+ });
95
+ return wireJson({ ...expected, locations, packages }) === storedWire;
96
+ }
97
+ function isCompleteUnscoredOpenGrepReport(report) {
98
+ let rows = report.excluded.length;
99
+ for (const { evidence } of report.findings) {
100
+ const traces = evidence.source_data?.["results"];
101
+ if (evidence.details?.scanner_origin !== "OpenGrep" ||
102
+ evidence.severity !== "unknown" ||
103
+ !Array.isArray(traces))
104
+ return false;
105
+ rows += traces.length;
106
+ }
107
+ return rows === report.read;
108
+ }
109
+ function repositoryFindings(report, severities) {
110
+ return severities
111
+ ? report.findings.filter(({ evidence }) => severities.includes(evidence.severity))
112
+ : report.findings;
113
+ }
114
+ function repositorySelection(report, severities, sourcePositions) {
115
+ const findings = repositoryFindings(report, severities);
116
+ if (findings.length === report.findings.length)
117
+ return report;
118
+ const excluded = [...report.excluded];
119
+ const skipped = new Set(excluded.map(({ position }) => position));
120
+ function exclude(position, id, severity) {
121
+ excluded.push({
122
+ position,
123
+ source_finding_id: id,
124
+ reason: `The repository publisher does not accept severity ${JSON.stringify(severity)}. A local import or an available private inbox can retain this finding.`,
125
+ });
126
+ }
127
+ // Serialized OpenGrep selections retain every trace but lose the reader's
128
+ // position map. All these traces have unknown severity, so the remaining
129
+ // source rows are provably excluded even when their individual IDs are lost.
130
+ if (!sourcePositions && isCompleteUnscoredOpenGrepReport(report)) {
131
+ const id = report.findings.length === 1
132
+ ? report.findings[0].source_finding_id
133
+ : null;
134
+ for (let row = 1; row <= report.read; row++)
135
+ if (!skipped.has(row))
136
+ exclude(row, id, "unknown");
137
+ }
138
+ else {
139
+ const retained = new Set(findings);
140
+ let position = 0;
141
+ for (const finding of report.findings) {
142
+ do {
143
+ position++;
144
+ } while (skipped.has(position));
145
+ if (retained.has(finding))
146
+ continue;
147
+ const { source_finding_id, evidence } = finding;
148
+ for (const row of sourcePositions?.get(source_finding_id) ?? [position])
149
+ exclude(row, source_finding_id, evidence.severity);
150
+ }
151
+ }
152
+ return {
153
+ ...report,
154
+ findings,
155
+ excluded: excluded.sort((a, b) => a.position - b.position),
156
+ };
157
+ }
44
158
  function sameSubmission(left, right) {
45
159
  return (left.accountId === right.accountId &&
160
+ left.trivyRepositoryUrl === right.trivyRepositoryUrl &&
46
161
  left.inspectorProjectMapRequired === right.inspectorProjectMapRequired &&
47
- canonicalJson(left.requests) === canonicalJson(right.requests));
162
+ wireJson(left.requests) === wireJson(right.requests));
163
+ }
164
+ function wireFinding({ source_finding_id, evidence, }) {
165
+ return { source_finding_id, evidence };
48
166
  }
49
167
  function submissionFindings(requests) {
50
- return requests.flatMap((batch) => batch.items.map(({ source_finding_id, evidence }) => ({
51
- source_finding_id,
52
- evidence,
53
- })));
168
+ return requests.flatMap((batch) => batch.items.map(wireFinding));
169
+ }
170
+ function sameLegacyJson(saved, current, equivalent) {
171
+ if (saved === current || equivalent(saved, current))
172
+ return true;
173
+ if (Array.isArray(saved) && Array.isArray(current))
174
+ return (saved.length === current.length &&
175
+ saved.every((child, i) => sameLegacyJson(child, current[i], equivalent)));
176
+ if (!isRecord(saved) || !isRecord(current))
177
+ return false;
178
+ return (Object.keys(current).every((key) => Object.hasOwn(saved, key)) &&
179
+ Object.keys(saved).every((key) => sameLegacyJson(saved[key], current[key], equivalent)));
180
+ }
181
+ // A copied reader result can retain the JSON from Buffer.toJSON but lose
182
+ // the native Buffer that the previous reader cloned into a Uint8Array.
183
+ function previousBufferJson(saved, current) {
184
+ if (!isRecord(current) ||
185
+ current["type"] !== "Buffer" ||
186
+ !Array.isArray(current["data"]))
187
+ return false;
188
+ return (isRecord(saved) &&
189
+ Object.keys(current).length === 2 &&
190
+ Object.keys(saved).length === current["data"].length &&
191
+ current["data"].every((byte, index) => Number.isInteger(byte) &&
192
+ byte >= 0 &&
193
+ byte <= 255 &&
194
+ saved[index] === byte));
54
195
  }
55
196
  function sameFindingSelection(left, right) {
56
197
  return (left.length === right.length &&
57
198
  left.every((finding, index) => finding.source_finding_id === right[index].source_finding_id));
58
199
  }
59
200
  async function readOptionalFile(path) {
60
- return readFile(path, "utf8").catch((error) => {
61
- if (error.code === "ENOENT")
62
- return undefined;
63
- throw error;
64
- });
201
+ return (await readFile(path, "utf8").catch(nullIfMissingFile)) ?? undefined;
65
202
  }
66
- function reportForJson(report) {
67
- const errors = report?.["errors"];
68
- if (!Array.isArray(errors))
69
- return report;
70
- const json = JSON.stringify(errors, (_key, entry) => entry instanceof Error ? { message: entry.message } : entry);
71
- return { ...report, errors: JSON.parse(json) };
203
+ export function externalPublicationStateDirectory(environment) {
204
+ return join(codexSecurityStateDirectory(environment), "external-finding-publications");
72
205
  }
73
- async function writeAtomicJson(path, value) {
206
+ export async function writeAtomicJson(path, value) {
74
207
  const temporary = `${path}.${randomUUID()}.tmp`;
75
208
  try {
76
209
  const file = await open(temporary, "wx", 0o600);
@@ -99,15 +232,13 @@ class CloudImportError extends CodexSecurityError {
99
232
  }
100
233
  function validatePublicationReceipt(value, batch) {
101
234
  const receipt = validateImportReceipt(value);
102
- const batchItems = new Map(batch.items.map((item) => [item.client_id, item]));
235
+ const remaining = new Set(batch.items.map((item) => item.client_id));
103
236
  if (receipt.id !== batch.request_id ||
104
- canonicalJson(receipt.repository) !== canonicalJson(batch.repository) ||
105
- canonicalJson(receipt.source) !== canonicalJson(batch.source) ||
237
+ wireJson(receipt.repository) !== wireJson(batch.repository) ||
238
+ wireJson(receipt.source) !== wireJson(batch.source) ||
106
239
  receipt.item_count !== batch.items.length ||
107
240
  receipt.results.length !== batch.items.length ||
108
- new Set(receipt.results.map((item) => item.client_id)).size !==
109
- batch.items.length ||
110
- receipt.results.some((item) => !batchItems.has(item.client_id)))
241
+ receipt.results.some((item) => !remaining.delete(item.client_id)))
111
242
  throw new CodexSecurityError("Cloud returned a receipt for a different publication.");
112
243
  const actual = { created: 0, updated: 0, unchanged: 0, error: 0 };
113
244
  for (const result of receipt.results) {
@@ -127,138 +258,800 @@ function validatePublicationReceipt(value, batch) {
127
258
  result.version === null)
128
259
  throw new CodexSecurityError("Cloud returned an incomplete saved finding.");
129
260
  }
130
- if (canonicalJson(receipt.counts) !== canonicalJson(actual))
261
+ if (wireJson(receipt.counts) !== wireJson(actual))
131
262
  throw new CodexSecurityError("Cloud returned inconsistent publication counts.");
132
263
  return receipt;
133
264
  }
134
- async function withImportLock(state, key, signal, operation) {
265
+ async function withImportLocks(state, keys, signal, operation) {
135
266
  if (process.platform === "win32")
136
267
  await createWindowsOutputDirectory(state, true);
137
268
  else
138
269
  await mkdir(state, { recursive: true, mode: 0o700 });
139
270
  await validatePreparedOutputDir(state, undefined, true);
140
- const path = join(state, `${key}.lock.sqlite`);
141
- const metadata = await lstat(path).catch((error) => {
142
- if (error.code === "ENOENT")
143
- return undefined;
144
- throw error;
145
- });
146
- if (metadata && !metadata.isFile())
147
- throw new CodexSecurityError("The publication lock is not a regular file.");
148
- const require = createRequire(import.meta.url);
149
- const Database = (process.versions["bun"]
150
- ? require("bun:sqlite").Database
151
- : require("node:sqlite").DatabaseSync);
152
- const guard = new Database(path);
271
+ const openDatabase = sqliteDatabaseOpener();
272
+ const guards = [];
153
273
  try {
154
- if (process.platform !== "win32")
155
- chmodSync(path, 0o600);
156
- guard.exec("PRAGMA busy_timeout = 0");
274
+ for (const key of [...new Set(keys)].sort()) {
275
+ const path = join(state, `${key}.lock.sqlite`);
276
+ const metadata = await lstat(path).catch(nullIfMissingFile);
277
+ if (metadata && !metadata.isFile())
278
+ throw new CodexSecurityError("The publication lock is not a regular file.");
279
+ const guard = openDatabase(path);
280
+ guards.push(guard);
281
+ if (process.platform !== "win32")
282
+ chmodSync(path, 0o600);
283
+ guard.exec("PRAGMA busy_timeout = 0");
284
+ }
157
285
  for (;;) {
158
286
  signal?.throwIfAborted();
287
+ let acquired = 0;
159
288
  try {
160
- // Keep this inode across invocations. SQLite releases the transaction
161
- // on process exit, so a paused publisher cannot lose its lock to a lease.
162
- guard.exec("BEGIN EXCLUSIVE");
289
+ // SQLite releases these transactions on process exit.
290
+ for (const guard of guards) {
291
+ guard.exec("BEGIN EXCLUSIVE");
292
+ acquired++;
293
+ }
163
294
  break;
164
295
  }
165
296
  catch (error) {
166
297
  const sqliteError = error;
167
298
  if (sqliteError.errcode !== 5 && sqliteError.code !== "SQLITE_BUSY")
168
299
  throw error;
300
+ // Older SDKs can acquire historical keys in a different order.
301
+ for (const guard of guards.slice(0, acquired))
302
+ guard.exec("ROLLBACK");
169
303
  await delay(50, undefined, { signal });
170
304
  }
171
305
  }
172
306
  return await operation();
173
307
  }
174
308
  finally {
175
- guard.close();
309
+ for (const guard of guards)
310
+ guard.close();
311
+ }
312
+ }
313
+ function hostedSourceReferences(readerSources, url, sourcePlatform) {
314
+ // The catalog supplies a GitHub identity only when the reader has no SCM mapping.
315
+ const sources = readerSources.length || !url
316
+ ? readerSources
317
+ : [{ url, platform: "github" }];
318
+ return sourcePlatform === "other"
319
+ ? sources.filter(({ platform }) => platform !== "github")
320
+ : sources;
321
+ }
322
+ function storedFindingId(finding) {
323
+ const seen = new Set();
324
+ for (let owner = finding; owner && !seen.has(owner); owner = Object.getPrototypeOf(owner)) {
325
+ const property = Object.getOwnPropertyDescriptor(owner, "source_finding_id");
326
+ if (property) {
327
+ const id = property.value;
328
+ return typeof id === "string" || isStringObject(id)
329
+ ? String.prototype.valueOf.call(id)
330
+ : "";
331
+ }
332
+ seen.add(owner);
333
+ }
334
+ return "";
335
+ }
336
+ function checkFindingProvider(input, provider) {
337
+ function checkFormat(format) {
338
+ if (format === tenableReportFormat)
339
+ requireCloudReader({ provider: "tenable" });
340
+ if (format === guardianSelectionFormat && provider !== "semgrep")
341
+ throw new CodexSecurityError("Use the semgrep provider for Guardian findings.");
342
+ }
343
+ checkFormat(ownData(ownData(input, "report"), "format"));
344
+ if (!Array.isArray(input?.findings))
345
+ return;
346
+ for (const row of input.findings) {
347
+ if (!row)
348
+ continue;
349
+ if (guardianFindingId.test(storedFindingId(row)))
350
+ checkFormat(guardianSelectionFormat);
351
+ checkFormat(ownData(ownData(row.evidence, "source_data"), "format"));
176
352
  }
177
353
  }
178
354
  export async function prepareExternalPublication(path, options, dependencies = {}) {
179
- requireExternalImports(dependencies.environment);
180
355
  const settings = snapshotImportOptions(options);
181
- const parsed = await readExternalFindings(path, settings);
182
- return preparePublicationInput(parsed.publication ?? normalizedPublicationInput(parsed.findings), { ...settings, provider: EXTERNAL_FINDINGS_PROVIDERS[settings.provider] }, dependencies);
356
+ requireCloudReader(settings);
357
+ const readerSources = snapshotSourceRepositories(dependencies.sourceRepositories);
358
+ const provider = EXTERNAL_FINDINGS_PROVIDERS[settings.provider];
359
+ let input;
360
+ let fileRepositories;
361
+ if (settings.provider === "semgrep" || settings.provider === "snyk") {
362
+ const semgrep = settings.provider === "semgrep";
363
+ const report = await (semgrep
364
+ ? readSemgrepReport(path)
365
+ : readSnykReport(path));
366
+ const normalize = async (url, platform) => {
367
+ const references = hostedSourceReferences(readerSources, url, semgrep ? platform : undefined);
368
+ const semgrepOptions = {
369
+ sourceRepositoryUrl: settings.sourceRepositoryUrl,
370
+ sourceRepositories: references,
371
+ };
372
+ const findings = semgrep
373
+ ? normalizeSemgrepFindings(report, semgrepOptions, "cloud")
374
+ : await normalizeSnykFile(report, "cloud", references);
375
+ const selection = normalizedPublicationInput(findings, provider);
376
+ if (semgrep && hasPreviousCloudProjection(findings))
377
+ selection.readPreviousFindings = () => normalizePreviousSemgrepFindings(report, semgrepOptions);
378
+ return selection;
379
+ };
380
+ const hosted = isRecord(report) &&
381
+ (semgrep
382
+ ? isSemgrepPlatformFormat(report["format"]) ||
383
+ report["format"] === guardianSelectionFormat
384
+ : isSnykRestFormat(report["format"]));
385
+ if (!semgrep && !hosted)
386
+ fileRepositories = () => snykFileRepositories(report);
387
+ input = hosted
388
+ ? async (destination) => {
389
+ try {
390
+ const projection = await normalize(destination.url);
391
+ if (!semgrep)
392
+ return projection;
393
+ return {
394
+ ...projection,
395
+ recheckFileRepository: async (approvedUrl, platform) => {
396
+ if (platform === "other")
397
+ await normalize(approvedUrl, platform);
398
+ },
399
+ };
400
+ }
401
+ catch (error) {
402
+ const project = report["project"];
403
+ const candidate = report["sourceRepositoryUrl"] ??
404
+ settings.sourceRepositoryUrl ??
405
+ (semgrep && isRecord(project) ? project["url"] : undefined);
406
+ if (typeof candidate !== "string" || candidate === destination.url)
407
+ throw error;
408
+ // A renamed repository can require its old spelling to identify the saved request.
409
+ // Only an approved checkpoint can later authorize that spelling.
410
+ const projection = await normalize(normalizeHostedGitRepositoryUrl(candidate, destination.url, true));
411
+ return {
412
+ ...projection,
413
+ recheckFileRepository: (approvedUrl, platform) => {
414
+ if (approvedUrl === destination.url)
415
+ throw error;
416
+ return normalize(approvedUrl, platform);
417
+ },
418
+ };
419
+ }
420
+ }
421
+ : await normalize();
422
+ }
423
+ else {
424
+ const parsed = await readExternalFindings(path, settings);
425
+ checkFindingProvider(parsed.findings, provider);
426
+ input =
427
+ parsed.publication ??
428
+ normalizedPublicationInput(parsed.findings, provider);
429
+ input.readPreviousFindings = parsed.previousFindings;
430
+ if (provider === "endor") {
431
+ fileRepositories = () => parsed.sourceRepositories ?? [];
432
+ input.requiresStructuredReport = requiresStructuredEndorReport(parsed.findings);
433
+ }
434
+ }
435
+ return preparePublicationInput(input, { ...settings, provider }, dependencies, readerSources, settings.sourceRepositoryUrl, path, fileRepositories);
183
436
  }
184
437
  /** Prepare normalized provider evidence without reading a vendor export. */
185
438
  export async function prepareNormalizedExternalPublication(input, options, dependencies = {}) {
186
- requireExternalImports(dependencies.environment);
187
439
  const settings = snapshotImportOptions(options);
188
- return preparePublicationInput(normalizedPublicationInput(input), settings, dependencies);
440
+ requireCloudReader(settings);
441
+ return preparePublicationInput(normalizedPublicationInput(input, settings.provider), settings, dependencies, snapshotSourceRepositories(dependencies.sourceRepositories));
442
+ }
443
+ function cloudSourceKey(url, platform) {
444
+ return hostedRepositoryUrlKey(url, platform === "github", platform === "gitlab", "cloud");
445
+ }
446
+ /** Verify every source in a hosted selection, including SDK inputs containing typed-only rows. */
447
+ function hostedRepositoryMatcher(input, provider, declaredUrl, destination, readerSources, allowOpaqueLegacyRows = false) {
448
+ const references = hostedSourceReferences(readerSources, destination.url, input.sourceRepositoryPlatform);
449
+ if (provider === "snyk") {
450
+ const declaredSource = input.sourceRepositoryUrl !== undefined ||
451
+ input.sourceRepositoryPlatform !== undefined ||
452
+ declaredUrl !== undefined ||
453
+ readerSources.length > 0;
454
+ const binding = snykRestImportRepository(input, provider, [
455
+ ...readerSources.map(({ url }) => url),
456
+ ...(declaredUrl === undefined ? [] : [declaredUrl]),
457
+ ], references, undefined, allowOpaqueLegacyRows, declaredSource ? destination.url : undefined);
458
+ if (binding)
459
+ return {
460
+ github: binding.platform === "github",
461
+ matches: (url) => snykRestRepositoryMatches(binding, url),
462
+ };
463
+ }
464
+ if (provider !== "semgrep")
465
+ return;
466
+ const declared = input.sourceRepositoryUrl === undefined
467
+ ? undefined
468
+ : hostedRepositoryUrl(input.sourceRepositoryUrl, "Semgrep source repository");
469
+ const reportScope = semgrepPlatformReportScope(input, provider, references);
470
+ const hasNativeSource = reportScope !== undefined ||
471
+ input.findings.some((finding) => isSemgrepPlatformEvidence(finding, provider));
472
+ const claims = new Set();
473
+ let original = input.report?.["format"] === guardianSelectionFormat
474
+ ? declaredScannerRepositoryUrl(input.report["repositoryUrl"], "Guardian")
475
+ : undefined;
476
+ const nativeRows = [];
477
+ for (const finding of input.findings) {
478
+ const typed = finding.evidence.details?.repository?.url;
479
+ if (typed)
480
+ claims.add(typed);
481
+ const guardian = guardianFindingRepository(finding, "cloud");
482
+ if (guardian) {
483
+ original ??= guardian;
484
+ claims.add(guardian);
485
+ continue;
486
+ }
487
+ const data = finding.evidence.source_data;
488
+ const sarif = semgrepSarifRepositories(finding, hasNativeSource);
489
+ for (const { url } of sarif)
490
+ claims.add(hostedRepositoryUrl(url, "Semgrep source repository"));
491
+ const project = data?.["project"];
492
+ if (!allowOpaqueLegacyRows ||
493
+ !isSemgrepPlatformEvidence(finding, provider) ||
494
+ (isRecord(project) &&
495
+ project["id"] != null &&
496
+ project["name"] != null &&
497
+ data?.["deployment"] != null &&
498
+ data["ref"] != null &&
499
+ data["product"] != null &&
500
+ ((isRecord(data["finding"]) && data["finding"]["id"] != null) ||
501
+ typeof data["semgrepPlatformFindingJson"] === "string"))) {
502
+ nativeRows.push({ finding, sarif });
503
+ continue;
504
+ }
505
+ // Released SDKs could save partial native records. Retain any source still
506
+ // identifiable on that row while validating complete siblings under the new mapping.
507
+ if (!data)
508
+ continue;
509
+ if (reportScope?.projectId !== undefined) {
510
+ const id = isRecord(project) ? project["id"] : undefined;
511
+ if ((id != null &&
512
+ id !== reportScope.projectId &&
513
+ semgrepV1Id(id) !== reportScope.projectId) ||
514
+ ["deployment", "ref", "product"].some((field) => data[field] != null && data[field] !== reportScope[field]))
515
+ throw new CodexSecurityError("The retained Semgrep finding does not match the supplied report scope.");
516
+ }
517
+ const rows = [data["finding"]];
518
+ const raw = data["semgrepPlatformFindingJson"];
519
+ if (typeof raw === "string") {
520
+ try {
521
+ rows.push(parseSemgrepResponse(raw));
522
+ }
523
+ catch {
524
+ // An unreadable historical row contributes no URL. Check the remaining claims.
525
+ }
526
+ }
527
+ const nativeUrls = [isRecord(project) ? project["url"] : undefined];
528
+ if (data["format"] === semgrepPlatformFormat)
529
+ nativeUrls.push(data["nativeProjectUrl"]);
530
+ for (const row of rows) {
531
+ if (!isRecord(row))
532
+ continue;
533
+ if (reportScope?.ref !== undefined &&
534
+ row["ref"] !== undefined &&
535
+ row["ref"] !== reportScope.ref)
536
+ throw new CodexSecurityError("The retained Semgrep finding does not match the supplied report scope.");
537
+ const repository = row["repository"];
538
+ if (isRecord(repository))
539
+ nativeUrls.push(repository["url"]);
540
+ }
541
+ const urls = nativeUrls.map((url) => semgrepNativeProjectUrl(url, references));
542
+ for (const url of [...urls, data["sourceRepositoryUrl"]]) {
543
+ if (typeof url !== "string")
544
+ continue;
545
+ original ??= url;
546
+ claims.add(url);
547
+ }
548
+ }
549
+ const witnesses = semgrepLegacyRepositoryWitnesses(nativeRows.map(({ finding }) => finding), provider, references, "cloud");
550
+ for (const { finding, sarif } of nativeRows) {
551
+ const native = semgrepPlatformFindingBinding(finding, provider, references, witnesses, reportScope, [{ url: projectedBlackDuckRepositoryUrl(finding, "cloud") }, ...sarif], "cloud", allowOpaqueLegacyRows)?.url;
552
+ if (native) {
553
+ original ??= native;
554
+ claims.add(native);
555
+ }
556
+ }
557
+ const source = original ??
558
+ declared ??
559
+ declaredUrl ??
560
+ (readerSources.length || input.sourceRepositoryPlatform !== undefined
561
+ ? destination.url
562
+ : undefined);
563
+ if (source === undefined)
564
+ return;
565
+ const { platform } = semgrepPlatformRepositoryReference(source, references);
566
+ const expected = cloudSourceKey(source, platform);
567
+ const matches = (url) => cloudSourceKey(url, platform) === expected;
568
+ if (declared !== undefined)
569
+ claims.add(declared);
570
+ if (declaredUrl !== undefined)
571
+ claims.add(declaredUrl);
572
+ for (const reader of readerSources)
573
+ claims.add(normalizeHostedGitRepositoryUrl(reader.url, source, reader.platform !== undefined));
574
+ for (const url of claims)
575
+ if (!matches(hostedRepositoryUrl(url, "Semgrep source repository")))
576
+ throw new CodexSecurityError("The finding does not match the original Semgrep project repository.");
577
+ return { github: platform === "github", matches };
578
+ }
579
+ function ownData(value, key) {
580
+ return isRecord(value) && !isProxy(value)
581
+ ? Object.getOwnPropertyDescriptor(value, key)?.value
582
+ : undefined;
583
+ }
584
+ // When native capture would invoke caller code, only the original data
585
+ // properties can identify the earlier selection. Unknown IDs cannot rule it out.
586
+ function previousFindingIds(input) {
587
+ const rows = ownData(input, "findings");
588
+ if (!Array.isArray(rows) || isProxy(rows))
589
+ return null;
590
+ return Array.from({ length: rows.length }, (_, index) => {
591
+ const row = Object.getOwnPropertyDescriptor(rows, index)?.value;
592
+ const id = ownData(row, "source_finding_id");
593
+ return typeof id === "string" ? id : undefined;
594
+ });
595
+ }
596
+ function withDataProperty(record, key, value) {
597
+ const fields = Object.getOwnPropertyDescriptors(record);
598
+ return Object.create(Object.getPrototypeOf(record), {
599
+ ...fields,
600
+ [key]: { ...fields[key], value },
601
+ });
602
+ }
603
+ // Native cloning discarded enumerable diagnostic fields. Recover its message
604
+ // without reading the caller's stack getter or changing their report.
605
+ const nativeDiagnosticPrototypes = new Set([
606
+ Error.prototype,
607
+ EvalError.prototype,
608
+ RangeError.prototype,
609
+ ReferenceError.prototype,
610
+ SyntaxError.prototype,
611
+ TypeError.prototype,
612
+ URIError.prototype,
613
+ ]);
614
+ function previousDiagnosticMessage(diagnostic) {
615
+ const stack = Object.getOwnPropertyDescriptor(diagnostic, "stack");
616
+ const message = ownData(diagnostic, "message");
617
+ const cause = Object.getOwnPropertyDescriptor(diagnostic, "cause");
618
+ const name = Object.getOwnPropertyDescriptor(diagnostic, "name");
619
+ if (!nativeDiagnosticPrototypes.has(Object.getPrototypeOf(diagnostic)) ||
620
+ typeof message !== "string" ||
621
+ (stack?.get && stack.get !== nativeErrorStackGetter) ||
622
+ (name && typeof name.value !== "string") ||
623
+ (cause &&
624
+ (!Object.hasOwn(cause, "value") ||
625
+ !canSnapshotWithoutGetters(cause.value))))
626
+ return;
627
+ return message;
628
+ }
629
+ function previousDiagnosticInput(input, nativeDiagnostics) {
630
+ const report = ownData(input, "report");
631
+ const errors = ownData(report, "errors");
632
+ if (!isRecord(report) || !Array.isArray(errors) || isProxy(errors))
633
+ return input;
634
+ let entries;
635
+ for (let index = 0; index < errors.length; index++) {
636
+ const descriptor = Object.getOwnPropertyDescriptor(errors, index);
637
+ const diagnostic = descriptor?.value;
638
+ if (!descriptor || !isNativeError(diagnostic))
639
+ continue;
640
+ const message = previousDiagnosticMessage(diagnostic);
641
+ if (message === undefined)
642
+ return input;
643
+ nativeDiagnostics.set(index, diagnostic);
644
+ (entries ??= Object.getOwnPropertyDescriptors(errors))[index] = {
645
+ ...descriptor,
646
+ value: { message },
647
+ };
648
+ }
649
+ if (!entries)
650
+ return input;
651
+ const capturedErrors = Object.defineProperties([], entries);
652
+ const capturedReport = withDataProperty(report, "errors", capturedErrors);
653
+ return withDataProperty(input, "report", capturedReport);
189
654
  }
190
- function normalizedPublicationInput(input) {
191
- // Validation adds Cloud defaults; leave the caller's objects unchanged.
655
+ // Capture before JSON hooks can change their source. If native capture would
656
+ // invoke caller code too, the original bytes cannot be observed independently.
657
+ function previousNormalizedFindings(input, nativeDiagnostics, observe) {
658
+ input = previousDiagnosticInput(input, nativeDiagnostics);
659
+ if (!canSnapshotWithoutGetters(input, observe))
660
+ return;
661
+ // Object spread skips a hidden report, but explicitly reading it could run a getter.
662
+ const reportField = Object.getOwnPropertyDescriptor(input, "report");
663
+ if (reportField && !Object.hasOwn(reportField, "value"))
664
+ return;
192
665
  const { report, ...parsed } = input;
193
- const current = structuredClone(parsed);
194
- if (report !== undefined) {
195
- current.report = structuredClone(report);
196
- validateExternalReport(current.report);
197
- }
198
- // structuredClone preserves aliases. Detach every raw record before adding
199
- // typed defaults, including aliases shared with another finding's evidence.
200
- for (const finding of current.findings)
201
- if (finding.evidence.source_data !== undefined)
202
- finding.evidence.source_data = structuredClone(finding.evidence.source_data);
203
- const identities = new Set();
204
- for (const finding of current.findings) {
666
+ const snapshot = structuredClone(parsed);
667
+ if (report !== undefined)
668
+ snapshot.report = structuredClone(report);
669
+ return snapshot;
670
+ }
671
+ function publicationIdentity(parsed, provider) {
672
+ // For Trivy, the independently checked header binds the repository. Files
673
+ // and normalized rows use the same checkpoint when their findings agree.
674
+ if (parsed.report === undefined ||
675
+ (provider === "trivy" && isTrivyReportHeader(parsed.report)))
676
+ return parsed.findings;
677
+ return { findings: parsed.findings, report: reportForJson(parsed.report) };
678
+ }
679
+ function validatePublicationFindings(parsed, validateEvidence) {
680
+ validateExternalReport(parsed.report);
681
+ const ids = new Set();
682
+ for (const finding of parsed.findings) {
205
683
  const id = finding.source_finding_id;
206
684
  if (!id.trim() || id.includes("\0") || Buffer.byteLength(id) > 512)
207
685
  throw new CodexSecurityError("Source finding id must contain 1–512 UTF-8 bytes and no NUL characters.");
208
- if (identities.has(id))
686
+ if (ids.has(id))
209
687
  throw new CodexSecurityError(`Duplicate source finding id ${JSON.stringify(id)}.`);
210
- identities.add(id);
211
- finding.evidence = validateExternalEvidence(finding.evidence);
688
+ ids.add(id);
689
+ finding.evidence = validateEvidence(finding.evidence);
212
690
  }
213
- const identity = current.report === undefined
214
- ? current.findings
215
- : { findings: current.findings, report: reportForJson(current.report) };
691
+ }
692
+ function historicalPublication(native) {
693
+ if (!native)
694
+ return;
695
+ try {
696
+ // Older SDKs detached all raw records before Cloud validation added defaults.
697
+ for (const finding of native.findings)
698
+ if (finding.evidence.source_data !== undefined)
699
+ finding.evidence.source_data = structuredClone(finding.evidence.source_data);
700
+ validatePublicationFindings(native, validatePreviousCloudEvidence);
701
+ const identity = publicationIdentity(native);
702
+ let hasBoxedScalars = false;
703
+ const wire = canonicalJson(identity, undefined, (value) => {
704
+ if (!isBoxedPrimitive(value))
705
+ return false;
706
+ hasBoxedScalars = true;
707
+ return true;
708
+ });
709
+ return { parsed: native, identity, wire, hasBoxedScalars };
710
+ }
711
+ catch {
712
+ // The caller may export valid JSON even if the native candidate cannot be
713
+ // validated or hashed, for example when an unused row property holds a BigInt.
714
+ return;
715
+ }
716
+ }
717
+ function normalizedPublicationInput(input, provider) {
718
+ const nativeDiagnostics = new Map();
719
+ let native;
720
+ let plainJson = true;
721
+ try {
722
+ native = previousNormalizedFindings(input, nativeDiagnostics, (value) => {
723
+ if (!plainJson)
724
+ return;
725
+ const prototype = Object.getPrototypeOf(value);
726
+ plainJson =
727
+ (prototype === null ||
728
+ prototype === Object.prototype ||
729
+ prototype === Array.prototype) &&
730
+ !("toJSON" in value);
731
+ });
732
+ }
733
+ catch {
734
+ /* A valid JSON export can contain an uncloneable value. */
735
+ }
736
+ let priorIds = native ? undefined : previousFindingIds(input);
737
+ let current;
738
+ let freshError;
739
+ try {
740
+ current = snapshotNormalizedFindings(input, undefined, "cloud");
741
+ }
742
+ catch (error) {
743
+ if ((!isNativeError(error) && !(error instanceof Error)) ||
744
+ !native?.findings.length)
745
+ throw error;
746
+ freshError = error;
747
+ current = { read: native.read, findings: [], excluded: [] };
748
+ }
749
+ // Repository and format declarations can be non-enumerable SDK getters.
750
+ for (const key of [
751
+ "sourceFormat",
752
+ "semgrepPlatformScope",
753
+ "snykRestContext",
754
+ ...(provider === "semgrep" || provider === "snyk"
755
+ ? ["sourceRepositoryUrl", "sourceRepositoryPlatform"]
756
+ : []),
757
+ ]) {
758
+ if (!(key in current) &&
759
+ !Object.prototype.propertyIsEnumerable.call(input, key) &&
760
+ key in input)
761
+ Object.assign(current, {
762
+ [key]: structuredClone(Reflect.get(input, key)),
763
+ });
764
+ }
765
+ const parsed = current;
766
+ if (parsed.snykRestContext !== undefined) {
767
+ if (provider !== "snyk")
768
+ throw new CodexSecurityError("Snyk REST evidence requires the snyk provider.");
769
+ if (!isSnykRestFormat(parsed.snykRestContext?.["format"]))
770
+ throw new CodexSecurityError("Expected a versioned Snyk REST report context.");
771
+ }
772
+ const markedSemgrep = "sourceFormat" in parsed && isSemgrepPlatformFormat(parsed.sourceFormat);
773
+ if (markedSemgrep && provider !== "semgrep")
774
+ throw new CodexSecurityError("Semgrep Platform evidence requires the semgrep provider.");
775
+ if (markedSemgrep || "semgrepPlatformScope" in parsed)
776
+ semgrepPlatformReportScope(parsed, provider);
777
+ if (native && nativeDiagnostics.size) {
778
+ const historical = native.report["errors"];
779
+ const exported = current.report?.["errors"];
780
+ for (const [index, source] of nativeDiagnostics) {
781
+ const message = historical[index].message;
782
+ let diagnostic = Array.isArray(exported) && exported[index];
783
+ // A failed export may never reach the report. Its hooks have already run,
784
+ // so check the original descriptors again before native cloning reads a stack.
785
+ if (freshError && previousDiagnosticMessage(source) === message)
786
+ diagnostic = source;
787
+ if (isNativeError(diagnostic) &&
788
+ Object.getPrototypeOf(diagnostic) === Object.getPrototypeOf(source)) {
789
+ try {
790
+ const copy = structuredClone(diagnostic);
791
+ if (copy.message === message) {
792
+ historical[index] = copy;
793
+ continue;
794
+ }
795
+ }
796
+ catch {
797
+ // A failed native clone cannot establish a historical report.
798
+ }
799
+ }
800
+ if (freshError)
801
+ throw freshError;
802
+ priorIds = previousFindingIds(native);
803
+ native = undefined;
804
+ break;
805
+ }
806
+ }
807
+ // A Cloud depth error can interrupt a local reader's JSON export. Its plain,
808
+ // independently captured output can still verify the original reader selection.
809
+ const readerOutput = freshError
810
+ ? freshError instanceof CloudEvidenceDepthError &&
811
+ plainJson &&
812
+ !nativeDiagnostics.size &&
813
+ native
814
+ : current;
815
+ const reader = readerOutput
816
+ ? previousReaderSelection(input, readerOutput, native ?? null)
817
+ : undefined;
818
+ if (reader)
819
+ native = reader.snapshot;
820
+ const original = historicalPublication(native);
821
+ if (original)
822
+ checkFindingProvider(original.parsed, provider);
823
+ try {
824
+ validatePublicationFindings(current, validateExternalEvidence);
825
+ }
826
+ catch (error) {
827
+ if (!(error instanceof Error) || !original?.parsed.findings.length)
828
+ throw error;
829
+ freshError = error;
830
+ }
831
+ if (freshError) {
832
+ if (!original)
833
+ throw freshError;
834
+ // Only the old selection can be reviewed when fresh validation failed.
835
+ current = { read: original.parsed.read, findings: [], excluded: [] };
836
+ }
837
+ checkFindingProvider(current, provider);
838
+ const identity = publicationIdentity(current, provider);
216
839
  return {
217
840
  current,
218
841
  legacy: current,
219
842
  currentIdentity: identity,
220
843
  legacyIdentity: identity,
844
+ sourcePositions: getOpenGrepSourcePositions(ownData(input, "findings")),
845
+ original,
846
+ freshError,
847
+ unprovenIdPrefixes: reader?.unprovenIdPrefixes,
848
+ uncertainFindingIds: reader?.uncertainFindingIds,
849
+ unprovenOccurrenceCount: reader?.unprovenOccurrenceCount,
850
+ knownFindings: reader?.knownFindings,
851
+ potentialIds: reader?.sourceFindingIds ??
852
+ (original
853
+ ? undefined
854
+ : (native?.findings.map((row) => row.source_finding_id) ?? priorIds)),
221
855
  };
222
856
  }
223
- async function preparePublicationInput(input, options, dependencies) {
224
- const { inspectorProjectMap } = options;
225
- const progress = (event) => {
857
+ // The old hash represented Number objects as {}. Unpublished row properties
858
+ // and report metadata cannot be recovered from the immutable saved request.
859
+ function verifiableLegacyMetadata({ report, findings }, wireIdentity) {
860
+ let verifiable = true;
861
+ // Only values in these records can be old scalar projections. Their record
862
+ // containers are required by the normalized selection, including an empty report.
863
+ const { errors, ...metadata } = report ?? {};
864
+ JSON.stringify([
865
+ Object.values(metadata),
866
+ // The original hash used the serialized form of report.errors arrays.
867
+ Array.isArray(errors) ? undefined : errors,
868
+ findings.map(({ evidence: _, ...fields }) => Object.values(fields)),
869
+ ], (_key, value) => {
870
+ if (isRecord(value) && !(wireIdentity && isBoxedPrimitive(value))) {
871
+ // {} and string-index records can both be the old form of a scalar.
872
+ let index = 0;
873
+ if (Object.keys(value).every((key) => {
874
+ const item = value[key];
875
+ return (item === undefined ||
876
+ (key === String(index++) &&
877
+ typeof item === "string" &&
878
+ item.length === 1));
879
+ }))
880
+ verifiable = false;
881
+ }
882
+ return value;
883
+ });
884
+ return verifiable;
885
+ }
886
+ // Prefer the current native evidence for new publications. The original SDK input
887
+ // remains a candidate so an approved request can resume under its exact digest.
888
+ function* hostedPublicationProjections(input, provider, destination, readerSources) {
889
+ const current = input.current;
890
+ if (provider === "snyk") {
891
+ const first = current.findings.find(isSnykRestFinding);
892
+ if (!first && current.snykRestContext === undefined)
893
+ return;
894
+ // A previous publisher may have approved the exact raw selection.
895
+ yield input;
896
+ const originalUrl = current.snykRestContext?.["sourceRepositoryUrl"] ??
897
+ current.sourceRepositoryUrl ??
898
+ first?.evidence.source_data?.["sourceRepositoryUrl"];
899
+ // The original spelling reconstructs a lookup key after a catalog rename.
900
+ // Admission validates the input against the saved approval or live destination.
901
+ const retained = structuredClone(current);
902
+ try {
903
+ snykRestImportRepository(retained, provider, [], hostedSourceReferences(readerSources, typeof originalUrl === "string" ? originalUrl : destination.url, current.sourceRepositoryPlatform), (finding, source) => {
904
+ finding.evidence.source_data = source;
905
+ });
906
+ }
907
+ catch (error) {
908
+ // A headerless input can still identify an already-approved opaque request.
909
+ if (current.snykRestContext !== undefined)
910
+ throw error;
911
+ return;
912
+ }
913
+ yield {
914
+ ...input,
915
+ current: retained,
916
+ // The raw input above owns its historical native checkpoint.
917
+ original: undefined,
918
+ currentIdentity: publicationIdentity(retained),
919
+ };
920
+ return;
921
+ }
922
+ const originalRow = (finding) => isRecord(finding.evidence.source_data?.["finding"]) &&
923
+ isSemgrepPlatformEvidence(finding, "semgrep");
924
+ const currentBulkRow = (finding) => finding.evidence.source_data?.["format"] === semgrepPlatformFormat;
925
+ const first = current.findings.find(originalRow) ?? current.findings.find(currentBulkRow);
926
+ if (!first)
927
+ return;
928
+ const reportScope = semgrepPlatformReportScope(current, "semgrep");
929
+ const retained = first.evidence.source_data;
930
+ const project = retained?.["project"];
931
+ const retainedUrl = current.sourceRepositoryUrl ??
932
+ retained?.["sourceRepositoryUrl"] ??
933
+ (isRecord(project) ? project["url"] : undefined);
934
+ // A renamed repository may need the original spelling to identify its checkpoint.
935
+ // Preparation validates the unmodified report against the authenticated approval.
936
+ for (const url of new Set([destination.url, retainedUrl])) {
937
+ if (typeof url !== "string")
938
+ continue;
939
+ const references = hostedSourceReferences(readerSources, url, current.sourceRepositoryPlatform);
226
940
  try {
227
- dependencies.onProgress?.(event);
941
+ const witnesses = semgrepLegacyRepositoryWitnesses(current.findings, "semgrep", references, "cloud");
942
+ const findings = current.findings.map((finding) => {
943
+ const original = originalRow(finding);
944
+ if (!original && !currentBulkRow(finding))
945
+ return finding;
946
+ const binding = semgrepPlatformFindingBinding(finding, "semgrep", references, witnesses, reportScope, undefined, "cloud");
947
+ // The authenticated repository can make an offline backlog row selectable.
948
+ return binding?.sourceData &&
949
+ (original || binding.sourceData["format"] !== semgrepPlatformFormat)
950
+ ? {
951
+ ...finding,
952
+ evidence: {
953
+ ...finding.evidence,
954
+ ...binding.migration,
955
+ source_data: binding.sourceData,
956
+ },
957
+ }
958
+ : finding;
959
+ });
960
+ const upgraded = { ...current, findings };
961
+ yield {
962
+ ...input,
963
+ current: upgraded,
964
+ original: undefined,
965
+ currentIdentity: publicationIdentity(upgraded),
966
+ };
228
967
  }
229
968
  catch {
230
- // Optional progress must not interrupt preparation or publication.
969
+ // The released SDK also accepted partial original records; keep the raw
970
+ // identity as a fallback so its exact saved request remains recoverable.
231
971
  }
232
- };
972
+ }
973
+ yield input;
974
+ }
975
+ async function preparePublicationInput(supplied, options, dependencies, readerSources, declaredUrl, nativePath, fileRepositories) {
976
+ const { inspectorProjectMap } = options;
977
+ const progress = (event) => notify(() => dependencies.onProgress?.(event));
233
978
  if (!options.sourceKey.trim() ||
234
979
  options.sourceKey.includes("\0") ||
235
980
  Buffer.byteLength(options.sourceKey) > 512) {
236
981
  throw new CodexSecurityError("The source key must contain 1–512 UTF-8 bytes and no NUL characters. Use a stable scanner organization/project/branch namespace across imports.");
237
982
  }
983
+ const environment = dependencies.environment ?? process.env;
984
+ dependencies.signal?.throwIfAborted();
238
985
  progress({ phase: "reading", completed: 0 });
239
- const { current: parsed, legacy: legacyParsed, currentIdentity, legacyIdentity, } = input;
240
- if (parsed.findings.length === 0) {
241
- const diagnostics = reportForJson(parsed.report)?.["errors"];
242
- throw new CodexSecurityError(`No supported findings are ready to publish. ${parsed.excluded.map((item) => `Item ${item.position}: ${item.reason}`).join(" ")}${Array.isArray(diagnostics) && diagnostics.length ? ` Scanner diagnostics: ${JSON.stringify(diagnostics)}` : ""}`);
986
+ const trivyReportUrls = options.provider === "trivy" && typeof supplied !== "function"
987
+ ? trivyBatchSourceUrls(supplied.current, supplied.current.findings.length > 0)
988
+ : undefined;
989
+ if (trivyReportUrls)
990
+ validateTrivyRepositoryUrl(options.repository);
991
+ const catalogKey = trivyReportUrls
992
+ ? (url) => cloudSourceKey(url, "github")
993
+ : cloudRepositoryUrlKey;
994
+ const state = externalPublicationStateDirectory(environment);
995
+ function pendingFile(key) {
996
+ return join(state, `${key}.pending.json`);
997
+ }
998
+ let previousFindings;
999
+ async function previousFindingsIfSaved(input, currentKey) {
1000
+ if (!input.readPreviousFindings || previousFindings)
1001
+ return previousFindings;
1002
+ // An empty state directory (or just this current receipt) needs only the
1003
+ // current reader for its preview.
1004
+ const entries = (await readdir(state).catch(nullIfMissingFile)) ?? [];
1005
+ if (entries.some((name) => /^[a-f0-9]{64}\.(?:pending|private-import)\.json$/u.test(name) &&
1006
+ name.split(".", 1)[0] !== currentKey))
1007
+ previousFindings = input.readPreviousFindings();
1008
+ return previousFindings;
243
1009
  }
1010
+ async function checkSelection(input) {
1011
+ const parsed = input.current;
1012
+ checkTrivyReportProvider(options.provider, parsed.report);
1013
+ for (const finding of parsed.findings)
1014
+ checkRetainedTrivyProvider(options.provider, finding);
1015
+ if (!parsed.findings.length &&
1016
+ !input.original?.parsed.findings.length &&
1017
+ input.potentialIds !== null &&
1018
+ !input.potentialIds?.length &&
1019
+ !input.uncertainFindingIds?.length &&
1020
+ !input.unprovenOccurrenceCount &&
1021
+ !input.unprovenIdPrefixes?.length &&
1022
+ !(await previousFindingsIfSaved(input))?.findings.length)
1023
+ throw new NoPublishableFindingsError(parsed);
1024
+ }
1025
+ if (typeof supplied !== "function")
1026
+ await checkSelection(supplied);
244
1027
  progress({ phase: "discovering", completed: 0 });
245
- const environment = dependencies.environment ?? process.env;
246
- requireExternalImports(environment);
247
1028
  const apiBaseUrl = cloudBaseUrl(environment);
248
1029
  const cloudApiUrl = `${apiBaseUrl}/external`;
249
1030
  // Validate the configured return destination before authentication or uploads.
250
1031
  cloudFindingsUrl(environment, options.repository);
251
1032
  const credentials = await (dependencies.credentials ?? (() => readCloudCredentials(environment)))();
1033
+ let canStartJob = false;
1034
+ let repositorySeverities;
1035
+ const jobVersionHeader = "Codex-Security-Private-Import-Job-Version";
1036
+ const providerJobVersion = privateJobVersions[options.provider];
1037
+ // Older services recognize an exact v2 request for AWS. Only ask for v3
1038
+ // when publishing a provider introduced by that contract.
1039
+ const requestedJobVersion = String(providerJobVersion ?? 2);
1040
+ let jobApiVersion;
252
1041
  async function request(endpoint, body) {
253
1042
  dependencies.signal?.throwIfAborted();
254
1043
  // Allow upload and response transport around the server's publication budget.
255
1044
  const timeout = AbortSignal.timeout(body ? 60_000 : 30_000);
1045
+ const repositoryCatalog = endpoint.startsWith("/repositories?");
256
1046
  const response = await (dependencies.fetch ?? globalThis.fetch)(`${cloudApiUrl}${endpoint}`, {
257
1047
  method: body ? "POST" : "GET",
258
1048
  headers: {
259
1049
  Authorization: `Bearer ${credentials.access_token}`,
260
1050
  "ChatGPT-Account-ID": credentials.account_id,
261
1051
  Accept: "application/json",
1052
+ ...(repositoryCatalog
1053
+ ? { [jobVersionHeader]: requestedJobVersion }
1054
+ : {}),
262
1055
  ...(body ? { "Content-Type": "application/json" } : {}),
263
1056
  },
264
1057
  ...(body ? { body: JSON.stringify(body) } : {}),
@@ -272,30 +1065,54 @@ async function preparePublicationInput(input, options, dependencies) {
272
1065
  const retryAfter = response.headers.get("Retry-After");
273
1066
  throw new CloudImportError(response.status, `Cloud import failed (HTTP ${response.status}). ${payload?.error?.message ?? response.statusText}${retryAfter ? ` Retry-After: ${retryAfter}.` : ""}`);
274
1067
  }
1068
+ if (repositoryCatalog) {
1069
+ const advertised = (header) => response.headers
1070
+ .get(header)
1071
+ ?.split(",")
1072
+ .map((item) => item.trim());
1073
+ repositorySeverities = advertised("Codex-Security-Repository-Import-Severities");
1074
+ canStartJob =
1075
+ advertised("Codex-Security-Private-Import-Providers")?.includes(options.provider) ?? false;
1076
+ jobApiVersion =
1077
+ response.headers.get(jobVersionHeader) === requestedJobVersion
1078
+ ? providerJobVersion
1079
+ : undefined;
1080
+ }
275
1081
  return response.json();
276
1082
  }
277
- const destinations = [];
278
- let page = null;
279
- const seenPages = new Set();
280
- do {
281
- const result = validateRepositories(await request(`/repositories?limit=20&repository=${encodeURIComponent(options.repository)}${page ? `&page=${encodeURIComponent(page)}` : ""}`));
282
- destinations.push(...result.data.filter((item) => item.id === options.repository ||
283
- cloudRepositoryUrlKey(item.url) ===
284
- cloudRepositoryUrlKey(options.repository)));
285
- page = result.next;
286
- if (result.has_more !== (page !== null) ||
287
- (page !== null && seenPages.has(page)))
288
- throw new CodexSecurityError("Cloud returned invalid repository pagination.");
289
- if (page !== null)
290
- seenPages.add(page);
291
- } while (page !== null);
292
- if (destinations.length !== 1)
293
- throw new CodexSecurityError(destinations.length
294
- ? "The repository matches multiple connectors. Select an unambiguous Cloud repository."
295
- : "The destination repository is not available to this Cloud account. Copy its repository URL or ID from Codex Security Cloud and check that you are signed into the intended account.");
296
- const destination = destinations[0];
1083
+ async function discoverDestination() {
1084
+ const destinations = [];
1085
+ let page = null;
1086
+ const seenPages = new Set();
1087
+ do {
1088
+ const result = validateRepositories(await request(`/repositories?limit=20&repository=${encodeURIComponent(options.repository)}${page ? `&page=${encodeURIComponent(page)}` : ""}`));
1089
+ destinations.push(...result.data.filter((item) => item.id === options.repository ||
1090
+ catalogKey(item.url) === catalogKey(options.repository)));
1091
+ page = result.next;
1092
+ if (result.has_more !== (page !== null) ||
1093
+ (page !== null && seenPages.has(page)))
1094
+ throw new CodexSecurityError("Cloud returned invalid repository pagination.");
1095
+ if (page !== null)
1096
+ seenPages.add(page);
1097
+ } while (page !== null);
1098
+ if (destinations.length !== 1)
1099
+ throw new CodexSecurityError(destinations.length
1100
+ ? "The repository matches multiple connectors. Select an unambiguous Cloud repository."
1101
+ : "The destination repository is not available to this Cloud account. Copy its repository URL or ID from Codex Security Cloud and check that you are signed into the intended account.");
1102
+ const destination = destinations[0];
1103
+ if (trivyReportUrls)
1104
+ validateTrivyRepositoryUrl(destination.url);
1105
+ return destination;
1106
+ }
1107
+ const destination = await discoverDestination();
1108
+ const deferred = typeof supplied === "function";
1109
+ const resolved = deferred ? await supplied(destination) : supplied;
1110
+ if (deferred)
1111
+ await checkSelection(resolved);
1112
+ const report = resolved.current;
297
1113
  const source = { provider: options.provider, source_key: options.sourceKey };
298
- const state = join(codexSecurityStateDirectory(environment), "external-finding-publications");
1114
+ const importPrefix = source.provider === "sonar" ? "/v2" : "";
1115
+ const sourceReportsPath = importPrefix + sourcePath(destination);
299
1116
  const checkpointIdentity = [
300
1117
  // Keep existing production checkpoints readable; other deployments must
301
1118
  // never share a saved request or receipt with the production default.
@@ -305,214 +1122,1369 @@ async function preparePublicationInput(input, options, dependencies) {
305
1122
  destination.repo_connector_id,
306
1123
  source,
307
1124
  ];
308
- const inputDigest = hash("sha256", canonicalJson(currentIdentity));
309
- const legacyInputDigest = hash("sha256", canonicalJson(legacyIdentity));
310
- function validateSaved(serialized, legacy = false) {
311
- const content = JSON.parse(serialized);
312
- if (content.accountId !== credentials.account_id ||
1125
+ function scopeKey(identity, serialize = wireJson) {
1126
+ return jsonDigest([...checkpointIdentity, identity], serialize);
1127
+ }
1128
+ function localeKey(rows) {
1129
+ return hash("sha256", canonicalJson([...checkpointIdentity, rows], (left, right) => left.localeCompare(right)));
1130
+ }
1131
+ const hosted = options.provider === "snyk" || options.provider === "semgrep";
1132
+ // Different selections can share native occurrences. Serialize their checkpoint
1133
+ // creation under the authenticated source and release before calling Cloud.
1134
+ const sourceLock = "source-" + hash("sha256", canonicalJson(checkpointIdentity));
1135
+ const withSourceAdmission = (operation) => hosted ? underLocks([sourceLock], operation) : operation();
1136
+ let projection = resolved;
1137
+ let capturedPending;
1138
+ const projectionKeys = new Set();
1139
+ if (options.provider === "semgrep" || options.provider === "snyk") {
1140
+ for (const candidate of hostedPublicationProjections(resolved, options.provider, destination, readerSources)) {
1141
+ const candidateKey = scopeKey(candidate.currentIdentity);
1142
+ if (projectionKeys.has(candidateKey))
1143
+ continue;
1144
+ // New imports use the first reconstructed projection; an exact pending key
1145
+ // takes precedence, including an earlier SDK's unmodified original input.
1146
+ capturedPending = await readOptionalFile(pendingFile(candidateKey));
1147
+ if (projection === resolved || capturedPending !== undefined)
1148
+ projection = candidate;
1149
+ projectionKeys.add(candidateKey);
1150
+ if (capturedPending !== undefined)
1151
+ break;
1152
+ }
1153
+ }
1154
+ // Changed reader projections get new keys, but both transports must replay the
1155
+ // exact bytes already approved by the earlier reader when a checkpoint exists.
1156
+ const privateClient = privateImportClient(apiBaseUrl, credentials, dependencies.fetch ?? globalThis.fetch, dependencies.signal);
1157
+ let previewContext;
1158
+ const currentReaderKey = resolved.readPreviousFindings
1159
+ ? scopeKey(resolved.currentIdentity)
1160
+ : undefined;
1161
+ let alternateKey;
1162
+ let resumingPreviousReader = false;
1163
+ function previousCandidateKey(previous) {
1164
+ if (!previous?.findings.length)
1165
+ return;
1166
+ // The file reader already validated plain JSON evidence for Cloud. Only a
1167
+ // matching checkpoint needs the full normalized publication snapshot.
1168
+ const previousKey = scopeKey(publicationIdentity(previous, options.provider));
1169
+ return previousKey !== currentReaderKey ? previousKey : undefined;
1170
+ }
1171
+ async function alternateCheckpoint(key, context) {
1172
+ const pending = await readOptionalFile(pendingFile(key));
1173
+ if (pending !== undefined)
1174
+ return { pending };
1175
+ const saved = await readOptionalFile(join(state, key + ".private-import.json"));
1176
+ if (saved === undefined)
1177
+ return;
1178
+ const checkpoint = parsePrivateCheckpoint(saved);
1179
+ // A completed import cannot acknowledge findings newly admitted by the
1180
+ // current reader. Pending uploads and jobs always retain their original bytes.
1181
+ if (!resumingPreviousReader &&
1182
+ previousFindings &&
1183
+ checkpoint.receipt?.status === "completed" &&
1184
+ !sameFindingSelection(previousFindings.findings, resolved.current.findings))
1185
+ return;
1186
+ if (checkpoint.contextVersion === (await context()).context_version)
1187
+ return { pending };
1188
+ }
1189
+ async function assertNoAlternateCheckpoint(context = () => privateClient.context()) {
1190
+ if (alternateKey && (await alternateCheckpoint(alternateKey, context)))
1191
+ throw new CodexSecurityError(resumingPreviousReader
1192
+ ? "Both reader versions have saved publications for this report. Resume either one using its original normalized findings."
1193
+ : "Another publication prepared this input. Run the command again to review and resume the saved request.");
1194
+ }
1195
+ if (currentReaderKey)
1196
+ alternateKey = previousCandidateKey(await previousFindingsIfSaved(resolved, currentReaderKey));
1197
+ if (alternateKey && previousFindings) {
1198
+ const context = async () => (previewContext ??= await privateClient.context());
1199
+ const checkpoint = await alternateCheckpoint(alternateKey, context);
1200
+ capturedPending = checkpoint?.pending;
1201
+ if (checkpoint) {
1202
+ projection = normalizedPublicationInput(previousFindings, options.provider);
1203
+ resumingPreviousReader = true;
1204
+ alternateKey = currentReaderKey;
1205
+ await assertNoAlternateCheckpoint(context);
1206
+ }
1207
+ }
1208
+ const input = projection;
1209
+ let { current: parsed, legacy: legacyParsed, currentIdentity, legacyIdentity, recheckFileRepository, } = input;
1210
+ const scopedSemgrepReport = options.provider === "semgrep" &&
1211
+ "sourceFormat" in parsed &&
1212
+ isSemgrepPlatformFormat(parsed.sourceFormat);
1213
+ const original = input.original;
1214
+ const originalParsed = original?.parsed ?? parsed;
1215
+ const originalIdentity = original?.identity ?? currentIdentity;
1216
+ const hasBoxedScalars = original?.hasBoxedScalars === true;
1217
+ const changedCapture = original && original.wire !== wireJson(currentIdentity);
1218
+ const originalKey = changedCapture || hasBoxedScalars
1219
+ ? scopeKey(originalIdentity, canonicalJson)
1220
+ : undefined;
1221
+ let historicalOnly = false;
1222
+ const originalWireKey = changedCapture
1223
+ ? scopeKey(originalIdentity)
1224
+ : undefined;
1225
+ if (changedCapture && originalKey && originalWireKey) {
1226
+ // A released SDK could stage either spelling. A modern approval for the
1227
+ // fresh JSON can occupy the very same historical filename.
1228
+ for (const pathKey of new Set([
1229
+ originalKey,
1230
+ originalWireKey,
1231
+ localeKey(original.parsed.findings),
1232
+ ])) {
1233
+ const bytes = await readOptionalFile(pendingFile(pathKey));
1234
+ if (bytes === undefined)
1235
+ continue;
1236
+ const record = JSON.parse(bytes);
1237
+ if ((record?.legacyApproval &&
1238
+ (record.wireKey ?? pathKey) === originalWireKey) ||
1239
+ (record?.wireKey === undefined &&
1240
+ record?.inputProjection?.identityFormat !== 2 &&
1241
+ (pathKey === originalKey ||
1242
+ pathKey === originalWireKey ||
1243
+ original.parsed.report === undefined ||
1244
+ record?.inputProjection?.inputDigest ===
1245
+ jsonDigest(originalIdentity, canonicalJson)))) {
1246
+ parsed = legacyParsed = original.parsed;
1247
+ currentIdentity = legacyIdentity = original.identity;
1248
+ historicalOnly = true;
1249
+ break;
1250
+ }
1251
+ }
1252
+ }
1253
+ if (input.freshError && !historicalOnly)
1254
+ throw input.freshError;
1255
+ const inputDigest = jsonDigest(currentIdentity);
1256
+ const legacyInputDigest = currentIdentity === legacyIdentity
1257
+ ? inputDigest
1258
+ : jsonDigest(legacyIdentity);
1259
+ function matchesSavedDestination(requests) {
1260
+ return (requests.length > 0 &&
1261
+ requests.every((batch) => batch.repository.id === destination.id &&
1262
+ batch.repository.repo_connector_id ===
1263
+ destination.repo_connector_id &&
1264
+ wireJson(batch.source) === wireJson(source)));
1265
+ }
1266
+ const originalHostedRetry = "This source has a pending hosted Cloud request. Resume it with the SDK build that created it and the original input before publishing another selection.";
1267
+ const unknownHostedRetry = "A pending hosted request matches this provider and repository, but its deployment could not be established. Resume it with the SDK build that created it, on the original deployment, using the full original input and metadata.";
1268
+ function originalHostedPending(content, filenameKey) {
1269
+ const projection = content.inputProjection;
1270
+ if ((options.provider !== "semgrep" && options.provider !== "snyk") ||
1271
+ content.accountId !== credentials.account_id ||
1272
+ !projection ||
1273
+ !content.requests?.length ||
1274
+ !content.requests.every(({ repository, source: savedSource }) => repository.id === destination.id &&
1275
+ repository.repo_connector_id === destination.repo_connector_id &&
1276
+ canonicalJson(savedSource) === canonicalJson(source)))
1277
+ return false;
1278
+ const findings = submissionFindings(content.requests);
1279
+ if (projection.savedEvidenceDigest !== hash("sha256", canonicalJson(findings)))
1280
+ return false;
1281
+ const projections = [findings];
1282
+ // Historical filenames include caller fields discarded by request validation.
1283
+ // Reuse them by stable ID with the evidence and order of the approved request.
1284
+ const metadata = new Map(legacyParsed.findings
1285
+ .filter((row) => Object.keys(row).some((key) => key !== "evidence" && key !== "source_finding_id"))
1286
+ .map((row) => [row.source_finding_id, row]));
1287
+ if (findings.some((row) => metadata.has(row.source_finding_id)))
1288
+ projections.push(findings.map((row) => ({
1289
+ ...metadata.get(row.source_finding_id),
1290
+ ...row,
1291
+ })));
1292
+ const identities = [...projections];
1293
+ if (parsed.report !== undefined) {
1294
+ const report = reportForJson(parsed.report);
1295
+ identities.push(...projections.map((rows) => ({ findings: rows, report })));
1296
+ }
1297
+ // Caller metadata on a normalized finding is part of the filename but not the
1298
+ // request body. An exact retry still supplies that original identity.
1299
+ if (filenameKey === scopeKey(legacyIdentity))
1300
+ identities.unshift(legacyIdentity);
1301
+ const identity = identities.find((value) => filenameKey === scopeKey(value));
1302
+ const digest = (value) => hash("sha256", canonicalJson(value));
1303
+ const semgrep = options.provider === "semgrep";
1304
+ const isHosted = (row) => semgrep
1305
+ ? isSemgrepPlatformEvidence(row, "semgrep")
1306
+ : isSnykRestFinding(row);
1307
+ const scannerId = (row) => isHosted({
1308
+ ...row,
1309
+ evidence: { ...row.evidence, source_data: undefined },
1310
+ });
1311
+ const hasNativeEvidence = (row) => {
1312
+ const data = row.evidence.source_data;
1313
+ // A discriminator alone cannot identify a native occurrence.
1314
+ return (data &&
1315
+ isHosted({
1316
+ source_finding_id: "",
1317
+ evidence: {
1318
+ ...row.evidence,
1319
+ source_data: { ...data, format: undefined },
1320
+ },
1321
+ }));
1322
+ };
1323
+ const references = hostedSourceReferences(readerSources, content.approvedRepositoryUrl ?? destination.url, content.approvedRepositoryPlatform);
1324
+ const occurrenceIds = (rows) => {
1325
+ const ids = new Set(rows.map((row) => row.source_finding_id));
1326
+ let native = false;
1327
+ let unresolved = false;
1328
+ for (const row of rows) {
1329
+ if (!hasNativeEvidence(row))
1330
+ continue;
1331
+ native = true;
1332
+ try {
1333
+ const id = semgrep
1334
+ ? semgrepPlatformFindingBinding({ ...row, source_finding_id: "" }, "semgrep", references, undefined, undefined, undefined, "cloud")?.nativeId
1335
+ : snykRestCheckpointFindingId(row, references);
1336
+ if (id) {
1337
+ ids.add(id);
1338
+ continue;
1339
+ }
1340
+ }
1341
+ catch {
1342
+ // An earlier SDK may have accepted a partial native projection.
1343
+ }
1344
+ if (!scannerId(row))
1345
+ unresolved = true;
1346
+ }
1347
+ return { ids, native, unresolved };
1348
+ };
1349
+ const previous = occurrenceIds(findings);
1350
+ const requested = occurrenceIds(parsed.findings);
1351
+ const hostedSelection = projection.retainedHostedSelection ||
1352
+ scopedSemgrepReport ||
1353
+ previous.native ||
1354
+ requested.native;
1355
+ // Submitted and native IDs share one namespace even when one side drops its raw evidence.
1356
+ const couldOverlap = (hostedSelection &&
1357
+ Array.from(requested.ids).some((id) => previous.ids.has(id))) ||
1358
+ (previous.native &&
1359
+ requested.native &&
1360
+ (previous.unresolved || requested.unresolved));
1361
+ if (content.approvedRepositoryUrl && !projection.retainedHostedSelection) {
1362
+ if (!couldOverlap)
1363
+ return false;
1364
+ if (identity)
1365
+ return projection.inputDigest === digest(identity) && "verified";
1366
+ // The filename includes metadata omitted from the saved request. A reproducible
1367
+ // original digest under another filename proves this is another deployment.
1368
+ return (!identities.some((value) => digest(value) === projection.inputDigest) &&
1369
+ "unidentified");
1370
+ }
1371
+ if (identity && projection.inputDigest !== digest(identity))
1372
+ return ((couldOverlap || !previous.native || !requested.native) && "verified");
1373
+ if (!couldOverlap)
1374
+ return false;
1375
+ if (!identity) {
1376
+ // If the saved selections reconstruct an old input digest but not its scoped
1377
+ // filename, the pending request belongs to another deployment. The older
1378
+ // publisher included its repository comparison flags in the digest.
1379
+ const repositories = new Set([destination.url]);
1380
+ for (const { evidence } of findings) {
1381
+ const data = evidence.source_data;
1382
+ const project = data?.["project"];
1383
+ const retained = (semgrep && isRecord(project) ? project["url"] : undefined) ??
1384
+ data?.["sourceRepositoryUrl"];
1385
+ if (typeof retained === "string")
1386
+ repositories.add(retained);
1387
+ }
1388
+ const paths = semgrep ? [null, true] : [false, true];
1389
+ const suffixes = semgrep ? [false] : [false, true];
1390
+ for (const candidate of identities) {
1391
+ if (digest(candidate) === projection.inputDigest)
1392
+ return false;
1393
+ for (const strict of paths) {
1394
+ for (const suffix of suffixes) {
1395
+ for (const repository of repositories) {
1396
+ const url = hostedRepositoryUrlKey(repository, !strict, suffix);
1397
+ if (digest([candidate, [url, strict, suffix]]) ===
1398
+ projection.inputDigest)
1399
+ return false;
1400
+ }
1401
+ }
1402
+ }
1403
+ }
1404
+ return "unidentified";
1405
+ }
1406
+ return "verified";
1407
+ }
1408
+ function wasReset(content) {
1409
+ return content.requests.some(({ repository }) => repository.reset_marker !== destination.reset_marker);
1410
+ }
1411
+ async function confirmReset() {
1412
+ // A newer pending request may have been written while the catalog response
1413
+ // was in flight. Confirm the reset after observing this immutable request.
1414
+ const confirmed = await discoverDestination();
1415
+ if (confirmed.id !== destination.id ||
1416
+ confirmed.repo_connector_id !== destination.repo_connector_id ||
1417
+ confirmed.reset_marker !== destination.reset_marker)
1418
+ throw new CodexSecurityError("The repository changed while preparing this publication. Review the destination again.");
1419
+ }
1420
+ function suppliedSemgrepScopeDigest() {
1421
+ const scope = semgrepPlatformReportScope(report, options.provider);
1422
+ return hash("sha256", canonicalJson(scope?.format
1423
+ ? [
1424
+ scope.deployment,
1425
+ canonicalNumber(scope.projectId),
1426
+ scope.ref,
1427
+ scope.product,
1428
+ ]
1429
+ : null));
1430
+ }
1431
+ async function retireIfReset(content, retire) {
1432
+ if (wasReset(content)) {
1433
+ if (hosted)
1434
+ await confirmReset();
1435
+ await retire();
1436
+ throw new CloudImportError(409, "The repository was reset after this submission. The saved request was retired without uploading. Review the destination and run the command again to approve a fresh publication.");
1437
+ }
1438
+ }
1439
+ function retirePendingIfReset(content, keys, path, remove = () => rm(path)) {
1440
+ return retireIfReset(content, () => underLocks(keys, async () => {
1441
+ const bytes = await readOptionalFile(path);
1442
+ if (bytes !== undefined && sameSubmission(JSON.parse(bytes), content))
1443
+ await remove();
1444
+ }));
1445
+ }
1446
+ function readSaved(content) {
1447
+ if (!content ||
1448
+ content.accountId !== credentials.account_id ||
313
1449
  !Array.isArray(content.requests) ||
314
1450
  content.requests.length === 0)
315
1451
  throw new Error("Saved import account or requests do not match.");
316
1452
  content.requests = content.requests.map(validateImportRequest);
317
- const savedFindings = submissionFindings(content.requests);
318
- const savedEvidence = canonicalJson(savedFindings);
319
- const expected = legacy ? legacyParsed.findings : parsed.findings;
320
- const matches = content.inputProjection !== undefined
321
- ? sameFindingSelection(savedFindings, expected) &&
322
- content.inputProjection?.inputDigest ===
323
- (legacy ? legacyInputDigest : inputDigest) &&
324
- content.inputProjection?.savedEvidenceDigest ===
325
- hash("sha256", savedEvidence)
326
- : savedEvidence === canonicalJson(expected);
327
- if (!matches)
1453
+ for (const finding of submissionFindings(content.requests))
1454
+ checkRetainedTrivyProvider(options.provider, finding);
1455
+ if (!matchesSavedDestination(content.requests))
1456
+ throw new Error("Saved import destination does not match.");
1457
+ if (source.provider === "trivy" && !content.trivyRepositoryUrl)
1458
+ throw new CodexSecurityError("This build cannot verify the original repository for this pending Trivy import. Check its destination before resuming with the build that created it.");
1459
+ return content;
1460
+ }
1461
+ function validateSaved(record, legacy = false, filenameKey = key) {
1462
+ const content = readSaved(record);
1463
+ const saved = submissionFindings(content.requests);
1464
+ const wire = wireJson(saved);
1465
+ const projection = content.inputProjection;
1466
+ const expected = repositoryFindings(legacy ? legacyParsed : parsed, projection?.repositorySeverities);
1467
+ if (projection &&
1468
+ (!sameFindingSelection(saved, expected) ||
1469
+ projection.inputDigest !== (legacy ? legacyInputDigest : inputDigest) ||
1470
+ projection.savedEvidenceDigest !== hash("sha256", wire))) {
1471
+ const status = originalHostedPending(content, filenameKey);
1472
+ if (status === "unidentified")
1473
+ throw new CodexSecurityError(unknownHostedRetry);
1474
+ if (status) {
1475
+ if (wasReset(content))
1476
+ return content;
1477
+ throw new CodexSecurityError(originalHostedRetry);
1478
+ }
328
1479
  throw new Error("Saved import evidence does not match the selected input.");
329
- for (const submission of content.requests) {
330
- if (submission.repository.id !== destination.id ||
331
- submission.repository.repo_connector_id !==
332
- destination.repo_connector_id ||
333
- canonicalJson(submission.source) !== canonicalJson(source))
334
- throw new Error("Saved import destination does not match.");
335
1480
  }
1481
+ // Older file readers can have a different, explicitly bound projection.
1482
+ // A corrected numeric checkpoint must carry the selected evidence verbatim.
1483
+ const exactWire = historicalOnly || hasBoxedScalars || content.wireKey !== undefined;
1484
+ if ((!projection || exactWire) &&
1485
+ wire !== wireJson(exactWire ? expected.map(wireFinding) : expected))
1486
+ throw new Error("Saved import evidence does not match the selected input.");
1487
+ if (!legacy &&
1488
+ hasBoxedScalars &&
1489
+ projection?.identityFormat !== 2 &&
1490
+ !verifiableLegacyMetadata(originalParsed, true))
1491
+ throw unmatchedPreviousNumbers();
1492
+ checkFindingProvider({ ...parsed, findings: saved }, options.provider);
1493
+ const approvedScope = content.inputProjection?.semgrepPartialScopeDigest;
1494
+ if (approvedScope !== undefined &&
1495
+ !wasReset(content) &&
1496
+ approvedScope !== suppliedSemgrepScopeDigest())
1497
+ throw new CodexSecurityError("The supplied Semgrep report scope does not match the approved request.");
336
1498
  return content;
337
1499
  }
338
- const key = hash("sha256", canonicalJson([...checkpointIdentity, currentIdentity]));
339
- const pendingPath = join(state, `${key}.pending.json`);
340
- const historicalKey = hash("sha256", canonicalJson([...checkpointIdentity, legacyParsed.findings], (left, right) => left.localeCompare(right)));
341
- const legacyKeys = new Set([
342
- hash("sha256", canonicalJson([...checkpointIdentity, legacyIdentity])),
343
- historicalKey,
344
- ]);
345
- async function rejectUnidentifiedLegacyCheckpoint() {
346
- const entries = await readdir(state, { withFileTypes: true }).catch((error) => {
347
- if (error.code === "ENOENT")
348
- return [];
349
- throw error;
350
- });
351
- if (entries.length)
352
- await validatePreparedOutputDir(state, undefined, true);
353
- for (const entry of entries) {
354
- if (!entry.isFile() || !/^[a-f0-9]{64}\.pending\.json$/u.test(entry.name))
355
- continue;
356
- const candidateKey = entry.name.slice(0, -".pending.json".length);
357
- if (candidateKey === key || legacyKeys.has(candidateKey))
358
- continue;
359
- const serialized = await readOptionalFile(join(state, entry.name));
360
- if (serialized === undefined)
361
- continue;
362
- try {
363
- // Locale-dependent filenames predate input projections. Later formats
364
- // must remain isolated by their deployment-bound checkpoint keys.
365
- const content = JSON.parse(serialized);
366
- if (!content || content.inputProjection !== undefined)
367
- continue;
368
- validateSaved(serialized, true);
369
- }
370
- catch {
371
- continue;
372
- }
373
- // These older bodies contain no deployment identity. Do not guess a
374
- // destination or replace their immutable request with a fresh publication.
375
- throw new CodexSecurityError("An older pending import matches this input, but its checkpoint identity could not be established. Resume it using the original deployment and locale before publishing this selection. The saved request was preserved without uploading.");
1500
+ // The original filename and digests identify the previous selection, but
1501
+ // contained {} for every boxed number. Only its actual saved request can
1502
+ // prove which numbers were approved.
1503
+ function readPreviousNumbers(record) {
1504
+ const content = readSaved(record);
1505
+ const projection = content.inputProjection;
1506
+ const saved = submissionFindings(content.requests);
1507
+ const savedWire = wireJson(saved);
1508
+ const wireDigest = hash("sha256", savedWire);
1509
+ if (projection?.inputDigest !== jsonDigest(originalIdentity, canonicalJson))
1510
+ throw unmatchedPreviousNumbers();
1511
+ if (projection.identityFormat === 2) {
1512
+ // A current writer may own the old filename for an ordinary object.
1513
+ if (projection.savedEvidenceDigest === wireDigest)
1514
+ return undefined;
1515
+ throw unmatchedPreviousNumbers();
1516
+ }
1517
+ const selected = originalParsed.findings.map(wireFinding);
1518
+ if (projection.savedEvidenceDigest !== jsonDigest(selected, canonicalJson))
1519
+ throw unmatchedPreviousNumbers();
1520
+ if (savedWire !== wireJson(selected)) {
1521
+ // A genuinely empty object at this key is a separate ordinary selection.
1522
+ if (wireDigest === projection.savedEvidenceDigest)
1523
+ return undefined;
1524
+ return "different-wire";
376
1525
  }
1526
+ if ((changedCapture && !historicalOnly) ||
1527
+ !verifiableLegacyMetadata(originalParsed, false))
1528
+ throw unmatchedPreviousNumbers();
1529
+ checkFindingProvider({ ...parsed, findings: saved }, options.provider);
1530
+ content.legacyApproval = true;
1531
+ content.inputProjection = {
1532
+ ...content.inputProjection,
1533
+ identityFormat: 2,
1534
+ inputDigest,
1535
+ savedEvidenceDigest: wireDigest,
1536
+ };
1537
+ return content;
1538
+ }
1539
+ function unmatchedPreviousNumbers() {
1540
+ return new CodexSecurityError("The older saved import cannot be matched to the selected input. Its checkpoint was preserved without uploading.");
1541
+ }
1542
+ const key = scopeKey(currentIdentity);
1543
+ const stagingKey = originalKey ?? key;
1544
+ const canonicalPath = pendingFile(key);
1545
+ const lookupPath = join(state, `${key}.lookup.json`);
1546
+ // A lookup only suggests a regular file to validate. A missing or stale hint
1547
+ // leaves the same on-disk discovery available as before.
1548
+ async function readOptionalRegularFile(path) {
1549
+ try {
1550
+ if ((await lstat(path)).isFile())
1551
+ return await readFile(path, "utf8");
1552
+ }
1553
+ catch {
1554
+ // Optional discovery must not veto an independently verified checkpoint.
1555
+ }
1556
+ }
1557
+ async function lookupFileKey() {
1558
+ const serialized = await readOptionalRegularFile(lookupPath);
1559
+ return serialized?.trim().match(/^"([a-f0-9]{64})"$/u)?.[1];
1560
+ }
1561
+ const historicalKey = localeKey(legacyParsed.findings);
1562
+ const legacyIdentityKey = scopeKey(legacyIdentity);
1563
+ const legacyKeys = new Set([legacyIdentityKey, historicalKey]);
1564
+ function underLocks(keys, operation) {
1565
+ return withImportLocks(state, keys, dependencies.signal, operation);
377
1566
  }
378
1567
  let recovered;
379
1568
  for (const legacyKey of legacyKeys) {
380
- if (legacyKey === key)
1569
+ if (legacyKey === key || legacyKey === originalKey)
381
1570
  continue;
382
- const legacyPath = join(state, `${legacyKey}.pending.json`);
1571
+ const legacyPath = pendingFile(legacyKey);
383
1572
  const captured = await readOptionalFile(legacyPath);
384
1573
  if (captured !== undefined) {
385
1574
  // Preserve requests staged by the earlier locale-based filename format.
386
1575
  // Hold both publication locks while moving the original checkpoint.
387
- await withImportLock(state, key, dependencies.signal, async () => withImportLock(state, legacyKey, dependencies.signal, async () => {
388
- const current = await readOptionalFile(pendingPath);
1576
+ await underLocks([key, legacyKey], async () => {
1577
+ const current = await readOptionalFile(canonicalPath);
389
1578
  const serialized = await readOptionalFile(legacyPath);
390
- if (current !== undefined) {
391
- recovered = validateSaved(current);
392
- if (serialized !== undefined) {
393
- // A crash may leave both copies after installing the canonical file.
394
- // Retire only an identical immutable submission, never another request.
395
- let duplicate = null;
396
- try {
397
- duplicate = JSON.parse(serialized);
398
- }
399
- catch (error) {
400
- if (!(error instanceof SyntaxError))
401
- throw error;
402
- }
403
- if (duplicate !== null &&
404
- typeof duplicate === "object" &&
405
- sameSubmission(duplicate, recovered))
406
- await rm(legacyPath);
1579
+ let prior;
1580
+ if (serialized !== undefined) {
1581
+ try {
1582
+ prior = JSON.parse(serialized);
407
1583
  }
1584
+ catch (error) {
1585
+ if (current === undefined || !(error instanceof SyntaxError))
1586
+ throw error;
1587
+ }
1588
+ }
1589
+ if (prior?.wireKey !== undefined)
408
1590
  return;
1591
+ if (current !== undefined) {
1592
+ recovered = validateSaved(JSON.parse(current));
1593
+ if (prior && sameSubmission(prior, recovered))
1594
+ await rm(legacyPath);
1595
+ return;
1596
+ }
1597
+ // A report-free approval at the locale-era filename can be a separate input.
1598
+ if (legacyKey !== legacyIdentityKey &&
1599
+ prior?.inputProjection &&
1600
+ prior.inputProjection.inputDigest !== legacyInputDigest) {
1601
+ const projection = prior.inputProjection;
1602
+ const rows = legacyParsed.findings;
1603
+ if (projection.identityFormat === 2 ||
1604
+ (projection.inputDigest === jsonDigest(rows) &&
1605
+ projection.savedEvidenceDigest ===
1606
+ jsonDigest(submissionFindings(prior.requests))))
1607
+ return;
1608
+ // The released SDK hashed report-free rows as an array and boxed scores as {}.
1609
+ const originalRows = original?.parsed.findings ?? rows;
1610
+ if (hasBoxedScalars &&
1611
+ legacyParsed.report !== undefined &&
1612
+ legacyKey === scopeKey(originalRows, canonicalJson) &&
1613
+ projection.inputDigest ===
1614
+ jsonDigest(originalRows, canonicalJson) &&
1615
+ projection.savedEvidenceDigest ===
1616
+ jsonDigest(originalRows.map(wireFinding), canonicalJson) &&
1617
+ sameFindingSelection(submissionFindings(readSaved(prior).requests), rows))
1618
+ return;
409
1619
  }
410
- // A competing resumer may have completed and removed both paths while
411
- // this caller waited. Retain its observed immutable request and receipts.
412
- const content = validateSaved(serialized ?? captured, true);
1620
+ const content = validateSaved(serialized === undefined ? JSON.parse(captured) : prior, true, legacyKey);
413
1621
  if (!sameFindingSelection(legacyParsed.findings, parsed.findings))
414
1622
  throw new CodexSecurityError("The saved import selects different findings from this input. Resume the original selection before publishing this selection.");
415
- if (content.requests.some((submission) => submission.repository.reset_marker !== destination.reset_marker)) {
1623
+ await retireIfReset(content, async () => {
416
1624
  if (serialized !== undefined)
417
1625
  await rm(legacyPath);
418
- throw new CloudImportError(409, "The repository was reset after this submission. The saved request was retired without uploading. Review the destination and run the command again to approve a fresh publication.");
419
- }
1626
+ });
420
1627
  const savedEvidenceDigest = content.inputProjection?.savedEvidenceDigest ??
421
- hash("sha256", canonicalJson(legacyParsed.findings));
1628
+ jsonDigest(legacyParsed.findings);
422
1629
  const rebindProjection = content.inputProjection !== undefined ||
423
1630
  savedEvidenceDigest !== inputDigest;
424
- if (rebindProjection)
425
- content.inputProjection = { inputDigest, savedEvidenceDigest };
1631
+ if (rebindProjection) {
1632
+ if (content.inputProjection?.identityFormat !== 2)
1633
+ content.legacyApproval = true;
1634
+ content.inputProjection = {
1635
+ ...content.inputProjection,
1636
+ identityFormat: 2,
1637
+ inputDigest,
1638
+ savedEvidenceDigest,
1639
+ };
1640
+ }
426
1641
  recovered = content;
427
1642
  if (serialized === undefined)
428
1643
  return;
429
- if (!rebindProjection) {
430
- await rename(legacyPath, pendingPath);
431
- }
432
- else {
433
- // Bind the new input projection to the original immutable evidence,
434
- // so later inventory reordering still resumes these exact requests.
435
- await writeAtomicJson(pendingPath, content);
436
- await rm(legacyPath);
1644
+ await withSourceAdmission(async () => {
1645
+ if (!rebindProjection) {
1646
+ await rename(legacyPath, canonicalPath);
1647
+ }
1648
+ else {
1649
+ // Bind the new input projection to the original immutable evidence,
1650
+ // so later inventory reordering still resumes these exact requests.
1651
+ await writeAtomicJson(canonicalPath, content);
1652
+ await rm(legacyPath);
1653
+ }
1654
+ });
1655
+ });
1656
+ }
1657
+ }
1658
+ // Some getter-backed batches cannot reproduce a released filename. Their IDs
1659
+ // can stop a conflicting upload, but never authorize replay of a saved request.
1660
+ const potentialIds = input.potentialIds === undefined && changedCapture
1661
+ ? original.parsed.findings.map((row) => row.source_finding_id)
1662
+ : input.potentialIds;
1663
+ const unprovenPrefixes = new Set(input.unprovenIdPrefixes);
1664
+ const uncertainIds = new Set(input.uncertainFindingIds);
1665
+ const uncertainMembership = input.unprovenOccurrenceCount ||
1666
+ uncertainIds.size > 0 ||
1667
+ unprovenPrefixes.size > 0;
1668
+ function uncertainId(id) {
1669
+ return (uncertainIds.has(id) ||
1670
+ unprovenPrefixes.has(id.replace(/\/[a-f0-9]{64}$/u, "/")));
1671
+ }
1672
+ const stableIds = uncertainMembership
1673
+ ? (potentialIds ?? parsed.findings.map((row) => row.source_finding_id)).filter((id) => id === undefined || !uncertainId(id))
1674
+ : undefined;
1675
+ const knownMembership = input.unprovenOccurrenceCount
1676
+ ? new Set(stableIds)
1677
+ : undefined;
1678
+ const knownRows = new Map(input.knownFindings?.map((row) => [
1679
+ row.source_finding_id,
1680
+ wireFinding(row),
1681
+ ]));
1682
+ let originalInputDigest;
1683
+ const cannotCaptureOriginal = !input.original &&
1684
+ (potentialIds === null || !!(potentialIds?.length || uncertainMembership));
1685
+ const possibleBufferExport = options.provider === "github" && original && !changedCapture;
1686
+ function potentialOriginal(record, fileKey) {
1687
+ const projection = record.inputProjection;
1688
+ if (record.accountId !== credentials.account_id)
1689
+ return false;
1690
+ if (!projection) {
1691
+ // Locale-era requests had no projection. Their scoped occurrence IDs
1692
+ // can stop an opaque retry but cannot authorize replay.
1693
+ if (!cannotCaptureOriginal || record.wireKey !== undefined)
1694
+ return false;
1695
+ }
1696
+ else if (!/^[a-f0-9]{64}$/u.test(projection.inputDigest) ||
1697
+ !/^[a-f0-9]{64}$/u.test(projection.savedEvidenceDigest) ||
1698
+ (projection.identityFormat === 2 && !record.legacyApproval))
1699
+ return false;
1700
+ try {
1701
+ // Older evidence may fail today's limits. Scope and selection can still
1702
+ // identify an outstanding approval and prevent a duplicate submission.
1703
+ if (!matchesSavedDestination(record.requests))
1704
+ return false;
1705
+ const rows = submissionFindings(record.requests);
1706
+ const savedWire = wireJson(rows);
1707
+ const evidenceMatches = projection &&
1708
+ projection.savedEvidenceDigest === hash("sha256", savedWire);
1709
+ if (evidenceMatches) {
1710
+ // Matching old digests reconstruct its scoped filename. A report from
1711
+ // this caller is useful only when the saved digest independently binds it.
1712
+ const reportFree = projection.inputDigest === projection.savedEvidenceDigest;
1713
+ const identity = reportFree
1714
+ ? rows
1715
+ : parsed.report !== undefined
1716
+ ? { findings: rows, report: reportForJson(parsed.report) }
1717
+ : undefined;
1718
+ if (identity &&
1719
+ (reportFree || projection.inputDigest === jsonDigest(identity))) {
1720
+ if (record.wireKey
1721
+ ? record.wireKey !== scopeKey(identity)
1722
+ : fileKey !== scopeKey(identity, canonicalJson) &&
1723
+ fileKey !== localeKey(rows))
1724
+ return false;
1725
+ if (possibleBufferExport &&
1726
+ projection.inputDigest !== inputDigest &&
1727
+ sameLegacyJson(identity, currentIdentity, previousBufferJson)) {
1728
+ return true;
1729
+ }
437
1730
  }
438
- }));
1731
+ if (!record.wireKey &&
1732
+ reportFree &&
1733
+ original &&
1734
+ original.wire !== savedWire)
1735
+ return false;
1736
+ }
1737
+ if (record.wireKey && !cannotCaptureOriginal)
1738
+ return false;
1739
+ if (projection &&
1740
+ original &&
1741
+ fileKey !== stagingKey &&
1742
+ fileKey !== originalWireKey &&
1743
+ projection.inputDigest ===
1744
+ (originalInputDigest ??= jsonDigest(original.identity, canonicalJson)) &&
1745
+ fileKey !== localeKey(original.parsed.findings) &&
1746
+ projection.savedEvidenceDigest ===
1747
+ jsonDigest(original.parsed.findings.map(wireFinding), canonicalJson) &&
1748
+ savedWire === wireJson(original.parsed.findings.map(wireFinding)))
1749
+ return false;
1750
+ if (stableIds) {
1751
+ // An opaque occurrence may have had a different old ID. Compare all
1752
+ // definite members and count the remaining IDs without claiming them.
1753
+ const certain = rows.filter((row) => !uncertainId(row.source_finding_id));
1754
+ const stableSaved = knownMembership
1755
+ ? certain.filter((row) => knownMembership.has(row.source_finding_id))
1756
+ : certain;
1757
+ if (certain.length - stableSaved.length >
1758
+ (input.unprovenOccurrenceCount ?? 0))
1759
+ return false;
1760
+ return (rows.length > 0 &&
1761
+ stableSaved.length === stableIds.length &&
1762
+ stableSaved.every((row, i) => {
1763
+ const known = knownRows.get(row.source_finding_id);
1764
+ return (row.source_finding_id === stableIds[i] &&
1765
+ (!known || wireJson(row) === wireJson(known)));
1766
+ }));
1767
+ }
1768
+ const candidates = originalParsed.findings;
1769
+ const ids = potentialIds === undefined
1770
+ ? parsed.findings.map((row) => row.source_finding_id)
1771
+ : potentialIds;
1772
+ if (ids &&
1773
+ (!ids.length ||
1774
+ rows.length !== ids.length ||
1775
+ !rows.every((row, i) => ids[i] === undefined || row.source_finding_id === ids[i])))
1776
+ return false;
1777
+ // An opaque serializer can add a report or replace every visible field.
1778
+ // Only scoped, independently known occurrences can rule out its old request.
1779
+ if (cannotCaptureOriginal || !projection)
1780
+ return true;
1781
+ if (projection.inputDigest === projection.savedEvidenceDigest &&
1782
+ parsed.report !== undefined)
1783
+ return false;
1784
+ if (potentialIds === undefined && (hasBoxedScalars || evidenceMatches))
1785
+ return false;
1786
+ return (!sameFindingSelection(rows, candidates) ||
1787
+ rows.every((row, i) => {
1788
+ const { source_data: savedData, ...savedFields } = row.evidence;
1789
+ const { source_data: selectedData, ...selectedFields } = candidates[i].evidence;
1790
+ if (wireJson(savedFields) !== wireJson(selectedFields))
1791
+ return false;
1792
+ // Once the original SDK discarded boxed positions, a plain JSON export
1793
+ // alone cannot attribute a new numeric spelling to that saved request.
1794
+ return sameLegacyJson(savedData, selectedData, (value) => typeof value === "number");
1795
+ }));
1796
+ }
1797
+ catch {
1798
+ return false;
439
1799
  }
440
1800
  }
441
- const serialized = await readOptionalFile(pendingPath);
442
- const saved = serialized === undefined ? recovered : validateSaved(serialized);
443
- if (!saved)
444
- await rejectUnidentifiedLegacyCheckpoint();
445
- if (saved) {
446
- const content = saved;
447
- for (const submission of content.requests) {
448
- if (submission.repository.reset_marker !== destination.reset_marker) {
449
- // The old request is never sent after reset. A subsequent explicit
450
- // invocation prepares a fresh submission and asks for approval again.
451
- await withImportLock(state, key, dependencies.signal, async () => {
452
- const current = await readOptionalFile(pendingPath);
453
- if (current !== undefined &&
454
- sameSubmission(JSON.parse(current), content))
455
- await rm(pendingPath);
1801
+ async function selectedCheckpoint(knownFileKey, preparing = false) {
1802
+ const directory = await validatePreparedOutputDir(state, undefined, true).catch(nullIfMissingFile);
1803
+ if (!directory)
1804
+ return undefined;
1805
+ const known = new Set([key, originalKey, knownFileKey].filter((value) => value !== undefined));
1806
+ let unidentified = false;
1807
+ let unprovenOriginal = false;
1808
+ let incomplete = false;
1809
+ let stagingPathOccupied = false;
1810
+ async function readCandidate(fileKey, selected, contents) {
1811
+ const path = pendingFile(fileKey);
1812
+ const serialized = contents ??
1813
+ (selected?.fileKey === key && !known.has(fileKey)
1814
+ ? await readOptionalRegularFile(path)
1815
+ : await readOptionalFile(path)) ??
1816
+ (preparing && fileKey === key ? capturedPending : undefined);
1817
+ if (serialized === undefined) {
1818
+ if (!known.has(fileKey))
1819
+ incomplete = true;
1820
+ return selected;
1821
+ }
1822
+ if (fileKey === stagingKey)
1823
+ stagingPathOccupied = true;
1824
+ let record;
1825
+ try {
1826
+ record = JSON.parse(serialized);
1827
+ }
1828
+ catch (error) {
1829
+ if (known.has(fileKey))
1830
+ throw error;
1831
+ incomplete = true;
1832
+ return selected;
1833
+ }
1834
+ // A migrated checkpoint records the wire identity it belongs to even
1835
+ // when a separate input hashes to its historical filename.
1836
+ const tag = record?.wireKey;
1837
+ const tagged = tag !== undefined;
1838
+ const ownerKey = tagged ? tag : fileKey;
1839
+ const historical = !tagged && fileKey === originalKey && fileKey !== key;
1840
+ if (ownerKey !== key && !historical) {
1841
+ if (hosted && record?.inputProjection) {
1842
+ let status;
1843
+ try {
1844
+ status = originalHostedPending(record, ownerKey);
1845
+ }
1846
+ catch {
1847
+ // An unrelated or unreadable checkpoint cannot identify this source.
1848
+ }
1849
+ if (status === "unidentified")
1850
+ throw new CodexSecurityError(unknownHostedRetry);
1851
+ if (status) {
1852
+ if (wasReset(record)) {
1853
+ if (!preparing)
1854
+ throw new CodexSecurityError("The pending request and this preview have different repository reset markers. Review the destination again.");
1855
+ await retirePendingIfReset(record, [fileKey], path);
1856
+ }
1857
+ throw new CodexSecurityError(originalHostedRetry);
1858
+ }
1859
+ }
1860
+ if (record &&
1861
+ (!tagged || cannotCaptureOriginal || possibleBufferExport) &&
1862
+ potentialOriginal(record, fileKey))
1863
+ unprovenOriginal = true;
1864
+ // Some early locale filenames carry no deployment identity. Never guess their target.
1865
+ if (!tagged &&
1866
+ record &&
1867
+ !record.inputProjection &&
1868
+ !legacyKeys.has(fileKey)) {
1869
+ try {
1870
+ // A different locale cannot authorize replay, even when the
1871
+ // original native capture proves these rows were selected.
1872
+ if (!original ||
1873
+ wireJson(submissionFindings(readSaved(record).requests)) !==
1874
+ wireJson(original.parsed.findings.map(wireFinding)))
1875
+ validateSaved(record, true);
1876
+ unidentified = true;
1877
+ }
1878
+ catch {
1879
+ /* Unrelated input. */
1880
+ }
1881
+ }
1882
+ return selected;
1883
+ }
1884
+ const content = historical
1885
+ ? readPreviousNumbers(record)
1886
+ : validateSaved(record);
1887
+ if (content === "different-wire") {
1888
+ // Another spelling may already have a verified checkpoint for this wire value.
1889
+ unprovenOriginal = true;
1890
+ return selected;
1891
+ }
1892
+ if (!content)
1893
+ return selected;
1894
+ if (selected) {
1895
+ const conflict = new CodexSecurityError("Two saved imports match this input. Their original requests and receipts were preserved without uploading.");
1896
+ if (!preparing)
1897
+ throw conflict;
1898
+ // The two unlocked reads may have straddled completion of the first request.
1899
+ return await underLocks([key, selected.fileKey, fileKey], async () => {
1900
+ const first = await readCandidate(selected.fileKey);
1901
+ if (first) {
1902
+ if (await readCandidate(fileKey))
1903
+ throw conflict;
1904
+ if (sameSubmission(first.content, selected.content))
1905
+ return first;
1906
+ }
1907
+ return completedCheckpoint(selected);
456
1908
  });
457
- throw new CloudImportError(409, "The repository was reset after this submission. The saved request was retired without uploading. Review the destination and run the command again to approve a fresh publication.");
1909
+ }
1910
+ return { fileKey, content, historical };
1911
+ }
1912
+ let selected;
1913
+ for (const fileKey of known)
1914
+ selected = await readCandidate(fileKey, selected);
1915
+ // A known file that disappeared can be checked against its completed receipt.
1916
+ if (!selected && knownFileKey !== undefined && !hosted)
1917
+ return undefined;
1918
+ const hint = await lookupFileKey();
1919
+ if (hint && !known.has(hint)) {
1920
+ const contents = await readOptionalRegularFile(pendingFile(hint));
1921
+ if (contents !== undefined) {
1922
+ selected = await readCandidate(hint, selected, contents);
1923
+ known.add(hint);
458
1924
  }
459
1925
  }
1926
+ // A released publisher could stage the canonical request while another SDK
1927
+ // tagged its old filename but had not yet written the optional lookup.
1928
+ if (selected &&
1929
+ !cannotCaptureOriginal &&
1930
+ !selected.historical &&
1931
+ (selected.fileKey !== key ||
1932
+ selected.content.canonicalChecked ||
1933
+ selected.content.inputProjection?.identityFormat === 2))
1934
+ return selected;
1935
+ const entries = (await readdir(state, { withFileTypes: true }).catch(nullIfMissingFile)) ?? [];
1936
+ for (const entry of entries) {
1937
+ if (!entry.isFile() || !/^[a-f0-9]{64}\.pending\.json$/u.test(entry.name))
1938
+ continue;
1939
+ const fileKey = entry.name.slice(0, -".pending.json".length);
1940
+ if (!known.has(fileKey))
1941
+ selected = await readCandidate(fileKey, selected);
1942
+ }
1943
+ if (unprovenOriginal && (!selected || cannotCaptureOriginal))
1944
+ throw unmatchedPreviousNumbers();
1945
+ if (!selected && stagingPathOccupied)
1946
+ throw occupiedCheckpoint();
1947
+ if (!selected && unidentified)
1948
+ throw new CodexSecurityError("An older pending import matches this input, but its checkpoint identity could not be established. Resume it using the original deployment and locale before publishing this selection. The saved request was preserved without uploading.");
1949
+ if (selected?.fileKey === key && !preparing && !incomplete)
1950
+ selected.checkedCanonical = true;
1951
+ return selected;
460
1952
  }
1953
+ async function readCompletedPublication(requests, observedKey) {
1954
+ for (const resultKey of new Set([key, observedKey])) {
1955
+ const serialized = await readOptionalFile(join(state, `${resultKey}.result.json`));
1956
+ if (serialized === undefined)
1957
+ continue;
1958
+ const completed = JSON.parse(serialized);
1959
+ if ((completed?.status === "complete" || completed?.status === "partial") &&
1960
+ Array.isArray(completed.receipts) &&
1961
+ completed.receipts.length === requests.length &&
1962
+ completed.receipts.every((receipt, i) => receipt?.id === requests[i].request_id))
1963
+ return {
1964
+ receipts: completed.receipts.map((receipt, i) => validatePublicationReceipt(receipt, requests[i])),
1965
+ verified: completed.verified,
1966
+ };
1967
+ }
1968
+ }
1969
+ async function completedCheckpoint(observed) {
1970
+ if (await readCompletedPublication(observed.content.requests, observed.fileKey))
1971
+ return observed;
1972
+ throw new CodexSecurityError("The saved publication changed while preparing this input. Review it again.");
1973
+ }
1974
+ function occupiedCheckpoint() {
1975
+ return new CodexSecurityError("Finish the other pending import at this checkpoint first, then review this input again.");
1976
+ }
1977
+ let selected = await selectedCheckpoint(recovered ? key : undefined, true);
1978
+ const fileKey = selected?.fileKey ?? (recovered ? key : stagingKey);
1979
+ const pendingPath = pendingFile(fileKey);
1980
+ async function rememberFile() {
1981
+ if (fileKey !== key && (await lookupFileKey()) !== fileKey)
1982
+ await writeAtomicJson(lookupPath, fileKey).catch(() => undefined);
1983
+ }
1984
+ async function removePending() {
1985
+ await rm(pendingPath, { force: true });
1986
+ if (fileKey !== key && (await lookupFileKey()) === fileKey)
1987
+ await rm(lookupPath, { force: true }).catch(() => undefined);
1988
+ }
1989
+ const lockKeys = [key, fileKey, stagingKey];
1990
+ if (alternateKey)
1991
+ lockKeys.push(alternateKey);
1992
+ async function underPublishLocks(operation) {
1993
+ // An older publisher may already hold its lock before its checkpoint exists.
1994
+ // Derive that key for actual writes, even after a checkpoint-free preview.
1995
+ if (resolved.readPreviousFindings && !previousFindings) {
1996
+ previousFindings = resolved.readPreviousFindings();
1997
+ alternateKey = previousCandidateKey(previousFindings);
1998
+ if (alternateKey)
1999
+ lockKeys.push(alternateKey);
2000
+ }
2001
+ return underLocks(lockKeys, operation);
2002
+ }
2003
+ if (selected?.historical) {
2004
+ const observed = selected;
2005
+ selected = await underLocks(lockKeys, async () => {
2006
+ const current = await selectedCheckpoint(fileKey);
2007
+ if (!current ||
2008
+ current.fileKey !== fileKey ||
2009
+ !sameSubmission(current.content, observed.content))
2010
+ return completedCheckpoint(observed);
2011
+ if (current.historical) {
2012
+ current.content.wireKey = key;
2013
+ await writeAtomicJson(pendingPath, current.content);
2014
+ await rememberFile();
2015
+ }
2016
+ return current;
2017
+ });
2018
+ }
2019
+ const saved = selected?.content ?? recovered;
2020
+ if (historicalOnly && !saved)
2021
+ throw unmatchedPreviousNumbers();
2022
+ if (!parsed.findings.length)
2023
+ throw new NoPublishableFindingsError(parsed);
2024
+ if (saved)
2025
+ await retirePendingIfReset(saved, lockKeys, pendingPath, removePending);
461
2026
  if (saved?.inspectorProjectMapRequired && !inspectorProjectMap)
462
2027
  throw new CodexSecurityError("Supply the verified Inspector project binding again to resume.");
463
- function checkRepositories(findings) {
464
- // Validated non-Inspector checkpoints retain their original provider and repository ID.
465
- if (saved && options.provider !== "inspector")
466
- return;
467
- const binding = inspectorReportRepository(options.provider, findings, "cloud", inspectorProjectMap);
2028
+ // Existing repository submissions keep their original request bodies and receipts.
2029
+ const privatePath = join(state, `${key}.private-import.json`);
2030
+ const privateSaved = await readOptionalFile(privatePath);
2031
+ if (resumingPreviousReader && !saved && privateSaved === undefined)
2032
+ throw new CodexSecurityError("The saved publication changed. Run the command again to preview this report.");
2033
+ const usePrivateImport = !saved && (canStartJob || privateSaved !== undefined);
2034
+ async function preparePrivateAttempt() {
2035
+ const context = previewContext ?? (await privateClient.context());
2036
+ if (source.source_key.trim() !== source.source_key)
2037
+ throw new CodexSecurityError("A private source key cannot have leading or trailing whitespace.");
2038
+ const report = await selectPrivateReport(parsed, source.provider, nativePath, context, privateSaved, resolved.requiresStructuredReport);
2039
+ const binding = {
2040
+ format: report.format,
2041
+ apiBaseUrl,
2042
+ accountId: credentials.account_id,
2043
+ source,
2044
+ repository: {
2045
+ id: destination.id,
2046
+ repo_connector_id: destination.repo_connector_id,
2047
+ },
2048
+ sha256: report.digest,
2049
+ contextVersion: context.context_version,
2050
+ };
2051
+ return { context, report, binding };
2052
+ }
2053
+ function acceptsJob(context, format) {
2054
+ return context.can_create_import_jobs && context.formats.includes(format);
2055
+ }
2056
+ // Use an earlier catalog URL only after the account, connector, repository,
2057
+ // inbox context and exact upload digest all match the immutable checkpoint.
2058
+ const existingPrivate = usePrivateImport && privateSaved !== undefined
2059
+ ? await preparePrivateAttempt()
2060
+ : undefined;
2061
+ const previous = existingPrivate &&
2062
+ readPrivateCheckpoint(privateSaved, existingPrivate.binding);
2063
+ const retryOfJobId = unsuccessfulJob(previous?.receipt) &&
2064
+ canStartJob &&
2065
+ existingPrivate?.context.can_upload &&
2066
+ acceptsJob(existingPrivate.context, existingPrivate.report.format)
2067
+ ? previous?.jobId
2068
+ : undefined;
2069
+ const resumed = previous !== undefined && retryOfJobId === undefined;
2070
+ const privateApproval = resumed ? previous : undefined;
2071
+ const privateUrl = privateApproval?.approvedRepositoryUrl;
2072
+ const approvedPlatform = saved?.approvedRepositoryPlatform ??
2073
+ privateApproval?.approvedRepositoryPlatform;
2074
+ // The findings-only checkpoint key lets projected and native callers resume one immutable
2075
+ // request. Supplemental report claims are outside that digest and must still match its source.
2076
+ const recheckOriginal = options.provider === "semgrep" &&
2077
+ projection !== resolved &&
2078
+ saved?.approvedRepositoryPlatform === "other";
2079
+ // Native Semgrep and OpenGrep files include their required source URL in every
2080
+ // saved finding. An exact replay by repository ID survives a catalog rename.
2081
+ const recordedFileSource = saved &&
2082
+ !saved.approvedRepositoryUrl &&
2083
+ options.provider === "semgrep" &&
2084
+ !deferred &&
2085
+ declaredUrl !== undefined &&
2086
+ saved.requests.every(({ items }) => items.every(({ evidence }) => evidence.details?.repository?.url === declaredUrl));
2087
+ const declaredSource = recheckOriginal ||
2088
+ readerSources.length > 0 ||
2089
+ (declaredUrl !== undefined && !recordedFileSource) ||
2090
+ report.sourceRepositoryUrl !== undefined ||
2091
+ report.sourceRepositoryPlatform !== undefined ||
2092
+ report.snykRestContext !== undefined;
2093
+ let legacyTypedUrl;
2094
+ let suppliedReport = report;
2095
+ const sourceFor = (url, platform) => {
2096
+ try {
2097
+ return hostedRepositoryMatcher(platform
2098
+ ? { ...suppliedReport, sourceRepositoryPlatform: platform }
2099
+ : suppliedReport, options.provider, declaredUrl, { url }, readerSources, saved !== undefined);
2100
+ }
2101
+ catch (error) {
2102
+ if (!recheckOriginal || readerSources.length)
2103
+ throw error;
2104
+ // An older strict approval did not distinguish GitLab from another case-sensitive SCM.
2105
+ throw new CodexSecurityError(`${error instanceof Error ? error.message : String(error)} Resume with the original Semgrep input and SCM repository mapping, or the SDK build that created this request.`, { cause: error });
2106
+ }
2107
+ };
2108
+ if (hosted && saved && declaredSource) {
2109
+ // The released publisher checked typed repository URLs against its authenticated catalog.
2110
+ // Opaque original records could name a different project and cannot establish a rename.
2111
+ const findings = submissionFindings(saved.requests);
2112
+ if (!saved.approvedRepositoryUrl)
2113
+ legacyTypedUrl = findings.find(({ evidence }) => evidence.details?.repository?.url)?.evidence.details?.repository?.url;
2114
+ // Supplemental claims must agree with each verifiable row from the immutable request.
2115
+ suppliedReport = {
2116
+ ...report,
2117
+ // The v1 upgrade may drop a redundant mapping using the catalog's GitHub casing.
2118
+ findings: recheckOriginal ? [...report.findings, ...findings] : findings,
2119
+ };
2120
+ }
2121
+ // The released request retained its typed finding URL, but only the authenticated
2122
+ // catalog or a previously recorded approval can bind a new source declaration.
2123
+ const approvedUrl = saved?.approvedRepositoryUrl ?? privateUrl ?? destination.url;
2124
+ if (hosted &&
2125
+ readerSources.some(({ platform, url }) => {
2126
+ const source = normalizeHostedGitRepositoryUrl(url, approvedUrl, platform !== undefined);
2127
+ return (cloudSourceKey(source, platform) !==
2128
+ cloudSourceKey(approvedUrl, platform));
2129
+ }))
2130
+ throw new CodexSecurityError("The reader's source repository does not match the selected Cloud repository.");
2131
+ await recheckFileRepository?.(approvedUrl, approvedPlatform);
2132
+ const hostedSource = hosted && (!saved || declaredSource)
2133
+ ? sourceFor(approvedUrl, approvedPlatform)
2134
+ : undefined;
2135
+ const matchesHostedRepository = hostedSource?.matches;
2136
+ function requireApprovedSource(url, platform) {
2137
+ const binding = platform && hostedSource?.github
2138
+ ? sourceFor(url, platform)
2139
+ : hostedSource;
2140
+ if (binding && !binding.matches(url))
2141
+ throw new CodexSecurityError("The source project does not match the selected Cloud repository.");
2142
+ }
2143
+ requireApprovedSource(approvedUrl);
2144
+ if (legacyTypedUrl)
2145
+ requireApprovedSource(legacyTypedUrl);
2146
+ const trivyTarget = saved?.trivyRepositoryUrl ?? privateUrl ?? destination.url;
2147
+ function requireTrivyRemote(url, id) {
2148
+ if (trivyRemoteKey(url, trivyTarget) !== catalogKey(trivyTarget)) {
2149
+ const label = id === undefined ? "Source report" : `Finding ${JSON.stringify(id)}`;
2150
+ throw new CodexSecurityError(`${label} belongs to ${JSON.stringify(url)}, which does not match the selected Cloud repository ${JSON.stringify(trivyTarget)}. Verify the source repository mapping before publishing.`);
2151
+ }
2152
+ }
2153
+ if (trivyReportUrls) {
2154
+ for (const url of trivyReportUrls)
2155
+ requireTrivyRemote(url);
2156
+ if (parsed !== resolved.current)
2157
+ for (const url of trivyBatchSourceUrls(parsed, true))
2158
+ requireTrivyRemote(url);
2159
+ }
2160
+ function checkRepositories({ findings, report }, historical = Boolean(saved)) {
2161
+ const { provider } = options;
2162
+ checkTrivyReportProvider(provider, report);
2163
+ // A catalog casing or spelling change keeps the original source comparison target.
2164
+ const approved = hosted ? saved?.approvedRepositoryUrl : undefined;
2165
+ const sameCatalog = approved !== undefined &&
2166
+ cloudSourceKey(approved, "github") ===
2167
+ cloudSourceKey(destination.url, "github");
2168
+ const comparisonUrl = trivyReportUrls
2169
+ ? trivyTarget
2170
+ : sameCatalog
2171
+ ? approved
2172
+ : (privateUrl ?? destination.url);
2173
+ const key = sameCatalog
2174
+ ? (url) => cloudSourceKey(url, saved?.approvedRepositoryPlatform === "other"
2175
+ ? undefined
2176
+ : "github")
2177
+ : catalogKey;
468
2178
  function requireDestination(id, url) {
469
2179
  if (url &&
470
- cloudRepositoryUrlKey(url) !== cloudRepositoryUrlKey(destination.url))
2180
+ (matchesHostedRepository
2181
+ ? !matchesHostedRepository(url) ||
2182
+ !matchesHostedRepository(comparisonUrl)
2183
+ : key(url) !== key(comparisonUrl)))
471
2184
  throw new CodexSecurityError(`Finding ${JSON.stringify(id)} belongs to ${JSON.stringify(url)}, which does not match the selected Cloud repository ${JSON.stringify(destination.url)}. Verify the source repository mapping before publishing.`);
472
2185
  }
473
- if (binding)
474
- requireDestination(binding.findingId, binding.url);
2186
+ if (report?.["format"] === guardianSelectionFormat)
2187
+ requireDestination("Guardian source", declaredScannerRepositoryUrl(report["repositoryUrl"], "Guardian"));
2188
+ // An immutable retry may refer to an earlier repository name. Additional
2189
+ // file declarations outside its saved findings authorize no new writes.
2190
+ if (!historical)
2191
+ for (const { url } of fileRepositories?.() ?? [])
2192
+ requireDestination("Source report", provider === "snyk"
2193
+ ? normalizeHostedGitRepositoryUrl(url, destination.url, true)
2194
+ : url);
2195
+ // Historical checkpoints already fixed the provider and request bytes.
2196
+ // Recheck any projected source against the destination discovered for this retry.
2197
+ const checkNative = (...providers) => !historical ||
2198
+ trivyReportUrls !== undefined ||
2199
+ providers.includes(provider);
2200
+ if (checkNative("sonar")) {
2201
+ for (const url of sonarRepositoryUrls(provider, findings, report, "cloud"))
2202
+ requireDestination("Sonar source", url);
2203
+ }
2204
+ const checkmarx = checkmarxSelection(provider, findings, report, "cloud", historical);
2205
+ requireDestination("Checkmarx source", checkmarx.reportUrl);
2206
+ const blackDuck = checkNative("blackduck")
2207
+ ? blackDuckReportRepositories(provider, findings, "cloud")
2208
+ : undefined;
2209
+ const checkmarxRepository = checkmarx.repositories();
2210
+ for (const [scanner, repositories] of [
2211
+ ["veracode", veracodeReportRepositories],
2212
+ ["continuum", continuumReportRepositories],
2213
+ ]) {
2214
+ if (!checkNative(scanner))
2215
+ continue;
2216
+ for (const [id, source] of repositories(provider, findings, "cloud"))
2217
+ requireDestination(id, source.url);
2218
+ }
2219
+ if (checkNative("blackduck", "inspector", "veracode")) {
2220
+ const binding = inspectorReportRepository(provider, findings, "cloud", inspectorProjectMap);
2221
+ if (binding)
2222
+ requireDestination(binding.findingId, binding.url);
2223
+ }
2224
+ const checkOnlyProjected = !checkNative("blackduck", "inspector", "veracode", "checkmarx", "continuum", "sonar");
475
2225
  for (const finding of findings) {
476
2226
  const { source_finding_id: id, evidence } = finding;
477
- const urls = [
2227
+ const guardian = guardianFindingRepository(finding, "cloud");
2228
+ if (guardian) {
2229
+ requireDestination(id, guardian);
2230
+ requireDestination(id, evidence.details?.repository?.url);
2231
+ }
2232
+ checkRetainedTrivyProvider(provider, finding);
2233
+ if (trivyReportUrls) {
2234
+ const remote = trivyRepositoryUrl(retainedTrivyReport(finding), trivyTarget);
2235
+ if (remote)
2236
+ requireTrivyRemote(remote, id);
2237
+ }
2238
+ const blackDuckUrl = blackDuck
2239
+ ? blackDuck.get(id)?.url
2240
+ : projectedBlackDuckRepositoryUrl(finding, "cloud");
2241
+ const hasProjection = !!blackDuckUrl || checkmarx.projections.has(finding);
2242
+ if (checkOnlyProjected && !hasProjection)
2243
+ continue;
2244
+ let urls = [
2245
+ blackDuckUrl,
2246
+ checkmarxRepository(finding)?.url,
478
2247
  evidence.details?.repository?.url,
479
- ...(options.provider === "wiz" ? [wizCsvRepositoryUrl(evidence)] : []),
480
- ...(options.provider === "snyk"
481
- ? snykRepositoryUrls(evidence.source_data, {
482
- cloudRepositoryUrl: destination.url,
483
- })
484
- : []),
485
- ...(options.provider === "endor"
486
- ? endorRepositoryUrls(evidence.source_data)
487
- : []),
488
- options.provider === "github"
489
- ? dependabotRepository(finding)
490
- : undefined,
491
2248
  ];
2249
+ if (provider === "wiz") {
2250
+ const native = hasProjection ||
2251
+ (!historical && isWizPackageFinding(evidence.source_data));
2252
+ urls.push(native
2253
+ ? wizFindingRepository(evidence, "cloud")?.url
2254
+ : wizCsvRepositoryUrl(evidence));
2255
+ }
2256
+ else if (provider === "snyk") {
2257
+ urls = urls.concat(snykRepositoryUrls(evidence.source_data, {
2258
+ cloudRepositoryUrl: destination.url,
2259
+ }));
2260
+ }
2261
+ else if (provider === "endor") {
2262
+ urls = urls.concat(endorRepositoryUrls(evidence.source_data, !blackDuck && !blackDuckUrl));
2263
+ }
2264
+ else if (provider === "github") {
2265
+ if (!historical)
2266
+ urls.push(retainedGitHubRepository(finding));
2267
+ urls.push(dependabotRepository(finding));
2268
+ }
492
2269
  for (const url of urls)
493
2270
  requireDestination(id, url);
494
2271
  }
495
2272
  }
496
- checkRepositories(parsed.findings);
2273
+ if (!saved) {
2274
+ for (const finding of parsed.findings) {
2275
+ if (options.provider !== "semgrep" &&
2276
+ isSemgrepPlatformEvidence(finding, options.provider))
2277
+ throw new CodexSecurityError("Semgrep Platform evidence requires the semgrep provider.");
2278
+ if (options.provider !== "snyk" && isSnykRestFinding(finding))
2279
+ throw new CodexSecurityError("Snyk REST evidence requires the snyk provider.");
2280
+ }
2281
+ }
2282
+ if (!usePrivateImport)
2283
+ checkRepositories(parsed);
497
2284
  // A retry sends its saved projection, which may differ from today's reader.
498
- if (saved)
499
- checkRepositories(submissionFindings(saved.requests));
2285
+ if (saved) {
2286
+ // Only use the report associated with the input digest in the saved checkpoint.
2287
+ const report = saved.inputProjection?.inputDigest === inputDigest
2288
+ ? parsed.report
2289
+ : undefined;
2290
+ checkRepositories({ findings: submissionFindings(saved.requests), report });
2291
+ }
2292
+ // An older, genuinely unbound checkpoint first acquires a URL on a matching preview.
2293
+ // Retain it at the original path so its bare caller can still recover the same request.
2294
+ if (saved && !saved.approvedRepositoryUrl && hostedSource) {
2295
+ await underLocks(lockKeys, async () => {
2296
+ const serialized = await readOptionalFile(pendingPath);
2297
+ if (serialized === undefined)
2298
+ return;
2299
+ const current = validateSaved(JSON.parse(serialized));
2300
+ if (!sameSubmission(current, saved))
2301
+ throw new CodexSecurityError("Another publication prepared this input. Review the saved request again.");
2302
+ if (current.approvedRepositoryUrl) {
2303
+ requireApprovedSource(current.approvedRepositoryUrl, current.approvedRepositoryPlatform);
2304
+ }
2305
+ else {
2306
+ // Adding the catalog URL cannot erase an older SDK's same-selection hold.
2307
+ if (current.inputProjection &&
2308
+ originalHostedPending(current, current.wireKey ?? fileKey) ===
2309
+ "verified")
2310
+ current.inputProjection.retainedHostedSelection = true;
2311
+ current.approvedRepositoryUrl = approvedUrl;
2312
+ await writeAtomicJson(pendingPath, current);
2313
+ }
2314
+ saved.inputProjection = current.inputProjection;
2315
+ saved.approvedRepositoryUrl = current.approvedRepositoryUrl;
2316
+ saved.approvedRepositoryPlatform = current.approvedRepositoryPlatform;
2317
+ });
2318
+ }
2319
+ if (usePrivateImport) {
2320
+ const client = privateClient;
2321
+ const { context, report, binding } = existingPrivate ?? (await preparePrivateAttempt());
2322
+ function requireAccess(value, saved) {
2323
+ if (!value.can_read ||
2324
+ (!saved && !canStartJob) ||
2325
+ (!saved?.jobId &&
2326
+ ((!saved?.fileId && !value.can_upload) ||
2327
+ !acceptsJob(value, report.format))))
2328
+ throw new CodexSecurityError("Private findings import is unavailable for this account or report format.");
2329
+ }
2330
+ checkRepositories(parsed, resumed);
2331
+ requireAccess(context, previous);
2332
+ const privateImport = {
2333
+ format: report.format,
2334
+ contextVersion: binding.contextVersion,
2335
+ sha256: report.digest,
2336
+ bytes: Buffer.byteLength(report.bytes),
2337
+ ...(retryOfJobId ? { retryOfJobId } : {}),
2338
+ };
2339
+ const preview = {
2340
+ ...parsed,
2341
+ cloudApiUrl: `${apiBaseUrl}/finding-imports`,
2342
+ accountId: credentials.account_id,
2343
+ destination,
2344
+ source,
2345
+ resumed,
2346
+ requests: [],
2347
+ privateImport,
2348
+ };
2349
+ return {
2350
+ preview: structuredClone(preview),
2351
+ publish: () => underPublishLocks(async () => {
2352
+ const current = await client.context();
2353
+ if (current.context_version !== binding.contextVersion)
2354
+ throw new CodexSecurityError("The private inbox changed. Run the command again to review the new destination.");
2355
+ await assertNoAlternateCheckpoint(async () => current);
2356
+ const stored = readPrivateCheckpoint(await readOptionalFile(privatePath), binding);
2357
+ if (!stored &&
2358
+ (resumingPreviousReader ||
2359
+ (privateUrl && privateUrl !== destination.url)))
2360
+ throw new CodexSecurityError("The saved private import changed. Run the command again to preview this report.");
2361
+ // Only a preview of this terminal job can start a new attempt.
2362
+ const retrying = retryOfJobId !== undefined && stored?.receipt?.id === retryOfJobId;
2363
+ const active = retrying ? undefined : stored;
2364
+ if (resumed && !active)
2365
+ checkRepositories(parsed);
2366
+ requireAccess(current, active);
2367
+ if (await selectedCheckpoint())
2368
+ throw new CodexSecurityError("A repository submission is pending. Review its saved request before continuing.");
2369
+ // Both routes share request IDs. A server upgrade can promote an
2370
+ // existing job, and its saved route also survives gate rollback.
2371
+ const checkpoint = {
2372
+ ...(active ?? {
2373
+ ...binding,
2374
+ approvedRepositoryUrl: destination.url,
2375
+ approvedRepositoryPlatform: hostedSource?.github === false ? "other" : undefined,
2376
+ uploadRequestId: randomUUID(),
2377
+ jobRequestId: randomUUID(),
2378
+ fileId: undefined,
2379
+ jobId: undefined,
2380
+ receipt: undefined,
2381
+ previousReceipt: retrying ? stored?.receipt : undefined,
2382
+ }),
2383
+ jobApiVersion: stored?.jobApiVersion ?? jobApiVersion,
2384
+ };
2385
+ const result = {
2386
+ cloudApiUrl: preview.cloudApiUrl,
2387
+ status: "interrupted",
2388
+ read: parsed.read,
2389
+ ready: parsed.findings.length,
2390
+ report: parsed.report,
2391
+ excluded: parsed.excluded,
2392
+ receipts: [],
2393
+ counts: { created: 0, updated: 0, unchanged: 0, error: 0 },
2394
+ failures: [],
2395
+ unacknowledged: parsed.findings.length,
2396
+ verified: 0,
2397
+ };
2398
+ function acknowledge(job) {
2399
+ const { created, updated, unchanged, error } = job.counts;
2400
+ const processed = created + updated + unchanged + error;
2401
+ if (job.counts.discovered !== processed + job.counts.unprocessed ||
2402
+ job.counts.discovered > result.ready ||
2403
+ (job.status === "completed" &&
2404
+ (processed !== result.ready ||
2405
+ job.counts.total !== result.ready)))
2406
+ throw new CodexSecurityError("Cloud returned counts that do not match the selected report.");
2407
+ result.privateJob = job;
2408
+ result.counts = { created, updated, unchanged, error };
2409
+ result.unacknowledged = result.ready - processed;
2410
+ result.status =
2411
+ job.status === "completed" && !error && !parsed.excluded.length
2412
+ ? "complete"
2413
+ : "partial";
2414
+ if (job.error)
2415
+ result.error = job.error.message;
2416
+ else
2417
+ delete result.error;
2418
+ }
2419
+ try {
2420
+ const savedReceipt = checkpoint.receipt ?? checkpoint.previousReceipt;
2421
+ if (savedReceipt)
2422
+ acknowledge(savedReceipt);
2423
+ if (!active || active.jobApiVersion !== checkpoint.jobApiVersion)
2424
+ await writeAtomicJson(privatePath, checkpoint);
2425
+ if (!checkpoint.fileId) {
2426
+ progress({ phase: "uploading", completed: 0 });
2427
+ checkpoint.fileId = await client.upload({
2428
+ requestId: checkpoint.uploadRequestId,
2429
+ contextVersion: binding.contextVersion,
2430
+ ...report,
2431
+ recovering: active !== undefined,
2432
+ });
2433
+ await writeAtomicJson(privatePath, checkpoint);
2434
+ }
2435
+ progress({ phase: "verifying", completed: 0 });
2436
+ const job = await client.job({
2437
+ requestId: checkpoint.jobRequestId,
2438
+ contextVersion: binding.contextVersion,
2439
+ fileId: checkpoint.fileId,
2440
+ format: report.format,
2441
+ source,
2442
+ repository: binding.repository,
2443
+ jobId: checkpoint.jobId,
2444
+ jobApiVersion: checkpoint.jobApiVersion,
2445
+ }, async (id) => {
2446
+ checkpoint.jobId = id;
2447
+ await writeAtomicJson(privatePath, checkpoint);
2448
+ });
2449
+ acknowledge(job);
2450
+ checkpoint.jobId = job.id;
2451
+ checkpoint.receipt = job;
2452
+ await writeAtomicJson(privatePath, checkpoint);
2453
+ return result;
2454
+ }
2455
+ catch (error) {
2456
+ result.status = "interrupted";
2457
+ result.error =
2458
+ error instanceof Error ? error.message : String(error);
2459
+ result.savedSubmission = privatePath;
2460
+ throw new ExternalPublicationError(result.error, result, error);
2461
+ }
2462
+ }),
2463
+ };
2464
+ }
2465
+ // The original selection and request bytes govern a retry even if discovery changes.
2466
+ const severities = saved
2467
+ ? saved.inputProjection?.repositorySeverities
2468
+ : repositorySeverities;
2469
+ const direct = repositorySelection(parsed, severities, input.sourcePositions);
2470
+ if (!direct.findings.length)
2471
+ throw new NoPublishableFindingsError(direct);
500
2472
  let requests = saved?.requests;
501
2473
  if (!requests) {
502
2474
  let preparedCount = 0;
503
2475
  progress({
504
2476
  phase: "preparing",
505
2477
  completed: 0,
506
- total: parsed.findings.length,
2478
+ total: direct.findings.length,
507
2479
  });
508
- const previousReports = await readInBatches(parsed.findings, async (finding) => {
2480
+ const previousReports = await readInBatches(direct.findings, async (finding) => {
509
2481
  const query = new URLSearchParams({
510
2482
  provider: source.provider,
511
2483
  source_key: source.source_key,
512
2484
  source_finding_id: finding.source_finding_id,
513
2485
  limit: "2",
514
2486
  });
515
- const summaries = validateSourceReports(await request(`${sourcePath(destination)}?${query}`));
2487
+ const summaries = validateSourceReports(await request(`${sourceReportsPath}?${query}`));
516
2488
  if (summaries.has_more || summaries.data.length > 1)
517
2489
  throw new CodexSecurityError("Cloud returned more than one report for a source identity.");
518
2490
  const previous = summaries.data[0];
@@ -526,14 +2498,14 @@ async function preparePublicationInput(input, options, dependencies) {
526
2498
  progress({
527
2499
  phase: "preparing",
528
2500
  completed: ++preparedCount,
529
- total: parsed.findings.length,
2501
+ total: direct.findings.length,
530
2502
  });
531
2503
  return previous;
532
2504
  });
533
2505
  requests = [];
534
2506
  let current;
535
2507
  let currentBytes = 0;
536
- for (const [index, finding] of parsed.findings.entries()) {
2508
+ for (const [index, finding] of direct.findings.entries()) {
537
2509
  const previous = previousReports[index];
538
2510
  const environmentId = previous?.environment_id ?? destination.import_environment_id;
539
2511
  if (environmentId == null)
@@ -575,19 +2547,40 @@ async function preparePublicationInput(input, options, dependencies) {
575
2547
  if (current)
576
2548
  requests.push(validateImportRequest(current));
577
2549
  }
2550
+ const bindSemgrepScope = !saved &&
2551
+ options.provider === "semgrep" &&
2552
+ report.findings.some((finding) => !isSemgrepPlatformEvidence(finding, "semgrep") &&
2553
+ (scopedSemgrepReport ||
2554
+ hasRetainedSemgrepScope(finding.evidence.source_data?.["finding"])));
578
2555
  const submission = {
579
2556
  accountId: credentials.account_id,
2557
+ ...(trivyReportUrls && { trivyRepositoryUrl: trivyTarget }),
580
2558
  requests,
581
2559
  receipts: saved?.receipts,
2560
+ legacyApproval: saved?.legacyApproval,
2561
+ wireKey: saved?.wireKey ?? (fileKey === key ? undefined : key),
582
2562
  inputProjection: saved?.inputProjection ?? {
2563
+ ...(direct !== parsed && { repositorySeverities: severities }),
2564
+ identityFormat: saved ? undefined : 2,
2565
+ ...(scopedSemgrepReport && { retainedHostedSelection: true }),
2566
+ ...(bindSemgrepScope && {
2567
+ semgrepPartialScopeDigest: suppliedSemgrepScopeDigest(),
2568
+ }),
583
2569
  inputDigest,
584
- savedEvidenceDigest: hash("sha256", canonicalJson(submissionFindings(requests))),
2570
+ savedEvidenceDigest: jsonDigest(submissionFindings(requests)),
585
2571
  },
586
2572
  };
2573
+ if (hosted &&
2574
+ (!saved || saved.approvedRepositoryUrl || matchesHostedRepository))
2575
+ submission.approvedRepositoryUrl =
2576
+ saved?.approvedRepositoryUrl ?? approvedUrl;
2577
+ if (saved?.approvedRepositoryPlatform ||
2578
+ (hostedSource && !hostedSource.github))
2579
+ submission.approvedRepositoryPlatform = "other";
587
2580
  if (inspectorProjectMap)
588
2581
  submission.inspectorProjectMapRequired = true;
589
2582
  const preview = {
590
- ...parsed,
2583
+ ...direct,
591
2584
  findings: submissionFindings(requests),
592
2585
  cloudApiUrl,
593
2586
  cloudUrl: cloudFindingsUrl(environment, destination.id),
@@ -598,7 +2591,7 @@ async function preparePublicationInput(input, options, dependencies) {
598
2591
  requests,
599
2592
  };
600
2593
  return {
601
- preview: structuredClone(preview),
2594
+ preview: cloneImportPreview(preview),
602
2595
  async publish() {
603
2596
  progress({ phase: "waiting", completed: 0 });
604
2597
  const result = (acknowledged, verified) => {
@@ -621,77 +2614,144 @@ async function preparePublicationInput(input, options, dependencies) {
621
2614
  return {
622
2615
  cloudApiUrl,
623
2616
  cloudUrl: preview.cloudUrl,
624
- status: parsed.excluded.length || counts.error ? "partial" : "complete",
2617
+ status: direct.excluded.length || counts.error ? "partial" : "complete",
625
2618
  read: parsed.read,
626
2619
  ...(parsed.report === undefined
627
2620
  ? {}
628
- : { report: structuredClone(parsed.report) }),
629
- ready: parsed.findings.length,
630
- excluded: parsed.excluded,
2621
+ : snapshotNormalizedFindings({ report: parsed.report })),
2622
+ ready: direct.findings.length,
2623
+ excluded: direct.excluded,
631
2624
  receipts: acknowledged,
632
2625
  counts,
633
2626
  failures,
634
- unacknowledged: parsed.findings.length -
2627
+ unacknowledged: direct.findings.length -
635
2628
  Object.values(counts).reduce((sum, count) => sum + count, 0),
636
2629
  verified,
637
2630
  };
638
2631
  };
2632
+ function interruptedPreview(message, cause, savedSubmission) {
2633
+ // The expected file is gone or another publisher owns the lock. Use only
2634
+ // the acknowledgements this caller saw when it prepared the request.
2635
+ const known = submission.receipts ?? [];
2636
+ if (known.length > submission.requests.length)
2637
+ throw new CodexSecurityError("Saved publication has more receipts than requests.");
2638
+ const acknowledged = known.map((receipt, index) => validatePublicationReceipt(receipt, submission.requests[index]));
2639
+ return new ExternalPublicationError(message, {
2640
+ ...result(acknowledged, 0),
2641
+ status: "interrupted",
2642
+ error: message,
2643
+ ...(savedSubmission === undefined ? {} : { savedSubmission }),
2644
+ }, cause);
2645
+ }
639
2646
  let acquired = false;
640
- return await withImportLock(state, key, dependencies.signal, async () => {
2647
+ return await underPublishLocks(async () => {
641
2648
  acquired = true;
2649
+ await assertNoAlternateCheckpoint();
642
2650
  // Re-read under the lock to keep checkpoints from another publisher.
643
- const pending = await readOptionalFile(pendingPath);
644
- if (pending === undefined && !saved)
645
- await rejectUnidentifiedLegacyCheckpoint();
646
- const current = pending === undefined
647
- ? submission
648
- : JSON.parse(pending);
2651
+ const selected = await selectedCheckpoint(saved ? fileKey : undefined).catch((error) => {
2652
+ if (!submission.receipts?.length)
2653
+ throw error;
2654
+ throw interruptedPreview(errorMessage(error), error);
2655
+ });
2656
+ let pending = selected?.content;
2657
+ const current = pending ?? submission;
649
2658
  const adoptProjectMap = submission.inspectorProjectMapRequired &&
650
2659
  current.inspectorProjectMapRequired === undefined;
651
2660
  if (adoptProjectMap)
652
2661
  current.inspectorProjectMapRequired = true;
653
- if (!sameSubmission(current, submission))
654
- throw new CodexSecurityError("Another publication prepared this input. Run the command again to review and resume that saved request.");
655
- const receipts = (current.receipts ?? []).map(validateImportReceipt);
656
- if (receipts.length > requests.length)
2662
+ if (current.approvedRepositoryUrl)
2663
+ requireApprovedSource(current.approvedRepositoryUrl, current.approvedRepositoryPlatform);
2664
+ if (!selected ||
2665
+ selected.fileKey !== fileKey ||
2666
+ selected.historical ||
2667
+ !sameSubmission(current, submission)) {
2668
+ const message = selected
2669
+ ? "Another publication prepared this input. Run the command again to review and resume that saved request."
2670
+ : "The saved publication changed after preview. Review its result before publishing again.";
2671
+ if (saved) {
2672
+ const completed = await readCompletedPublication(requests, fileKey);
2673
+ if (completed)
2674
+ return result(completed.receipts, completed.verified);
2675
+ throw interruptedPreview(message);
2676
+ }
2677
+ if (selected)
2678
+ throw new CodexSecurityError(message);
2679
+ if ((await readOptionalFile(privatePath)) !== undefined)
2680
+ throw new CodexSecurityError("A private import already owns this selection. Review its saved job before continuing.");
2681
+ if ((await readOptionalFile(pendingPath)) !== undefined)
2682
+ throw occupiedCheckpoint();
2683
+ }
2684
+ // A preview can pin the catalog URL. Tighten its SCM comparison only
2685
+ // once the caller confirms publication, including when resuming a request.
2686
+ const adoptStrictRepository = submission.approvedRepositoryPlatform === "other" &&
2687
+ !current.approvedRepositoryPlatform;
2688
+ if (adoptStrictRepository)
2689
+ current.approvedRepositoryPlatform = "other";
2690
+ const cached = (current.receipts ?? []).map(validateImportReceipt);
2691
+ if (cached.length > requests.length)
657
2692
  throw new CodexSecurityError("Saved publication has more receipts than requests.");
658
2693
  const acknowledged = [];
659
2694
  let verified = 0;
660
- let persisted = pending !== undefined;
661
2695
  try {
662
- if (pending === undefined || adoptProjectMap) {
2696
+ if (selected?.checkedCanonical)
2697
+ current.canonicalChecked = true;
2698
+ if (pending === undefined) {
2699
+ await withSourceAdmission(async () => {
2700
+ if (hosted) {
2701
+ for (const otherKey of new Set([
2702
+ ...projectionKeys,
2703
+ ...legacyKeys,
2704
+ ])) {
2705
+ if (otherKey !== key &&
2706
+ otherKey !== fileKey &&
2707
+ (await readOptionalFile(pendingFile(otherKey))) !==
2708
+ undefined)
2709
+ throw new CodexSecurityError("Another publication prepared this input. Run the command again to review and resume that saved request.");
2710
+ }
2711
+ if (await selectedCheckpoint())
2712
+ throw new CodexSecurityError("Another publication prepared this input. Run the command again to review and resume that saved request.");
2713
+ }
2714
+ await writeAtomicJson(pendingPath, current);
2715
+ });
2716
+ pending = current;
2717
+ }
2718
+ else if (adoptProjectMap || adoptStrictRepository) {
663
2719
  await writeAtomicJson(pendingPath, current);
664
- persisted = true;
665
2720
  }
2721
+ else if (selected?.checkedCanonical) {
2722
+ // The existing approval and receipts are usable if caching the scan fails.
2723
+ await writeAtomicJson(pendingPath, current).catch(() => undefined);
2724
+ }
2725
+ await rememberFile();
666
2726
  progress({
667
2727
  phase: "uploading",
668
2728
  completed: 0,
669
- total: parsed.findings.length,
2729
+ total: direct.findings.length,
670
2730
  });
671
2731
  let acknowledgedCount = 0;
672
2732
  for (const [batchIndex, batch] of requests.entries()) {
673
- const receipt = validatePublicationReceipt(receipts[batchIndex] ??
674
- (await request("/finding_imports", batch)), batch);
2733
+ const receipt = validatePublicationReceipt(cached[batchIndex] ??
2734
+ (await request(`${importPrefix}/finding_imports`, batch)), batch);
675
2735
  acknowledged.push(receipt);
676
2736
  acknowledgedCount += receipt.item_count;
677
2737
  progress({
678
2738
  phase: "uploading",
679
2739
  completed: acknowledgedCount,
680
- total: parsed.findings.length,
2740
+ total: direct.findings.length,
681
2741
  });
682
- if (batchIndex === receipts.length) {
683
- receipts.push(receipt);
2742
+ if (batchIndex >= cached.length) {
684
2743
  // Acknowledged batches must not consume another POST quota on retry.
685
2744
  // Readback can still resume after this checkpoint without replaying writes.
686
2745
  await writeAtomicJson(pendingPath, {
687
2746
  ...submission,
688
- receipts,
2747
+ canonicalChecked: current.canonicalChecked,
2748
+ receipts: acknowledged,
689
2749
  });
690
2750
  }
691
2751
  }
692
2752
  // Verify readable source records without mistaking a newer concurrent
693
2753
  // observation for failure of the original, immutable import receipt.
694
- const readbacks = receipts.flatMap((receipt, batchIndex) => {
2754
+ const readbacks = acknowledged.flatMap((receipt, batchIndex) => {
695
2755
  const batch = requests[batchIndex];
696
2756
  const batchItems = new Map(batch.items.map((item) => [item.client_id, item]));
697
2757
  return receipt.results
@@ -708,19 +2768,18 @@ async function preparePublicationInput(input, options, dependencies) {
708
2768
  total: readbacks.length,
709
2769
  });
710
2770
  await readInBatches(readbacks, async ({ item, expected, batch }) => {
711
- const report = validateSourceReport(await request(`${sourcePath(destination)}/${encodeURIComponent(item.source_report_id)}`));
2771
+ const report = validateSourceReport(await request(`${sourceReportsPath}/${encodeURIComponent(item.source_report_id)}`));
712
2772
  if (report.id !== item.source_report_id ||
713
2773
  report.canonical_finding_id !== item.canonical_finding_id ||
714
2774
  report.version < item.version ||
715
2775
  (report.version === item.version &&
716
2776
  (report.observation_id !== item.observation_id ||
717
- canonicalJson(report.evidence) !==
718
- canonicalJson(expected.evidence))) ||
2777
+ !matchesRepositoryEvidence(expected.evidence, report.evidence))) ||
719
2778
  report.source_finding_id !== expected.source_finding_id ||
720
2779
  report.repo_id !== destination.id ||
721
2780
  report.repo_connector_id !== destination.repo_connector_id ||
722
2781
  report.environment_id !== batch.repository.environment_id ||
723
- canonicalJson(report.source) !== canonicalJson(source))
2782
+ wireJson(report.source) !== wireJson(source))
724
2783
  throw new CodexSecurityError("Cloud readback did not match the saved finding identity.");
725
2784
  progress({
726
2785
  phase: "verifying",
@@ -733,40 +2792,29 @@ async function preparePublicationInput(input, options, dependencies) {
733
2792
  ...completed,
734
2793
  report: reportForJson(completed.report),
735
2794
  });
736
- await rm(pendingPath, { force: true });
2795
+ await removePending();
737
2796
  return completed;
738
2797
  }
739
2798
  catch (error) {
740
2799
  if (error instanceof CloudImportError && error.status === 409) {
741
- await rm(pendingPath, { force: true });
742
- persisted = false;
2800
+ await removePending();
2801
+ pending = undefined;
743
2802
  }
744
- const message = `${error instanceof Error ? error.message : String(error)} ${error instanceof CloudImportError && error.status === 409 ? "The old submission was retired. Rediscover and approve a fresh publication." : persisted ? `Repeat the same command to resume the saved request. Saved submission: ${pendingPath}` : "No resumable request was saved. Correct the error and review the input before retrying."}`;
2803
+ const message = `${error instanceof Error ? error.message : String(error)} ${error instanceof CloudImportError && error.status === 409 ? "The old submission was retired. Rediscover and approve a fresh publication." : pending ? `Repeat the same command to resume the saved request. Saved submission: ${pendingPath}` : "No resumable request was saved. Correct the error and review the input before retrying."}`;
745
2804
  throw new ExternalPublicationError(message, {
746
2805
  ...result(acknowledged, verified),
747
2806
  status: "interrupted",
748
2807
  error: message,
749
- ...(persisted ? { savedSubmission: pendingPath } : {}),
2808
+ ...(pending ? { savedSubmission: pendingPath } : {}),
750
2809
  }, error);
751
2810
  }
752
2811
  }).catch((error) => {
753
2812
  if (acquired || !dependencies.signal?.aborted)
754
2813
  throw error;
755
- // The active publisher owns the checkpoint. Report only acknowledgements
756
- // already observed during preparation, without rewriting its files.
757
- const known = submission.receipts ?? [];
758
- if (known.length > requests.length)
759
- throw new CodexSecurityError("Saved publication has more receipts than requests.");
760
- const acknowledged = known.map((receipt, index) => validatePublicationReceipt(receipt, requests[index]));
761
2814
  const message = saved
762
2815
  ? `Publication canceled while waiting. Repeat the same command to resume the saved request. Saved submission: ${pendingPath}`
763
2816
  : "Publication canceled while waiting. No resumable request was saved by this invocation.";
764
- throw new ExternalPublicationError(message, {
765
- ...result(acknowledged, 0),
766
- status: "interrupted",
767
- error: message,
768
- ...(saved ? { savedSubmission: pendingPath } : {}),
769
- }, error);
2817
+ throw interruptedPreview(message, error, saved ? pendingPath : undefined);
770
2818
  });
771
2819
  },
772
2820
  };