@openai/codex-security 0.8.2 → 0.8.3

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 (375) hide show
  1. package/README.md +43 -61
  2. package/_bundled_plugin/.app.json +4 -0
  3. package/_bundled_plugin/.mcp.json +1 -0
  4. package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
  5. package/_bundled_plugin/mcp/helpers.mjs.br.part-001 +0 -0
  6. package/_bundled_plugin/mcp/native/darwin-arm64/unix.node +0 -0
  7. package/_bundled_plugin/mcp/native/darwin-x64/unix.node +0 -0
  8. package/_bundled_plugin/mcp/native/linux-arm64-gnu/unix.node +0 -0
  9. package/_bundled_plugin/mcp/native/linux-arm64-musl/unix.node +0 -0
  10. package/_bundled_plugin/mcp/native/linux-x64-gnu/unix.node +0 -0
  11. package/_bundled_plugin/mcp/native/linux-x64-musl/unix.node +0 -0
  12. package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
  13. package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
  14. package/_bundled_plugin/mcp/permission-profile-preflight.mjs +46 -12
  15. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  16. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  17. package/_bundled_plugin/mcp/server.mjs.br.part-002 +0 -0
  18. package/_bundled_plugin/references/artifact-storage.md +1 -1
  19. package/_bundled_plugin/references/core-scan.md +14 -5
  20. package/_bundled_plugin/references/default-severity-rubric.md +28 -0
  21. package/_bundled_plugin/references/finding-detail-fields.md +1 -0
  22. package/_bundled_plugin/references/threat-model.md +1 -0
  23. package/_bundled_plugin/scripts/deep_scan_workbench.py +23 -112
  24. package/_bundled_plugin/scripts/finalize_scan_contract.py +167 -153
  25. package/_bundled_plugin/scripts/finding_preview.py +5 -29
  26. package/_bundled_plugin/scripts/posix_scan_local_files.py +187 -0
  27. package/_bundled_plugin/scripts/report_projection.py +87 -136
  28. package/_bundled_plugin/scripts/validate_scan_contract.py +2 -6
  29. package/_bundled_plugin/scripts/windows_scan_local_files.py +212 -55
  30. package/_bundled_plugin/scripts/workbench/retained_coverage.py +8 -24
  31. package/_bundled_plugin/scripts/workbench/saved_coverage.py +143 -231
  32. package/_bundled_plugin/scripts/workbench_cli.py +102 -13
  33. package/_bundled_plugin/scripts/workbench_constants.py +9 -0
  34. package/_bundled_plugin/scripts/workbench_db.py +186 -360
  35. package/_bundled_plugin/scripts/workbench_finding_workflows.py +86 -4
  36. package/_bundled_plugin/scripts/workbench_native_indexes.py +17 -21
  37. package/_bundled_plugin/scripts/workbench_remediation.py +495 -11
  38. package/_bundled_plugin/scripts/workbench_scan_history.py +22 -25
  39. package/_bundled_plugin/scripts/workbench_scan_start.py +1 -1
  40. package/_bundled_plugin/scripts/workbench_scan_usage.py +25 -52
  41. package/_bundled_plugin/scripts/workbench_schema.py +1 -1
  42. package/_bundled_plugin/scripts/workbench_setup.py +239 -0
  43. package/_bundled_plugin/scripts/workbench_source_scopes.py +15 -23
  44. package/_bundled_plugin/scripts/workbench_target.py +213 -20
  45. package/_bundled_plugin/scripts/workbench_validation.py +1 -9
  46. package/_bundled_plugin/shared/workbench-history-repairs.json +7 -0
  47. package/_bundled_plugin/shared/workbench-migrations.json +19 -0
  48. package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +1 -1
  49. package/_bundled_plugin/skills/attack-path-analysis/references/severity-policy.md +4 -68
  50. package/_bundled_plugin/skills/check-skills/SKILL.md +43 -0
  51. package/_bundled_plugin/skills/check-skills/agents/openai.yaml +4 -0
  52. package/_bundled_plugin/skills/cloud-findings/SKILL.md +2 -0
  53. package/_bundled_plugin/skills/fix-finding/SKILL.md +18 -1
  54. package/_bundled_plugin/skills/publish-findings/SKILL.md +64 -35
  55. package/_bundled_plugin/skills/publish-findings/references/wiz-import.md +2 -21
  56. package/_bundled_plugin/skills/threat-model-visualize/scripts/build_explorer.py +14 -8
  57. package/_bundled_plugin/skills/track-findings/SKILL.md +5 -3
  58. package/_bundled_plugin/skills/triage-finding/references/github-rest-intake.md +22 -68
  59. package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +6 -4
  60. package/dist/api.d.ts +6 -1
  61. package/dist/api.d.ts.map +1 -1
  62. package/dist/api.js +411 -160
  63. package/dist/api.js.map +1 -1
  64. package/dist/auth.js +6 -1
  65. package/dist/auth.js.map +1 -1
  66. package/dist/blackduck-findings.d.ts +8 -0
  67. package/dist/blackduck-findings.d.ts.map +1 -0
  68. package/dist/blackduck-findings.js +305 -0
  69. package/dist/blackduck-findings.js.map +1 -0
  70. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  71. package/dist/bulk-scan-discovery.js +51 -36
  72. package/dist/bulk-scan-discovery.js.map +1 -1
  73. package/dist/classify-severity-records.d.ts +5 -0
  74. package/dist/classify-severity-records.d.ts.map +1 -1
  75. package/dist/classify-severity-records.js +6 -8
  76. package/dist/classify-severity-records.js.map +1 -1
  77. package/dist/classify-severity.d.ts +4 -1
  78. package/dist/classify-severity.d.ts.map +1 -1
  79. package/dist/classify-severity.js +28 -9
  80. package/dist/classify-severity.js.map +1 -1
  81. package/dist/cli.d.ts +3 -0
  82. package/dist/cli.d.ts.map +1 -1
  83. package/dist/cli.js +608 -258
  84. package/dist/cli.js.map +1 -1
  85. package/dist/codex-prompt.d.ts +2 -0
  86. package/dist/codex-prompt.d.ts.map +1 -1
  87. package/dist/codex-prompt.js +6 -0
  88. package/dist/codex-prompt.js.map +1 -1
  89. package/dist/component-scan.d.ts.map +1 -1
  90. package/dist/component-scan.js +6 -5
  91. package/dist/component-scan.js.map +1 -1
  92. package/dist/config.d.ts +2 -1
  93. package/dist/config.d.ts.map +1 -1
  94. package/dist/config.js +162 -40
  95. package/dist/config.js.map +1 -1
  96. package/dist/continuum-findings.d.ts +7 -0
  97. package/dist/continuum-findings.d.ts.map +1 -0
  98. package/dist/continuum-findings.js +157 -0
  99. package/dist/continuum-findings.js.map +1 -0
  100. package/dist/contract-values.d.ts +28 -0
  101. package/dist/contract-values.d.ts.map +1 -0
  102. package/dist/contract-values.js +75 -0
  103. package/dist/contract-values.js.map +1 -0
  104. package/dist/contract.d.ts +2 -20
  105. package/dist/contract.d.ts.map +1 -1
  106. package/dist/contract.js +58 -669
  107. package/dist/contract.js.map +1 -1
  108. package/dist/cost-model.d.ts.map +1 -1
  109. package/dist/cost-model.js +1 -4
  110. package/dist/cost-model.js.map +1 -1
  111. package/dist/cost.d.ts +19 -1
  112. package/dist/cost.d.ts.map +1 -1
  113. package/dist/cost.js +2120 -39
  114. package/dist/cost.js.map +1 -1
  115. package/dist/custom-validation-prompt.d.ts +1 -1
  116. package/dist/custom-validation-prompt.d.ts.map +1 -1
  117. package/dist/custom-validation-prompt.js +5 -0
  118. package/dist/custom-validation-prompt.js.map +1 -1
  119. package/dist/custom-validation.d.ts +2 -0
  120. package/dist/custom-validation.d.ts.map +1 -1
  121. package/dist/custom-validation.js +4 -0
  122. package/dist/custom-validation.js.map +1 -1
  123. package/dist/deduplication/deduplication-prompts.d.ts +4 -4
  124. package/dist/deduplication/deduplication-prompts.d.ts.map +1 -1
  125. package/dist/deduplication/deduplication-prompts.js +2 -2
  126. package/dist/deduplication/deduplication-prompts.js.map +1 -1
  127. package/dist/deduplication/deduplication-reviewer.d.ts +14 -11
  128. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
  129. package/dist/deduplication/deduplication-reviewer.js +11 -4
  130. package/dist/deduplication/deduplication-reviewer.js.map +1 -1
  131. package/dist/deduplication/deduplication.js.map +1 -1
  132. package/dist/deduplication/finding-schema.d.ts +19 -1
  133. package/dist/deduplication/finding-schema.d.ts.map +1 -1
  134. package/dist/deduplication/finding-schema.js +25 -0
  135. package/dist/deduplication/finding-schema.js.map +1 -1
  136. package/dist/deduplication/local.d.ts.map +1 -1
  137. package/dist/deduplication/local.js +3 -8
  138. package/dist/deduplication/local.js.map +1 -1
  139. package/dist/deduplication/records.d.ts +8 -2
  140. package/dist/deduplication/records.d.ts.map +1 -1
  141. package/dist/deduplication/records.js +36 -8
  142. package/dist/deduplication/records.js.map +1 -1
  143. package/dist/deep-scan.d.ts +11 -0
  144. package/dist/deep-scan.d.ts.map +1 -1
  145. package/dist/deep-scan.js +235 -4
  146. package/dist/deep-scan.js.map +1 -1
  147. package/dist/default-severity-rubric.d.ts +1 -2
  148. package/dist/default-severity-rubric.d.ts.map +1 -1
  149. package/dist/default-severity-rubric.js +2 -66
  150. package/dist/default-severity-rubric.js.map +1 -1
  151. package/dist/endor-findings.d.ts.map +1 -1
  152. package/dist/endor-findings.js +7 -37
  153. package/dist/endor-findings.js.map +1 -1
  154. package/dist/errors.js +1 -1
  155. package/dist/errors.js.map +1 -1
  156. package/dist/external-findings-import.d.ts.map +1 -1
  157. package/dist/external-findings-import.js +5 -11
  158. package/dist/external-findings-import.js.map +1 -1
  159. package/dist/external-findings-local.d.ts +9 -0
  160. package/dist/external-findings-local.d.ts.map +1 -1
  161. package/dist/external-findings-local.js +325 -179
  162. package/dist/external-findings-local.js.map +1 -1
  163. package/dist/external-findings-publish.d.ts +12 -0
  164. package/dist/external-findings-publish.d.ts.map +1 -1
  165. package/dist/external-findings-publish.js +1202 -214
  166. package/dist/external-findings-publish.js.map +1 -1
  167. package/dist/external-findings-read.d.ts +8 -0
  168. package/dist/external-findings-read.d.ts.map +1 -1
  169. package/dist/external-findings-read.js +65 -54
  170. package/dist/external-findings-read.js.map +1 -1
  171. package/dist/external-findings.d.ts +2 -0
  172. package/dist/external-findings.d.ts.map +1 -1
  173. package/dist/external-findings.js +111 -0
  174. package/dist/external-findings.js.map +1 -1
  175. package/dist/external-import-contract.d.ts +29 -1
  176. package/dist/external-import-contract.d.ts.map +1 -1
  177. package/dist/external-import-contract.js +314 -44
  178. package/dist/external-import-contract.js.map +1 -1
  179. package/dist/external-import-models.d.ts +1 -1
  180. package/dist/external-import-models.d.ts.map +1 -1
  181. package/dist/external-import-models.js.map +1 -1
  182. package/dist/finding-normalization.d.ts +1 -1
  183. package/dist/finding-normalization.d.ts.map +1 -1
  184. package/dist/finding-normalization.js +46 -11
  185. package/dist/finding-normalization.js.map +1 -1
  186. package/dist/finding-retrieval.d.ts +3 -3
  187. package/dist/finding-retrieval.d.ts.map +1 -1
  188. package/dist/finding-retrieval.js.map +1 -1
  189. package/dist/finding-workflow.d.ts +6 -1
  190. package/dist/finding-workflow.d.ts.map +1 -1
  191. package/dist/finding-workflow.js +2 -2
  192. package/dist/finding-workflow.js.map +1 -1
  193. package/dist/git-remote.d.ts +4 -0
  194. package/dist/git-remote.d.ts.map +1 -1
  195. package/dist/git-remote.js +20 -0
  196. package/dist/git-remote.js.map +1 -1
  197. package/dist/github-advisories.d.ts +3 -12
  198. package/dist/github-advisories.d.ts.map +1 -1
  199. package/dist/github-advisories.js +9 -29
  200. package/dist/github-advisories.js.map +1 -1
  201. package/dist/github-dependabot-findings.d.ts.map +1 -1
  202. package/dist/github-dependabot-findings.js +7 -16
  203. package/dist/github-dependabot-findings.js.map +1 -1
  204. package/dist/github-findings.d.ts.map +1 -1
  205. package/dist/github-findings.js +344 -56
  206. package/dist/github-findings.js.map +1 -1
  207. package/dist/github.d.ts +17 -7
  208. package/dist/github.d.ts.map +1 -1
  209. package/dist/github.js +43 -30
  210. package/dist/github.js.map +1 -1
  211. package/dist/host-protocol.d.ts +2 -2
  212. package/dist/host-protocol.d.ts.map +1 -1
  213. package/dist/host-protocol.js.map +1 -1
  214. package/dist/host-records-protocol.d.ts +10 -11
  215. package/dist/host-records-protocol.d.ts.map +1 -1
  216. package/dist/host-records-protocol.js +69 -9
  217. package/dist/host-records-protocol.js.map +1 -1
  218. package/dist/hosted-scan.d.ts +15 -0
  219. package/dist/hosted-scan.d.ts.map +1 -1
  220. package/dist/hosted-scan.js +55 -33
  221. package/dist/hosted-scan.js.map +1 -1
  222. package/dist/index.d.ts +7 -0
  223. package/dist/index.d.ts.map +1 -1
  224. package/dist/index.js +6 -0
  225. package/dist/index.js.map +1 -1
  226. package/dist/inspector-findings.d.ts.map +1 -1
  227. package/dist/inspector-findings.js +13 -18
  228. package/dist/inspector-findings.js.map +1 -1
  229. package/dist/knowledge-base.js +18 -5
  230. package/dist/knowledge-base.js.map +1 -1
  231. package/dist/local-git-metadata.d.ts +5 -0
  232. package/dist/local-git-metadata.d.ts.map +1 -0
  233. package/dist/local-git-metadata.js +131 -0
  234. package/dist/local-git-metadata.js.map +1 -0
  235. package/dist/multiscan.d.ts.map +1 -1
  236. package/dist/multiscan.js +22 -12
  237. package/dist/multiscan.js.map +1 -1
  238. package/dist/opengrep-findings.d.ts.map +1 -1
  239. package/dist/opengrep-findings.js +34 -20
  240. package/dist/opengrep-findings.js.map +1 -1
  241. package/dist/patch-tui.d.ts.map +1 -1
  242. package/dist/patch-tui.js +90 -35
  243. package/dist/patch-tui.js.map +1 -1
  244. package/dist/private-finding-import.d.ts +246 -0
  245. package/dist/private-finding-import.d.ts.map +1 -0
  246. package/dist/private-finding-import.js +387 -0
  247. package/dist/private-finding-import.js.map +1 -0
  248. package/dist/project-config.d.ts.map +1 -1
  249. package/dist/project-config.js +2 -2
  250. package/dist/project-config.js.map +1 -1
  251. package/dist/provider-profile.d.ts +10 -2
  252. package/dist/provider-profile.d.ts.map +1 -1
  253. package/dist/provider-profile.js +72 -10
  254. package/dist/provider-profile.js.map +1 -1
  255. package/dist/publication.js +7 -1
  256. package/dist/publication.js.map +1 -1
  257. package/dist/reader-selection.d.ts +13 -0
  258. package/dist/reader-selection.d.ts.map +1 -0
  259. package/dist/reader-selection.js +81 -0
  260. package/dist/reader-selection.js.map +1 -0
  261. package/dist/record.d.ts +6 -1
  262. package/dist/record.d.ts.map +1 -1
  263. package/dist/record.js +53 -2
  264. package/dist/record.js.map +1 -1
  265. package/dist/remediation.d.ts +5 -0
  266. package/dist/remediation.d.ts.map +1 -1
  267. package/dist/remediation.js +30 -0
  268. package/dist/remediation.js.map +1 -1
  269. package/dist/result.d.ts +2 -0
  270. package/dist/result.d.ts.map +1 -1
  271. package/dist/result.js +3 -1
  272. package/dist/result.js.map +1 -1
  273. package/dist/runtime.d.ts +2 -1
  274. package/dist/runtime.d.ts.map +1 -1
  275. package/dist/runtime.js +95 -52
  276. package/dist/runtime.js.map +1 -1
  277. package/dist/scan-activity.js +40 -17
  278. package/dist/scan-activity.js.map +1 -1
  279. package/dist/scan-comparison.d.ts +1 -1
  280. package/dist/scan-comparison.d.ts.map +1 -1
  281. package/dist/scan-comparison.js +11 -13
  282. package/dist/scan-comparison.js.map +1 -1
  283. package/dist/scan-dashboard.d.ts +1 -0
  284. package/dist/scan-dashboard.d.ts.map +1 -1
  285. package/dist/scan-dashboard.js +16 -37
  286. package/dist/scan-dashboard.js.map +1 -1
  287. package/dist/scan-executor.d.ts +2 -0
  288. package/dist/scan-executor.d.ts.map +1 -1
  289. package/dist/scan-executor.js.map +1 -1
  290. package/dist/scan-history-renderer.d.ts.map +1 -1
  291. package/dist/scan-history-renderer.js +22 -9
  292. package/dist/scan-history-renderer.js.map +1 -1
  293. package/dist/scan-logs.d.ts.map +1 -1
  294. package/dist/scan-logs.js +5 -5
  295. package/dist/scan-logs.js.map +1 -1
  296. package/dist/scanner-import.d.ts +46 -6
  297. package/dist/scanner-import.d.ts.map +1 -1
  298. package/dist/scanner-import.js +208 -42
  299. package/dist/scanner-import.js.map +1 -1
  300. package/dist/scanner-repository-url.d.ts +14 -0
  301. package/dist/scanner-repository-url.d.ts.map +1 -0
  302. package/dist/scanner-repository-url.js +67 -0
  303. package/dist/scanner-repository-url.js.map +1 -0
  304. package/dist/semgrep-findings.d.ts.map +1 -1
  305. package/dist/semgrep-findings.js +20 -48
  306. package/dist/semgrep-findings.js.map +1 -1
  307. package/dist/semgrep-platform-findings.d.ts +12 -0
  308. package/dist/semgrep-platform-findings.d.ts.map +1 -0
  309. package/dist/semgrep-platform-findings.js +198 -0
  310. package/dist/semgrep-platform-findings.js.map +1 -0
  311. package/dist/severity-rubric.d.ts +20 -0
  312. package/dist/severity-rubric.d.ts.map +1 -0
  313. package/dist/severity-rubric.js +73 -0
  314. package/dist/severity-rubric.js.map +1 -0
  315. package/dist/severity-store.js +15 -5
  316. package/dist/severity-store.js.map +1 -1
  317. package/dist/snyk-findings.d.ts.map +1 -1
  318. package/dist/snyk-findings.js +21 -20
  319. package/dist/snyk-findings.js.map +1 -1
  320. package/dist/sonar-collector.d.ts +16 -0
  321. package/dist/sonar-collector.d.ts.map +1 -0
  322. package/dist/sonar-collector.js +383 -0
  323. package/dist/sonar-collector.js.map +1 -0
  324. package/dist/sonar-findings.d.ts +49 -0
  325. package/dist/sonar-findings.d.ts.map +1 -0
  326. package/dist/sonar-findings.js +288 -0
  327. package/dist/sonar-findings.js.map +1 -0
  328. package/dist/sonar-import.d.ts +111 -0
  329. package/dist/sonar-import.d.ts.map +1 -0
  330. package/dist/sonar-import.js +279 -0
  331. package/dist/sonar-import.js.map +1 -0
  332. package/dist/suggest-owners.d.ts.map +1 -1
  333. package/dist/suggest-owners.js +2 -1
  334. package/dist/suggest-owners.js.map +1 -1
  335. package/dist/targets.d.ts +3 -0
  336. package/dist/targets.d.ts.map +1 -1
  337. package/dist/targets.js +18 -12
  338. package/dist/targets.js.map +1 -1
  339. package/dist/terminal-columns.d.ts +4 -0
  340. package/dist/terminal-columns.d.ts.map +1 -0
  341. package/dist/terminal-columns.js +19 -0
  342. package/dist/terminal-columns.js.map +1 -0
  343. package/dist/validation-records.d.ts +0 -3
  344. package/dist/validation-records.d.ts.map +1 -1
  345. package/dist/validation-records.js +0 -16
  346. package/dist/validation-records.js.map +1 -1
  347. package/dist/veracode-findings.d.ts +7 -0
  348. package/dist/veracode-findings.d.ts.map +1 -0
  349. package/dist/veracode-findings.js +371 -0
  350. package/dist/veracode-findings.js.map +1 -0
  351. package/dist/windows-path.d.ts.map +1 -1
  352. package/dist/windows-path.js +1 -0
  353. package/dist/windows-path.js.map +1 -1
  354. package/dist/wiz-csv.d.ts +2 -2
  355. package/dist/wiz-csv.d.ts.map +1 -1
  356. package/dist/wiz-csv.js +1 -3
  357. package/dist/wiz-csv.js.map +1 -1
  358. package/dist/wiz-findings.d.ts +10 -2
  359. package/dist/wiz-findings.d.ts.map +1 -1
  360. package/dist/wiz-findings.js +88 -49
  361. package/dist/wiz-findings.js.map +1 -1
  362. package/docs/classify-severity-records.md +1 -1
  363. package/docs/cli.md +310 -477
  364. package/docs/dedupe-records.md +10 -0
  365. package/docs/findings-service.md +21 -8
  366. package/docs/hosted-scans.md +16 -1
  367. package/package.json +8 -9
  368. package/schemas/external-findings.schema.json +12 -1
  369. package/schemas/hosted-scan-v2.fixture.json +16 -1
  370. package/_bundled_plugin/scripts/validate_tracking_source.py +0 -82
  371. package/_bundled_plugin/scripts/workbench_local_dedupe.py +0 -136
  372. package/dist/deduplication/records-protocol.d.ts +0 -4
  373. package/dist/deduplication/records-protocol.d.ts.map +0 -1
  374. package/dist/deduplication/records-protocol.js +0 -49
  375. package/dist/deduplication/records-protocol.js.map +0 -1
