@openai/codex-security 0.8.1 → 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 (396) hide show
  1. package/README.md +45 -57
  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/THIRD_PARTY_NOTICES.txt +4 -4
  7. package/_bundled_plugin/mcp/native/darwin-arm64/unix.node +0 -0
  8. package/_bundled_plugin/mcp/native/darwin-x64/unix.node +0 -0
  9. package/_bundled_plugin/mcp/native/linux-arm64-gnu/unix.node +0 -0
  10. package/_bundled_plugin/mcp/native/linux-arm64-musl/unix.node +0 -0
  11. package/_bundled_plugin/mcp/native/linux-x64-gnu/unix.node +0 -0
  12. package/_bundled_plugin/mcp/native/linux-x64-musl/unix.node +0 -0
  13. package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
  14. package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
  15. package/_bundled_plugin/mcp/permission-profile-preflight.mjs +46 -12
  16. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  17. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  18. package/_bundled_plugin/mcp/server.mjs.br.part-002 +0 -0
  19. package/_bundled_plugin/references/artifact-storage.md +1 -1
  20. package/_bundled_plugin/references/core-scan.md +14 -5
  21. package/_bundled_plugin/references/default-severity-rubric.md +28 -0
  22. package/_bundled_plugin/references/finding-detail-fields.md +1 -0
  23. package/_bundled_plugin/references/threat-model.md +1 -0
  24. package/_bundled_plugin/scripts/deep_scan_workbench.py +23 -112
  25. package/_bundled_plugin/scripts/finalize_scan_contract.py +167 -153
  26. package/_bundled_plugin/scripts/finding_preview.py +5 -29
  27. package/_bundled_plugin/scripts/posix_scan_local_files.py +187 -0
  28. package/_bundled_plugin/scripts/report_projection.py +87 -136
  29. package/_bundled_plugin/scripts/validate_scan_contract.py +2 -6
  30. package/_bundled_plugin/scripts/windows_scan_local_files.py +212 -55
  31. package/_bundled_plugin/scripts/workbench/retained_coverage.py +8 -24
  32. package/_bundled_plugin/scripts/workbench/saved_coverage.py +143 -231
  33. package/_bundled_plugin/scripts/workbench_cli.py +102 -13
  34. package/_bundled_plugin/scripts/workbench_constants.py +9 -0
  35. package/_bundled_plugin/scripts/workbench_coverage_receipts.py +0 -17
  36. package/_bundled_plugin/scripts/workbench_db.py +187 -363
  37. package/_bundled_plugin/scripts/workbench_finding_workflows.py +86 -4
  38. package/_bundled_plugin/scripts/workbench_native_indexes.py +17 -21
  39. package/_bundled_plugin/scripts/workbench_remediation.py +495 -11
  40. package/_bundled_plugin/scripts/workbench_saved_results.py +0 -66
  41. package/_bundled_plugin/scripts/workbench_scan_history.py +22 -25
  42. package/_bundled_plugin/scripts/workbench_scan_start.py +1 -1
  43. package/_bundled_plugin/scripts/workbench_scan_usage.py +25 -52
  44. package/_bundled_plugin/scripts/workbench_schema.py +1 -1
  45. package/_bundled_plugin/scripts/workbench_setup.py +239 -0
  46. package/_bundled_plugin/scripts/workbench_source_scopes.py +15 -23
  47. package/_bundled_plugin/scripts/workbench_target.py +120 -35
  48. package/_bundled_plugin/scripts/workbench_validation.py +1 -9
  49. package/_bundled_plugin/shared/workbench-history-repairs.json +7 -0
  50. package/_bundled_plugin/shared/workbench-migrations.json +19 -0
  51. package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +1 -1
  52. package/_bundled_plugin/skills/attack-path-analysis/references/severity-policy.md +4 -68
  53. package/_bundled_plugin/skills/check-skills/SKILL.md +43 -0
  54. package/_bundled_plugin/skills/check-skills/agents/openai.yaml +4 -0
  55. package/_bundled_plugin/skills/cloud-findings/SKILL.md +2 -0
  56. package/_bundled_plugin/skills/fix-finding/SKILL.md +18 -1
  57. package/_bundled_plugin/skills/publish-findings/SKILL.md +68 -35
  58. package/_bundled_plugin/skills/publish-findings/references/wiz-import.md +2 -21
  59. package/_bundled_plugin/skills/resolve-dependency-alert/SKILL.md +18 -0
  60. package/_bundled_plugin/skills/resolve-dependency-alert/agents/openai.yaml +4 -0
  61. package/_bundled_plugin/skills/threat-model-visualize/SKILL.md +83 -0
  62. package/_bundled_plugin/skills/threat-model-visualize/agents/openai.yaml +4 -0
  63. package/_bundled_plugin/skills/threat-model-visualize/assets/context-reference-overview.svg +366 -0
  64. package/_bundled_plugin/skills/threat-model-visualize/assets/context-reference-path.svg +558 -0
  65. package/_bundled_plugin/skills/threat-model-visualize/assets/control-effects.js +85 -0
  66. package/_bundled_plugin/skills/threat-model-visualize/assets/explorer.css +17 -0
  67. package/_bundled_plugin/skills/threat-model-visualize/assets/explorer.html +38 -0
  68. package/_bundled_plugin/skills/threat-model-visualize/assets/explorer.js +372 -0
  69. package/_bundled_plugin/skills/threat-model-visualize/assets/route-paths.js +71 -0
  70. package/_bundled_plugin/skills/threat-model-visualize/assets/source-roles.js +47 -0
  71. package/_bundled_plugin/skills/threat-model-visualize/assets/svg-layout.js +149 -0
  72. package/_bundled_plugin/skills/threat-model-visualize/references/context-explorer.md +21 -0
  73. package/_bundled_plugin/skills/threat-model-visualize/references/view-model.md +49 -0
  74. package/_bundled_plugin/skills/threat-model-visualize/scripts/build_explorer.py +569 -0
  75. package/_bundled_plugin/skills/track-findings/SKILL.md +5 -3
  76. package/_bundled_plugin/skills/triage-finding/references/github-rest-intake.md +22 -68
  77. package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +6 -4
  78. package/dist/api.d.ts +6 -1
  79. package/dist/api.d.ts.map +1 -1
  80. package/dist/api.js +414 -163
  81. package/dist/api.js.map +1 -1
  82. package/dist/auth.js +7 -2
  83. package/dist/auth.js.map +1 -1
  84. package/dist/blackduck-findings.d.ts +8 -0
  85. package/dist/blackduck-findings.d.ts.map +1 -0
  86. package/dist/blackduck-findings.js +305 -0
  87. package/dist/blackduck-findings.js.map +1 -0
  88. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  89. package/dist/bulk-scan-discovery.js +51 -36
  90. package/dist/bulk-scan-discovery.js.map +1 -1
  91. package/dist/classify-severity-records.d.ts +5 -0
  92. package/dist/classify-severity-records.d.ts.map +1 -1
  93. package/dist/classify-severity-records.js +6 -8
  94. package/dist/classify-severity-records.js.map +1 -1
  95. package/dist/classify-severity.d.ts +4 -1
  96. package/dist/classify-severity.d.ts.map +1 -1
  97. package/dist/classify-severity.js +28 -9
  98. package/dist/classify-severity.js.map +1 -1
  99. package/dist/cli.d.ts +3 -0
  100. package/dist/cli.d.ts.map +1 -1
  101. package/dist/cli.js +635 -266
  102. package/dist/cli.js.map +1 -1
  103. package/dist/codex-prompt.d.ts +2 -0
  104. package/dist/codex-prompt.d.ts.map +1 -1
  105. package/dist/codex-prompt.js +6 -0
  106. package/dist/codex-prompt.js.map +1 -1
  107. package/dist/component-scan.d.ts.map +1 -1
  108. package/dist/component-scan.js +6 -5
  109. package/dist/component-scan.js.map +1 -1
  110. package/dist/config.d.ts +2 -1
  111. package/dist/config.d.ts.map +1 -1
  112. package/dist/config.js +162 -40
  113. package/dist/config.js.map +1 -1
  114. package/dist/continuum-findings.d.ts +7 -0
  115. package/dist/continuum-findings.d.ts.map +1 -0
  116. package/dist/continuum-findings.js +157 -0
  117. package/dist/continuum-findings.js.map +1 -0
  118. package/dist/contract-values.d.ts +28 -0
  119. package/dist/contract-values.d.ts.map +1 -0
  120. package/dist/contract-values.js +75 -0
  121. package/dist/contract-values.js.map +1 -0
  122. package/dist/contract.d.ts +2 -20
  123. package/dist/contract.d.ts.map +1 -1
  124. package/dist/contract.js +58 -669
  125. package/dist/contract.js.map +1 -1
  126. package/dist/cost-model.d.ts.map +1 -1
  127. package/dist/cost-model.js +1 -4
  128. package/dist/cost-model.js.map +1 -1
  129. package/dist/cost.d.ts +19 -1
  130. package/dist/cost.d.ts.map +1 -1
  131. package/dist/cost.js +2120 -39
  132. package/dist/cost.js.map +1 -1
  133. package/dist/custom-validation-prompt.d.ts +1 -1
  134. package/dist/custom-validation-prompt.d.ts.map +1 -1
  135. package/dist/custom-validation-prompt.js +5 -0
  136. package/dist/custom-validation-prompt.js.map +1 -1
  137. package/dist/custom-validation.d.ts +2 -0
  138. package/dist/custom-validation.d.ts.map +1 -1
  139. package/dist/custom-validation.js +6 -2
  140. package/dist/custom-validation.js.map +1 -1
  141. package/dist/deduplication/deduplication-prompts.d.ts +4 -4
  142. package/dist/deduplication/deduplication-prompts.d.ts.map +1 -1
  143. package/dist/deduplication/deduplication-prompts.js +2 -2
  144. package/dist/deduplication/deduplication-prompts.js.map +1 -1
  145. package/dist/deduplication/deduplication-reviewer.d.ts +14 -11
  146. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
  147. package/dist/deduplication/deduplication-reviewer.js +12 -5
  148. package/dist/deduplication/deduplication-reviewer.js.map +1 -1
  149. package/dist/deduplication/deduplication.js.map +1 -1
  150. package/dist/deduplication/finding-schema.d.ts +19 -1
  151. package/dist/deduplication/finding-schema.d.ts.map +1 -1
  152. package/dist/deduplication/finding-schema.js +25 -0
  153. package/dist/deduplication/finding-schema.js.map +1 -1
  154. package/dist/deduplication/local.d.ts.map +1 -1
  155. package/dist/deduplication/local.js +3 -8
  156. package/dist/deduplication/local.js.map +1 -1
  157. package/dist/deduplication/records.d.ts +11 -3
  158. package/dist/deduplication/records.d.ts.map +1 -1
  159. package/dist/deduplication/records.js +48 -16
  160. package/dist/deduplication/records.js.map +1 -1
  161. package/dist/deep-scan.d.ts +11 -0
  162. package/dist/deep-scan.d.ts.map +1 -1
  163. package/dist/deep-scan.js +235 -4
  164. package/dist/deep-scan.js.map +1 -1
  165. package/dist/default-severity-rubric.d.ts +1 -2
  166. package/dist/default-severity-rubric.d.ts.map +1 -1
  167. package/dist/default-severity-rubric.js +2 -66
  168. package/dist/default-severity-rubric.js.map +1 -1
  169. package/dist/endor-findings.d.ts.map +1 -1
  170. package/dist/endor-findings.js +44 -89
  171. package/dist/endor-findings.js.map +1 -1
  172. package/dist/errors.js +1 -1
  173. package/dist/errors.js.map +1 -1
  174. package/dist/external-findings-import.d.ts.map +1 -1
  175. package/dist/external-findings-import.js +11 -15
  176. package/dist/external-findings-import.js.map +1 -1
  177. package/dist/external-findings-local.d.ts +12 -1
  178. package/dist/external-findings-local.d.ts.map +1 -1
  179. package/dist/external-findings-local.js +411 -184
  180. package/dist/external-findings-local.js.map +1 -1
  181. package/dist/external-findings-publish.d.ts +15 -1
  182. package/dist/external-findings-publish.d.ts.map +1 -1
  183. package/dist/external-findings-publish.js +1238 -226
  184. package/dist/external-findings-publish.js.map +1 -1
  185. package/dist/external-findings-read.d.ts +10 -0
  186. package/dist/external-findings-read.d.ts.map +1 -1
  187. package/dist/external-findings-read.js +71 -51
  188. package/dist/external-findings-read.js.map +1 -1
  189. package/dist/external-findings.d.ts +13 -0
  190. package/dist/external-findings.d.ts.map +1 -1
  191. package/dist/external-findings.js +124 -1
  192. package/dist/external-findings.js.map +1 -1
  193. package/dist/external-import-contract.d.ts +29 -1
  194. package/dist/external-import-contract.d.ts.map +1 -1
  195. package/dist/external-import-contract.js +314 -44
  196. package/dist/external-import-contract.js.map +1 -1
  197. package/dist/external-import-models.d.ts +1 -1
  198. package/dist/external-import-models.d.ts.map +1 -1
  199. package/dist/external-import-models.js.map +1 -1
  200. package/dist/finding-normalization.d.ts +1 -1
  201. package/dist/finding-normalization.d.ts.map +1 -1
  202. package/dist/finding-normalization.js +46 -11
  203. package/dist/finding-normalization.js.map +1 -1
  204. package/dist/finding-retrieval.d.ts +3 -3
  205. package/dist/finding-retrieval.d.ts.map +1 -1
  206. package/dist/finding-retrieval.js.map +1 -1
  207. package/dist/finding-workflow.d.ts +6 -1
  208. package/dist/finding-workflow.d.ts.map +1 -1
  209. package/dist/finding-workflow.js +2 -2
  210. package/dist/finding-workflow.js.map +1 -1
  211. package/dist/git-remote.d.ts +4 -0
  212. package/dist/git-remote.d.ts.map +1 -1
  213. package/dist/git-remote.js +20 -0
  214. package/dist/git-remote.js.map +1 -1
  215. package/dist/github-advisories.d.ts +3 -12
  216. package/dist/github-advisories.d.ts.map +1 -1
  217. package/dist/github-advisories.js +9 -29
  218. package/dist/github-advisories.js.map +1 -1
  219. package/dist/github-dependabot-findings.d.ts.map +1 -1
  220. package/dist/github-dependabot-findings.js +7 -16
  221. package/dist/github-dependabot-findings.js.map +1 -1
  222. package/dist/github-findings.d.ts.map +1 -1
  223. package/dist/github-findings.js +344 -56
  224. package/dist/github-findings.js.map +1 -1
  225. package/dist/github.d.ts +17 -7
  226. package/dist/github.d.ts.map +1 -1
  227. package/dist/github.js +43 -30
  228. package/dist/github.js.map +1 -1
  229. package/dist/host-protocol.d.ts +7 -2
  230. package/dist/host-protocol.d.ts.map +1 -1
  231. package/dist/host-protocol.js +27 -3
  232. package/dist/host-protocol.js.map +1 -1
  233. package/dist/host-records-protocol.d.ts +10 -11
  234. package/dist/host-records-protocol.d.ts.map +1 -1
  235. package/dist/host-records-protocol.js +69 -9
  236. package/dist/host-records-protocol.js.map +1 -1
  237. package/dist/hosted-scan.d.ts +15 -0
  238. package/dist/hosted-scan.d.ts.map +1 -1
  239. package/dist/hosted-scan.js +55 -33
  240. package/dist/hosted-scan.js.map +1 -1
  241. package/dist/index.d.ts +8 -0
  242. package/dist/index.d.ts.map +1 -1
  243. package/dist/index.js +7 -0
  244. package/dist/index.js.map +1 -1
  245. package/dist/inspector-findings.d.ts +9 -0
  246. package/dist/inspector-findings.d.ts.map +1 -0
  247. package/dist/inspector-findings.js +512 -0
  248. package/dist/inspector-findings.js.map +1 -0
  249. package/dist/knowledge-base.js +18 -5
  250. package/dist/knowledge-base.js.map +1 -1
  251. package/dist/local-git-metadata.d.ts +5 -0
  252. package/dist/local-git-metadata.d.ts.map +1 -0
  253. package/dist/local-git-metadata.js +131 -0
  254. package/dist/local-git-metadata.js.map +1 -0
  255. package/dist/multiscan.d.ts.map +1 -1
  256. package/dist/multiscan.js +22 -12
  257. package/dist/multiscan.js.map +1 -1
  258. package/dist/opengrep-findings.d.ts.map +1 -1
  259. package/dist/opengrep-findings.js +34 -20
  260. package/dist/opengrep-findings.js.map +1 -1
  261. package/dist/patch-tui.d.ts +1 -1
  262. package/dist/patch-tui.d.ts.map +1 -1
  263. package/dist/patch-tui.js +94 -36
  264. package/dist/patch-tui.js.map +1 -1
  265. package/dist/private-finding-import.d.ts +246 -0
  266. package/dist/private-finding-import.d.ts.map +1 -0
  267. package/dist/private-finding-import.js +387 -0
  268. package/dist/private-finding-import.js.map +1 -0
  269. package/dist/project-config.d.ts.map +1 -1
  270. package/dist/project-config.js +2 -2
  271. package/dist/project-config.js.map +1 -1
  272. package/dist/provider-profile.d.ts +10 -2
  273. package/dist/provider-profile.d.ts.map +1 -1
  274. package/dist/provider-profile.js +72 -10
  275. package/dist/provider-profile.js.map +1 -1
  276. package/dist/publication.js +7 -1
  277. package/dist/publication.js.map +1 -1
  278. package/dist/reader-selection.d.ts +13 -0
  279. package/dist/reader-selection.d.ts.map +1 -0
  280. package/dist/reader-selection.js +81 -0
  281. package/dist/reader-selection.js.map +1 -0
  282. package/dist/record.d.ts +6 -1
  283. package/dist/record.d.ts.map +1 -1
  284. package/dist/record.js +53 -2
  285. package/dist/record.js.map +1 -1
  286. package/dist/remediation.d.ts +5 -0
  287. package/dist/remediation.d.ts.map +1 -1
  288. package/dist/remediation.js +30 -0
  289. package/dist/remediation.js.map +1 -1
  290. package/dist/result.d.ts +2 -0
  291. package/dist/result.d.ts.map +1 -1
  292. package/dist/result.js +3 -1
  293. package/dist/result.js.map +1 -1
  294. package/dist/runtime.d.ts +5 -3
  295. package/dist/runtime.d.ts.map +1 -1
  296. package/dist/runtime.js +110 -57
  297. package/dist/runtime.js.map +1 -1
  298. package/dist/scan-activity.js +40 -17
  299. package/dist/scan-activity.js.map +1 -1
  300. package/dist/scan-comparison.d.ts +1 -1
  301. package/dist/scan-comparison.d.ts.map +1 -1
  302. package/dist/scan-comparison.js +11 -13
  303. package/dist/scan-comparison.js.map +1 -1
  304. package/dist/scan-dashboard.d.ts +1 -0
  305. package/dist/scan-dashboard.d.ts.map +1 -1
  306. package/dist/scan-dashboard.js +37 -48
  307. package/dist/scan-dashboard.js.map +1 -1
  308. package/dist/scan-executor.d.ts +2 -0
  309. package/dist/scan-executor.d.ts.map +1 -1
  310. package/dist/scan-executor.js.map +1 -1
  311. package/dist/scan-history-renderer.d.ts.map +1 -1
  312. package/dist/scan-history-renderer.js +22 -9
  313. package/dist/scan-history-renderer.js.map +1 -1
  314. package/dist/scan-logs.d.ts.map +1 -1
  315. package/dist/scan-logs.js +5 -5
  316. package/dist/scan-logs.js.map +1 -1
  317. package/dist/scanner-import.d.ts +48 -3
  318. package/dist/scanner-import.d.ts.map +1 -1
  319. package/dist/scanner-import.js +214 -39
  320. package/dist/scanner-import.js.map +1 -1
  321. package/dist/scanner-repository-url.d.ts +14 -0
  322. package/dist/scanner-repository-url.d.ts.map +1 -0
  323. package/dist/scanner-repository-url.js +67 -0
  324. package/dist/scanner-repository-url.js.map +1 -0
  325. package/dist/semgrep-findings.d.ts.map +1 -1
  326. package/dist/semgrep-findings.js +50 -88
  327. package/dist/semgrep-findings.js.map +1 -1
  328. package/dist/semgrep-platform-findings.d.ts +12 -0
  329. package/dist/semgrep-platform-findings.d.ts.map +1 -0
  330. package/dist/semgrep-platform-findings.js +198 -0
  331. package/dist/semgrep-platform-findings.js.map +1 -0
  332. package/dist/severity-rubric.d.ts +20 -0
  333. package/dist/severity-rubric.d.ts.map +1 -0
  334. package/dist/severity-rubric.js +73 -0
  335. package/dist/severity-rubric.js.map +1 -0
  336. package/dist/severity-store.js +15 -5
  337. package/dist/severity-store.js.map +1 -1
  338. package/dist/snyk-findings.d.ts.map +1 -1
  339. package/dist/snyk-findings.js +97 -106
  340. package/dist/snyk-findings.js.map +1 -1
  341. package/dist/sonar-collector.d.ts +16 -0
  342. package/dist/sonar-collector.d.ts.map +1 -0
  343. package/dist/sonar-collector.js +383 -0
  344. package/dist/sonar-collector.js.map +1 -0
  345. package/dist/sonar-findings.d.ts +49 -0
  346. package/dist/sonar-findings.d.ts.map +1 -0
  347. package/dist/sonar-findings.js +288 -0
  348. package/dist/sonar-findings.js.map +1 -0
  349. package/dist/sonar-import.d.ts +111 -0
  350. package/dist/sonar-import.d.ts.map +1 -0
  351. package/dist/sonar-import.js +279 -0
  352. package/dist/sonar-import.js.map +1 -0
  353. package/dist/suggest-owners.d.ts.map +1 -1
  354. package/dist/suggest-owners.js +2 -1
  355. package/dist/suggest-owners.js.map +1 -1
  356. package/dist/targets.d.ts +3 -0
  357. package/dist/targets.d.ts.map +1 -1
  358. package/dist/targets.js +18 -12
  359. package/dist/targets.js.map +1 -1
  360. package/dist/terminal-columns.d.ts +4 -0
  361. package/dist/terminal-columns.d.ts.map +1 -0
  362. package/dist/terminal-columns.js +19 -0
  363. package/dist/terminal-columns.js.map +1 -0
  364. package/dist/validation-records.d.ts +0 -3
  365. package/dist/validation-records.d.ts.map +1 -1
  366. package/dist/validation-records.js +0 -16
  367. package/dist/validation-records.js.map +1 -1
  368. package/dist/veracode-findings.d.ts +7 -0
  369. package/dist/veracode-findings.d.ts.map +1 -0
  370. package/dist/veracode-findings.js +371 -0
  371. package/dist/veracode-findings.js.map +1 -0
  372. package/dist/windows-path.d.ts.map +1 -1
  373. package/dist/windows-path.js +1 -0
  374. package/dist/windows-path.js.map +1 -1
  375. package/dist/wiz-csv.d.ts +2 -2
  376. package/dist/wiz-csv.d.ts.map +1 -1
  377. package/dist/wiz-csv.js +1 -3
  378. package/dist/wiz-csv.js.map +1 -1
  379. package/dist/wiz-findings.d.ts +10 -2
  380. package/dist/wiz-findings.d.ts.map +1 -1
  381. package/dist/wiz-findings.js +88 -49
  382. package/dist/wiz-findings.js.map +1 -1
  383. package/docs/classify-severity-records.md +1 -1
  384. package/docs/cli.md +320 -481
  385. package/docs/dedupe-records.md +27 -4
  386. package/docs/findings-service.md +21 -8
  387. package/docs/hosted-scans.md +16 -1
  388. package/package.json +9 -10
  389. package/schemas/external-findings.schema.json +12 -1
  390. package/schemas/hosted-scan-v2.fixture.json +16 -1
  391. package/_bundled_plugin/scripts/validate_tracking_source.py +0 -82
  392. package/_bundled_plugin/scripts/workbench_local_dedupe.py +0 -136
  393. package/dist/deduplication/records-protocol.d.ts +0 -4
  394. package/dist/deduplication/records-protocol.d.ts.map +0 -1
  395. package/dist/deduplication/records-protocol.js +0 -35
  396. package/dist/deduplication/records-protocol.js.map +0 -1