@@ -4,20 +4,29 @@ import { lstat, mkdir, open, readFile, readdir, rename, rm, } from "node:fs/prom
4
4
  import { createRequire } from "node:module";
5
5
  import { join } from "node:path";
6
6
  import { setTimeout as delay } from "node:timers/promises";
7
+ import { isBoxedPrimitive, isNativeError, isProxy } from "node:util/types";
8
+ import { privateImportClient, selectPrivateReport, readPrivateCheckpoint, unsuccessfulJob, } from "./private-finding-import.js";
7
9
  import { readCloudCredentials } from "./cloud-publish.js";
8
10
  import { cloudBaseUrl, cloudFindingsUrl, DEFAULT_CLOUD_BASE_URL, } from "./cloud-endpoint.js";
9
- import { CodexSecurityError } from "./errors.js";
11
+ import { CodexSecurityError, errorMessage } from "./errors.js";
10
12
  import { requireExternalImports } from "./external-imports-feature.js";
11
13
  import { codexSecurityStateDirectory, createWindowsOutputDirectory, validatePreparedOutputDir, } from "./runtime.js";
12
14
  import { EXTERNAL_FINDINGS_PROVIDERS, readExternalFindings, } from "./external-findings-read.js";
13
- import { canonicalJson } from "./record.js";
15
+ import { canSnapshotWithoutGetters, canonicalJson, isRecord, } from "./record.js";
16
+ import { notify } from "./value.js";
14
17
  import { wizCsvRepositoryUrl } from "./wiz-csv.js";
18
+ import { wizFindingRepository } from "./wiz-findings.js";
19
+ import { previousReaderSelection } from "./reader-selection.js";
15
20
  import { dependabotRepository } from "./github-dependabot-findings.js";
16
21
  import { snykRepositoryUrls } from "./snyk-findings.js";
17
22
  import { endorRepositoryUrls } from "./endor-findings.js";