@@ -4,18 +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";
18
- import { cloudRepositoryUrlKey, validateImportRequest, validateExternalEvidence, validateExternalReport, validateImportReceipt, validateRepositories, validateSourceReports, validateSourceReport, } from "./external-import-contract.js";
23
+ import { blackDuckReportRepositories, projectedBlackDuckRepositoryUrl, } from "./blackduck-findings.js";
24
+ import { continuumReportRepositories } from "./continuum-findings.js";
25
+ import { veracodeReportRepositories } from "./veracode-findings.js";
26
+ import { inspectorReportRepository } from "./inspector-findings.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";
19
30
  const MAX_REQUEST_BYTES = 5 * 1024 * 1024;
20
31
  /** Preserve accepted work in CLI JSON output when publication cannot finish. */
21
32
  export class ExternalPublicationError extends CodexSecurityError {
@@ -39,15 +50,50 @@ async function readInBatches(items, read) {
39
50
  }
40
51
  return output;
41
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
+ }
42
61
  function sameSubmission(left, right) {
43
62
  return (left.accountId === right.accountId &&
44
- canonicalJson(left.requests) === canonicalJson(right.requests));
63
+ left.inspectorProjectMapRequired === right.inspectorProjectMapRequired &&
64
+ wireJson(left.requests) === wireJson(right.requests));
65
+ }
66
+ function wireFinding({ source_finding_id, evidence, }) {
67
+ return { source_finding_id, evidence };
45
68
  }
46
69
  function submissionFindings(requests) {
47
- return requests.flatMap((batch) => batch.items.map(({ source_finding_id, evidence }) => ({
48
- source_finding_id,
49
- evidence,
50
- })));
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));
51
97
  }
52
98
  function sameFindingSelection(left, right) {
53
99
  return (left.length === right.length &&
@@ -64,10 +110,13 @@ function reportForJson(report) {
64
110
  const errors = report?.["errors"];
65
111
  if (!Array.isArray(errors))
66
112
  return report;
67
- 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);
68
114
  return { ...report, errors: JSON.parse(json) };
69
115
  }
70
- 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) {
71
120
  const temporary = `${path}.${randomUUID()}.tmp`;
72
121
  try {
73
122
  const file = await open(temporary, "wx", 0o600);
@@ -98,8 +147,8 @@ function validatePublicationReceipt(value, batch) {
98
147
  const receipt = validateImportReceipt(value);
99
148
  const batchItems = new Map(batch.items.map((item) => [item.client_id, item]));
100
149
  if (receipt.id !== batch.request_id ||
101
- canonicalJson(receipt.repository) !== canonicalJson(batch.repository) ||
102
- canonicalJson(receipt.source) !== canonicalJson(batch.source) ||
150
+ wireJson(receipt.repository) !== wireJson(batch.repository) ||
151
+ wireJson(receipt.source) !== wireJson(batch.source) ||
103
152
  receipt.item_count !== batch.items.length ||
104
153
  receipt.results.length !== batch.items.length ||
105
154
  new Set(receipt.results.map((item) => item.client_id)).size !==
@@ -124,120 +173,371 @@ function validatePublicationReceipt(value, batch) {
124
173
  result.version === null)
125
174
  throw new CodexSecurityError("Cloud returned an incomplete saved finding.");
126
175
  }
127
- if (canonicalJson(receipt.counts) !== canonicalJson(actual))
176
+ if (wireJson(receipt.counts) !== wireJson(actual))
128
177
  throw new CodexSecurityError("Cloud returned inconsistent publication counts.");
129
178
  return receipt;
130
179
  }
131
- async function withImportLock(state, key, signal, operation) {
180
+ async function withImportLocks(state, keys, signal, operation) {
132
181
  if (process.platform === "win32")
133
182
  await createWindowsOutputDirectory(state, true);
134
183
  else
135
184
  await mkdir(state, { recursive: true, mode: 0o700 });
136
185
  await validatePreparedOutputDir(state, undefined, true);
137
- const path = join(state, `${key}.lock.sqlite`);
138
- const metadata = await lstat(path).catch((error) => {
139
- if (error.code === "ENOENT")
140
- return undefined;
141
- throw error;
142
- });
143
- if (metadata && !metadata.isFile())
144
- throw new CodexSecurityError("The publication lock is not a regular file.");
145
186
  const require = createRequire(import.meta.url);
146
187
  const Database = (process.versions["bun"]
147
188
  ? require("bun:sqlite").Database
148
189
  : require("node:sqlite").DatabaseSync);
149
- const guard = new Database(path);
190
+ const guards = [];
150
191
  try {
151
- if (process.platform !== "win32")
152
- chmodSync(path, 0o600);
153
- 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
+ }
154
207
  for (;;) {
155
208
  signal?.throwIfAborted();
209
+ let acquired = 0;
156
210
  try {
157
- // Keep this inode across invocations. SQLite releases the transaction
158
- // on process exit, so a paused publisher cannot lose its lock to a lease.
159
- 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
+ }
160
216
  break;
161
217
  }
162
218
  catch (error) {
163
219
  const sqliteError = error;
164
220
  if (sqliteError.errcode !== 5 && sqliteError.code !== "SQLITE_BUSY")
165
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");
166
225
  await delay(50, undefined, { signal });
167
226
  }
168
227
  }
169
228
  return await operation();
170
229
  }
171
230
  finally {
172
- guard.close();
231
+ for (const guard of guards)
232
+ guard.close();
173
233
  }
174
234
  }
175
235
  export async function prepareExternalPublication(path, options, dependencies = {}) {
176
236
  requireExternalImports(dependencies.environment);
177
- const parsed = await readExternalFindings(path, options);
178
- const normalizedOptions = {
179
- ...options,
180
- provider: EXTERNAL_FINDINGS_PROVIDERS[options.provider],
181
- };
182
- return parsed.publication
183
- ? preparePublicationInput(parsed.publication, normalizedOptions, dependencies)
184
- : prepareNormalizedExternalPublication(parsed.findings, normalizedOptions, dependencies);
237
+ const settings = snapshotImportOptions(options);
238
+ const parsed = await readExternalFindings(path, settings);
239
+ return preparePublicationInput(parsed.publication ?? normalizedPublicationInput(parsed.findings), { ...settings, provider: EXTERNAL_FINDINGS_PROVIDERS[settings.provider] }, dependencies, path);
185
240
  }
186
241
  /** Prepare normalized provider evidence without reading a vendor export. */
187
242
  export async function prepareNormalizedExternalPublication(input, options, dependencies = {}) {
188
243
  requireExternalImports(dependencies.environment);
189
- // Validation adds Cloud defaults; leave the caller's objects unchanged.
244
+ const settings = snapshotImportOptions(options);
245
+ return preparePublicationInput(normalizedPublicationInput(input), settings, dependencies);
246
+ }
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;
190
333
  const { report, ...parsed } = input;
191
- const current = structuredClone(parsed);
192
- if (report !== undefined) {
193
- current.report = structuredClone(report);
194
- validateExternalReport(current.report);
195
- }
196
- // structuredClone preserves aliases. Detach every raw record before adding
197
- // typed defaults, including aliases shared with another finding's evidence.
198
- for (const finding of current.findings)
199
- if (finding.evidence.source_data !== undefined)
200
- finding.evidence.source_data = structuredClone(finding.evidence.source_data);
201
- const identities = new Set();
202
- 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) {
203
348
  const id = finding.source_finding_id;
204
349
  if (!id.trim() || id.includes("\0") || Buffer.byteLength(id) > 512)
205
350
  throw new CodexSecurityError("Source finding id must contain 1–512 UTF-8 bytes and no NUL characters.");
206
- if (identities.has(id))
351
+ if (ids.has(id))
207
352
  throw new CodexSecurityError(`Duplicate source finding id ${JSON.stringify(id)}.`);
208
- identities.add(id);
209
- finding.evidence = validateExternalEvidence(finding.evidence);
353
+ ids.add(id);
354
+ finding.evidence = validateEvidence(finding.evidence);
210
355
  }