23
+ import { blackDuckReportRepositories, projectedBlackDuckRepositoryUrl, } from "./blackduck-findings.js";
24
+ import { continuumReportRepositories } from "./continuum-findings.js";
25
+ import { veracodeReportRepositories } from "./veracode-findings.js";
18
26
  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";
27
+ import { sonarRepositoryUrls } from "./sonar-findings.js";
28
+ import { snapshotImportOptions, cloneImportPreview, nativeErrorStackGetter, snapshotNormalizedFindings, } from "./external-findings.js";
29
+ import { cloudRepositoryUrlKey, CloudEvidenceDepthError, validateImportRequest, validateExternalEvidence, validatePreviousCloudEvidence, validateExternalReport, validateImportReceipt, validateRepositories, validateSourceReports, validateSourceReport, } from "./external-import-contract.js";
21
30
  const MAX_REQUEST_BYTES = 5 * 1024 * 1024;
22
31
  /** Preserve accepted work in CLI JSON output when publication cannot finish. */
23
32
  export class ExternalPublicationError extends CodexSecurityError {
@@ -41,16 +50,50 @@ async function readInBatches(items, read) {
41
50
  }
42
51
  return output;
43
52
  }
53
+ // JSON.stringify serializes boxed primitives after the replacer. Let it
54
+ // produce their actual wire values, including false and strings.
55
+ function wireJson(value) {
56
+ return canonicalJson(value, undefined, isBoxedPrimitive);
57
+ }
58
+ function jsonDigest(value, serialize = wireJson) {
59
+ return hash("sha256", serialize(value));
60
+ }
44
61
  function sameSubmission(left, right) {
45
62
  return (left.accountId === right.accountId &&
46
63
  left.inspectorProjectMapRequired === right.inspectorProjectMapRequired &&
47
- canonicalJson(left.requests) === canonicalJson(right.requests));
64
+ wireJson(left.requests) === wireJson(right.requests));
65
+ }
66
+ function wireFinding({ source_finding_id, evidence, }) {
67
+ return { source_finding_id, evidence };
48
68
  }
49
69
  function submissionFindings(requests) {
50
- return requests.flatMap((batch) => batch.items.map(({ source_finding_id, evidence }) => ({
51
- source_finding_id,
52
- evidence,
53
- })));
70
+ return requests.flatMap((batch) => batch.items.map(wireFinding));
71
+ }
72
+ function sameLegacyJson(saved, current, equivalent) {
73
+ if (saved === current || equivalent(saved, current))
74
+ return true;
75
+ if (Array.isArray(saved) && Array.isArray(current))
76
+ return (saved.length === current.length &&
77
+ saved.every((child, i) => sameLegacyJson(child, current[i], equivalent)));
78
+ if (!isRecord(saved) || !isRecord(current))
79
+ return false;
80
+ return (Object.keys(current).every((key) => Object.hasOwn(saved, key)) &&
81
+ Object.keys(saved).every((key) => sameLegacyJson(saved[key], current[key], equivalent)));
82
+ }
83
+ // A copied reader result can retain the JSON from Buffer.toJSON but lose
84
+ // the native Buffer that the previous reader cloned into a Uint8Array.
85
+ function previousBufferJson(saved, current) {
86
+ if (!isRecord(current) ||
87
+ current["type"] !== "Buffer" ||
88
+ !Array.isArray(current["data"]))
89
+ return false;
90
+ return (isRecord(saved) &&
91
+ Object.keys(current).length === 2 &&
92
+ Object.keys(saved).length === current["data"].length &&
93
+ current["data"].every((byte, index) => Number.isInteger(byte) &&
94
+ byte >= 0 &&
95
+ byte <= 255 &&
96
+ saved[index] === byte));
54
97
  }