211
- const identity = current.report === undefined
212
- ? current.findings
213
- : { findings: current.findings, report: reportForJson(current.report) };
214
- return preparePublicationInput({
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);
473
+ return {
215
474
  current,
216
475
  legacy: current,
217
476
  currentIdentity: identity,
218
477
  legacyIdentity: identity,
219
- }, options, dependencies);
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)),
488
+ };
220
489
  }
221
- async function preparePublicationInput(input, options, dependencies) {
222
- const progress = (event) => {
223
- try {
224
- dependencies.onProgress?.(event);
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;
225
514
  }
226
- catch {
227
- // Optional progress must not interrupt preparation or publication.
228
- }
229
- };
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));
230
522
  if (!options.sourceKey.trim() ||
231
523
  options.sourceKey.includes("\0") ||
232
524
  Buffer.byteLength(options.sourceKey) > 512) {
233
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.");
234
526
  }
235
527
  progress({ phase: "reading", completed: 0 });
236
- const { current: parsed, legacy: legacyParsed, currentIdentity, legacyIdentity, } = input;
237
- if (parsed.findings.length === 0) {
528
+ let { current: parsed, legacy: legacyParsed, currentIdentity, legacyIdentity, } = input;
529
+ function noFindings() {
238
530
  const diagnostics = reportForJson(parsed.report)?.["errors"];
239
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)}` : ""}`);
240
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();
241
541
  progress({ phase: "discovering", completed: 0 });
242
542
  const environment = dependencies.environment ?? process.env;
243
543
  requireExternalImports(environment);
@@ -246,6 +546,7 @@ async function preparePublicationInput(input, options, dependencies) {
246
546
  // Validate the configured return destination before authentication or uploads.
247
547
  cloudFindingsUrl(environment, options.repository);
248
548
  const credentials = await (dependencies.credentials ?? (() => readCloudCredentials(environment)))();
549
+ let privateProviders;
249
550
  async function request(endpoint, body) {
250
551
  dependencies.signal?.throwIfAborted();
251
552
  // Allow upload and response transport around the server's publication budget.
@@ -269,6 +570,11 @@ async function preparePublicationInput(input, options, dependencies) {
269
570
  const retryAfter = response.headers.get("Retry-After");
270
571
  throw new CloudImportError(response.status, `Cloud import failed (HTTP ${response.status}). ${payload?.error?.message ?? response.statusText}${retryAfter ? ` Retry-After: ${retryAfter}.` : ""}`);
271
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
+ }
272
578
  return response.json();
273
579
  }
274
580
  const destinations = [];
@@ -292,7 +598,9 @@ async function preparePublicationInput(input, options, dependencies) {
292
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.");
293
599
  const destination = destinations[0];
294
600
  const source = { provider: options.provider, source_key: options.sourceKey };
295
- 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);
296
604
  const checkpointIdentity = [
297
605
  // Keep existing production checkpoints readable; other deployments must
298
606
  // never share a saved request or receipt with the production default.
@@ -302,185 +610,851 @@ async function preparePublicationInput(input, options, dependencies) {
302
610
  destination.repo_connector_id,
303
611
  source,
304
612
  ];
305
- const inputDigest = hash("sha256", canonicalJson(currentIdentity));
306
- const legacyInputDigest = hash("sha256", canonicalJson(legacyIdentity));
307
- function validateSaved(serialized, legacy = false) {
308
- const content = JSON.parse(serialized);
309
- 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 ||
310
681
  !Array.isArray(content.requests) ||
311
682
  content.requests.length === 0)
312
683
  throw new Error("Saved import account or requests do not match.");
313
684
  content.requests = content.requests.map(validateImportRequest);
314
- const savedFindings = submissionFindings(content.requests);
315
- 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);
316
693
  const expected = legacy ? legacyParsed.findings : parsed.findings;
317
- const matches = content.inputProjection !== undefined
318
- ? sameFindingSelection(savedFindings, expected) &&
319
- content.inputProjection?.inputDigest ===
320
- (legacy ? legacyInputDigest : inputDigest) &&
321
- content.inputProjection?.savedEvidenceDigest ===
322
- hash("sha256", savedEvidence)
323
- : savedEvidence === canonicalJson(expected);
324
- 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)))
325
699
  throw new Error("Saved import evidence does not match the selected input.");