55
98
  function sameFindingSelection(left, right) {
56
99
  return (left.length === right.length &&
@@ -67,10 +110,13 @@ function reportForJson(report) {
67
110
  const errors = report?.["errors"];
68
111
  if (!Array.isArray(errors))
69
112
  return report;
70
- const json = JSON.stringify(errors, (_key, entry) => entry instanceof Error ? { message: entry.message } : entry);
113
+ const json = JSON.stringify(errors, (_key, entry) => entry instanceof Error ? { ...entry, message: entry.message } : entry);
71
114
  return { ...report, errors: JSON.parse(json) };
72
115
  }
73
- async function writeAtomicJson(path, value) {
116
+ export function externalPublicationStateDirectory(environment) {
117
+ return join(codexSecurityStateDirectory(environment), "external-finding-publications");
118
+ }
119
+ export async function writeAtomicJson(path, value) {
74
120
  const temporary = `${path}.${randomUUID()}.tmp`;
75
121
  try {
76
122
  const file = await open(temporary, "wx", 0o600);
@@ -101,8 +147,8 @@ function validatePublicationReceipt(value, batch) {
101
147
  const receipt = validateImportReceipt(value);
102
148
  const batchItems = new Map(batch.items.map((item) => [item.client_id, item]));
103
149
  if (receipt.id !== batch.request_id ||
104
- canonicalJson(receipt.repository) !== canonicalJson(batch.repository) ||
105
- canonicalJson(receipt.source) !== canonicalJson(batch.source) ||
150
+ wireJson(receipt.repository) !== wireJson(batch.repository) ||
151
+ wireJson(receipt.source) !== wireJson(batch.source) ||
106
152
  receipt.item_count !== batch.items.length ||
107
153
  receipt.results.length !== batch.items.length ||
108
154
  new Set(receipt.results.map((item) => item.client_id)).size !==
@@ -127,59 +173,70 @@ function validatePublicationReceipt(value, batch) {
127
173
  result.version === null)
128
174
  throw new CodexSecurityError("Cloud returned an incomplete saved finding.");
129
175
  }
130
- if (canonicalJson(receipt.counts) !== canonicalJson(actual))
176
+ if (wireJson(receipt.counts) !== wireJson(actual))
131
177
  throw new CodexSecurityError("Cloud returned inconsistent publication counts.");
132
178
  return receipt;
133
179
  }
134
- async function withImportLock(state, key, signal, operation) {
180
+ async function withImportLocks(state, keys, signal, operation) {
135
181
  if (process.platform === "win32")
136
182
  await createWindowsOutputDirectory(state, true);
137
183
  else
138
184
  await mkdir(state, { recursive: true, mode: 0o700 });
139
185
  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
186
  const require = createRequire(import.meta.url);
149
187
  const Database = (process.versions["bun"]
150
188
  ? require("bun:sqlite").Database
151
189
  : require("node:sqlite").DatabaseSync);
152
- const guard = new Database(path);
190
+ const guards = [];
153
191
  try {
154
- if (process.platform !== "win32")
155
- chmodSync(path, 0o600);
156
- guard.exec("PRAGMA busy_timeout = 0");
192
+ for (const key of [...new Set(keys)].sort()) {
193
+ const path = join(state, `${key}.lock.sqlite`);
194
+ const metadata = await lstat(path).catch((error) => {
195
+ if (error.code === "ENOENT")
196
+ return undefined;
197
+ throw error;
198
+ });
199
+ if (metadata && !metadata.isFile())
200
+ throw new CodexSecurityError("The publication lock is not a regular file.");
201
+ const guard = new Database(path);
202
+ guards.push(guard);
203
+ if (process.platform !== "win32")
204
+ chmodSync(path, 0o600);
205
+ guard.exec("PRAGMA busy_timeout = 0");
206
+ }
157
207
  for (;;) {
158
208
  signal?.throwIfAborted();
209
+ let acquired = 0;
159
210
  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");
211
+ // SQLite releases these transactions on process exit.
212
+ for (const guard of guards) {
213
+ guard.exec("BEGIN EXCLUSIVE");
214
+ acquired++;
215
+ }
163
216
  break;
164
217
  }
165
218
  catch (error) {
166
219
  const sqliteError = error;
167
220
  if (sqliteError.errcode !== 5 && sqliteError.code !== "SQLITE_BUSY")
168
221
  throw error;
222
+ // Older SDKs can acquire historical keys in a different order.
223
+ for (const guard of guards.slice(0, acquired))
224
+ guard.exec("ROLLBACK");
169
225
  await delay(50, undefined, { signal });
170
226
  }
171
227
  }
172
228
  return await operation();
173
229
  }
174
230
  finally {
175
- guard.close();
231
+ for (const guard of guards)
232
+ guard.close();
176
233
  }
177
234
  }
178
235
  export async function prepareExternalPublication(path, options, dependencies = {}) {
179
236
  requireExternalImports(dependencies.environment);
180
237
  const settings = snapshotImportOptions(options);
181
238
  const parsed = await readExternalFindings(path, settings);
182
- return preparePublicationInput(parsed.publication ?? normalizedPublicationInput(parsed.findings), { ...settings, provider: EXTERNAL_FINDINGS_PROVIDERS[settings.provider] }, dependencies);
239
+ return preparePublicationInput(parsed.publication ?? normalizedPublicationInput(parsed.findings), { ...settings, provider: EXTERNAL_FINDINGS_PROVIDERS[settings.provider] }, dependencies, path);
183
240
  }
184
241
  /** Prepare normalized provider evidence without reading a vendor export. */
185
242
  export async function prepareNormalizedExternalPublication(input, options, dependencies = {}) {
@@ -187,60 +244,300 @@ export async function prepareNormalizedExternalPublication(input, options, depen
187
244
  const settings = snapshotImportOptions(options);
188
245
  return preparePublicationInput(normalizedPublicationInput(input), settings, dependencies);
189
246
  }
190
- function normalizedPublicationInput(input) {
191
- // Validation adds Cloud defaults; leave the caller's objects unchanged.
247
+ function ownData(value, key) {
248
+ return isRecord(value) && !isProxy(value)
249
+ ? Object.getOwnPropertyDescriptor(value, key)?.value
250
+ : undefined;
251
+ }
252
+ // When native capture would invoke caller code, only the original data
253
+ // properties can identify the earlier selection. Unknown IDs cannot rule it out.
254
+ function previousFindingIds(input) {
255
+ const rows = ownData(input, "findings");
256
+ if (!Array.isArray(rows) || isProxy(rows))
257
+ return null;
258
+ return Array.from({ length: rows.length }, (_, index) => {
259
+ const row = Object.getOwnPropertyDescriptor(rows, index)?.value;
260
+ const id = ownData(row, "source_finding_id");
261
+ return typeof id === "string" ? id : undefined;
262
+ });
263
+ }
264
+ function withDataProperty(record, key, value) {
265
+ const fields = Object.getOwnPropertyDescriptors(record);
266
+ return Object.create(Object.getPrototypeOf(record), {
267
+ ...fields,
268
+ [key]: { ...fields[key], value },
269
+ });
270
+ }
271
+ // Native cloning discarded enumerable diagnostic fields. Recover its message
272
+ // without reading the caller's stack getter or changing their report.
273
+ const nativeDiagnosticPrototypes = new Set([
274
+ Error.prototype,
275
+ EvalError.prototype,
276
+ RangeError.prototype,
277
+ ReferenceError.prototype,
278
+ SyntaxError.prototype,
279
+ TypeError.prototype,
280
+ URIError.prototype,
281
+ ]);
282
+ function previousDiagnosticMessage(diagnostic) {
283
+ const stack = Object.getOwnPropertyDescriptor(diagnostic, "stack");
284
+ const message = ownData(diagnostic, "message");
285
+ const cause = Object.getOwnPropertyDescriptor(diagnostic, "cause");
286
+ const name = Object.getOwnPropertyDescriptor(diagnostic, "name");
287
+ if (!nativeDiagnosticPrototypes.has(Object.getPrototypeOf(diagnostic)) ||
288
+ typeof message !== "string" ||
289
+ (stack?.get && stack.get !== nativeErrorStackGetter) ||
290
+ (name && typeof name.value !== "string") ||
291
+ (cause &&
292
+ (!Object.hasOwn(cause, "value") ||
293
+ !canSnapshotWithoutGetters(cause.value))))
294
+ return;
295
+ return message;
296
+ }
297
+ function previousDiagnosticInput(input, nativeDiagnostics) {
298
+ const report = ownData(input, "report");
299
+ const errors = ownData(report, "errors");
300
+ if (!isRecord(report) || !Array.isArray(errors) || isProxy(errors))
301
+ return input;
302
+ let entries;
303
+ for (let index = 0; index < errors.length; index++) {
304
+ const descriptor = Object.getOwnPropertyDescriptor(errors, index);
305
+ const diagnostic = descriptor?.value;
306
+ if (!descriptor || !isNativeError(diagnostic))
307
+ continue;
308
+ const message = previousDiagnosticMessage(diagnostic);
309
+ if (message === undefined)
310
+ return input;
311
+ nativeDiagnostics.set(index, diagnostic);
312
+ (entries ??= Object.getOwnPropertyDescriptors(errors))[index] = {
313
+ ...descriptor,
314
+ value: { message },
315
+ };
316
+ }
317
+ if (!entries)
318
+ return input;
319
+ const capturedErrors = Object.defineProperties([], entries);
320
+ const capturedReport = withDataProperty(report, "errors", capturedErrors);
321
+ return withDataProperty(input, "report", capturedReport);
322
+ }
323
+ // Capture before JSON hooks can change their source. If native capture would
324
+ // invoke caller code too, the original bytes cannot be observed independently.
325
+ function previousNormalizedFindings(input, nativeDiagnostics, observe) {
326
+ input = previousDiagnosticInput(input, nativeDiagnostics);
327
+ if (!canSnapshotWithoutGetters(input, observe))
328
+ return;
329
+ // Object spread skips a hidden report, but explicitly reading it could run a getter.
330
+ const reportField = Object.getOwnPropertyDescriptor(input, "report");
331
+ if (reportField && !Object.hasOwn(reportField, "value"))
332
+ return;
192
333
  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) {
334
+ const snapshot = structuredClone(parsed);
335
+ if (report !== undefined)
336
+ snapshot.report = structuredClone(report);
337
+ return snapshot;
338
+ }
339
+ function publicationIdentity(parsed) {
340
+ return parsed.report === undefined
341
+ ? parsed.findings
342
+ : { findings: parsed.findings, report: reportForJson(parsed.report) };
343
+ }
344
+ function validatePublicationFindings(parsed, validateEvidence) {
345
+ validateExternalReport(parsed.report);
346
+ const ids = new Set();
347
+ for (const finding of parsed.findings) {
205
348
  const id = finding.source_finding_id;
206
349
  if (!id.trim() || id.includes("\0") || Buffer.byteLength(id) > 512)
207
350
  throw new CodexSecurityError("Source finding id must contain 1–512 UTF-8 bytes and no NUL characters.");
208
- if (identities.has(id))
351
+ if (ids.has(id))
209
352
  throw new CodexSecurityError(`Duplicate source finding id ${JSON.stringify(id)}.`);
210
- identities.add(id);
211
- finding.evidence = validateExternalEvidence(finding.evidence);
353
+ ids.add(id);
354
+ finding.evidence = validateEvidence(finding.evidence);
212
355
  }
213
- const identity = current.report === undefined
214
- ? current.findings
215
- : { findings: current.findings, report: reportForJson(current.report) };
356
+ }
357
+ function historicalPublication(native) {
358
+ if (!native)
359
+ return;
360
+ try {
361
+ // Older SDKs detached all raw records before Cloud validation added defaults.
362
+ for (const finding of native.findings)
363
+ if (finding.evidence.source_data !== undefined)
364
+ finding.evidence.source_data = structuredClone(finding.evidence.source_data);
365
+ validatePublicationFindings(native, validatePreviousCloudEvidence);
366
+ const identity = publicationIdentity(native);
367
+ let hasBoxedScalars = false;
368
+ const wire = canonicalJson(identity, undefined, (value) => {
369
+ if (!isBoxedPrimitive(value))
370
+ return false;
371
+ hasBoxedScalars = true;
372
+ return true;
373
+ });
374
+ return { parsed: native, identity, wire, hasBoxedScalars };
375
+ }
376
+ catch {
377
+ // The caller may export valid JSON even if the native candidate cannot be
378
+ // validated or hashed, for example when an unused row property holds a BigInt.
379
+ return;
380
+ }
381
+ }
382
+ function normalizedPublicationInput(input) {
383
+ const nativeDiagnostics = new Map();
384
+ let native;
385
+ let plainJson = true;
386
+ try {
387
+ native = previousNormalizedFindings(input, nativeDiagnostics, (value) => {
388
+ if (!plainJson)
389
+ return;
390
+ const prototype = Object.getPrototypeOf(value);
391
+ plainJson =
392
+ (prototype === null ||
393
+ prototype === Object.prototype ||
394
+ prototype === Array.prototype) &&
395
+ !("toJSON" in value);
396
+ });
397
+ }
398
+ catch {
399
+ /* A valid JSON export can contain an uncloneable value. */
400
+ }
401
+ let priorIds = native ? undefined : previousFindingIds(input);
402
+ let current;
403
+ let freshError;
404
+ try {
405
+ current = snapshotNormalizedFindings(input, undefined, "cloud");
406
+ }
407
+ catch (error) {
408
+ if ((!isNativeError(error) && !(error instanceof Error)) ||
409
+ !native?.findings.length)
410
+ throw error;
411
+ freshError = error;
412
+ current = { read: native.read, findings: [], excluded: [] };
413
+ }
414
+ if (native && nativeDiagnostics.size) {
415
+ const historical = native.report["errors"];
416
+ const exported = current.report?.["errors"];
417
+ for (const [index, source] of nativeDiagnostics) {
418
+ const message = historical[index].message;
419
+ let diagnostic = Array.isArray(exported) && exported[index];
420
+ // A failed export may never reach the report. Its hooks have already run,
421
+ // so check the original descriptors again before native cloning reads a stack.
422
+ if (freshError && previousDiagnosticMessage(source) === message)
423
+ diagnostic = source;
424
+ if (isNativeError(diagnostic) &&
425
+ Object.getPrototypeOf(diagnostic) === Object.getPrototypeOf(source)) {
426
+ try {
427
+ const copy = structuredClone(diagnostic);
428
+ if (copy.message === message) {
429
+ historical[index] = copy;
430
+ continue;
431
+ }
432
+ }
433
+ catch {
434
+ // A failed native clone cannot establish a historical report.
435
+ }
436
+ }
437
+ if (freshError)
438
+ throw freshError;
439
+ priorIds = previousFindingIds(native);
440
+ native = undefined;
441
+ break;
442
+ }
443
+ }
444
+ // A Cloud depth error can interrupt a local reader's JSON export. Its plain,
445
+ // independently captured output can still verify the original reader selection.
446
+ const readerOutput = freshError
447
+ ? freshError instanceof CloudEvidenceDepthError &&
448
+ plainJson &&
449
+ !nativeDiagnostics.size &&
450
+ native
451
+ : current;
452
+ const reader = readerOutput
453
+ ? previousReaderSelection(input, readerOutput, native ?? null)
454
+ : undefined;
455
+ if (reader)
456
+ native = reader.snapshot;
457
+ const original = historicalPublication(native);
458
+ try {
459
+ validatePublicationFindings(current, validateExternalEvidence);
460
+ }
461
+ catch (error) {
462
+ if (!(error instanceof Error) || !original?.parsed.findings.length)
463
+ throw error;
464
+ freshError = error;
465
+ }
466
+ if (freshError) {
467
+ if (!original)
468
+ throw freshError;
469
+ // Only the old selection can be reviewed when fresh validation failed.
470
+ current = { read: original.parsed.read, findings: [], excluded: [] };
471
+ }
472
+ const identity = publicationIdentity(current);
216
473
  return {
217
474
  current,
218
475
  legacy: current,
219
476
  currentIdentity: identity,
220
477
  legacyIdentity: identity,
478
+ original,
479
+ freshError,
480
+ unprovenIdPrefixes: reader?.unprovenIdPrefixes,
481
+ uncertainFindingIds: reader?.uncertainFindingIds,
482
+ unprovenOccurrenceCount: reader?.unprovenOccurrenceCount,
483
+ knownFindings: reader?.knownFindings,
484
+ potentialIds: reader?.sourceFindingIds ??
485
+ (original
486
+ ? undefined
487
+ : (native?.findings.map((row) => row.source_finding_id) ?? priorIds)),
221
488
  };
222
489
  }
223
- async function preparePublicationInput(input, options, dependencies) {
224
- const { inspectorProjectMap } = options;
225
- const progress = (event) => {
226
- try {
227
- dependencies.onProgress?.(event);
228
- }
229
- catch {
230
- // Optional progress must not interrupt preparation or publication.
490
+ // The old hash represented Number objects as {}. Unpublished row properties
491
+ // and report metadata cannot be recovered from the immutable saved request.
492
+ function verifiableLegacyMetadata({ report, findings }, wireIdentity) {
493
+ let verifiable = true;
494
+ // Only values in these records can be old scalar projections. Their record
495
+ // containers are required by the normalized selection, including an empty report.
496
+ const { errors, ...metadata } = report ?? {};
497
+ JSON.stringify([
498
+ Object.values(metadata),
499
+ // The original hash used the serialized form of report.errors arrays.
500
+ Array.isArray(errors) ? undefined : errors,
501
+ findings.map(({ evidence: _, ...fields }) => Object.values(fields)),
502
+ ], (_key, value) => {
503
+ if (isRecord(value) && !(wireIdentity && isBoxedPrimitive(value))) {
504
+ // {} and string-index records can both be the old form of a scalar.
505
+ let index = 0;
506
+ if (Object.keys(value).every((key) => {
507
+ const item = value[key];
508
+ return (item === undefined ||
509
+ (key === String(index++) &&
510
+ typeof item === "string" &&
511
+ item.length === 1));
512
+ }))
513
+ verifiable = false;
231
514
  }
232
- };
515
+ return value;
516
+ });
517
+ return verifiable;
518
+ }
519
+ async function preparePublicationInput(input, options, dependencies, nativePath) {
520
+ const { inspectorProjectMap } = options;
521
+ const progress = (event) => notify(() => dependencies.onProgress?.(event));
233
522
  if (!options.sourceKey.trim() ||
234
523
  options.sourceKey.includes("\0") ||
235
524
  Buffer.byteLength(options.sourceKey) > 512) {
236
525
  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
526
  }
238
527
  progress({ phase: "reading", completed: 0 });
239
- const { current: parsed, legacy: legacyParsed, currentIdentity, legacyIdentity, } = input;
240
- if (parsed.findings.length === 0) {
528
+ let { current: parsed, legacy: legacyParsed, currentIdentity, legacyIdentity, } = input;
529
+ function noFindings() {
241
530
  const diagnostics = reportForJson(parsed.report)?.["errors"];
242
531
  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)}` : ""}`);
243
532
  }
533
+ if (!parsed.findings.length &&
534
+ !input.original?.parsed.findings.length &&
535
+ input.potentialIds !== null &&
536
+ !input.potentialIds?.length &&
537
+ !input.uncertainFindingIds?.length &&
538
+ !input.unprovenOccurrenceCount &&
539
+ !input.unprovenIdPrefixes?.length)
540
+ noFindings();
244
541
  progress({ phase: "discovering", completed: 0 });
245
542
  const environment = dependencies.environment ?? process.env;
246
543
  requireExternalImports(environment);
@@ -249,6 +546,7 @@ async function preparePublicationInput(input, options, dependencies) {
249
546
  // Validate the configured return destination before authentication or uploads.
250
547
  cloudFindingsUrl(environment, options.repository);
251
548
  const credentials = await (dependencies.credentials ?? (() => readCloudCredentials(environment)))();
549
+ let privateProviders;
252
550
  async function request(endpoint, body) {
253
551
  dependencies.signal?.throwIfAborted();
254
552
  // Allow upload and response transport around the server's publication budget.
@@ -272,6 +570,11 @@ async function preparePublicationInput(input, options, dependencies) {
272
570
  const retryAfter = response.headers.get("Retry-After");
273
571
  throw new CloudImportError(response.status, `Cloud import failed (HTTP ${response.status}). ${payload?.error?.message ?? response.statusText}${retryAfter ? ` Retry-After: ${retryAfter}.` : ""}`);
274
572
  }
573
+ if (endpoint.startsWith("/repositories?")) {
574
+ const advertised = response.headers.get("Codex-Security-Private-Import-Providers");
575
+ if (advertised !== null)
576
+ privateProviders = new Set(advertised.split(",").map((provider) => provider.trim()));
577
+ }
275
578
  return response.json();
276
579
  }
277
580
  const destinations = [];
@@ -295,7 +598,9 @@ async function preparePublicationInput(input, options, dependencies) {
295
598
  : "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
599
  const destination = destinations[0];
297
600
  const source = { provider: options.provider, source_key: options.sourceKey };
298
- const state = join(codexSecurityStateDirectory(environment), "external-finding-publications");
601
+ const importPrefix = source.provider === "sonar" ? "/v2" : "";
602
+ const sourceReportsPath = importPrefix + sourcePath(destination);
603
+ const state = externalPublicationStateDirectory(environment);
299
604
  const checkpointIdentity = [
300
605
  // Keep existing production checkpoints readable; other deployments must
301
606
  // never share a saved request or receipt with the production default.
@@ -305,178 +610,662 @@ async function preparePublicationInput(input, options, dependencies) {
305
610
  destination.repo_connector_id,
306
611
  source,
307
612
  ];
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 ||
613
+ function scopeKey(identity, serialize = wireJson) {
614
+ return jsonDigest([...checkpointIdentity, identity], serialize);
615
+ }
616
+ function localeKey(rows) {
617
+ return hash("sha256", canonicalJson([...checkpointIdentity, rows], (left, right) => left.localeCompare(right)));
618
+ }
619
+ const original = input.original;
620
+ const originalParsed = original?.parsed ?? parsed;
621
+ const originalIdentity = original?.identity ?? currentIdentity;
622
+ const hasBoxedScalars = original?.hasBoxedScalars === true;
623
+ const changedCapture = original && original.wire !== wireJson(currentIdentity);
624
+ const originalKey = changedCapture || hasBoxedScalars
625
+ ? scopeKey(originalIdentity, canonicalJson)
626
+ : undefined;
627
+ let historicalOnly = false;
628
+ const originalWireKey = changedCapture
629
+ ? scopeKey(originalIdentity)
630
+ : undefined;
631
+ if (changedCapture && originalKey && originalWireKey) {
632
+ // A released SDK could stage either spelling. A modern approval for the
633
+ // fresh JSON can occupy the very same historical filename.
634
+ for (const pathKey of new Set([
635
+ originalKey,
636
+ originalWireKey,
637
+ localeKey(original.parsed.findings),
638
+ ])) {
639
+ const bytes = await readOptionalFile(join(state, `${pathKey}.pending.json`));
640
+ if (bytes === undefined)
641
+ continue;
642
+ const record = JSON.parse(bytes);
643
+ if ((record?.legacyApproval &&
644
+ (record.wireKey ?? pathKey) === originalWireKey) ||
645
+ (record?.wireKey === undefined &&
646
+ record?.inputProjection?.identityFormat !== 2 &&
647
+ (pathKey === originalKey ||
648
+ pathKey === originalWireKey ||
649
+ original.parsed.report === undefined ||
650
+ record?.inputProjection?.inputDigest ===
651
+ jsonDigest(originalIdentity, canonicalJson)))) {
652
+ parsed = legacyParsed = original.parsed;
653
+ currentIdentity = legacyIdentity = original.identity;
654
+ historicalOnly = true;
655
+ break;
656
+ }
657
+ }
658
+ }
659
+ if (input.freshError && !historicalOnly)
660
+ throw input.freshError;
661
+ const inputDigest = jsonDigest(currentIdentity);
662
+ const legacyInputDigest = currentIdentity === legacyIdentity
663
+ ? inputDigest
664
+ : jsonDigest(legacyIdentity);
665
+ function matchesSavedDestination(requests) {
666
+ return (requests.length > 0 &&
667
+ requests.every((batch) => batch.repository.id === destination.id &&
668
+ batch.repository.repo_connector_id ===
669
+ destination.repo_connector_id &&
670
+ wireJson(batch.source) === wireJson(source)));
671
+ }
672
+ async function retireIfReset(content, retire) {
673
+ if (content.requests.some((batch) => batch.repository.reset_marker !== destination.reset_marker)) {
674
+ await retire();
675
+ 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.");
676
+ }
677
+ }
678
+ function readSaved(content) {
679
+ if (!content ||
680
+ content.accountId !== credentials.account_id ||
313
681
  !Array.isArray(content.requests) ||
314
682
  content.requests.length === 0)
315
683
  throw new Error("Saved import account or requests do not match.");
316
684
  content.requests = content.requests.map(validateImportRequest);
317
- const savedFindings = submissionFindings(content.requests);
318
- const savedEvidence = canonicalJson(savedFindings);
685
+ if (!matchesSavedDestination(content.requests))
686
+ throw new Error("Saved import destination does not match.");
687
+ return content;
688
+ }
689
+ function validateSaved(record, legacy = false) {
690
+ const content = readSaved(record);
691
+ const saved = submissionFindings(content.requests);
692
+ const wire = wireJson(saved);
319
693
  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)
694
+ const projection = content.inputProjection;
695
+ if (projection &&
696
+ (!sameFindingSelection(saved, expected) ||
697
+ projection.inputDigest !== (legacy ? legacyInputDigest : inputDigest) ||
698
+ projection.savedEvidenceDigest !== hash("sha256", wire)))
328
699
  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.");
700
+ // Older file readers can have a different, explicitly bound projection.
701
+ // A corrected numeric checkpoint must carry the selected evidence verbatim.
702
+ const exactWire = historicalOnly || hasBoxedScalars || content.wireKey !== undefined;
703
+ if ((!projection || exactWire) &&
704
+ wire !== wireJson(exactWire ? expected.map(wireFinding) : expected))
705
+ throw new Error("Saved import evidence does not match the selected input.");
706
+ if (!legacy &&
707
+ hasBoxedScalars &&
708
+ projection?.identityFormat !== 2 &&
709
+ !verifiableLegacyMetadata(originalParsed, true))
710
+ throw unmatchedPreviousNumbers();
711
+ return content;
712
+ }
713
+ // The original filename and digests identify the previous selection, but
714
+ // contained {} for every boxed number. Only its actual saved request can
715
+ // prove which numbers were approved.
716
+ function readPreviousNumbers(record) {
717
+ const content = readSaved(record);
718
+ const projection = content.inputProjection;
719
+ const savedWire = wireJson(submissionFindings(content.requests));
720
+ const wireDigest = hash("sha256", savedWire);
721
+ if (projection?.inputDigest !== jsonDigest(originalIdentity, canonicalJson))
722
+ throw unmatchedPreviousNumbers();
723
+ if (projection.identityFormat === 2) {
724
+ // A current writer may own the old filename for an ordinary object.
725
+ if (projection.savedEvidenceDigest === wireDigest)
726
+ return undefined;
727
+ throw unmatchedPreviousNumbers();
335
728
  }
729
+ const selected = originalParsed.findings.map(wireFinding);
730
+ if (projection.savedEvidenceDigest !== jsonDigest(selected, canonicalJson))
731
+ throw unmatchedPreviousNumbers();
732
+ if (savedWire !== wireJson(selected)) {
733
+ // A genuinely empty object at this key is a separate ordinary selection.
734
+ if (wireDigest === projection.savedEvidenceDigest)
735
+ return undefined;
736
+ return "different-wire";
737
+ }
738
+ if ((changedCapture && !historicalOnly) ||
739
+ !verifiableLegacyMetadata(originalParsed, false))
740
+ throw unmatchedPreviousNumbers();
741
+ content.legacyApproval = true;
742
+ content.inputProjection = {
743
+ identityFormat: 2,
744
+ inputDigest,
745
+ savedEvidenceDigest: wireDigest,
746
+ };
336
747
  return content;
337
748
  }
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.");
749
+ function unmatchedPreviousNumbers() {
750
+ return new CodexSecurityError("The older saved import cannot be matched to the selected input. Its checkpoint was preserved without uploading.");
751
+ }
752
+ const key = scopeKey(currentIdentity);
753
+ const stagingKey = originalKey ?? key;
754
+ const canonicalPath = join(state, `${key}.pending.json`);
755
+ const lookupPath = join(state, `${key}.lookup.json`);
756
+ // A lookup only suggests a regular file to validate. A missing or stale hint
757
+ // leaves the same on-disk discovery available as before.
758
+ async function readOptionalRegularFile(path) {
759
+ try {
760
+ if ((await lstat(path)).isFile())
761
+ return await readFile(path, "utf8");
376
762
  }
763
+ catch {
764
+ // Optional discovery must not veto an independently verified checkpoint.
765
+ }
766
+ }
767
+ async function lookupFileKey() {
768
+ const serialized = await readOptionalRegularFile(lookupPath);
769
+ return serialized?.trim().match(/^"([a-f0-9]{64})"$/u)?.[1];
770
+ }
771
+ const historicalKey = localeKey(legacyParsed.findings);
772
+ const legacyIdentityKey = scopeKey(legacyIdentity);
773
+ const legacyKeys = new Set([legacyIdentityKey, historicalKey]);
774
+ function underLocks(keys, operation) {
775
+ return withImportLocks(state, keys, dependencies.signal, operation);
377
776
  }
378
777
  let recovered;
379
778
  for (const legacyKey of legacyKeys) {
380
- if (legacyKey === key)
779
+ if (legacyKey === key || legacyKey === originalKey)
381
780
  continue;
382
781
  const legacyPath = join(state, `${legacyKey}.pending.json`);
383
782
  const captured = await readOptionalFile(legacyPath);
384
783
  if (captured !== undefined) {
385
784
  // Preserve requests staged by the earlier locale-based filename format.
386
785
  // 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);
786
+ await underLocks([key, legacyKey], async () => {
787
+ const current = await readOptionalFile(canonicalPath);
389
788
  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);
789
+ let prior;
790
+ if (serialized !== undefined) {
791
+ try {
792
+ prior = JSON.parse(serialized);
793
+ }
794
+ catch (error) {
795
+ if (current === undefined || !(error instanceof SyntaxError))
796
+ throw error;
407
797
  }
798
+ }
799
+ if (prior?.wireKey !== undefined)
408
800
  return;
801
+ if (current !== undefined) {
802
+ recovered = validateSaved(JSON.parse(current));
803
+ if (prior && sameSubmission(prior, recovered))
804
+ await rm(legacyPath);
805
+ return;
806
+ }
807
+ // A report-free approval at the locale-era filename can be a separate input.
808
+ if (legacyKey !== legacyIdentityKey &&
809
+ prior?.inputProjection &&
810
+ prior.inputProjection.inputDigest !== legacyInputDigest) {
811
+ const projection = prior.inputProjection;
812
+ const rows = legacyParsed.findings;
813
+ if (projection.identityFormat === 2 ||
814
+ (projection.inputDigest === jsonDigest(rows) &&
815
+ projection.savedEvidenceDigest ===
816
+ jsonDigest(submissionFindings(prior.requests))))
817
+ return;
818
+ // The released SDK hashed report-free rows as an array and boxed scores as {}.
819
+ const originalRows = original?.parsed.findings ?? rows;
820
+ if (hasBoxedScalars &&
821
+ legacyParsed.report !== undefined &&
822
+ legacyKey === scopeKey(originalRows, canonicalJson) &&
823
+ projection.inputDigest ===
824
+ jsonDigest(originalRows, canonicalJson) &&
825
+ projection.savedEvidenceDigest ===
826
+ jsonDigest(originalRows.map(wireFinding), canonicalJson) &&
827
+ sameFindingSelection(submissionFindings(readSaved(prior).requests), rows))
828
+ return;
409
829
  }
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);
830
+ const content = validateSaved(serialized === undefined ? JSON.parse(captured) : prior, true);
413
831
  if (!sameFindingSelection(legacyParsed.findings, parsed.findings))
414
832
  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)) {
833
+ await retireIfReset(content, async () => {
416
834
  if (serialized !== undefined)
417
835
  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
- }
836
+ });
420
837
  const savedEvidenceDigest = content.inputProjection?.savedEvidenceDigest ??
421
- hash("sha256", canonicalJson(legacyParsed.findings));
838
+ jsonDigest(legacyParsed.findings);
422
839
  const rebindProjection = content.inputProjection !== undefined ||
423
840
  savedEvidenceDigest !== inputDigest;
424
- if (rebindProjection)
425
- content.inputProjection = { inputDigest, savedEvidenceDigest };
841
+ if (rebindProjection) {
842
+ if (content.inputProjection?.identityFormat !== 2)
843
+ content.legacyApproval = true;
844
+ content.inputProjection = {
845
+ ...content.inputProjection,
846
+ identityFormat: 2,
847
+ inputDigest,
848
+ savedEvidenceDigest,
849
+ };
850
+ }
426
851
  recovered = content;
427
852
  if (serialized === undefined)
428
853
  return;
429
854
  if (!rebindProjection) {
430
- await rename(legacyPath, pendingPath);
855
+ await rename(legacyPath, canonicalPath);
431
856
  }
432
857
  else {
433
858
  // Bind the new input projection to the original immutable evidence,
434
859
  // so later inventory reordering still resumes these exact requests.
435
- await writeAtomicJson(pendingPath, content);
860
+ await writeAtomicJson(canonicalPath, content);
436
861
  await rm(legacyPath);
437
862
  }
438
- }));
863
+ });
439
864
  }
440
865
  }
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);
866
+ // Some getter-backed batches cannot reproduce a released filename. Their IDs
867
+ // can stop a conflicting upload, but never authorize replay of a saved request.
868
+ const potentialIds = input.potentialIds === undefined && changedCapture
869
+ ? original.parsed.findings.map((row) => row.source_finding_id)
870
+ : input.potentialIds;
871
+ const unprovenPrefixes = new Set(input.unprovenIdPrefixes);
872
+ const uncertainIds = new Set(input.uncertainFindingIds);
873
+ const uncertainMembership = input.unprovenOccurrenceCount ||
874
+ uncertainIds.size > 0 ||
875
+ unprovenPrefixes.size > 0;
876
+ function uncertainId(id) {
877
+ return (uncertainIds.has(id) ||
878
+ unprovenPrefixes.has(id.replace(/\/[a-f0-9]{64}$/u, "/")));
879
+ }
880
+ const stableIds = uncertainMembership
881
+ ? (potentialIds ?? parsed.findings.map((row) => row.source_finding_id)).filter((id) => id === undefined || !uncertainId(id))
882
+ : undefined;
883
+ const knownMembership = input.unprovenOccurrenceCount
884
+ ? new Set(stableIds)
885
+ : undefined;
886
+ const knownRows = new Map(input.knownFindings?.map((row) => [
887
+ row.source_finding_id,
888
+ wireFinding(row),
889
+ ]));
890
+ let originalInputDigest;
891
+ const cannotCaptureOriginal = !input.original &&
892
+ (potentialIds === null || !!(potentialIds?.length || uncertainMembership));
893
+ const possibleBufferExport = options.provider === "github" && original && !changedCapture;
894
+ function potentialOriginal(record, fileKey) {
895
+ const projection = record.inputProjection;
896
+ if (record.accountId !== credentials.account_id)
897
+ return false;
898
+ if (!projection) {
899
+ // Locale-era requests had no projection. Their scoped occurrence IDs
900
+ // can stop an opaque retry but cannot authorize replay.
901
+ if (!cannotCaptureOriginal || record.wireKey !== undefined)
902
+ return false;
903
+ }
904
+ else if (!/^[a-f0-9]{64}$/u.test(projection.inputDigest) ||
905
+ !/^[a-f0-9]{64}$/u.test(projection.savedEvidenceDigest) ||
906
+ (projection.identityFormat === 2 && !record.legacyApproval))
907
+ return false;
908
+ try {
909
+ // Older evidence may fail today's limits. Scope and selection can still
910
+ // identify an outstanding approval and prevent a duplicate submission.
911
+ if (!matchesSavedDestination(record.requests))
912
+ return false;
913
+ const rows = submissionFindings(record.requests);
914
+ const savedWire = wireJson(rows);
915
+ const evidenceMatches = projection &&
916
+ projection.savedEvidenceDigest === hash("sha256", savedWire);
917
+ if (evidenceMatches) {
918
+ // Matching old digests reconstruct its scoped filename. A report from
919
+ // this caller is useful only when the saved digest independently binds it.
920
+ const reportFree = projection.inputDigest === projection.savedEvidenceDigest;
921
+ const identity = reportFree
922
+ ? rows
923
+ : parsed.report !== undefined
924
+ ? { findings: rows, report: reportForJson(parsed.report) }
925
+ : undefined;
926
+ if (identity &&
927
+ (reportFree || projection.inputDigest === jsonDigest(identity))) {
928
+ if (record.wireKey
929
+ ? record.wireKey !== scopeKey(identity)
930
+ : fileKey !== scopeKey(identity, canonicalJson) &&
931
+ fileKey !== localeKey(rows))
932
+ return false;
933
+ if (possibleBufferExport &&
934
+ projection.inputDigest !== inputDigest &&
935
+ sameLegacyJson(identity, currentIdentity, previousBufferJson)) {
936
+ return true;
937
+ }
938
+ }
939
+ if (!record.wireKey &&
940
+ reportFree &&
941
+ original &&
942
+ original.wire !== savedWire)
943
+ return false;
944
+ }
945
+ if (record.wireKey && !cannotCaptureOriginal)
946
+ return false;
947
+ if (projection &&
948
+ original &&
949
+ fileKey !== stagingKey &&
950
+ fileKey !== originalWireKey &&
951
+ projection.inputDigest ===
952
+ (originalInputDigest ??= jsonDigest(original.identity, canonicalJson)) &&
953
+ fileKey !== localeKey(original.parsed.findings) &&
954
+ projection.savedEvidenceDigest ===
955
+ jsonDigest(original.parsed.findings.map(wireFinding), canonicalJson) &&
956
+ savedWire === wireJson(original.parsed.findings.map(wireFinding)))
957
+ return false;
958
+ if (stableIds) {
959
+ // An opaque occurrence may have had a different old ID. Compare all
960
+ // definite members and count the remaining IDs without claiming them.
961
+ const certain = rows.filter((row) => !uncertainId(row.source_finding_id));
962
+ const stableSaved = knownMembership
963
+ ? certain.filter((row) => knownMembership.has(row.source_finding_id))
964
+ : certain;
965
+ if (certain.length - stableSaved.length >
966
+ (input.unprovenOccurrenceCount ?? 0))
967
+ return false;
968
+ return (rows.length > 0 &&
969
+ stableSaved.length === stableIds.length &&
970
+ stableSaved.every((row, i) => {
971
+ const known = knownRows.get(row.source_finding_id);
972
+ return (row.source_finding_id === stableIds[i] &&
973
+ (!known || wireJson(row) === wireJson(known)));
974
+ }));
975
+ }
976
+ const candidates = originalParsed.findings;
977
+ const ids = potentialIds === undefined
978
+ ? parsed.findings.map((row) => row.source_finding_id)
979
+ : potentialIds;
980
+ if (ids &&
981
+ (!ids.length ||
982
+ rows.length !== ids.length ||
983
+ !rows.every((row, i) => ids[i] === undefined || row.source_finding_id === ids[i])))
984
+ return false;
985
+ // An opaque serializer can add a report or replace every visible field.
986
+ // Only scoped, independently known occurrences can rule out its old request.
987
+ if (cannotCaptureOriginal || !projection)
988
+ return true;
989
+ if (projection.inputDigest === projection.savedEvidenceDigest &&
990
+ parsed.report !== undefined)
991
+ return false;
992
+ if (potentialIds === undefined && (hasBoxedScalars || evidenceMatches))
993
+ return false;
994
+ return (!sameFindingSelection(rows, candidates) ||
995
+ rows.every((row, i) => {
996
+ const { source_data: savedData, ...savedFields } = row.evidence;
997
+ const { source_data: selectedData, ...selectedFields } = candidates[i].evidence;
998
+ if (wireJson(savedFields) !== wireJson(selectedFields))
999
+ return false;
1000
+ // Once the original SDK discarded boxed positions, a plain JSON export
1001
+ // alone cannot attribute a new numeric spelling to that saved request.
1002
+ return sameLegacyJson(savedData, selectedData, (value) => typeof value === "number");
1003
+ }));
1004
+ }
1005
+ catch {
1006
+ return false;
1007
+ }
1008
+ }
1009
+ async function selectedCheckpoint(knownFileKey, preparing = false) {
1010
+ const directory = await validatePreparedOutputDir(state, undefined, true).catch((error) => {
1011
+ if (error.code === "ENOENT")
1012
+ return undefined;
1013
+ throw error;
1014
+ });
1015
+ if (!directory)
1016
+ return undefined;
1017
+ const known = new Set([key, originalKey, knownFileKey].filter((value) => value !== undefined));
1018
+ let unidentified = false;
1019
+ let unprovenOriginal = false;
1020
+ let incomplete = false;
1021
+ let stagingPathOccupied = false;
1022
+ async function readCandidate(fileKey, selected, contents) {
1023
+ const path = join(state, `${fileKey}.pending.json`);
1024
+ const serialized = contents ??
1025
+ (selected?.fileKey === key && !known.has(fileKey)
1026
+ ? await readOptionalRegularFile(path)
1027
+ : await readOptionalFile(path));
1028
+ if (serialized === undefined) {
1029
+ if (!known.has(fileKey))
1030
+ incomplete = true;
1031
+ return selected;
1032
+ }
1033
+ if (fileKey === stagingKey)
1034
+ stagingPathOccupied = true;
1035
+ let record;
1036
+ try {
1037
+ record = JSON.parse(serialized);
1038
+ }
1039
+ catch (error) {
1040
+ if (known.has(fileKey))
1041
+ throw error;
1042
+ incomplete = true;
1043
+ return selected;
1044
+ }
1045
+ // A migrated checkpoint records the wire identity it belongs to even
1046
+ // when a separate input hashes to its historical filename.
1047
+ const tag = record?.wireKey;
1048
+ const tagged = tag !== undefined;
1049
+ const ownerKey = tagged ? tag : fileKey;
1050
+ const historical = !tagged && fileKey === originalKey && fileKey !== key;
1051
+ if (ownerKey !== key && !historical) {
1052
+ if (record &&
1053
+ (!tagged || cannotCaptureOriginal || possibleBufferExport) &&
1054
+ potentialOriginal(record, fileKey))
1055
+ unprovenOriginal = true;
1056
+ // Some early locale filenames carry no deployment identity. Never guess their target.
1057
+ if (!tagged &&
1058
+ record &&
1059
+ !record.inputProjection &&
1060
+ !legacyKeys.has(fileKey)) {
1061
+ try {
1062
+ // A different locale cannot authorize replay, even when the
1063
+ // original native capture proves these rows were selected.
1064
+ if (!original ||
1065
+ wireJson(submissionFindings(readSaved(record).requests)) !==
1066
+ wireJson(original.parsed.findings.map(wireFinding)))
1067
+ validateSaved(record, true);
1068
+ unidentified = true;
1069
+ }
1070
+ catch {
1071
+ /* Unrelated input. */
1072
+ }
1073
+ }
1074
+ return selected;
1075
+ }
1076
+ const content = historical
1077
+ ? readPreviousNumbers(record)
1078
+ : validateSaved(record);
1079
+ if (content === "different-wire") {
1080
+ // Another spelling may already have a verified checkpoint for this wire value.
1081
+ unprovenOriginal = true;
1082
+ return selected;
1083
+ }
1084
+ if (!content)
1085
+ return selected;
1086
+ if (selected) {
1087
+ const conflict = new CodexSecurityError("Two saved imports match this input. Their original requests and receipts were preserved without uploading.");
1088
+ if (!preparing)
1089
+ throw conflict;
1090
+ // The two unlocked reads may have straddled completion of the first request.
1091
+ return await underLocks([key, selected.fileKey, fileKey], async () => {
1092
+ const first = await readCandidate(selected.fileKey);
1093
+ if (first) {
1094
+ if (await readCandidate(fileKey))
1095
+ throw conflict;
1096
+ if (sameSubmission(first.content, selected.content))
1097
+ return first;
1098
+ }
1099
+ return completedCheckpoint(selected);
456
1100
  });
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.");
458
1101
  }
1102
+ return { fileKey, content, historical };
1103
+ }
1104
+ let selected;
1105
+ for (const fileKey of known)
1106
+ selected = await readCandidate(fileKey, selected);
1107
+ // A known file that disappeared can be checked against its completed receipt.
1108
+ if (!selected && knownFileKey !== undefined)
1109
+ return undefined;
1110
+ const hint = await lookupFileKey();
1111
+ if (hint && !known.has(hint)) {
1112
+ const contents = await readOptionalRegularFile(join(state, `${hint}.pending.json`));
1113
+ if (contents !== undefined) {
1114
+ selected = await readCandidate(hint, selected, contents);
1115
+ known.add(hint);
1116
+ }
1117
+ }
1118
+ // A released publisher could stage the canonical request while another SDK
1119
+ // tagged its old filename but had not yet written the optional lookup.
1120
+ if (selected &&
1121
+ !cannotCaptureOriginal &&
1122
+ !selected.historical &&
1123
+ (selected.fileKey !== key ||
1124
+ selected.content.canonicalChecked ||
1125
+ selected.content.inputProjection?.identityFormat === 2))
1126
+ return selected;
1127
+ const entries = await readdir(state, { withFileTypes: true }).catch((error) => {
1128
+ if (error.code === "ENOENT")
1129
+ return [];
1130
+ throw error;
1131
+ });
1132
+ for (const entry of entries) {
1133
+ if (!entry.isFile() || !/^[a-f0-9]{64}\.pending\.json$/u.test(entry.name))
1134
+ continue;
1135
+ const fileKey = entry.name.slice(0, -".pending.json".length);
1136
+ if (!known.has(fileKey))
1137
+ selected = await readCandidate(fileKey, selected);
459
1138
  }
1139
+ if (unprovenOriginal && (!selected || cannotCaptureOriginal))
1140
+ throw unmatchedPreviousNumbers();
1141
+ if (!selected && stagingPathOccupied)
1142
+ throw occupiedCheckpoint();
1143
+ if (!selected && unidentified)
1144
+ 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.");
1145
+ if (selected?.fileKey === key && !preparing && !incomplete)
1146
+ selected.checkedCanonical = true;
1147
+ return selected;
1148
+ }
1149
+ async function readCompletedPublication(requests, observedKey) {
1150
+ for (const resultKey of new Set([key, observedKey])) {
1151
+ const serialized = await readOptionalFile(join(state, `${resultKey}.result.json`));
1152
+ if (serialized === undefined)
1153
+ continue;
1154
+ const completed = JSON.parse(serialized);
1155
+ if ((completed?.status === "complete" || completed?.status === "partial") &&
1156
+ Array.isArray(completed.receipts) &&
1157
+ completed.receipts.length === requests.length &&
1158
+ completed.receipts.every((receipt, i) => receipt?.id === requests[i].request_id))
1159
+ return {
1160
+ receipts: completed.receipts.map((receipt, i) => validatePublicationReceipt(receipt, requests[i])),
1161
+ verified: completed.verified,
1162
+ };
1163
+ }
1164
+ }
1165
+ async function completedCheckpoint(observed) {
1166
+ if (await readCompletedPublication(observed.content.requests, observed.fileKey))
1167
+ return observed;
1168
+ throw new CodexSecurityError("The saved publication changed while preparing this input. Review it again.");
1169
+ }
1170
+ function occupiedCheckpoint() {
1171
+ return new CodexSecurityError("Finish the other pending import at this checkpoint first, then review this input again.");
1172
+ }
1173
+ let selected = await selectedCheckpoint(recovered ? key : undefined, true);
1174
+ const fileKey = selected?.fileKey ?? (recovered ? key : stagingKey);
1175
+ const pendingPath = join(state, `${fileKey}.pending.json`);
1176
+ async function rememberFile() {
1177
+ if (fileKey !== key && (await lookupFileKey()) !== fileKey)
1178
+ await writeAtomicJson(lookupPath, fileKey).catch(() => undefined);
1179
+ }
1180
+ async function removePending() {
1181
+ await rm(pendingPath, { force: true });
1182
+ if (fileKey !== key && (await lookupFileKey()) === fileKey)
1183
+ await rm(lookupPath, { force: true }).catch(() => undefined);
1184
+ }
1185
+ const lockKeys = [key, fileKey, stagingKey];
1186
+ if (selected?.historical) {
1187
+ const observed = selected;
1188
+ selected = await underLocks(lockKeys, async () => {
1189
+ const current = await selectedCheckpoint(fileKey);
1190
+ if (!current ||
1191
+ current.fileKey !== fileKey ||
1192
+ !sameSubmission(current.content, observed.content))
1193
+ return completedCheckpoint(observed);
1194
+ if (current.historical) {
1195
+ current.content.wireKey = key;
1196
+ await writeAtomicJson(pendingPath, current.content);
1197
+ await rememberFile();
1198
+ }
1199
+ return current;
1200
+ });
1201
+ }
1202
+ const saved = selected?.content ?? recovered;
1203
+ if (historicalOnly && !saved)
1204
+ throw unmatchedPreviousNumbers();
1205
+ if (!parsed.findings.length)
1206
+ noFindings();
1207
+ if (saved) {
1208
+ await retireIfReset(saved, () => underLocks(lockKeys, async () => {
1209
+ const current = await readOptionalFile(pendingPath);
1210
+ if (current !== undefined &&
1211
+ sameSubmission(JSON.parse(current), saved))
1212
+ await removePending();
1213
+ }));
460
1214
  }
461
1215
  if (saved?.inspectorProjectMapRequired && !inspectorProjectMap)
462
1216
  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);
1217
+ function checkRepositories(findings, report) {
468
1218
  function requireDestination(id, url) {
469
1219
  if (url &&
470
1220
  cloudRepositoryUrlKey(url) !== cloudRepositoryUrlKey(destination.url))
471
1221
  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
1222
  }
473
- if (binding)
474
- requireDestination(binding.findingId, binding.url);
1223
+ // Historical checkpoints already fixed the provider and request bytes.
1224
+ // Recheck any projected source against the destination discovered for this retry.
1225
+ const replay = saved && options.provider !== "blackduck";
1226
+ if (!saved || options.provider === "sonar") {
1227
+ for (const url of sonarRepositoryUrls(options.provider, findings, report, "cloud"))
1228
+ requireDestination("Sonar source", url);
1229
+ }
1230
+ const repositories = replay
1231
+ ? undefined
1232
+ : blackDuckReportRepositories(options.provider, findings, "cloud");
1233
+ for (const [provider, repositories] of [
1234
+ ["veracode", veracodeReportRepositories],
1235
+ ["continuum", continuumReportRepositories],
1236
+ ]) {
1237
+ if (saved && options.provider !== provider)
1238
+ continue;
1239
+ for (const [id, source] of repositories(options.provider, findings, "cloud"))
1240
+ requireDestination(id, source.url);
1241
+ }
1242
+ if (!replay ||
1243
+ options.provider === "inspector" ||
1244
+ options.provider === "veracode") {
1245
+ const binding = inspectorReportRepository(options.provider, findings, "cloud", inspectorProjectMap);
1246
+ if (binding)
1247
+ requireDestination(binding.findingId, binding.url);
1248
+ }
475
1249
  for (const finding of findings) {
476
1250
  const { source_finding_id: id, evidence } = finding;
1251
+ const projected = repositories
1252
+ ? repositories.get(id)?.url
1253
+ : projectedBlackDuckRepositoryUrl(finding, "cloud");
1254
+ if (replay &&
1255
+ !projected &&
1256
+ options.provider !== "inspector" &&
1257
+ options.provider !== "veracode" &&
1258
+ options.provider !== "continuum" &&
1259
+ options.provider !== "sonar")
1260
+ continue;
477
1261
  const urls = [
1262
+ projected,
478
1263
  evidence.details?.repository?.url,
479
- ...(options.provider === "wiz" ? [wizCsvRepositoryUrl(evidence)] : []),
1264
+ options.provider === "wiz"
1265
+ ? projected
1266
+ ? wizFindingRepository(evidence, "cloud")?.url
1267
+ : wizCsvRepositoryUrl(evidence)
1268
+ : undefined,
480
1269
  ...(options.provider === "snyk"
481
1270
  ? snykRepositoryUrls(evidence.source_data, {
482
1271
  cloudRepositoryUrl: destination.url,
@@ -493,10 +1282,177 @@ async function preparePublicationInput(input, options, dependencies) {
493
1282
  requireDestination(id, url);
494
1283
  }
495
1284
  }
496
- checkRepositories(parsed.findings);
1285
+ checkRepositories(parsed.findings, parsed.report);
497
1286
  // A retry sends its saved projection, which may differ from today's reader.
498
- if (saved)
499
- checkRepositories(submissionFindings(saved.requests));
1287
+ if (saved) {
1288
+ // A common Sonar ref can supply missing row refs. Only use the report
1289
+ // associated with the input digest in the saved checkpoint.
1290
+ const report = saved.inputProjection?.inputDigest === inputDigest
1291
+ ? parsed.report
1292
+ : undefined;
1293
+ checkRepositories(submissionFindings(saved.requests), report);
1294
+ }
1295
+ // Existing repository submissions keep their original request bodies and receipts.
1296
+ const privatePath = join(state, `${key}.private-import.json`);
1297
+ const privateSaved = await readOptionalFile(privatePath);
1298
+ if (!saved &&
1299
+ (privateProviders?.has(source.provider) || privateSaved !== undefined)) {
1300
+ const client = privateImportClient(apiBaseUrl, credentials, dependencies.fetch ?? globalThis.fetch, dependencies.signal);
1301
+ const context = await client.context();
1302
+ if (source.source_key.trim() !== source.source_key)
1303
+ throw new CodexSecurityError("A private source key cannot have leading or trailing whitespace.");
1304
+ const report = await selectPrivateReport(parsed, source.provider, nativePath, context, privateSaved);
1305
+ const binding = {
1306
+ format: report.format,
1307
+ apiBaseUrl,
1308
+ accountId: credentials.account_id,
1309
+ source,
1310
+ repository: {
1311
+ id: destination.id,
1312
+ repo_connector_id: destination.repo_connector_id,
1313
+ },
1314
+ sha256: report.digest,
1315
+ contextVersion: context.context_version,
1316
+ };
1317
+ function checkpointForContext(data) {
1318
+ return data === undefined
1319
+ ? undefined
1320
+ : readPrivateCheckpoint(data, binding);
1321
+ }
1322
+ const previous = checkpointForContext(privateSaved);
1323
+ function requireAccess(value, savedFile, savedJob) {
1324
+ if (!value.can_read ||
1325
+ (!savedJob &&
1326
+ ((!savedFile && !value.can_upload) ||
1327
+ !value.can_create_import_jobs ||
1328
+ !value.formats.includes(report.format))))
1329
+ throw new CodexSecurityError("Private findings import is unavailable for this account or report format.");
1330
+ }
1331
+ requireAccess(context, !!previous?.fileId, !!previous?.jobId);
1332
+ const retryOfJobId = unsuccessfulJob(previous?.receipt) &&
1333
+ context.can_upload &&
1334
+ context.can_create_import_jobs &&
1335
+ context.formats.includes(report.format)
1336
+ ? previous?.jobId
1337
+ : undefined;
1338
+ const privateImport = {
1339
+ format: report.format,
1340
+ contextVersion: binding.contextVersion,
1341
+ sha256: report.digest,
1342
+ bytes: Buffer.byteLength(report.bytes),
1343
+ ...(retryOfJobId ? { retryOfJobId } : {}),
1344
+ };
1345
+ const preview = {
1346
+ ...parsed,
1347
+ cloudApiUrl: `${apiBaseUrl}/finding-imports`,
1348
+ accountId: credentials.account_id,
1349
+ destination,
1350
+ source,
1351
+ resumed: previous !== undefined && !retryOfJobId,
1352
+ requests: [],
1353
+ privateImport,
1354
+ };
1355
+ return {
1356
+ preview: structuredClone(preview),
1357
+ publish: () => underLocks(lockKeys, async () => {
1358
+ const current = await client.context();
1359
+ if (current.context_version !== binding.contextVersion)
1360
+ throw new CodexSecurityError("The private inbox changed. Run the command again to review the new destination.");
1361
+ const stored = checkpointForContext(await readOptionalFile(privatePath));
1362
+ // Only a preview of this terminal job can start a new attempt.
1363
+ const retrying = retryOfJobId !== undefined && stored?.receipt?.id === retryOfJobId;
1364
+ const active = retrying ? undefined : stored;
1365
+ requireAccess(current, !!active?.fileId, !!active?.jobId);
1366
+ if (await selectedCheckpoint())
1367
+ throw new CodexSecurityError("A repository submission is pending. Review its saved request before continuing.");
1368
+ const checkpoint = active ?? {
1369
+ ...binding,
1370
+ uploadRequestId: randomUUID(),
1371
+ jobRequestId: randomUUID(),
1372
+ fileId: undefined,
1373
+ jobId: undefined,
1374
+ receipt: undefined,
1375
+ previousReceipt: retrying ? stored?.receipt : undefined,
1376
+ };
1377
+ const result = {
1378
+ cloudApiUrl: preview.cloudApiUrl,
1379
+ status: "interrupted",
1380
+ read: parsed.read,
1381
+ ready: parsed.findings.length,
1382
+ report: parsed.report,
1383
+ excluded: parsed.excluded,
1384
+ receipts: [],
1385
+ counts: { created: 0, updated: 0, unchanged: 0, error: 0 },
1386
+ failures: [],
1387
+ unacknowledged: parsed.findings.length,
1388
+ verified: 0,
1389
+ };
1390
+ function acknowledge(job) {
1391
+ const { created, updated, unchanged, error } = job.counts;
1392
+ const processed = created + updated + unchanged + error;
1393
+ if (job.counts.discovered !== processed + job.counts.unprocessed ||
1394
+ job.counts.discovered > result.ready ||
1395
+ (job.status === "completed" &&
1396
+ (processed !== result.ready ||
1397
+ job.counts.total !== result.ready)))
1398
+ throw new CodexSecurityError("Cloud returned counts that do not match the selected report.");
1399
+ result.privateJob = job;
1400
+ result.counts = { created, updated, unchanged, error };
1401
+ result.unacknowledged = result.ready - processed;
1402
+ result.status =
1403
+ job.status === "completed" && !error && !parsed.excluded.length
1404
+ ? "complete"
1405
+ : "partial";
1406
+ if (job.error)
1407
+ result.error = job.error.message;
1408
+ else
1409
+ delete result.error;
1410
+ }
1411
+ try {
1412
+ const savedReceipt = checkpoint.receipt ?? checkpoint.previousReceipt;
1413
+ if (savedReceipt)
1414
+ acknowledge(savedReceipt);
1415
+ if (!active)
1416
+ await writeAtomicJson(privatePath, checkpoint);
1417
+ if (!checkpoint.fileId) {
1418
+ progress({ phase: "uploading", completed: 0 });
1419
+ checkpoint.fileId = await client.upload({
1420
+ requestId: checkpoint.uploadRequestId,
1421
+ contextVersion: binding.contextVersion,
1422
+ ...report,
1423
+ recovering: active !== undefined,
1424
+ });
1425
+ await writeAtomicJson(privatePath, checkpoint);
1426
+ }
1427
+ progress({ phase: "verifying", completed: 0 });
1428
+ const job = await client.job({
1429
+ requestId: checkpoint.jobRequestId,
1430
+ contextVersion: binding.contextVersion,
1431
+ fileId: checkpoint.fileId,
1432
+ format: report.format,
1433
+ source,
1434
+ repository: binding.repository,
1435
+ jobId: checkpoint.jobId,
1436
+ }, async (id) => {
1437
+ checkpoint.jobId = id;
1438
+ await writeAtomicJson(privatePath, checkpoint);
1439
+ });
1440
+ acknowledge(job);
1441
+ checkpoint.jobId = job.id;
1442
+ checkpoint.receipt = job;
1443
+ await writeAtomicJson(privatePath, checkpoint);
1444
+ return result;
1445
+ }
1446
+ catch (error) {
1447
+ result.status = "interrupted";
1448
+ result.error =
1449
+ error instanceof Error ? error.message : String(error);
1450
+ result.savedSubmission = privatePath;
1451
+ throw new ExternalPublicationError(result.error, result, error);
1452
+ }
1453
+ }),
1454
+ };
1455
+ }
500
1456
  let requests = saved?.requests;
501
1457
  if (!requests) {
502
1458
  let preparedCount = 0;
@@ -512,7 +1468,7 @@ async function preparePublicationInput(input, options, dependencies) {
512
1468
  source_finding_id: finding.source_finding_id,
513
1469
  limit: "2",
514
1470
  });
515
- const summaries = validateSourceReports(await request(`${sourcePath(destination)}?${query}`));
1471
+ const summaries = validateSourceReports(await request(`${sourceReportsPath}?${query}`));
516
1472
  if (summaries.has_more || summaries.data.length > 1)
517
1473
  throw new CodexSecurityError("Cloud returned more than one report for a source identity.");
518
1474
  const previous = summaries.data[0];
@@ -579,9 +1535,12 @@ async function preparePublicationInput(input, options, dependencies) {
579
1535
  accountId: credentials.account_id,
580
1536
  requests,
581
1537
  receipts: saved?.receipts,
1538
+ legacyApproval: saved?.legacyApproval,
1539
+ wireKey: saved?.wireKey ?? (fileKey === key ? undefined : key),
582
1540
  inputProjection: saved?.inputProjection ?? {
1541
+ identityFormat: saved ? undefined : 2,
583
1542
  inputDigest,
584
- savedEvidenceDigest: hash("sha256", canonicalJson(submissionFindings(requests))),
1543
+ savedEvidenceDigest: jsonDigest(submissionFindings(requests)),
585
1544
  },
586
1545
  };
587
1546
  if (inspectorProjectMap)
@@ -598,7 +1557,7 @@ async function preparePublicationInput(input, options, dependencies) {
598
1557
  requests,
599
1558
  };
600
1559
  return {
601
- preview: structuredClone(preview),
1560
+ preview: cloneImportPreview(preview),
602
1561
  async publish() {
603
1562
  progress({ phase: "waiting", completed: 0 });
604
1563
  const result = (acknowledged, verified) => {
@@ -625,7 +1584,7 @@ async function preparePublicationInput(input, options, dependencies) {
625
1584
  read: parsed.read,
626
1585
  ...(parsed.report === undefined
627
1586
  ? {}
628
- : { report: structuredClone(parsed.report) }),
1587
+ : snapshotNormalizedFindings({ report: parsed.report })),
629
1588
  ready: parsed.findings.length,
630
1589
  excluded: parsed.excluded,
631
1590
  receipts: acknowledged,
@@ -636,22 +1595,55 @@ async function preparePublicationInput(input, options, dependencies) {
636
1595
  verified,
637
1596
  };
638
1597
  };
1598
+ function interruptedPreview(message, cause, savedSubmission) {
1599
+ // The expected file is gone or another publisher owns the lock. Use only
1600
+ // the acknowledgements this caller saw when it prepared the request.
1601
+ const known = submission.receipts ?? [];
1602
+ if (known.length > submission.requests.length)
1603
+ throw new CodexSecurityError("Saved publication has more receipts than requests.");
1604
+ const acknowledged = known.map((receipt, index) => validatePublicationReceipt(receipt, submission.requests[index]));
1605
+ return new ExternalPublicationError(message, {
1606
+ ...result(acknowledged, 0),
1607
+ status: "interrupted",
1608
+ error: message,
1609
+ ...(savedSubmission === undefined ? {} : { savedSubmission }),
1610
+ }, cause);
1611
+ }
639
1612
  let acquired = false;
640
- return await withImportLock(state, key, dependencies.signal, async () => {
1613
+ return await underLocks(lockKeys, async () => {
641
1614
  acquired = true;
642
1615
  // 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);
1616
+ const selected = await selectedCheckpoint(saved ? fileKey : undefined).catch((error) => {
1617
+ if (!submission.receipts?.length)
1618
+ throw error;
1619
+ throw interruptedPreview(errorMessage(error), error);
1620
+ });
1621
+ const pending = selected?.content;
1622
+ const current = pending ?? submission;
649
1623
  const adoptProjectMap = submission.inspectorProjectMapRequired &&
650
1624
  current.inspectorProjectMapRequired === undefined;
651
1625
  if (adoptProjectMap)
652
1626
  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.");
1627
+ if (!selected ||
1628
+ selected.fileKey !== fileKey ||
1629
+ selected.historical ||
1630
+ !sameSubmission(current, submission)) {
1631
+ const message = selected
1632
+ ? "Another publication prepared this input. Run the command again to review and resume that saved request."
1633
+ : "The saved publication changed after preview. Review its result before publishing again.";
1634
+ if (saved) {
1635
+ const completed = await readCompletedPublication(requests, fileKey);
1636
+ if (completed)
1637
+ return result(completed.receipts, completed.verified);
1638
+ throw interruptedPreview(message);
1639
+ }
1640
+ if (selected)
1641
+ throw new CodexSecurityError(message);
1642
+ if ((await readOptionalFile(privatePath)) !== undefined)
1643
+ throw new CodexSecurityError("A private import already owns this selection. Review its saved job before continuing.");
1644
+ if ((await readOptionalFile(pendingPath)) !== undefined)
1645
+ throw occupiedCheckpoint();
1646
+ }
655
1647
  const receipts = (current.receipts ?? []).map(validateImportReceipt);
656
1648
  if (receipts.length > requests.length)
657
1649
  throw new CodexSecurityError("Saved publication has more receipts than requests.");
@@ -659,10 +1651,17 @@ async function preparePublicationInput(input, options, dependencies) {
659
1651
  let verified = 0;
660
1652
  let persisted = pending !== undefined;
661
1653
  try {
1654
+ if (selected?.checkedCanonical)
1655
+ current.canonicalChecked = true;
662
1656
  if (pending === undefined || adoptProjectMap) {
663
1657
  await writeAtomicJson(pendingPath, current);
664
1658
  persisted = true;
665
1659
  }
1660
+ else if (selected?.checkedCanonical) {
1661
+ // The existing approval and receipts are usable if caching the scan fails.
1662
+ await writeAtomicJson(pendingPath, current).catch(() => undefined);
1663
+ }
1664
+ await rememberFile();
666
1665
  progress({
667
1666
  phase: "uploading",
668
1667
  completed: 0,
@@ -671,7 +1670,7 @@ async function preparePublicationInput(input, options, dependencies) {
671
1670
  let acknowledgedCount = 0;
672
1671
  for (const [batchIndex, batch] of requests.entries()) {
673
1672
  const receipt = validatePublicationReceipt(receipts[batchIndex] ??
674
- (await request("/finding_imports", batch)), batch);
1673
+ (await request(`${importPrefix}/finding_imports`, batch)), batch);
675
1674
  acknowledged.push(receipt);
676
1675
  acknowledgedCount += receipt.item_count;
677
1676
  progress({
@@ -685,6 +1684,7 @@ async function preparePublicationInput(input, options, dependencies) {
685
1684
  // Readback can still resume after this checkpoint without replaying writes.
686
1685
  await writeAtomicJson(pendingPath, {
687
1686
  ...submission,
1687
+ canonicalChecked: current.canonicalChecked,
688
1688
  receipts,
689
1689
  });
690
1690
  }
@@ -708,19 +1708,18 @@ async function preparePublicationInput(input, options, dependencies) {
708
1708
  total: readbacks.length,
709
1709
  });
710
1710
  await readInBatches(readbacks, async ({ item, expected, batch }) => {
711
- const report = validateSourceReport(await request(`${sourcePath(destination)}/${encodeURIComponent(item.source_report_id)}`));
1711
+ const report = validateSourceReport(await request(`${sourceReportsPath}/${encodeURIComponent(item.source_report_id)}`));
712
1712
  if (report.id !== item.source_report_id ||
713
1713
  report.canonical_finding_id !== item.canonical_finding_id ||
714
1714
  report.version < item.version ||
715
1715
  (report.version === item.version &&
716
1716
  (report.observation_id !== item.observation_id ||
717
- canonicalJson(report.evidence) !==
718
- canonicalJson(expected.evidence))) ||
1717
+ wireJson(report.evidence) !== wireJson(expected.evidence))) ||
719
1718
  report.source_finding_id !== expected.source_finding_id ||
720
1719
  report.repo_id !== destination.id ||
721
1720
  report.repo_connector_id !== destination.repo_connector_id ||
722
1721
  report.environment_id !== batch.repository.environment_id ||
723
- canonicalJson(report.source) !== canonicalJson(source))
1722
+ wireJson(report.source) !== wireJson(source))
724
1723
  throw new CodexSecurityError("Cloud readback did not match the saved finding identity.");
725
1724
  progress({
726
1725
  phase: "verifying",
@@ -733,12 +1732,12 @@ async function preparePublicationInput(input, options, dependencies) {
733
1732
  ...completed,
734
1733
  report: reportForJson(completed.report),
735
1734
  });
736
- await rm(pendingPath, { force: true });
1735
+ await removePending();
737
1736
  return completed;
738
1737
  }
739
1738
  catch (error) {
740
1739
  if (error instanceof CloudImportError && error.status === 409) {
741
- await rm(pendingPath, { force: true });
1740
+ await removePending();
742
1741
  persisted = false;
743
1742
  }
744
1743
  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."}`;
@@ -752,21 +1751,10 @@ async function preparePublicationInput(input, options, dependencies) {
752
1751
  }).catch((error) => {
753
1752
  if (acquired || !dependencies.signal?.aborted)
754
1753
  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
1754
  const message = saved
762
1755
  ? `Publication canceled while waiting. Repeat the same command to resume the saved request. Saved submission: ${pendingPath}`
763
1756
  : "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);
1757
+ throw interruptedPreview(message, error, saved ? pendingPath : undefined);
770
1758
  });
771
1759
  },
772
1760
  };