326
- for (const submission of content.requests) {
327
- if (submission.repository.id !== destination.id ||
328
- submission.repository.repo_connector_id !==
329
- destination.repo_connector_id ||
330
- canonicalJson(submission.source) !== canonicalJson(source))
331
- 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();
332
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
+ };
333
747
  return content;
334
748
  }
335
- const key = hash("sha256", canonicalJson([...checkpointIdentity, currentIdentity]));
336
- const pendingPath = join(state, `${key}.pending.json`);
337
- const historicalKey = hash("sha256", canonicalJson([...checkpointIdentity, legacyParsed.findings], (left, right) => left.localeCompare(right)));
338
- const legacyKeys = new Set([
339
- hash("sha256", canonicalJson([...checkpointIdentity, legacyIdentity])),
340
- historicalKey,
341
- ]);
342
- async function rejectUnidentifiedLegacyCheckpoint() {
343
- const entries = await readdir(state, { withFileTypes: true }).catch((error) => {
344
- if (error.code === "ENOENT")
345
- return [];
346
- throw error;
347
- });
348
- if (entries.length)
349
- await validatePreparedOutputDir(state, undefined, true);
350
- for (const entry of entries) {
351
- if (!entry.isFile() || !/^[a-f0-9]{64}\.pending\.json$/u.test(entry.name))
352
- continue;
353
- const candidateKey = entry.name.slice(0, -".pending.json".length);
354
- if (candidateKey === key || legacyKeys.has(candidateKey))
355
- continue;
356
- const serialized = await readOptionalFile(join(state, entry.name));
357
- if (serialized === undefined)
358
- continue;
359
- try {
360
- // Locale-dependent filenames predate input projections. Later formats
361
- // must remain isolated by their deployment-bound checkpoint keys.
362
- const content = JSON.parse(serialized);
363
- if (!content || content.inputProjection !== undefined)
364
- continue;
365
- validateSaved(serialized, true);
366
- }
367
- catch {
368
- continue;
369
- }
370
- // These older bodies contain no deployment identity. Do not guess a
371
- // destination or replace their immutable request with a fresh publication.
372
- 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");
373
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);
374
776
  }
375
777
  let recovered;
376
778
  for (const legacyKey of legacyKeys) {
377
- if (legacyKey === key)
779
+ if (legacyKey === key || legacyKey === originalKey)
378
780
  continue;
379
781
  const legacyPath = join(state, `${legacyKey}.pending.json`);
380
782
  const captured = await readOptionalFile(legacyPath);
381
783
  if (captured !== undefined) {
382
784
  // Preserve requests staged by the earlier locale-based filename format.
383
785
  // Hold both publication locks while moving the original checkpoint.
384
- await withImportLock(state, key, dependencies.signal, async () => withImportLock(state, legacyKey, dependencies.signal, async () => {
385
- const current = await readOptionalFile(pendingPath);
786
+ await underLocks([key, legacyKey], async () => {
787
+ const current = await readOptionalFile(canonicalPath);
386
788
  const serialized = await readOptionalFile(legacyPath);
387
- if (current !== undefined) {
388
- recovered = validateSaved(current);
389
- if (serialized !== undefined) {
390
- // A crash may leave both copies after installing the canonical file.
391
- // Retire only an identical immutable submission, never another request.
392
- let duplicate = null;
393
- try {
394
- duplicate = JSON.parse(serialized);
395
- }
396
- catch (error) {
397
- if (!(error instanceof SyntaxError))
398
- throw error;
399
- }
400
- if (duplicate !== null &&
401
- typeof duplicate === "object" &&
402
- sameSubmission(duplicate, recovered))
403
- await rm(legacyPath);
789
+ let prior;
790
+ if (serialized !== undefined) {
791
+ try {
792
+ prior = JSON.parse(serialized);
404
793
  }
794
+ catch (error) {
795
+ if (current === undefined || !(error instanceof SyntaxError))
796
+ throw error;
797
+ }
798
+ }
799
+ if (prior?.wireKey !== undefined)
800
+ return;
801
+ if (current !== undefined) {
802
+ recovered = validateSaved(JSON.parse(current));
803
+ if (prior && sameSubmission(prior, recovered))
804
+ await rm(legacyPath);
405
805
  return;
406
806
  }
407
- // A competing resumer may have completed and removed both paths while
408
- // this caller waited. Retain its observed immutable request and receipts.
409
- const content = validateSaved(serialized ?? captured, true);
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;
829
+ }
830
+ const content = validateSaved(serialized === undefined ? JSON.parse(captured) : prior, true);
410
831
  if (!sameFindingSelection(legacyParsed.findings, parsed.findings))
411
832
  throw new CodexSecurityError("The saved import selects different findings from this input. Resume the original selection before publishing this selection.");
412
- if (content.requests.some((submission) => submission.repository.reset_marker !== destination.reset_marker)) {
833
+ await retireIfReset(content, async () => {
413
834
  if (serialized !== undefined)
414
835
  await rm(legacyPath);
415
- 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.");
416
- }
836
+ });
417
837
  const savedEvidenceDigest = content.inputProjection?.savedEvidenceDigest ??
418
- hash("sha256", canonicalJson(legacyParsed.findings));
838
+ jsonDigest(legacyParsed.findings);
419
839
  const rebindProjection = content.inputProjection !== undefined ||
420
840
  savedEvidenceDigest !== inputDigest;
421
- if (rebindProjection)
422
- 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
+ }
423
851
  recovered = content;
424
852
  if (serialized === undefined)
425
853
  return;
426
854
  if (!rebindProjection) {
427
- await rename(legacyPath, pendingPath);
855
+ await rename(legacyPath, canonicalPath);
428
856
  }
429
857
  else {
430
858
  // Bind the new input projection to the original immutable evidence,
431
859
  // so later inventory reordering still resumes these exact requests.
432
- await writeAtomicJson(pendingPath, content);
860
+ await writeAtomicJson(canonicalPath, content);
433
861
  await rm(legacyPath);
434
862
  }
435
- }));
863
+ });
436
864
  }
437
865
  }
438
- const serialized = await readOptionalFile(pendingPath);
439
- const saved = serialized === undefined ? recovered : validateSaved(serialized);
440
- if (!saved)
441
- await rejectUnidentifiedLegacyCheckpoint();
442
- if (saved) {
443
- const content = saved;
444
- for (const submission of content.requests) {
445
- if (submission.repository.reset_marker !== destination.reset_marker) {
446
- // The old request is never sent after reset. A subsequent explicit
447
- // invocation prepares a fresh submission and asks for approval again.
448
- await withImportLock(state, key, dependencies.signal, async () => {
449
- const current = await readOptionalFile(pendingPath);
450
- if (current !== undefined &&
451
- sameSubmission(JSON.parse(current), content))
452
- 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);
453
1100
  });
454
- 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.");
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);
455
1116
  }
456
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);
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;
457
1148
  }
458
- let requests = saved?.requests;
459
- if (!requests) {
460
- for (const finding of parsed.findings) {
461
- const sourceRepositories = [
462
- finding.evidence.details?.repository?.url,
463
- ...(options.provider === "wiz"
464
- ? [wizCsvRepositoryUrl(finding.evidence)]
465
- : []),
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
+ }));
1214
+ }
1215
+ if (saved?.inspectorProjectMapRequired && !inspectorProjectMap)
1216
+ throw new CodexSecurityError("Supply the verified Inspector project binding again to resume.");
1217
+ function checkRepositories(findings, report) {
1218
+ function requireDestination(id, url) {
1219
+ if (url &&
1220
+ cloudRepositoryUrlKey(url) !== cloudRepositoryUrlKey(destination.url))
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.`);
1222
+ }
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
+ }
1249
+ for (const finding of findings) {
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;
1261
+ const urls = [
1262
+ projected,
1263
+ evidence.details?.repository?.url,
1264
+ options.provider === "wiz"
1265
+ ? projected
1266
+ ? wizFindingRepository(evidence, "cloud")?.url
1267
+ : wizCsvRepositoryUrl(evidence)
1268
+ : undefined,
466
1269
  ...(options.provider === "snyk"
467
- ? snykRepositoryUrls(finding.evidence.source_data, {
1270
+ ? snykRepositoryUrls(evidence.source_data, {
468
1271
  cloudRepositoryUrl: destination.url,
469
1272
  })
470
1273
  : []),
471
1274
  ...(options.provider === "endor"
472
- ? endorRepositoryUrls(finding.evidence.source_data)
1275
+ ? endorRepositoryUrls(evidence.source_data)
473
1276
  : []),
474
1277
  options.provider === "github"
475
1278
  ? dependabotRepository(finding)
476
1279
  : undefined,
477
1280
  ];
478
- for (const sourceRepository of sourceRepositories)
479
- if (sourceRepository &&
480
- cloudRepositoryUrlKey(sourceRepository) !==
481
- cloudRepositoryUrlKey(destination.url))
482
- throw new CodexSecurityError(`Finding ${JSON.stringify(finding.source_finding_id)} belongs to ${JSON.stringify(sourceRepository)}, which does not match the selected Cloud repository ${JSON.stringify(destination.url)}. Verify the source repository mapping before publishing.`);
1281
+ for (const url of urls)
1282
+ requireDestination(id, url);
1283
+ }
1284
+ }
1285
+ checkRepositories(parsed.findings, parsed.report);
1286
+ // A retry sends its saved projection, which may differ from today's reader.
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);
483
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
+ }
1456
+ let requests = saved?.requests;
1457
+ if (!requests) {
484
1458
  let preparedCount = 0;
485
1459
  progress({
486
1460
  phase: "preparing",
@@ -494,7 +1468,7 @@ async function preparePublicationInput(input, options, dependencies) {
494
1468
  source_finding_id: finding.source_finding_id,
495
1469
  limit: "2",
496
1470
  });
497
- const summaries = validateSourceReports(await request(`${sourcePath(destination)}?${query}`));
1471
+ const summaries = validateSourceReports(await request(`${sourceReportsPath}?${query}`));
498
1472
  if (summaries.has_more || summaries.data.length > 1)
499
1473
  throw new CodexSecurityError("Cloud returned more than one report for a source identity.");
500
1474
  const previous = summaries.data[0];
@@ -561,11 +1535,16 @@ async function preparePublicationInput(input, options, dependencies) {
561
1535
  accountId: credentials.account_id,
562
1536
  requests,
563
1537
  receipts: saved?.receipts,
1538
+ legacyApproval: saved?.legacyApproval,
1539
+ wireKey: saved?.wireKey ?? (fileKey === key ? undefined : key),
564
1540
  inputProjection: saved?.inputProjection ?? {
1541
+ identityFormat: saved ? undefined : 2,
565
1542
  inputDigest,
566
- savedEvidenceDigest: hash("sha256", canonicalJson(submissionFindings(requests))),
1543
+ savedEvidenceDigest: jsonDigest(submissionFindings(requests)),
567
1544
  },
568
1545
  };
1546
+ if (inspectorProjectMap)
1547
+ submission.inspectorProjectMapRequired = true;
569
1548
  const preview = {
570
1549
  ...parsed,
571
1550
  findings: submissionFindings(requests),
@@ -578,7 +1557,7 @@ async function preparePublicationInput(input, options, dependencies) {
578
1557
  requests,
579
1558
  };
580
1559
  return {
581
- preview: structuredClone(preview),
1560
+ preview: cloneImportPreview(preview),
582
1561
  async publish() {
583
1562
  progress({ phase: "waiting", completed: 0 });
584
1563
  const result = (acknowledged, verified) => {
@@ -605,7 +1584,7 @@ async function preparePublicationInput(input, options, dependencies) {
605
1584
  read: parsed.read,
606
1585
  ...(parsed.report === undefined
607
1586
  ? {}
608
- : { report: structuredClone(parsed.report) }),
1587
+ : snapshotNormalizedFindings({ report: parsed.report })),
609
1588
  ready: parsed.findings.length,
610
1589
  excluded: parsed.excluded,
611
1590
  receipts: acknowledged,
@@ -616,18 +1595,55 @@ async function preparePublicationInput(input, options, dependencies) {
616
1595
  verified,
617
1596
  };
618
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
+ }
619
1612
  let acquired = false;
620
- return await withImportLock(state, key, dependencies.signal, async () => {
1613
+ return await underLocks(lockKeys, async () => {
621
1614
  acquired = true;
622
1615
  // Re-read under the lock to keep checkpoints from another publisher.
623
- const pending = await readOptionalFile(pendingPath);
624
- if (pending === undefined && !saved)
625
- await rejectUnidentifiedLegacyCheckpoint();
626
- const current = pending === undefined
627
- ? submission
628
- : JSON.parse(pending);
629
- if (!sameSubmission(current, submission))
630
- throw new CodexSecurityError("Another publication prepared this input. Run the command again to review and resume that saved request.");
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;
1623
+ const adoptProjectMap = submission.inspectorProjectMapRequired &&
1624
+ current.inspectorProjectMapRequired === undefined;
1625
+ if (adoptProjectMap)
1626
+ current.inspectorProjectMapRequired = true;
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
+ }
631
1647
  const receipts = (current.receipts ?? []).map(validateImportReceipt);
632
1648
  if (receipts.length > requests.length)
633
1649
  throw new CodexSecurityError("Saved publication has more receipts than requests.");
@@ -635,10 +1651,17 @@ async function preparePublicationInput(input, options, dependencies) {
635
1651
  let verified = 0;
636
1652
  let persisted = pending !== undefined;
637
1653
  try {
638
- if (pending === undefined) {
639
- await writeAtomicJson(pendingPath, submission);
1654
+ if (selected?.checkedCanonical)
1655
+ current.canonicalChecked = true;
1656
+ if (pending === undefined || adoptProjectMap) {
1657
+ await writeAtomicJson(pendingPath, current);
640
1658
  persisted = true;
641
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();
642
1665
  progress({
643
1666
  phase: "uploading",
644
1667
  completed: 0,
@@ -647,7 +1670,7 @@ async function preparePublicationInput(input, options, dependencies) {
647
1670
  let acknowledgedCount = 0;
648
1671
  for (const [batchIndex, batch] of requests.entries()) {
649
1672
  const receipt = validatePublicationReceipt(receipts[batchIndex] ??
650
- (await request("/finding_imports", batch)), batch);
1673
+ (await request(`${importPrefix}/finding_imports`, batch)), batch);
651
1674
  acknowledged.push(receipt);
652
1675
  acknowledgedCount += receipt.item_count;
653
1676
  progress({
@@ -661,6 +1684,7 @@ async function preparePublicationInput(input, options, dependencies) {
661
1684
  // Readback can still resume after this checkpoint without replaying writes.
662
1685
  await writeAtomicJson(pendingPath, {
663
1686
  ...submission,
1687
+ canonicalChecked: current.canonicalChecked,
664
1688
  receipts,
665
1689
  });
666
1690
  }
@@ -684,19 +1708,18 @@ async function preparePublicationInput(input, options, dependencies) {
684
1708
  total: readbacks.length,
685
1709
  });
686
1710
  await readInBatches(readbacks, async ({ item, expected, batch }) => {
687
- const report = validateSourceReport(await request(`${sourcePath(destination)}/${encodeURIComponent(item.source_report_id)}`));
1711
+ const report = validateSourceReport(await request(`${sourceReportsPath}/${encodeURIComponent(item.source_report_id)}`));
688
1712
  if (report.id !== item.source_report_id ||
689
1713
  report.canonical_finding_id !== item.canonical_finding_id ||
690
1714
  report.version < item.version ||
691
1715
  (report.version === item.version &&
692
1716
  (report.observation_id !== item.observation_id ||
693
- canonicalJson(report.evidence) !==
694
- canonicalJson(expected.evidence))) ||
1717
+ wireJson(report.evidence) !== wireJson(expected.evidence))) ||
695
1718
  report.source_finding_id !== expected.source_finding_id ||
696
1719
  report.repo_id !== destination.id ||
697
1720
  report.repo_connector_id !== destination.repo_connector_id ||
698
1721
  report.environment_id !== batch.repository.environment_id ||
699
- canonicalJson(report.source) !== canonicalJson(source))
1722
+ wireJson(report.source) !== wireJson(source))
700
1723
  throw new CodexSecurityError("Cloud readback did not match the saved finding identity.");
701
1724
  progress({
702
1725
  phase: "verifying",
@@ -709,12 +1732,12 @@ async function preparePublicationInput(input, options, dependencies) {
709
1732
  ...completed,
710
1733
  report: reportForJson(completed.report),
711
1734
  });
712
- await rm(pendingPath, { force: true });
1735
+ await removePending();
713
1736
  return completed;
714
1737
  }
715
1738
  catch (error) {
716
1739
  if (error instanceof CloudImportError && error.status === 409) {
717
- await rm(pendingPath, { force: true });
1740
+ await removePending();
718
1741
  persisted = false;
719
1742
  }
720
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."}`;
@@ -728,21 +1751,10 @@ async function preparePublicationInput(input, options, dependencies) {
728
1751
  }).catch((error) => {
729
1752
  if (acquired || !dependencies.signal?.aborted)
730
1753
  throw error;
731
- // The active publisher owns the checkpoint. Report only acknowledgements
732
- // already observed during preparation, without rewriting its files.
733
- const known = submission.receipts ?? [];
734
- if (known.length > requests.length)
735
- throw new CodexSecurityError("Saved publication has more receipts than requests.");
736
- const acknowledged = known.map((receipt, index) => validatePublicationReceipt(receipt, requests[index]));
737
1754
  const message = saved
738
1755
  ? `Publication canceled while waiting. Repeat the same command to resume the saved request. Saved submission: ${pendingPath}`
739
1756
  : "Publication canceled while waiting. No resumable request was saved by this invocation.";
740
- throw new ExternalPublicationError(message, {
741
- ...result(acknowledged, 0),
742
- status: "interrupted",
743
- error: message,
744
- ...(saved ? { savedSubmission: pendingPath } : {}),
745
- }, error);
1757
+ throw interruptedPreview(message, error, saved ? pendingPath : undefined);
746
1758
  });
747
1759
  },
748
1760
  };