@openai/codex-security 0.8.3 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (658) hide show
  1. package/README.md +36 -746
  2. package/_bundled_plugin/.codex-plugin/plugin.json +2 -0
  3. package/_bundled_plugin/.mcp.json +7 -1
  4. package/_bundled_plugin/examples/completed-scan/findings.json +16 -4
  5. package/_bundled_plugin/examples/completed-scan/report.md +11 -8
  6. package/_bundled_plugin/examples/completed-scan/scan-manifest.json +4 -2
  7. package/_bundled_plugin/mcp/fzstd-LICENSE.txt +21 -0
  8. package/_bundled_plugin/mcp/helpers.mjs +2 -11
  9. package/_bundled_plugin/mcp/helpers.mjs.br +0 -0
  10. package/_bundled_plugin/mcp/native/THIRD_PARTY_NOTICES.txt +3 -210
  11. package/_bundled_plugin/mcp/native/darwin-arm64/unix.node +0 -0
  12. package/_bundled_plugin/mcp/native/darwin-x64/unix.node +0 -0
  13. package/_bundled_plugin/mcp/native/linux-arm64-gnu/unix.node +0 -0
  14. package/_bundled_plugin/mcp/native/linux-arm64-musl/unix.node +0 -0
  15. package/_bundled_plugin/mcp/native/linux-x64-gnu/unix.node +0 -0
  16. package/_bundled_plugin/mcp/native/linux-x64-musl/unix.node +0 -0
  17. package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
  18. package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
  19. package/_bundled_plugin/mcp/package.json +1 -0
  20. package/_bundled_plugin/mcp/permission-profile-preflight.mjs +5 -2
  21. package/_bundled_plugin/mcp/server.mjs +2 -11
  22. package/_bundled_plugin/mcp/server.mjs.br +0 -0
  23. package/_bundled_plugin/mcp/session-decompression.mjs +712 -0
  24. package/_bundled_plugin/references/artifact-storage.md +1 -1
  25. package/_bundled_plugin/references/baseline-auditor.md +18 -0
  26. package/_bundled_plugin/references/core-scan.md +5 -42
  27. package/_bundled_plugin/references/default-severity-rubric.md +9 -4
  28. package/_bundled_plugin/references/final-report.md +5 -3
  29. package/_bundled_plugin/references/finding-detail-fields.md +18 -18
  30. package/_bundled_plugin/references/focused-investigator.md +13 -0
  31. package/_bundled_plugin/references/sarif-adapter.md +8 -2
  32. package/_bundled_plugin/references/scan-artifacts.md +21 -12
  33. package/_bundled_plugin/references/scan-contract.md +27 -33
  34. package/_bundled_plugin/references/scan-prologue.md +2 -2
  35. package/_bundled_plugin/references/scan-runtime.md +4 -2
  36. package/_bundled_plugin/references/security-guidance.md +14 -1
  37. package/_bundled_plugin/references/validate-scan-findings.md +3 -0
  38. package/_bundled_plugin/schemas/tools/scan-draft.schema.json +2 -2
  39. package/_bundled_plugin/scripts/check_deep_scan_target.py +97 -0
  40. package/_bundled_plugin/scripts/codex_profile.mjs +7 -1
  41. package/_bundled_plugin/scripts/deep_scan_config.py +20 -1
  42. package/_bundled_plugin/scripts/deep_scan_workbench.py +192 -352
  43. package/_bundled_plugin/scripts/dispatch_inventory_tool +7 -0
  44. package/_bundled_plugin/scripts/filesystem_identity.py +1 -0
  45. package/_bundled_plugin/scripts/finalize_scan_contract.py +182 -230
  46. package/_bundled_plugin/scripts/finding_preview.py +20 -0
  47. package/_bundled_plugin/scripts/launch_codex_security_mcp +55 -16
  48. package/_bundled_plugin/scripts/legacy_findings.py +148 -0
  49. package/_bundled_plugin/scripts/posix_scan_local_files.py +37 -1
  50. package/_bundled_plugin/scripts/read_codex_session.mjs +21 -0
  51. package/_bundled_plugin/scripts/report_projection.py +81 -43
  52. package/_bundled_plugin/scripts/review_state.py +8 -2
  53. package/_bundled_plugin/scripts/servicenow_avit.py +20 -0
  54. package/_bundled_plugin/scripts/snapshot_sqlite.py +9 -2
  55. package/_bundled_plugin/scripts/threat_model_projection.py +22 -26
  56. package/_bundled_plugin/scripts/validate_scan_contract.py +7 -1
  57. package/_bundled_plugin/scripts/windows_scan_local_files.py +582 -20
  58. package/_bundled_plugin/scripts/workbench/__init__.py +1 -1
  59. package/_bundled_plugin/scripts/workbench/handoff.py +100 -23
  60. package/_bundled_plugin/scripts/workbench/retained_coverage.py +30 -51
  61. package/_bundled_plugin/scripts/workbench/retained_records.py +18 -30
  62. package/_bundled_plugin/scripts/workbench/runtime_toml.py +4 -1
  63. package/_bundled_plugin/scripts/workbench/saved_coverage.py +9 -17
  64. package/_bundled_plugin/scripts/workbench/saved_outputs.py +9 -0
  65. package/_bundled_plugin/scripts/workbench/saved_sources.py +47 -40
  66. package/_bundled_plugin/scripts/workbench/storage.py +91 -7
  67. package/_bundled_plugin/scripts/workbench_cli.py +54 -14
  68. package/_bundled_plugin/scripts/workbench_constants.py +12 -0
  69. package/_bundled_plugin/scripts/workbench_coverage_receipts.py +3 -3
  70. package/_bundled_plugin/scripts/workbench_db.py +562 -303
  71. package/_bundled_plugin/scripts/workbench_feedback.py +57 -11
  72. package/_bundled_plugin/scripts/workbench_finding_index.py +1 -0
  73. package/_bundled_plugin/scripts/workbench_finding_workflows.py +4 -0
  74. package/_bundled_plugin/scripts/workbench_native_indexes.py +238 -41
  75. package/_bundled_plugin/scripts/workbench_progress.py +169 -4
  76. package/_bundled_plugin/scripts/workbench_publication.py +16 -13
  77. package/_bundled_plugin/scripts/workbench_remediation.py +155 -2
  78. package/_bundled_plugin/scripts/workbench_saved_results.py +176 -81
  79. package/_bundled_plugin/scripts/workbench_scan_history.py +398 -174
  80. package/_bundled_plugin/scripts/workbench_scan_start.py +224 -31
  81. package/_bundled_plugin/scripts/workbench_scan_usage.py +675 -73
  82. package/_bundled_plugin/scripts/workbench_schema.py +258 -63
  83. package/_bundled_plugin/scripts/workbench_setup.py +14 -2
  84. package/_bundled_plugin/scripts/workbench_source_excerpt.py +13 -0
  85. package/_bundled_plugin/scripts/workbench_source_scopes.py +18 -0
  86. package/_bundled_plugin/scripts/workbench_target.py +594 -136
  87. package/_bundled_plugin/scripts/workbench_target_state.py +1110 -21
  88. package/_bundled_plugin/scripts/workbench_validation.py +43 -3
  89. package/_bundled_plugin/shared/workbench-history-repairs.json +77 -35
  90. package/_bundled_plugin/shared/workbench-migrations.json +100 -0
  91. package/_bundled_plugin/skills/cloud-findings/SKILL.md +5 -5
  92. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +7 -50
  93. package/_bundled_plugin/skills/fix-finding/SKILL.md +2 -1
  94. package/_bundled_plugin/skills/publish-findings/SKILL.md +24 -153
  95. package/_bundled_plugin/skills/publish-findings/agents/openai.yaml +2 -2
  96. package/_bundled_plugin/skills/publish-findings/references/checkmarx-import.md +37 -0
  97. package/_bundled_plugin/skills/publish-findings/references/wiz-import.md +3 -3
  98. package/_bundled_plugin/skills/publish-findings/scripts/collect_checkmarx.py +409 -0
  99. package/_bundled_plugin/skills/publish-findings/scripts/collect_continuum.py +224 -0
  100. package/_bundled_plugin/skills/publish-findings/scripts/collector_paths.py +63 -0
  101. package/_bundled_plugin/skills/security-scan/SKILL.md +9 -46
  102. package/_bundled_plugin/skills/security-scan/agents/openai.yaml +2 -2
  103. package/_bundled_plugin/skills/security-scan/references/desktop-scan.md +3 -21
  104. package/_bundled_plugin/skills/security-scan/references/managed-scan.md +11 -0
  105. package/_bundled_plugin/skills/security-scan/references/scan-results.md +15 -0
  106. package/_bundled_plugin/skills/security-scan/references/scoped-paths.md +23 -0
  107. package/_bundled_plugin/skills/security-scan/references/sdk-files.md +24 -0
  108. package/_bundled_plugin/skills/threat-model-visualize/scripts/build_explorer.py +22 -1
  109. package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +6 -2
  110. package/bin/codex-security.mjs +2 -2
  111. package/dist/api.d.ts +45 -5
  112. package/dist/api.d.ts.map +1 -1
  113. package/dist/api.js +1192 -427
  114. package/dist/api.js.map +1 -1
  115. package/dist/armorcode-findings.d.ts +9 -0
  116. package/dist/armorcode-findings.d.ts.map +1 -0
  117. package/dist/armorcode-findings.js +145 -0
  118. package/dist/armorcode-findings.js.map +1 -0
  119. package/dist/artifact-export.d.ts +2 -0
  120. package/dist/artifact-export.d.ts.map +1 -1
  121. package/dist/artifact-export.js +29 -13
  122. package/dist/artifact-export.js.map +1 -1
  123. package/dist/auth.d.ts +6 -2
  124. package/dist/auth.d.ts.map +1 -1
  125. package/dist/auth.js +37 -18
  126. package/dist/auth.js.map +1 -1
  127. package/dist/batch-cost.d.ts +18 -0
  128. package/dist/batch-cost.d.ts.map +1 -0
  129. package/dist/batch-cost.js +41 -0
  130. package/dist/batch-cost.js.map +1 -0
  131. package/dist/bedrock-oss-pricing.d.ts +6 -0
  132. package/dist/bedrock-oss-pricing.d.ts.map +1 -0
  133. package/dist/bedrock-oss-pricing.js +58 -0
  134. package/dist/bedrock-oss-pricing.js.map +1 -0
  135. package/dist/bedrock-pricing.d.ts +15 -0
  136. package/dist/bedrock-pricing.d.ts.map +1 -0
  137. package/dist/bedrock-pricing.js +117 -0
  138. package/dist/bedrock-pricing.js.map +1 -0
  139. package/dist/blackduck-findings.d.ts +1 -1
  140. package/dist/blackduck-findings.d.ts.map +1 -1
  141. package/dist/blackduck-findings.js +24 -22
  142. package/dist/blackduck-findings.js.map +1 -1
  143. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  144. package/dist/bulk-scan-discovery.js +4 -4
  145. package/dist/bulk-scan-discovery.js.map +1 -1
  146. package/dist/bundled-plugin.d.ts +4 -0
  147. package/dist/bundled-plugin.d.ts.map +1 -0
  148. package/dist/bundled-plugin.js +23 -0
  149. package/dist/bundled-plugin.js.map +1 -0
  150. package/dist/checkmarx-evidence.d.ts +19 -0
  151. package/dist/checkmarx-evidence.d.ts.map +1 -0
  152. package/dist/checkmarx-evidence.js +36 -0
  153. package/dist/checkmarx-evidence.js.map +1 -0
  154. package/dist/checkmarx-findings.d.ts +4 -0
  155. package/dist/checkmarx-findings.d.ts.map +1 -0
  156. package/dist/checkmarx-findings.js +27 -0
  157. package/dist/checkmarx-findings.js.map +1 -0
  158. package/dist/checkmarx-one.d.ts +4 -0
  159. package/dist/checkmarx-one.d.ts.map +1 -0
  160. package/dist/checkmarx-one.js +53 -0
  161. package/dist/checkmarx-one.js.map +1 -0
  162. package/dist/checkmarx-repository.d.ts +12 -0
  163. package/dist/checkmarx-repository.d.ts.map +1 -0
  164. package/dist/checkmarx-repository.js +181 -0
  165. package/dist/checkmarx-repository.js.map +1 -0
  166. package/dist/checkmarx-xml.d.ts +12 -0
  167. package/dist/checkmarx-xml.d.ts.map +1 -0
  168. package/dist/checkmarx-xml.js +196 -0
  169. package/dist/checkmarx-xml.js.map +1 -0
  170. package/dist/classify-scan-severity.d.ts +1 -1
  171. package/dist/classify-scan-severity.d.ts.map +1 -1
  172. package/dist/classify-scan-severity.js +11 -3
  173. package/dist/classify-scan-severity.js.map +1 -1
  174. package/dist/classify-severity-records.d.ts +1 -9
  175. package/dist/classify-severity-records.d.ts.map +1 -1
  176. package/dist/classify-severity-records.js +3 -13
  177. package/dist/classify-severity-records.js.map +1 -1
  178. package/dist/classify-severity.d.ts.map +1 -1
  179. package/dist/classify-severity.js +11 -10
  180. package/dist/classify-severity.js.map +1 -1
  181. package/dist/cli-argument.d.ts +4 -0
  182. package/dist/cli-argument.d.ts.map +1 -0
  183. package/dist/cli-argument.js +17 -0
  184. package/dist/cli-argument.js.map +1 -0
  185. package/dist/cli-help.d.ts +1 -1
  186. package/dist/cli-help.d.ts.map +1 -1
  187. package/dist/cli-help.js +71 -29
  188. package/dist/cli-help.js.map +1 -1
  189. package/dist/cli-hook.d.ts +3 -0
  190. package/dist/cli-hook.d.ts.map +1 -0
  191. package/dist/cli-hook.js +87 -0
  192. package/dist/cli-hook.js.map +1 -0
  193. package/dist/cli-integrations.d.ts +13 -0
  194. package/dist/cli-integrations.d.ts.map +1 -0
  195. package/dist/cli-integrations.js +333 -0
  196. package/dist/cli-integrations.js.map +1 -0
  197. package/dist/cli-mcp.d.ts +15 -0
  198. package/dist/cli-mcp.d.ts.map +1 -0
  199. package/dist/cli-mcp.js +178 -0
  200. package/dist/cli-mcp.js.map +1 -0
  201. package/dist/cli-token-view.d.ts +10 -0
  202. package/dist/cli-token-view.d.ts.map +1 -0
  203. package/dist/cli-token-view.js +41 -0
  204. package/dist/cli-token-view.js.map +1 -0
  205. package/dist/cli.d.ts +16 -8
  206. package/dist/cli.d.ts.map +1 -1
  207. package/dist/cli.js +3382 -2017
  208. package/dist/cli.js.map +1 -1
  209. package/dist/cloud-publish.d.ts.map +1 -1
  210. package/dist/cloud-publish.js +35 -33
  211. package/dist/cloud-publish.js.map +1 -1
  212. package/dist/codex-sdk-environment.d.ts +6 -0
  213. package/dist/codex-sdk-environment.d.ts.map +1 -1
  214. package/dist/codex-sdk-environment.js +19 -0
  215. package/dist/codex-sdk-environment.js.map +1 -1
  216. package/dist/command-words.d.ts +3 -0
  217. package/dist/command-words.d.ts.map +1 -0
  218. package/dist/command-words.js +46 -0
  219. package/dist/command-words.js.map +1 -0
  220. package/dist/component-scan.d.ts +19 -4
  221. package/dist/component-scan.d.ts.map +1 -1
  222. package/dist/component-scan.js +67 -9
  223. package/dist/component-scan.js.map +1 -1
  224. package/dist/config.d.ts +1 -1
  225. package/dist/config.d.ts.map +1 -1
  226. package/dist/config.js +130 -67
  227. package/dist/config.js.map +1 -1
  228. package/dist/continuum-findings.d.ts +1 -1
  229. package/dist/continuum-findings.d.ts.map +1 -1
  230. package/dist/continuum-findings.js +149 -19
  231. package/dist/continuum-findings.js.map +1 -1
  232. package/dist/contract-values.d.ts +2 -0
  233. package/dist/contract-values.d.ts.map +1 -1
  234. package/dist/contract-values.js +12 -9
  235. package/dist/contract-values.js.map +1 -1
  236. package/dist/cost-model.d.ts +13 -1
  237. package/dist/cost-model.d.ts.map +1 -1
  238. package/dist/cost-model.js +66 -112
  239. package/dist/cost-model.js.map +1 -1
  240. package/dist/cost.d.ts +42 -3
  241. package/dist/cost.d.ts.map +1 -1
  242. package/dist/cost.js +484 -149
  243. package/dist/cost.js.map +1 -1
  244. package/dist/custom-publish.d.ts +4 -0
  245. package/dist/custom-publish.d.ts.map +1 -1
  246. package/dist/custom-publish.js +40 -3
  247. package/dist/custom-publish.js.map +1 -1
  248. package/dist/custom-validation-prompt.d.ts +1 -1
  249. package/dist/custom-validation-prompt.d.ts.map +1 -1
  250. package/dist/custom-validation-prompt.js +29 -19
  251. package/dist/custom-validation-prompt.js.map +1 -1
  252. package/dist/deduplication/local.d.ts +4 -2
  253. package/dist/deduplication/local.d.ts.map +1 -1
  254. package/dist/deduplication/local.js +9 -2
  255. package/dist/deduplication/local.js.map +1 -1
  256. package/dist/deduplication/refusal.d.ts.map +1 -1
  257. package/dist/deduplication/refusal.js +26 -5
  258. package/dist/deduplication/refusal.js.map +1 -1
  259. package/dist/deduplication/scan.d.ts +5 -0
  260. package/dist/deduplication/scan.d.ts.map +1 -1
  261. package/dist/deduplication/scan.js +24 -7
  262. package/dist/deduplication/scan.js.map +1 -1
  263. package/dist/deep-config.d.ts +0 -1
  264. package/dist/deep-config.d.ts.map +1 -1
  265. package/dist/deep-config.js +22 -33
  266. package/dist/deep-config.js.map +1 -1
  267. package/dist/deep-scan-defaults.d.ts +2 -0
  268. package/dist/deep-scan-defaults.d.ts.map +1 -1
  269. package/dist/deep-scan-defaults.js +28 -1
  270. package/dist/deep-scan-defaults.js.map +1 -1
  271. package/dist/deep-scan.d.ts +2 -0
  272. package/dist/deep-scan.d.ts.map +1 -1
  273. package/dist/deep-scan.js +44 -19
  274. package/dist/deep-scan.js.map +1 -1
  275. package/dist/default-severity-rubric.d.ts +1 -1
  276. package/dist/default-severity-rubric.d.ts.map +1 -1
  277. package/dist/default-severity-rubric.js +1 -1
  278. package/dist/default-severity-rubric.js.map +1 -1
  279. package/dist/endor-findings.d.ts +3 -1
  280. package/dist/endor-findings.d.ts.map +1 -1
  281. package/dist/endor-findings.js +86 -45
  282. package/dist/endor-findings.js.map +1 -1
  283. package/dist/errors.d.ts +4 -2
  284. package/dist/errors.d.ts.map +1 -1
  285. package/dist/errors.js +15 -4
  286. package/dist/errors.js.map +1 -1
  287. package/dist/executable-path.d.ts +3 -0
  288. package/dist/executable-path.d.ts.map +1 -1
  289. package/dist/executable-path.js +26 -0
  290. package/dist/executable-path.js.map +1 -1
  291. package/dist/external-findings-import.d.ts +5 -3
  292. package/dist/external-findings-import.d.ts.map +1 -1
  293. package/dist/external-findings-import.js +19 -7
  294. package/dist/external-findings-import.js.map +1 -1
  295. package/dist/external-findings-local.d.ts +69 -5
  296. package/dist/external-findings-local.d.ts.map +1 -1
  297. package/dist/external-findings-local.js +537 -108
  298. package/dist/external-findings-local.js.map +1 -1
  299. package/dist/external-findings-publish.d.ts +13 -6
  300. package/dist/external-findings-publish.d.ts.map +1 -1
  301. package/dist/external-findings-publish.js +895 -1142
  302. package/dist/external-findings-publish.js.map +1 -1
  303. package/dist/external-findings-read.d.ts +21 -2
  304. package/dist/external-findings-read.d.ts.map +1 -1
  305. package/dist/external-findings-read.js +130 -68
  306. package/dist/external-findings-read.js.map +1 -1
  307. package/dist/external-findings.d.ts +15 -3
  308. package/dist/external-findings.d.ts.map +1 -1
  309. package/dist/external-findings.js +54 -2
  310. package/dist/external-findings.js.map +1 -1
  311. package/dist/external-import-contract.d.ts +8 -12
  312. package/dist/external-import-contract.d.ts.map +1 -1
  313. package/dist/external-import-contract.js +51 -184
  314. package/dist/external-import-contract.js.map +1 -1
  315. package/dist/external-import-models.d.ts +1 -1
  316. package/dist/external-import-models.d.ts.map +1 -1
  317. package/dist/external-import-models.js.map +1 -1
  318. package/dist/feedback.d.ts.map +1 -1
  319. package/dist/feedback.js +5 -4
  320. package/dist/feedback.js.map +1 -1
  321. package/dist/finding-prompt.d.ts +5 -0
  322. package/dist/finding-prompt.d.ts.map +1 -0
  323. package/dist/finding-prompt.js +32 -0
  324. package/dist/finding-prompt.js.map +1 -0
  325. package/dist/findings-client.d.ts +1 -0
  326. package/dist/findings-client.d.ts.map +1 -1
  327. package/dist/findings-client.js +27 -3
  328. package/dist/findings-client.js.map +1 -1
  329. package/dist/github-advisories.d.ts.map +1 -1
  330. package/dist/github-advisories.js +0 -2
  331. package/dist/github-advisories.js.map +1 -1
  332. package/dist/github-dependabot-findings.d.ts.map +1 -1
  333. package/dist/github-dependabot-findings.js +18 -8
  334. package/dist/github-dependabot-findings.js.map +1 -1
  335. package/dist/github-findings.d.ts +4 -1
  336. package/dist/github-findings.d.ts.map +1 -1
  337. package/dist/github-findings.js +73 -316
  338. package/dist/github-findings.js.map +1 -1
  339. package/dist/hosted-git-remotes.d.ts +5 -0
  340. package/dist/hosted-git-remotes.d.ts.map +1 -0
  341. package/dist/hosted-git-remotes.js +285 -0
  342. package/dist/hosted-git-remotes.js.map +1 -0
  343. package/dist/hosted-git-repository.d.ts +28 -0
  344. package/dist/hosted-git-repository.d.ts.map +1 -0
  345. package/dist/hosted-git-repository.js +222 -0
  346. package/dist/hosted-git-repository.js.map +1 -0
  347. package/dist/hosted-remediation.d.ts +1 -0
  348. package/dist/hosted-remediation.d.ts.map +1 -1
  349. package/dist/hosted-remediation.js +23 -3
  350. package/dist/hosted-remediation.js.map +1 -1
  351. package/dist/hosted-scan-protocol.js +2 -2
  352. package/dist/hosted-scan-protocol.js.map +1 -1
  353. package/dist/hosted-scan.d.ts.map +1 -1
  354. package/dist/hosted-scan.js +16 -11
  355. package/dist/hosted-scan.js.map +1 -1
  356. package/dist/hosted-scanner-http.d.ts +21 -0
  357. package/dist/hosted-scanner-http.d.ts.map +1 -0
  358. package/dist/hosted-scanner-http.js +204 -0
  359. package/dist/hosted-scanner-http.js.map +1 -0
  360. package/dist/import-scan.d.ts.map +1 -1
  361. package/dist/import-scan.js +3 -0
  362. package/dist/import-scan.js.map +1 -1
  363. package/dist/index.d.ts +21 -9
  364. package/dist/index.d.ts.map +1 -1
  365. package/dist/index.js +12 -3
  366. package/dist/index.js.map +1 -1
  367. package/dist/inspector-findings.d.ts.map +1 -1
  368. package/dist/inspector-findings.js +52 -86
  369. package/dist/inspector-findings.js.map +1 -1
  370. package/dist/knowledge-base.d.ts +1 -1
  371. package/dist/knowledge-base.d.ts.map +1 -1
  372. package/dist/knowledge-base.js +102 -32
  373. package/dist/knowledge-base.js.map +1 -1
  374. package/dist/linear.d.ts +11 -0
  375. package/dist/linear.d.ts.map +1 -1
  376. package/dist/linear.js.map +1 -1
  377. package/dist/local-git-metadata.d.ts +5 -2
  378. package/dist/local-git-metadata.d.ts.map +1 -1
  379. package/dist/local-git-metadata.js +8 -5
  380. package/dist/local-git-metadata.js.map +1 -1
  381. package/dist/multiscan.d.ts +10 -4
  382. package/dist/multiscan.d.ts.map +1 -1
  383. package/dist/multiscan.js +217 -102
  384. package/dist/multiscan.js.map +1 -1
  385. package/dist/native-json-findings.d.ts +2 -1
  386. package/dist/native-json-findings.d.ts.map +1 -1
  387. package/dist/native-json-findings.js +12 -2
  388. package/dist/native-json-findings.js.map +1 -1
  389. package/dist/opengrep-findings.d.ts +5 -2
  390. package/dist/opengrep-findings.d.ts.map +1 -1
  391. package/dist/opengrep-findings.js +54 -33
  392. package/dist/opengrep-findings.js.map +1 -1
  393. package/dist/operation-telemetry.d.ts +104 -0
  394. package/dist/operation-telemetry.d.ts.map +1 -0
  395. package/dist/operation-telemetry.js +293 -0
  396. package/dist/operation-telemetry.js.map +1 -0
  397. package/dist/plugin-python-probe.d.ts +4 -0
  398. package/dist/plugin-python-probe.d.ts.map +1 -0
  399. package/dist/plugin-python-probe.js +10 -0
  400. package/dist/plugin-python-probe.js.map +1 -0
  401. package/dist/private-finding-import.d.ts +99 -9
  402. package/dist/private-finding-import.d.ts.map +1 -1
  403. package/dist/private-finding-import.js +116 -152
  404. package/dist/private-finding-import.js.map +1 -1
  405. package/dist/project-config.d.ts.map +1 -1
  406. package/dist/project-config.js +53 -13
  407. package/dist/project-config.js.map +1 -1
  408. package/dist/prompt-files.d.ts +7 -2
  409. package/dist/prompt-files.d.ts.map +1 -1
  410. package/dist/prompt-files.js +42 -20
  411. package/dist/prompt-files.js.map +1 -1
  412. package/dist/provider-profile.d.ts +1 -1
  413. package/dist/provider-profile.d.ts.map +1 -1
  414. package/dist/provider-profile.js +12 -10
  415. package/dist/provider-profile.js.map +1 -1
  416. package/dist/publication-telemetry.d.ts +45 -0
  417. package/dist/publication-telemetry.d.ts.map +1 -0
  418. package/dist/publication-telemetry.js +50 -0
  419. package/dist/publication-telemetry.js.map +1 -0
  420. package/dist/publication.d.ts +1 -1
  421. package/dist/publication.d.ts.map +1 -1
  422. package/dist/publication.js.map +1 -1
  423. package/dist/publish.d.ts +4 -0
  424. package/dist/publish.d.ts.map +1 -1
  425. package/dist/publish.js +42 -2
  426. package/dist/publish.js.map +1 -1
  427. package/dist/record.d.ts +1 -5
  428. package/dist/record.d.ts.map +1 -1
  429. package/dist/record.js +3 -51
  430. package/dist/record.js.map +1 -1
  431. package/dist/remediation.d.ts +1 -4
  432. package/dist/remediation.d.ts.map +1 -1
  433. package/dist/remediation.js +6 -8
  434. package/dist/remediation.js.map +1 -1
  435. package/dist/request-metadata.d.ts +3 -0
  436. package/dist/request-metadata.d.ts.map +1 -1
  437. package/dist/request-metadata.js +9 -0
  438. package/dist/request-metadata.js.map +1 -1
  439. package/dist/result.d.ts +4 -0
  440. package/dist/result.d.ts.map +1 -1
  441. package/dist/result.js +1 -1
  442. package/dist/result.js.map +1 -1
  443. package/dist/retry-windows-rename.d.ts +8 -0
  444. package/dist/retry-windows-rename.d.ts.map +1 -0
  445. package/dist/retry-windows-rename.js +24 -0
  446. package/dist/retry-windows-rename.js.map +1 -0
  447. package/dist/runtime.d.ts +54 -6
  448. package/dist/runtime.d.ts.map +1 -1
  449. package/dist/runtime.js +1084 -256
  450. package/dist/runtime.js.map +1 -1
  451. package/dist/saved-scan-bootstrap.d.ts +7 -1
  452. package/dist/saved-scan-bootstrap.d.ts.map +1 -1
  453. package/dist/saved-scan-bootstrap.js +269 -114
  454. package/dist/saved-scan-bootstrap.js.map +1 -1
  455. package/dist/scan-activity.d.ts.map +1 -1
  456. package/dist/scan-activity.js +141 -56
  457. package/dist/scan-activity.js.map +1 -1
  458. package/dist/scan-archive-path.d.ts +2 -0
  459. package/dist/scan-archive-path.d.ts.map +1 -0
  460. package/dist/scan-archive-path.js +5 -0
  461. package/dist/scan-archive-path.js.map +1 -0
  462. package/dist/scan-comparison.d.ts +10 -5
  463. package/dist/scan-comparison.d.ts.map +1 -1
  464. package/dist/scan-comparison.js +66 -17
  465. package/dist/scan-comparison.js.map +1 -1
  466. package/dist/scan-dashboard.d.ts +7 -6
  467. package/dist/scan-dashboard.d.ts.map +1 -1
  468. package/dist/scan-dashboard.js +92 -43
  469. package/dist/scan-dashboard.js.map +1 -1
  470. package/dist/scan-executor.d.ts +10 -1
  471. package/dist/scan-executor.d.ts.map +1 -1
  472. package/dist/scan-executor.js +24 -2
  473. package/dist/scan-executor.js.map +1 -1
  474. package/dist/scan-history-renderer.d.ts +1 -0
  475. package/dist/scan-history-renderer.d.ts.map +1 -1
  476. package/dist/scan-history-renderer.js +34 -28
  477. package/dist/scan-history-renderer.js.map +1 -1
  478. package/dist/scan-logs.d.ts +12 -0
  479. package/dist/scan-logs.d.ts.map +1 -1
  480. package/dist/scan-logs.js +131 -78
  481. package/dist/scan-logs.js.map +1 -1
  482. package/dist/scan-measurement.d.ts +26 -0
  483. package/dist/scan-measurement.d.ts.map +1 -0
  484. package/dist/scan-measurement.js +77 -0
  485. package/dist/scan-measurement.js.map +1 -0
  486. package/dist/scan-progress.d.ts +1 -0
  487. package/dist/scan-progress.d.ts.map +1 -1
  488. package/dist/scan-progress.js +3 -0
  489. package/dist/scan-progress.js.map +1 -1
  490. package/dist/scan-sessions.d.ts.map +1 -1
  491. package/dist/scan-sessions.js +4 -1
  492. package/dist/scan-sessions.js.map +1 -1
  493. package/dist/scan-settings.d.ts +1 -1
  494. package/dist/scan-settings.d.ts.map +1 -1
  495. package/dist/scan-settings.js +1 -24
  496. package/dist/scan-settings.js.map +1 -1
  497. package/dist/scanner-import.d.ts +25 -13
  498. package/dist/scanner-import.d.ts.map +1 -1
  499. package/dist/scanner-import.js +216 -97
  500. package/dist/scanner-import.js.map +1 -1
  501. package/dist/scanner-repository-url.d.ts +4 -1
  502. package/dist/scanner-repository-url.d.ts.map +1 -1
  503. package/dist/scanner-repository-url.js +10 -6
  504. package/dist/scanner-repository-url.js.map +1 -1
  505. package/dist/security-policy.d.ts +1 -1
  506. package/dist/security-policy.d.ts.map +1 -1
  507. package/dist/security-policy.js +26 -6
  508. package/dist/security-policy.js.map +1 -1
  509. package/dist/semgrep-findings.d.ts +6 -3
  510. package/dist/semgrep-findings.d.ts.map +1 -1
  511. package/dist/semgrep-findings.js +72 -42
  512. package/dist/semgrep-findings.js.map +1 -1
  513. package/dist/semgrep-guardian-findings.d.ts +38 -0
  514. package/dist/semgrep-guardian-findings.d.ts.map +1 -0
  515. package/dist/semgrep-guardian-findings.js +165 -0
  516. package/dist/semgrep-guardian-findings.js.map +1 -0
  517. package/dist/semgrep-platform-collector.d.ts +26 -0
  518. package/dist/semgrep-platform-collector.d.ts.map +1 -0
  519. package/dist/semgrep-platform-collector.js +108 -0
  520. package/dist/semgrep-platform-collector.js.map +1 -0
  521. package/dist/semgrep-platform-findings.d.ts +157 -6
  522. package/dist/semgrep-platform-findings.d.ts.map +1 -1
  523. package/dist/semgrep-platform-findings.js +766 -103
  524. package/dist/semgrep-platform-findings.js.map +1 -1
  525. package/dist/server/api.d.ts +1 -1
  526. package/dist/server/api.d.ts.map +1 -1
  527. package/dist/server/api.js.map +1 -1
  528. package/dist/server/embedding-tokenizer.cdx.json +33 -0
  529. package/dist/server/embedding-tokenizer.d.ts +2 -0
  530. package/dist/server/embedding-tokenizer.d.ts.map +1 -0
  531. package/dist/server/embedding-tokenizer.js +322 -0
  532. package/dist/server/embedding-tokenizer.js.map +6 -0
  533. package/dist/server/embeddings.d.ts +16 -2
  534. package/dist/server/embeddings.d.ts.map +1 -1
  535. package/dist/server/embeddings.js +35 -5
  536. package/dist/server/embeddings.js.map +1 -1
  537. package/dist/session-lines.d.ts +5 -0
  538. package/dist/session-lines.d.ts.map +1 -0
  539. package/dist/session-lines.js +60 -0
  540. package/dist/session-lines.js.map +1 -0
  541. package/dist/severity-review.d.ts +3 -1
  542. package/dist/severity-review.d.ts.map +1 -1
  543. package/dist/severity-review.js +3 -2
  544. package/dist/severity-review.js.map +1 -1
  545. package/dist/skill-authentication.d.ts +1 -0
  546. package/dist/skill-authentication.d.ts.map +1 -1
  547. package/dist/skill-authentication.js +1 -0
  548. package/dist/skill-authentication.js.map +1 -1
  549. package/dist/snyk-findings.d.ts +9 -2
  550. package/dist/snyk-findings.d.ts.map +1 -1
  551. package/dist/snyk-findings.js +90 -70
  552. package/dist/snyk-findings.js.map +1 -1
  553. package/dist/snyk-rest-findings.d.ts +59 -0
  554. package/dist/snyk-rest-findings.d.ts.map +1 -0
  555. package/dist/snyk-rest-findings.js +879 -0
  556. package/dist/snyk-rest-findings.js.map +1 -0
  557. package/dist/snyk-token-origin.d.ts +4 -0
  558. package/dist/snyk-token-origin.d.ts.map +1 -0
  559. package/dist/snyk-token-origin.js +82 -0
  560. package/dist/snyk-token-origin.js.map +1 -0
  561. package/dist/sonar-collector.d.ts.map +1 -1
  562. package/dist/sonar-collector.js +0 -3
  563. package/dist/sonar-collector.js.map +1 -1
  564. package/dist/sonar-findings.d.ts +1 -1
  565. package/dist/sonar-findings.d.ts.map +1 -1
  566. package/dist/sonar-findings.js +2 -4
  567. package/dist/sonar-findings.js.map +1 -1
  568. package/dist/sonar-import.d.ts +3 -0
  569. package/dist/sonar-import.d.ts.map +1 -1
  570. package/dist/sonar-import.js +30 -34
  571. package/dist/sonar-import.js.map +1 -1
  572. package/dist/stored-finding-projection.d.ts +6 -0
  573. package/dist/stored-finding-projection.d.ts.map +1 -0
  574. package/dist/stored-finding-projection.js +104 -0
  575. package/dist/stored-finding-projection.js.map +1 -0
  576. package/dist/stored-findings-publication.d.ts +23 -0
  577. package/dist/stored-findings-publication.d.ts.map +1 -0
  578. package/dist/stored-findings-publication.js +169 -0
  579. package/dist/stored-findings-publication.js.map +1 -0
  580. package/dist/target-drift.d.ts +9 -0
  581. package/dist/target-drift.d.ts.map +1 -0
  582. package/dist/target-drift.js +27 -0
  583. package/dist/target-drift.js.map +1 -0
  584. package/dist/targets.d.ts +8 -1
  585. package/dist/targets.d.ts.map +1 -1
  586. package/dist/targets.js +131 -42
  587. package/dist/targets.js.map +1 -1
  588. package/dist/tenable-findings.d.ts +6 -0
  589. package/dist/tenable-findings.d.ts.map +1 -0
  590. package/dist/tenable-findings.js +122 -0
  591. package/dist/tenable-findings.js.map +1 -0
  592. package/dist/terminal-stream.d.ts +13 -0
  593. package/dist/terminal-stream.d.ts.map +1 -0
  594. package/dist/terminal-stream.js +35 -0
  595. package/dist/terminal-stream.js.map +1 -0
  596. package/dist/trivy-findings.d.ts +19 -0
  597. package/dist/trivy-findings.d.ts.map +1 -0
  598. package/dist/trivy-findings.js +424 -0
  599. package/dist/trivy-findings.js.map +1 -0
  600. package/dist/trivy-import.d.ts +19 -0
  601. package/dist/trivy-import.d.ts.map +1 -0
  602. package/dist/trivy-import.js +151 -0
  603. package/dist/trivy-import.js.map +1 -0
  604. package/dist/trusted-executable.d.ts +6 -0
  605. package/dist/trusted-executable.d.ts.map +1 -1
  606. package/dist/trusted-executable.js +140 -37
  607. package/dist/trusted-executable.js.map +1 -1
  608. package/dist/validation-records.js +1 -1
  609. package/dist/validation-records.js.map +1 -1
  610. package/dist/veracode-findings.d.ts +1 -1
  611. package/dist/veracode-findings.d.ts.map +1 -1
  612. package/dist/veracode-findings.js +19 -6
  613. package/dist/veracode-findings.js.map +1 -1
  614. package/dist/version.d.ts +3 -1
  615. package/dist/version.d.ts.map +1 -1
  616. package/dist/version.js +184 -32
  617. package/dist/version.js.map +1 -1
  618. package/dist/vertex-pricing.d.ts +8 -0
  619. package/dist/vertex-pricing.d.ts.map +1 -0
  620. package/dist/vertex-pricing.js +20 -0
  621. package/dist/vertex-pricing.js.map +1 -0
  622. package/dist/windows-path.d.ts +1 -0
  623. package/dist/windows-path.d.ts.map +1 -1
  624. package/dist/windows-path.js +26 -1
  625. package/dist/windows-path.js.map +1 -1
  626. package/dist/wiz-findings.d.ts +6 -2
  627. package/dist/wiz-findings.d.ts.map +1 -1
  628. package/dist/wiz-findings.js +65 -89
  629. package/dist/wiz-findings.js.map +1 -1
  630. package/docs/classify-severity-records.md +27 -12
  631. package/docs/cli.md +853 -639
  632. package/docs/dedupe-records.md +44 -24
  633. package/docs/findings-service.md +84 -44
  634. package/docs/hosted-remediation.md +27 -11
  635. package/docs/hosted-scans.md +50 -42
  636. package/docs/sdk.md +903 -0
  637. package/docs/telemetry.md +236 -0
  638. package/docs/validate-records.md +78 -44
  639. package/package.json +23 -16
  640. package/schemas/external-findings.schema.json +4 -1
  641. package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
  642. package/_bundled_plugin/mcp/helpers.mjs.br.part-001 +0 -0
  643. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  644. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  645. package/_bundled_plugin/mcp/server.mjs.br.part-002 +0 -0
  646. package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +0 -96
  647. package/dist/external-imports-feature.d.ts +0 -6
  648. package/dist/external-imports-feature.d.ts.map +0 -1
  649. package/dist/external-imports-feature.js +0 -51
  650. package/dist/external-imports-feature.js.map +0 -1
  651. package/dist/finding-normalization.d.ts +0 -5
  652. package/dist/finding-normalization.d.ts.map +0 -1
  653. package/dist/finding-normalization.js +0 -155
  654. package/dist/finding-normalization.js.map +0 -1
  655. package/dist/reader-selection.d.ts +0 -13
  656. package/dist/reader-selection.d.ts.map +0 -1
  657. package/dist/reader-selection.js +0 -81
  658. package/dist/reader-selection.js.map +0 -1
@@ -1,32 +1,45 @@
1
+ import { readScannerJson } from "./scanner-import.js";
1
2
  import { hash, randomUUID } from "node:crypto";
2
3
  import { chmodSync } from "node:fs";
3
- import { lstat, mkdir, open, readFile, readdir, rename, rm, } from "node:fs/promises";
4
- import { createRequire } from "node:module";
4
+ import { lstat, mkdir, open, readFile, rename, rm } from "node:fs/promises";
5
5
  import { join } from "node:path";
6
+ import { nullIfMissingFile } from "./targets.js";
6
7
  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";
8
+ import { isProxy } from "node:util/types";
9
+ import { privateImportClient, privateJobVersions, selectPrivateReport, readPrivateCheckpoint, parsePrivateCheckpoint, unsuccessfulJob, } from "./private-finding-import.js";
9
10
  import { readCloudCredentials } from "./cloud-publish.js";
10
- import { cloudBaseUrl, cloudFindingsUrl, DEFAULT_CLOUD_BASE_URL, } from "./cloud-endpoint.js";
11
+ import { armorCodeRepositoryUrl } from "./armorcode-findings.js";
12
+ import { declaredScannerRepositoryUrl } from "./scanner-repository-url.js";
13
+ import { tenableReportFormat } from "./tenable-findings.js";
14
+ import { guardianFindingId, guardianFindingRepository, guardianSelectionFormat, } from "./semgrep-guardian-findings.js";
15
+ import { cloudBaseUrl, cloudFindingsUrl } from "./cloud-endpoint.js";
11
16
  import { CodexSecurityError, errorMessage } from "./errors.js";
12
- import { requireExternalImports } from "./external-imports-feature.js";
13
- import { codexSecurityStateDirectory, createWindowsOutputDirectory, validatePreparedOutputDir, } from "./runtime.js";
14
- import { EXTERNAL_FINDINGS_PROVIDERS, readExternalFindings, } from "./external-findings-read.js";
15
- import { canSnapshotWithoutGetters, canonicalJson, isRecord, } from "./record.js";
17
+ import { codexSecurityStateDirectory, createWindowsOutputDirectory, validatePreparedOutputDir, sqliteDatabaseOpener, } from "./runtime.js";
18
+ import { EXTERNAL_FINDINGS_PROVIDERS, readExternalFindings, requireCloudReader, semgrepSarifRepositories, snykFileRepositories, } from "./external-findings-read.js";
19
+ import { canonicalJson, isRecord } from "./record.js";
16
20
  import { notify } from "./value.js";
17
21
  import { wizCsvRepositoryUrl } from "./wiz-csv.js";
18
- import { wizFindingRepository } from "./wiz-findings.js";
19
- import { previousReaderSelection } from "./reader-selection.js";
22
+ import { isWizPackageFinding, wizFindingRepository } from "./wiz-findings.js";
23
+ import { getOpenGrepSourcePositions } from "./opengrep-findings.js";
20
24
  import { dependabotRepository } from "./github-dependabot-findings.js";
21
- import { snykRepositoryUrls } from "./snyk-findings.js";
25
+ import { retainedGitHubRepository } from "./github-findings.js";
26
+ import { normalizeSemgrepFindings, readSemgrepReport, } from "./semgrep-findings.js";
27
+ import { normalizeSnykFile, snykRepositoryUrls } from "./snyk-findings.js";
28
+ import { isSnykRestFinding, isSnykRestFormat, snykRestImportRepository, snykRestRepositoryMatches, } from "./snyk-rest-findings.js";
29
+ import { hasRetainedSemgrepScope, isSemgrepPlatformEvidence, isSemgrepPlatformFormat, semgrepPlatformFormat, semgrepPlatformFindingBinding, semgrepPlatformRepositoryReference, semgrepPlatformReportScope, } from "./semgrep-platform-findings.js";
30
+ import { hostedRepositoryUrlKey, normalizeHostedGitRepositoryUrl, } from "./hosted-git-repository.js";
31
+ import { trivyRemoteKey, trivyRepositoryUrl } from "./trivy-findings.js";
32
+ import { hostedRepositoryUrl } from "./hosted-scanner-http.js";
22
33
  import { endorRepositoryUrls } from "./endor-findings.js";
23
34
  import { blackDuckReportRepositories, projectedBlackDuckRepositoryUrl, } from "./blackduck-findings.js";
35
+ import { checkmarxSelection } from "./checkmarx-repository.js";
24
36
  import { continuumReportRepositories } from "./continuum-findings.js";
25
37
  import { veracodeReportRepositories } from "./veracode-findings.js";
26
38
  import { inspectorReportRepository } from "./inspector-findings.js";
27
39
  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";
40
+ import { checkRetainedTrivyProvider, checkTrivyReportProvider, isTrivyReportHeader, retainedTrivyReport, trivyBatchSourceUrls, validateTrivyRepositoryUrl, } from "./trivy-import.js";
41
+ import { NoPublishableFindingsError, reportForJson, snapshotSourceRepositories, snapshotImportOptions, cloneImportPreview, snapshotNormalizedFindings, } from "./external-findings.js";
42
+ import { canonicalNumber, cloudRepositoryUrlKey, validateImportRequest, validateExternalEvidence, validateExternalReport, validateImportReceipt, validateRepositories, validateSourceReports, validateSourceReport, } from "./external-import-contract.js";
30
43
  const MAX_REQUEST_BYTES = 5 * 1024 * 1024;
31
44
  /** Preserve accepted work in CLI JSON output when publication cannot finish. */
32
45
  export class ExternalPublicationError extends CodexSecurityError {
@@ -36,6 +49,27 @@ export class ExternalPublicationError extends CodexSecurityError {
36
49
  this.result = result;
37
50
  }
38
51
  }
52
+ /** Bind reviewed evidence to its account, repository, connector and destination version. */
53
+ export function cloudReviewDigest(preview, checkout, findings) {
54
+ return hash("sha256", canonicalJson({
55
+ checkout,
56
+ api: preview.cloudApiUrl,
57
+ account: preview.accountId,
58
+ repository: preview.destination.id,
59
+ connector: preview.destination.repo_connector_id,
60
+ url: preview.destination.url,
61
+ reset: preview.destination.reset_marker,
62
+ source: preview.source,
63
+ privateImport: preview.privateImport,
64
+ ...(findings && { findings, excluded: preview.excluded }),
65
+ items: preview.requests.flatMap((request) => request.items.map((item) => [
66
+ item.source_finding_id,
67
+ item.expected_version,
68
+ request.repository.environment_id,
69
+ item.evidence,
70
+ ])),
71
+ }));
72
+ }
39
73
  // Limit read concurrency to avoid one network round trip per finding in series.
40
74
  // Wait for the entire batch on failure so callers never outlive their read tasks.
41
75
  async function readInBatches(items, read) {
@@ -50,18 +84,95 @@ async function readInBatches(items, read) {
50
84
  }
51
85
  return output;
52
86
  }
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);
87
+ function matchesRepositoryEvidence(expected, stored) {
88
+ const storedWire = canonicalJson(stored);
89
+ if (canonicalJson(expected) === storedWire)
90
+ return true;
91
+ // A repository can store a literal first-component colon with an explicit
92
+ // relative prefix. Match only that spelling in typed locations and their manifests.
93
+ const qualified = new Map();
94
+ const locations = expected.locations?.map((location, index) => {
95
+ const { path } = location;
96
+ const actual = stored.locations?.[index]?.path;
97
+ if (actual !== `./${path}` || !/^[^/:\\@]+:[^/\\]/u.test(path))
98
+ return location;
99
+ qualified.set(path, actual);
100
+ return { ...location, path: actual };
101
+ });
102
+ if (!qualified.size)
103
+ return false;
104
+ const packages = expected.packages?.map((item) => {
105
+ const path = item.manifest_path && qualified.get(item.manifest_path);
106
+ return path ? { ...item, manifest_path: path } : item;
107
+ });
108
+ return canonicalJson({ ...expected, locations, packages }) === storedWire;
109
+ }
110
+ function isCompleteUnscoredOpenGrepReport(report) {
111
+ let rows = report.excluded.length;
112
+ for (const { evidence } of report.findings) {
113
+ const traces = evidence.source_data?.["results"];
114
+ if (evidence.details?.scanner_origin !== "OpenGrep" ||
115
+ evidence.severity !== "unknown" ||
116
+ !Array.isArray(traces))
117
+ return false;
118
+ rows += traces.length;
119
+ }
120
+ return rows === report.read;
121
+ }
122
+ function repositoryFindings(report, severities) {
123
+ return severities
124
+ ? report.findings.filter(({ evidence }) => severities.includes(evidence.severity))
125
+ : report.findings;
57
126
  }
58
- function jsonDigest(value, serialize = wireJson) {
59
- return hash("sha256", serialize(value));
127
+ function repositorySelection(report, severities, sourcePositions) {
128
+ const findings = repositoryFindings(report, severities);
129
+ if (findings.length === report.findings.length)
130
+ return report;
131
+ const excluded = [...report.excluded];
132
+ const skipped = new Set(excluded.map(({ position }) => position));
133
+ function exclude(position, id, severity) {
134
+ excluded.push({
135
+ position,
136
+ source_finding_id: id,
137
+ reason: `The repository publisher does not accept severity ${JSON.stringify(severity)}. A local import or an available private inbox can retain this finding.`,
138
+ });
139
+ }
140
+ // Serialized OpenGrep selections retain every trace but lose the reader's
141
+ // position map. All these traces have unknown severity, so the remaining
142
+ // source rows are provably excluded even when their individual IDs are lost.
143
+ if (!sourcePositions && isCompleteUnscoredOpenGrepReport(report)) {
144
+ const id = report.findings.length === 1
145
+ ? report.findings[0].source_finding_id
146
+ : null;
147
+ for (let row = 1; row <= report.read; row++)
148
+ if (!skipped.has(row))
149
+ exclude(row, id, "unknown");
150
+ }
151
+ else {
152
+ const retained = new Set(findings);
153
+ let position = 0;
154
+ for (const finding of report.findings) {
155
+ do {
156
+ position++;
157
+ } while (skipped.has(position));
158
+ if (retained.has(finding))
159
+ continue;
160
+ const { source_finding_id, evidence } = finding;
161
+ for (const row of sourcePositions?.get(source_finding_id) ?? [position])
162
+ exclude(row, source_finding_id, evidence.severity);
163
+ }
164
+ }
165
+ return {
166
+ ...report,
167
+ findings,
168
+ excluded: excluded.sort((a, b) => a.position - b.position),
169
+ };
60
170
  }
61
171
  function sameSubmission(left, right) {
62
172
  return (left.accountId === right.accountId &&
173
+ left.trivyRepositoryUrl === right.trivyRepositoryUrl &&
63
174
  left.inspectorProjectMapRequired === right.inspectorProjectMapRequired &&
64
- wireJson(left.requests) === wireJson(right.requests));
175
+ canonicalJson(left.requests) === canonicalJson(right.requests));
65
176
  }
66
177
  function wireFinding({ source_finding_id, evidence, }) {
67
178
  return { source_finding_id, evidence };
@@ -69,49 +180,8 @@ function wireFinding({ source_finding_id, evidence, }) {
69
180
  function submissionFindings(requests) {
70
181
  return requests.flatMap((batch) => batch.items.map(wireFinding));
71
182
  }
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));
97
- }
98
- function sameFindingSelection(left, right) {
99
- return (left.length === right.length &&
100
- left.every((finding, index) => finding.source_finding_id === right[index].source_finding_id));
101
- }
102
183
  async function readOptionalFile(path) {
103
- return readFile(path, "utf8").catch((error) => {
104
- if (error.code === "ENOENT")
105
- return undefined;
106
- throw error;
107
- });
108
- }
109
- function reportForJson(report) {
110
- const errors = report?.["errors"];
111
- if (!Array.isArray(errors))
112
- return report;
113
- const json = JSON.stringify(errors, (_key, entry) => entry instanceof Error ? { ...entry, message: entry.message } : entry);
114
- return { ...report, errors: JSON.parse(json) };
184
+ return (await readFile(path, "utf8").catch(nullIfMissingFile)) ?? undefined;
115
185
  }
116
186
  export function externalPublicationStateDirectory(environment) {
117
187
  return join(codexSecurityStateDirectory(environment), "external-finding-publications");
@@ -145,15 +215,13 @@ class CloudImportError extends CodexSecurityError {
145
215
  }
146
216
  function validatePublicationReceipt(value, batch) {
147
217
  const receipt = validateImportReceipt(value);
148
- const batchItems = new Map(batch.items.map((item) => [item.client_id, item]));
218
+ const remaining = new Set(batch.items.map((item) => item.client_id));
149
219
  if (receipt.id !== batch.request_id ||
150
- wireJson(receipt.repository) !== wireJson(batch.repository) ||
151
- wireJson(receipt.source) !== wireJson(batch.source) ||
220
+ canonicalJson(receipt.repository) !== canonicalJson(batch.repository) ||
221
+ canonicalJson(receipt.source) !== canonicalJson(batch.source) ||
152
222
  receipt.item_count !== batch.items.length ||
153
223
  receipt.results.length !== batch.items.length ||
154
- new Set(receipt.results.map((item) => item.client_id)).size !==
155
- batch.items.length ||
156
- receipt.results.some((item) => !batchItems.has(item.client_id)))
224
+ receipt.results.some((item) => !remaining.delete(item.client_id)))
157
225
  throw new CodexSecurityError("Cloud returned a receipt for a different publication.");
158
226
  const actual = { created: 0, updated: 0, unchanged: 0, error: 0 };
159
227
  for (const result of receipt.results) {
@@ -173,175 +241,248 @@ function validatePublicationReceipt(value, batch) {
173
241
  result.version === null)
174
242
  throw new CodexSecurityError("Cloud returned an incomplete saved finding.");
175
243
  }
176
- if (wireJson(receipt.counts) !== wireJson(actual))
244
+ if (canonicalJson(receipt.counts) !== canonicalJson(actual))
177
245
  throw new CodexSecurityError("Cloud returned inconsistent publication counts.");
178
246
  return receipt;
179
247
  }
180
- async function withImportLocks(state, keys, signal, operation) {
248
+ function acknowledgedReceipts({ receipts = [], requests }) {
249
+ if (receipts.length > requests.length)
250
+ throw new CodexSecurityError("Saved publication has more receipts than requests.");
251
+ return receipts.map((receipt, index) => validatePublicationReceipt(receipt, requests[index]));
252
+ }
253
+ async function withImportLock(state, key, signal, operation) {
181
254
  if (process.platform === "win32")
182
255
  await createWindowsOutputDirectory(state, true);
183
256
  else
184
257
  await mkdir(state, { recursive: true, mode: 0o700 });
185
258
  await validatePreparedOutputDir(state, undefined, true);
186
- const require = createRequire(import.meta.url);
187
- const Database = (process.versions["bun"]
188
- ? require("bun:sqlite").Database
189
- : require("node:sqlite").DatabaseSync);
190
- const guards = [];
259
+ const path = join(state, key + ".lock.sqlite");
260
+ const metadata = await lstat(path).catch(nullIfMissingFile);
261
+ if (metadata && !metadata.isFile())
262
+ throw new CodexSecurityError("The publication lock is not a regular file.");
263
+ const guard = sqliteDatabaseOpener()(path);
191
264
  try {
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
- }
265
+ if (process.platform !== "win32")
266
+ chmodSync(path, 0o600);
267
+ guard.exec("PRAGMA busy_timeout = 0");
207
268
  for (;;) {
208
269
  signal?.throwIfAborted();
209
- let acquired = 0;
210
270
  try {
211
- // SQLite releases these transactions on process exit.
212
- for (const guard of guards) {
213
- guard.exec("BEGIN EXCLUSIVE");
214
- acquired++;
215
- }
271
+ guard.exec("BEGIN EXCLUSIVE");
216
272
  break;
217
273
  }
218
274
  catch (error) {
219
275
  const sqliteError = error;
220
276
  if (sqliteError.errcode !== 5 && sqliteError.code !== "SQLITE_BUSY")
221
277
  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");
225
278
  await delay(50, undefined, { signal });
226
279
  }
227
280
  }
228
281
  return await operation();
229
282
  }
230
283
  finally {
231
- for (const guard of guards)
232
- guard.close();
284
+ guard.close();
285
+ }
286
+ }
287
+ function hostedSourceReferences(readerSources, url, sourcePlatform) {
288
+ // The catalog supplies a GitHub identity only when the reader has no SCM mapping.
289
+ const sources = readerSources.length || !url
290
+ ? readerSources
291
+ : [{ url, platform: "github" }];
292
+ return sourcePlatform === "other"
293
+ ? sources.filter(({ platform }) => platform !== "github")
294
+ : sources;
295
+ }
296
+ function checkFindingProvider(input, provider) {
297
+ function checkFormat(format) {
298
+ if (format === tenableReportFormat)
299
+ requireCloudReader({ provider: "tenable" });
300
+ if (format === guardianSelectionFormat && provider !== "semgrep")
301
+ throw new CodexSecurityError("Use the semgrep provider for Guardian findings.");
302
+ }
303
+ checkFormat(input.report?.["format"]);
304
+ for (const { source_finding_id, evidence } of input.findings) {
305
+ if (guardianFindingId.test(source_finding_id))
306
+ checkFormat(guardianSelectionFormat);
307
+ checkFormat(evidence.source_data?.["format"]);
233
308
  }
234
309
  }
235
310
  export async function prepareExternalPublication(path, options, dependencies = {}) {
236
- requireExternalImports(dependencies.environment);
237
311
  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);
312
+ requireCloudReader(settings);
313
+ const readerSources = snapshotSourceRepositories(dependencies.sourceRepositories);
314
+ const provider = EXTERNAL_FINDINGS_PROVIDERS[settings.provider];
315
+ let input;
316
+ let fileRepositories;
317
+ if (settings.provider === "semgrep" || settings.provider === "snyk") {
318
+ const semgrep = settings.provider === "semgrep";
319
+ const report = await (semgrep
320
+ ? readSemgrepReport(path)
321
+ : readScannerJson(path));
322
+ const normalize = async (url, platform) => {
323
+ const references = hostedSourceReferences(readerSources, url, semgrep ? platform : undefined);
324
+ const semgrepOptions = {
325
+ sourceRepositoryUrl: settings.sourceRepositoryUrl,
326
+ sourceRepositories: references,
327
+ };
328
+ const findings = semgrep
329
+ ? normalizeSemgrepFindings(report, semgrepOptions, "cloud")
330
+ : await normalizeSnykFile(report, "cloud", references);
331
+ return normalizedPublicationInput(findings, provider);
332
+ };
333
+ const hosted = isRecord(report) &&
334
+ (semgrep
335
+ ? isSemgrepPlatformFormat(report["format"]) ||
336
+ report["format"] === guardianSelectionFormat
337
+ : isSnykRestFormat(report["format"]));
338
+ if (!semgrep && !hosted)
339
+ fileRepositories = () => snykFileRepositories(report);
340
+ input = hosted
341
+ ? async (destination) => {
342
+ try {
343
+ const projection = await normalize(destination.url);
344
+ if (!semgrep)
345
+ return projection;
346
+ return {
347
+ ...projection,
348
+ recheckFileRepository: async (approvedUrl, platform) => {
349
+ if (platform === "other")
350
+ await normalize(approvedUrl, platform);
351
+ },
352
+ };
353
+ }
354
+ catch (error) {
355
+ const project = report["project"];
356
+ const candidate = report["sourceRepositoryUrl"] ??
357
+ settings.sourceRepositoryUrl ??
358
+ (semgrep && isRecord(project) ? project["url"] : undefined);
359
+ if (typeof candidate !== "string" || candidate === destination.url)
360
+ throw error;
361
+ // A renamed repository can require its old spelling to identify the saved request.
362
+ // Only an approved checkpoint can later authorize that spelling.
363
+ const projection = await normalize(normalizeHostedGitRepositoryUrl(candidate, destination.url, true));
364
+ return {
365
+ ...projection,
366
+ recheckFileRepository: (approvedUrl, platform) => {
367
+ if (approvedUrl === destination.url)
368
+ throw error;
369
+ return normalize(approvedUrl, platform);
370
+ },
371
+ };
372
+ }
373
+ }
374
+ : await normalize();
375
+ }
376
+ else {
377
+ const parsed = await readExternalFindings(path, settings);
378
+ input = normalizedPublicationInput(parsed.findings, provider);
379
+ if (provider === "endor")
380
+ fileRepositories = () => parsed.sourceRepositories ?? [];
381
+ }
382
+ return preparePublicationInput(input, { ...settings, provider }, dependencies, readerSources, undefined, settings.sourceRepositoryUrl, fileRepositories);
240
383
  }
241
384
  /** Prepare normalized provider evidence without reading a vendor export. */
242
385
  export async function prepareNormalizedExternalPublication(input, options, dependencies = {}) {
243
- requireExternalImports(dependencies.environment);
244
- const settings = snapshotImportOptions(options);
245
- return preparePublicationInput(normalizedPublicationInput(input), settings, dependencies);
386
+ return prepareNormalizedPublication(input, options, dependencies);
246
387
  }
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
- });
388
+ /** @internal The workbench persists acknowledgements before a repository checkpoint is retired. */
389
+ export async function prepareNormalizedPublication(input, options, dependencies, workbench) {
390
+ const settings = snapshotImportOptions(options);
391
+ requireCloudReader(settings);
392
+ return preparePublicationInput(normalizedPublicationInput(input, settings.provider), settings, dependencies, snapshotSourceRepositories(dependencies.sourceRepositories), workbench);
263
393
  }
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
- });
394
+ function cloudSourceKey(url, platform) {
395
+ return hostedRepositoryUrlKey(url, platform === "github", platform === "gitlab", "cloud");
270
396
  }
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))))
397
+ /** Verify every source in a hosted selection, including SDK inputs containing typed-only rows. */
398
+ function hostedRepositoryMatcher(input, provider, declaredUrl, destination, readerSources) {
399
+ const references = hostedSourceReferences(readerSources, destination.url, input.sourceRepositoryPlatform);
400
+ if (provider === "snyk") {
401
+ const declaredSource = input.sourceRepositoryUrl !== undefined ||
402
+ input.sourceRepositoryPlatform !== undefined ||
403
+ declaredUrl !== undefined ||
404
+ readerSources.length > 0;
405
+ const binding = snykRestImportRepository(input, provider, [
406
+ ...readerSources.map(({ url }) => url),
407
+ ...(declaredUrl === undefined ? [] : [declaredUrl]),
408
+ ], references, undefined, declaredSource ? destination.url : undefined);
409
+ if (binding)
410
+ return {
411
+ github: binding.platform === "github",
412
+ matches: (url) => snykRestRepositoryMatches(binding, url),
413
+ };
414
+ }
415
+ if (provider !== "semgrep")
294
416
  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))
417
+ const declared = input.sourceRepositoryUrl === undefined
418
+ ? undefined
419
+ : hostedRepositoryUrl(input.sourceRepositoryUrl, "Semgrep source repository");
420
+ const reportScope = semgrepPlatformReportScope(input, provider, references);
421
+ const hasNativeSource = reportScope !== undefined ||
422
+ input.findings.some((finding) => isSemgrepPlatformEvidence(finding, provider));
423
+ const claims = new Set();
424
+ let original = input.report?.["format"] === guardianSelectionFormat
425
+ ? declaredScannerRepositoryUrl(input.report["repositoryUrl"], "Guardian")
426
+ : undefined;
427
+ const nativeRows = [];
428
+ for (const finding of input.findings) {
429
+ const typed = finding.evidence.details?.repository?.url;
430
+ if (typed)
431
+ claims.add(typed);
432
+ const guardian = guardianFindingRepository(finding, "cloud");
433
+ if (guardian) {
434
+ original ??= guardian;
435
+ claims.add(guardian);
307
436
  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
- };
437
+ }
438
+ const sarif = semgrepSarifRepositories(finding, hasNativeSource);
439
+ for (const { url } of sarif)
440
+ claims.add(hostedRepositoryUrl(url, "Semgrep source repository"));
441
+ nativeRows.push({ finding, sarif });
316
442
  }
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"))
443
+ for (const { finding, sarif } of nativeRows) {
444
+ const native = semgrepPlatformFindingBinding(finding, provider, references, reportScope, [{ url: projectedBlackDuckRepositoryUrl(finding, "cloud") }, ...sarif], "cloud")?.url;
445
+ if (native) {
446
+ original ??= native;
447
+ claims.add(native);
448
+ }
449
+ }
450
+ const source = original ??
451
+ declared ??
452
+ declaredUrl ??
453
+ (readerSources.length || input.sourceRepositoryPlatform !== undefined
454
+ ? destination.url
455
+ : undefined);
456
+ if (source === undefined)
332
457
  return;
333
- const { report, ...parsed } = input;
334
- const snapshot = structuredClone(parsed);
335
- if (report !== undefined)
336
- snapshot.report = structuredClone(report);
337
- return snapshot;
458
+ const { platform } = semgrepPlatformRepositoryReference(source, references);
459
+ const expected = cloudSourceKey(source, platform);
460
+ const matches = (url) => cloudSourceKey(url, platform) === expected;
461
+ if (declared !== undefined)
462
+ claims.add(declared);
463
+ if (declaredUrl !== undefined)
464
+ claims.add(declaredUrl);
465
+ for (const reader of readerSources)
466
+ claims.add(normalizeHostedGitRepositoryUrl(reader.url, source, reader.platform !== undefined));
467
+ for (const url of claims)
468
+ if (!matches(hostedRepositoryUrl(url, "Semgrep source repository")))
469
+ throw new CodexSecurityError("The finding does not match the original Semgrep project repository.");
470
+ return { github: platform === "github", matches };
471
+ }
472
+ function ownData(value, key) {
473
+ return isRecord(value) && !isProxy(value)
474
+ ? Object.getOwnPropertyDescriptor(value, key)?.value
475
+ : undefined;
338
476
  }
339
- function publicationIdentity(parsed) {
340
- return parsed.report === undefined
341
- ? parsed.findings
342
- : { findings: parsed.findings, report: reportForJson(parsed.report) };
477
+ function publicationIdentity(parsed, provider) {
478
+ // For Trivy, the independently checked header binds the repository. Files
479
+ // and normalized rows use the same checkpoint when their findings agree.
480
+ if (parsed.report === undefined ||
481
+ (provider === "trivy" && isTrivyReportHeader(parsed.report)))
482
+ return parsed.findings;
483
+ return { findings: parsed.findings, report: reportForJson(parsed.report) };
343
484
  }
344
- function validatePublicationFindings(parsed, validateEvidence) {
485
+ function validatePublicationFindings(parsed) {
345
486
  validateExternalReport(parsed.report);
346
487
  const ids = new Set();
347
488
  for (const finding of parsed.findings) {
@@ -351,172 +492,121 @@ function validatePublicationFindings(parsed, validateEvidence) {
351
492
  if (ids.has(id))
352
493
  throw new CodexSecurityError(`Duplicate source finding id ${JSON.stringify(id)}.`);
353
494
  ids.add(id);
354
- finding.evidence = validateEvidence(finding.evidence);
495
+ finding.evidence = validateExternalEvidence(finding.evidence);
355
496
  }
356
497
  }
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;
498
+ function normalizedPublicationInput(input, provider) {
499
+ const current = snapshotNormalizedFindings(input, undefined, "cloud", provider);
500
+ if (current.snykRestContext !== undefined) {
501
+ if (provider !== "snyk")
502
+ throw new CodexSecurityError("Snyk REST evidence requires the snyk provider.");
503
+ if (!isSnykRestFormat(current.snykRestContext?.["format"]))
504
+ throw new CodexSecurityError("Expected a versioned Snyk REST report context.");
380
505
  }
506
+ const markedSemgrep = "sourceFormat" in current && isSemgrepPlatformFormat(current.sourceFormat);
507
+ if (markedSemgrep && provider !== "semgrep")
508
+ throw new CodexSecurityError("Semgrep Platform evidence requires the semgrep provider.");
509
+ if (markedSemgrep || "semgrepPlatformScope" in current)
510
+ semgrepPlatformReportScope(current, provider);
511
+ validatePublicationFindings(current);
512
+ checkFindingProvider(current, provider);
513
+ return {
514
+ current,
515
+ sourcePositions: getOpenGrepSourcePositions(ownData(input, "findings")),
516
+ };
381
517
  }
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);
518
+ // A normalized hosted input can omit native fields that the authenticated
519
+ // repository and the retained source response establish for each occurrence.
520
+ function hostedPublicationFindings(current, provider, destination, readerSources) {
521
+ if (provider === "snyk") {
522
+ const first = current.findings.find(isSnykRestFinding);
523
+ if (!first && current.snykRestContext === undefined)
524
+ return current;
525
+ const url = current.snykRestContext?.["sourceRepositoryUrl"] ??
526
+ current.sourceRepositoryUrl ??
527
+ first?.evidence.source_data?.["sourceRepositoryUrl"];
528
+ snykRestImportRepository(current, provider, [], hostedSourceReferences(readerSources, typeof url === "string" ? url : destination.url, current.sourceRepositoryPlatform), (finding, source) => {
529
+ finding.evidence.source_data = source;
396
530
  });
531
+ return current;
397
532
  }
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;
533
+ if (provider !== "semgrep")
534
+ return current;
535
+ const first = current.findings.find((finding) => isSemgrepPlatformEvidence(finding, provider));
536
+ if (!first)
537
+ return current;
538
+ const retained = first.evidence.source_data;
539
+ const project = retained?.["project"];
540
+ const url = current.sourceRepositoryUrl ??
541
+ retained?.["sourceRepositoryUrl"] ??
542
+ (isRecord(project) ? project["url"] : undefined);
543
+ const references = hostedSourceReferences(readerSources, typeof url === "string"
544
+ ? normalizeHostedGitRepositoryUrl(url, destination.url, true)
545
+ : destination.url, current.sourceRepositoryPlatform);
546
+ const scope = semgrepPlatformReportScope(current, provider, references);
547
+ const findings = current.findings.map((finding) => {
548
+ const data = finding.evidence.source_data;
549
+ const original = isRecord(data?.["finding"]) &&
550
+ isSemgrepPlatformEvidence(finding, provider);
551
+ if (!original && data?.["format"] !== semgrepPlatformFormat)
552
+ return finding;
553
+ const binding = semgrepPlatformFindingBinding(finding, provider, references, scope, undefined, "cloud");
554
+ return binding?.sourceData &&
555
+ (original || binding.sourceData["format"] !== semgrepPlatformFormat)
556
+ ? {
557
+ ...finding,
558
+ evidence: {
559
+ ...finding.evidence,
560
+ ...binding.migration,
561
+ source_data: binding.sourceData,
562
+ },
563
+ }
564
+ : finding;
565
+ });
566
+ return { ...current, findings };
567
+ }
568
+ async function preparePublicationInput(supplied, options, dependencies, readerSources, workbench, declaredUrl, fileRepositories) {
569
+ const record = workbench !== "preview" && workbench?.record;
570
+ function withReceipts(scope, prepared) {
571
+ return {
572
+ preview: prepared.preview,
573
+ async publish() {
574
+ if (!record)
575
+ return prepared.publish();
576
+ let recorded = false;
577
+ const persist = async (result) => {
578
+ if (recorded)
579
+ return;
580
+ recorded = true;
581
+ const job = result.privateJob;
582
+ const privateJobId = job?.status === "completed" &&
583
+ !result.counts.error &&
584
+ !result.unacknowledged
585
+ ? job.id
586
+ : undefined;
587
+ if (!privateJobId && !result.receipts.length)
588
+ return;
589
+ try {
590
+ await record(scope, result.receipts, privateJobId);
431
591
  }
592
+ catch (error) {
593
+ throw new ExternalPublicationError(`Cloud returned receipts, but the workbench could not record them: ${errorMessage(error)}`, result, error);
594
+ }
595
+ };
596
+ let result;
597
+ try {
598
+ result = await prepared.publish(persist);
432
599
  }
433
- catch {
434
- // A failed native clone cannot establish a historical report.
600
+ catch (error) {
601
+ if (error instanceof ExternalPublicationError)
602
+ await persist(error.result);
603
+ throw error;
435
604
  }
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: [] };
605
+ await persist(result);
606
+ return result;
607
+ },
608
+ };
471
609
  }
472
- const identity = publicationIdentity(current);
473
- return {
474
- current,
475
- legacy: current,
476
- currentIdentity: identity,
477
- legacyIdentity: identity,
478
- original,
479
- freshError,
480
- unprovenIdPrefixes: reader?.unprovenIdPrefixes,
481
- uncertainFindingIds: reader?.uncertainFindingIds,
482
- unprovenOccurrenceCount: reader?.unprovenOccurrenceCount,
483
- knownFindings: reader?.knownFindings,
484
- potentialIds: reader?.sourceFindingIds ??
485
- (original
486
- ? undefined
487
- : (native?.findings.map((row) => row.source_finding_id) ?? priorIds)),
488
- };
489
- }
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;
514
- }
515
- return value;
516
- });
517
- return verifiable;
518
- }
519
- async function preparePublicationInput(input, options, dependencies, nativePath) {
520
610
  const { inspectorProjectMap } = options;
521
611
  const progress = (event) => notify(() => dependencies.onProgress?.(event));
522
612
  if (!options.sourceKey.trim() ||
@@ -524,39 +614,57 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
524
614
  Buffer.byteLength(options.sourceKey) > 512) {
525
615
  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.");
526
616
  }
617
+ const environment = dependencies.environment ?? process.env;
618
+ dependencies.signal?.throwIfAborted();
527
619
  progress({ phase: "reading", completed: 0 });
528
- let { current: parsed, legacy: legacyParsed, currentIdentity, legacyIdentity, } = input;
529
- function noFindings() {
530
- const diagnostics = reportForJson(parsed.report)?.["errors"];
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)}` : ""}`);
620
+ const trivyReportUrls = options.provider === "trivy" && typeof supplied !== "function"
621
+ ? trivyBatchSourceUrls(supplied.current, supplied.current.findings.length > 0)
622
+ : undefined;
623
+ if (trivyReportUrls)
624
+ validateTrivyRepositoryUrl(options.repository);
625
+ const catalogKey = trivyReportUrls
626
+ ? (url) => cloudSourceKey(url, "github")
627
+ : cloudRepositoryUrlKey;
628
+ const state = externalPublicationStateDirectory(environment);
629
+ function checkSelection(input) {
630
+ const parsed = input.current;
631
+ checkTrivyReportProvider(options.provider, parsed.report);
632
+ for (const finding of parsed.findings)
633
+ checkRetainedTrivyProvider(options.provider, finding);
634
+ if (!parsed.findings.length)
635
+ throw new NoPublishableFindingsError(parsed);
532
636
  }
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();
637
+ if (typeof supplied !== "function")
638
+ checkSelection(supplied);
541
639
  progress({ phase: "discovering", completed: 0 });
542
- const environment = dependencies.environment ?? process.env;
543
- requireExternalImports(environment);
544
640
  const apiBaseUrl = cloudBaseUrl(environment);
545
641
  const cloudApiUrl = `${apiBaseUrl}/external`;
546
642
  // Validate the configured return destination before authentication or uploads.
547
643
  cloudFindingsUrl(environment, options.repository);
548
- const credentials = await (dependencies.credentials ?? (() => readCloudCredentials(environment)))();
549
- let privateProviders;
644
+ const credentials = await (dependencies.credentials ??
645
+ (() => readCloudCredentials(environment, dependencies.signal)))();
646
+ let canStartJob = false;
647
+ let repositorySeverities;
648
+ const jobVersionHeader = "Codex-Security-Private-Import-Job-Version";
649
+ const providerJobVersion = privateJobVersions[options.provider];
650
+ // Older services recognize an exact v2 request for AWS. Only ask for v3
651
+ // when publishing a provider introduced by that contract.
652
+ const requestedJobVersion = String(providerJobVersion ?? 2);
653
+ let jobApiVersion;
550
654
  async function request(endpoint, body) {
551
655
  dependencies.signal?.throwIfAborted();
552
656
  // Allow upload and response transport around the server's publication budget.
553
657
  const timeout = AbortSignal.timeout(body ? 60_000 : 30_000);
658
+ const repositoryCatalog = endpoint.startsWith("/repositories?");
554
659
  const response = await (dependencies.fetch ?? globalThis.fetch)(`${cloudApiUrl}${endpoint}`, {
555
660
  method: body ? "POST" : "GET",
556
661
  headers: {
557
662
  Authorization: `Bearer ${credentials.access_token}`,
558
663
  "ChatGPT-Account-ID": credentials.account_id,
559
664
  Accept: "application/json",
665
+ ...(repositoryCatalog
666
+ ? { [jobVersionHeader]: requestedJobVersion }
667
+ : {}),
560
668
  ...(body ? { "Content-Type": "application/json" } : {}),
561
669
  },
562
670
  ...(body ? { body: JSON.stringify(body) } : {}),
@@ -568,112 +676,113 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
568
676
  if (!response.ok) {
569
677
  const payload = (await response.json().catch(() => null));
570
678
  const retryAfter = response.headers.get("Retry-After");
571
- throw new CloudImportError(response.status, `Cloud import failed (HTTP ${response.status}). ${payload?.error?.message ?? response.statusText}${retryAfter ? ` Retry-After: ${retryAfter}.` : ""}`);
679
+ const detail = payload?.error?.message ?? response.statusText;
680
+ let message = `Cloud import failed (HTTP ${response.status}).`;
681
+ if (detail)
682
+ message += ` ${detail}`;
683
+ if (repositoryCatalog && response.status === 404) {
684
+ if (!/[.!?]$/u.test(message))
685
+ message += ".";
686
+ message +=
687
+ " Cloud import is not available for this account or deployment.";
688
+ }
689
+ if (retryAfter)
690
+ message += ` Retry-After: ${retryAfter}.`;
691
+ throw new CloudImportError(response.status, message);
572
692
  }
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()));
693
+ if (repositoryCatalog) {
694
+ const advertised = (header) => response.headers
695
+ .get(header)
696
+ ?.split(",")
697
+ .map((item) => item.trim());
698
+ repositorySeverities = advertised("Codex-Security-Repository-Import-Severities");
699
+ canStartJob =
700
+ advertised("Codex-Security-Private-Import-Providers")?.includes(options.provider) ?? false;
701
+ jobApiVersion =
702
+ response.headers.get(jobVersionHeader) === requestedJobVersion
703
+ ? providerJobVersion
704
+ : undefined;
577
705
  }
578
706
  return response.json();
579
707
  }
580
- const destinations = [];
581
- let page = null;
582
- const seenPages = new Set();
583
- do {
584
- const result = validateRepositories(await request(`/repositories?limit=20&repository=${encodeURIComponent(options.repository)}${page ? `&page=${encodeURIComponent(page)}` : ""}`));
585
- destinations.push(...result.data.filter((item) => item.id === options.repository ||
586
- cloudRepositoryUrlKey(item.url) ===
587
- cloudRepositoryUrlKey(options.repository)));
588
- page = result.next;
589
- if (result.has_more !== (page !== null) ||
590
- (page !== null && seenPages.has(page)))
591
- throw new CodexSecurityError("Cloud returned invalid repository pagination.");
592
- if (page !== null)
593
- seenPages.add(page);
594
- } while (page !== null);
595
- if (destinations.length !== 1)
596
- throw new CodexSecurityError(destinations.length
597
- ? "The repository matches multiple connectors. Select an unambiguous Cloud repository."
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.");
599
- const destination = destinations[0];
708
+ async function discoverDestination() {
709
+ const destinations = [];
710
+ let page = null;
711
+ const seenPages = new Set();
712
+ do {
713
+ const result = validateRepositories(await request(`/repositories?limit=20&repository=${encodeURIComponent(options.repository)}${page ? `&page=${encodeURIComponent(page)}` : ""}`));
714
+ destinations.push(...result.data.filter((item) => item.id === options.repository ||
715
+ catalogKey(item.url) === catalogKey(options.repository)));
716
+ page = result.next;
717
+ if (result.has_more !== (page !== null) ||
718
+ (page !== null && seenPages.has(page)))
719
+ throw new CodexSecurityError("Cloud returned invalid repository pagination.");
720
+ if (page !== null)
721
+ seenPages.add(page);
722
+ } while (page !== null);
723
+ if (destinations.length !== 1)
724
+ throw new CodexSecurityError(destinations.length
725
+ ? "The repository matches multiple connectors. Select an unambiguous Cloud repository."
726
+ : "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.");
727
+ const destination = destinations[0];
728
+ if (trivyReportUrls)
729
+ validateTrivyRepositoryUrl(destination.url);
730
+ return destination;
731
+ }
732
+ const destination = await discoverDestination();
733
+ const deferred = typeof supplied === "function";
734
+ const resolved = deferred ? await supplied(destination) : supplied;
735
+ if (deferred)
736
+ checkSelection(resolved);
737
+ const report = resolved.current;
600
738
  const source = { provider: options.provider, source_key: options.sourceKey };
601
739
  const importPrefix = source.provider === "sonar" ? "/v2" : "";
602
740
  const sourceReportsPath = importPrefix + sourcePath(destination);
603
- const state = externalPublicationStateDirectory(environment);
604
- const checkpointIdentity = [
605
- // Keep existing production checkpoints readable; other deployments must
606
- // never share a saved request or receipt with the production default.
607
- ...(apiBaseUrl === DEFAULT_CLOUD_BASE_URL ? [] : [apiBaseUrl]),
741
+ const hosted = options.provider === "snyk" || options.provider === "semgrep";
742
+ const parsed = hostedPublicationFindings(report, options.provider, destination, readerSources);
743
+ const key = hash("sha256", canonicalJson([
744
+ "external-findings/2",
745
+ apiBaseUrl,
608
746
  credentials.account_id,
609
747
  destination.id,
610
748
  destination.repo_connector_id,
611
749
  source,
612
- ];
613
- function scopeKey(identity, serialize = wireJson) {
614
- return jsonDigest([...checkpointIdentity, identity], serialize);
750
+ publicationIdentity(parsed, options.provider),
751
+ ...(workbench === "preview"
752
+ ? ["preview"]
753
+ : workbench
754
+ ? [workbench.checkout, workbench.findings]
755
+ : []),
756
+ ]));
757
+ const pendingPath = join(state, key + ".pending.json");
758
+ const underPublishLock = (operation) => withImportLock(state, key, dependencies.signal, operation);
759
+ const removePending = () => rm(pendingPath, { force: true });
760
+ const privateClient = privateImportClient(apiBaseUrl, credentials, dependencies.fetch ?? globalThis.fetch, dependencies.signal);
761
+ const scopedSemgrepReport = options.provider === "semgrep" &&
762
+ "sourceFormat" in parsed &&
763
+ isSemgrepPlatformFormat(parsed.sourceFormat);
764
+ function wasReset(content) {
765
+ return content.requests.some(({ repository }) => repository.reset_marker !== destination.reset_marker);
615
766
  }
616
- function localeKey(rows) {
617
- return hash("sha256", canonicalJson([...checkpointIdentity, rows], (left, right) => left.localeCompare(right)));
767
+ async function confirmReset() {
768
+ // A newer pending request may have been written while the catalog response
769
+ // was in flight. Confirm the reset after observing this immutable request.
770
+ const confirmed = await discoverDestination();
771
+ if (confirmed.id !== destination.id ||
772
+ confirmed.repo_connector_id !== destination.repo_connector_id ||
773
+ confirmed.reset_marker !== destination.reset_marker)
774
+ throw new CodexSecurityError("The repository changed while preparing this publication. Review the destination again.");
618
775
  }
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
- }
776
+ function suppliedSemgrepScopeDigest() {
777
+ const scope = semgrepPlatformReportScope(report, options.provider);
778
+ return hash("sha256", canonicalJson(scope?.format
779
+ ? [
780
+ scope.deployment,
781
+ canonicalNumber(scope.projectId),
782
+ scope.ref,
783
+ scope.product,
784
+ ]
785
+ : null));
677
786
  }
678
787
  function readSaved(content) {
679
788
  if (!content ||
@@ -682,659 +791,289 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
682
791
  content.requests.length === 0)
683
792
  throw new Error("Saved import account or requests do not match.");
684
793
  content.requests = content.requests.map(validateImportRequest);
685
- if (!matchesSavedDestination(content.requests))
794
+ const actual = submissionFindings(content.requests);
795
+ for (const finding of actual)
796
+ checkRetainedTrivyProvider(options.provider, finding);
797
+ if (content.requests.some((batch) => batch.repository.id !== destination.id ||
798
+ batch.repository.repo_connector_id !==
799
+ destination.repo_connector_id ||
800
+ canonicalJson(batch.source) !== canonicalJson(source)))
686
801
  throw new Error("Saved import destination does not match.");
802
+ checkFindingProvider({ ...parsed, findings: actual }, options.provider);
803
+ const expected = repositoryFindings(parsed, content.repositorySeverities);
804
+ if (canonicalJson(actual) !== canonicalJson(expected.map(wireFinding)))
805
+ throw new CodexSecurityError("Saved import evidence does not match the selected input.");
806
+ const scope = content.semgrepPartialScopeDigest;
807
+ if (scope !== undefined &&
808
+ !wasReset(content) &&
809
+ scope !== suppliedSemgrepScopeDigest())
810
+ throw new CodexSecurityError("The supplied Semgrep report scope does not match the approved request.");
687
811
  return content;
688
812
  }
689
- function validateSaved(record, legacy = false) {
690
- const content = readSaved(record);
691
- const saved = submissionFindings(content.requests);
692
- const wire = wireJson(saved);
693
- const expected = legacy ? legacyParsed.findings : parsed.findings;
694
- const projection = content.inputProjection;
695
- if (projection &&
696
- (!sameFindingSelection(saved, expected) ||
697
- projection.inputDigest !== (legacy ? legacyInputDigest : inputDigest) ||
698
- projection.savedEvidenceDigest !== hash("sha256", wire)))
699
- throw new Error("Saved import evidence does not match the selected input.");
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();
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
- };
747
- return content;
748
- }
749
- function unmatchedPreviousNumbers() {
750
- return new CodexSecurityError("The older saved import cannot be matched to the selected input. Its checkpoint was preserved without uploading.");
813
+ async function readPending() {
814
+ const bytes = await readOptionalFile(pendingPath);
815
+ return bytes === undefined ? undefined : readSaved(JSON.parse(bytes));
751
816
  }
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");
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);
776
- }
777
- let recovered;
778
- for (const legacyKey of legacyKeys) {
779
- if (legacyKey === key || legacyKey === originalKey)
780
- continue;
781
- const legacyPath = join(state, `${legacyKey}.pending.json`);
782
- const captured = await readOptionalFile(legacyPath);
783
- if (captured !== undefined) {
784
- // Preserve requests staged by the earlier locale-based filename format.
785
- // Hold both publication locks while moving the original checkpoint.
786
- await underLocks([key, legacyKey], async () => {
787
- const current = await readOptionalFile(canonicalPath);
788
- const serialized = await readOptionalFile(legacyPath);
789
- let prior;
790
- if (serialized !== undefined) {
791
- try {
792
- prior = JSON.parse(serialized);
793
- }
794
- catch (error) {
795
- if (current === undefined || !(error instanceof SyntaxError))
796
- throw error;
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);
805
- return;
806
- }
807
- // A report-free approval at the locale-era filename can be a separate input.
808
- if (legacyKey !== legacyIdentityKey &&
809
- prior?.inputProjection &&
810
- prior.inputProjection.inputDigest !== legacyInputDigest) {
811
- const projection = prior.inputProjection;
812
- const rows = legacyParsed.findings;
813
- if (projection.identityFormat === 2 ||
814
- (projection.inputDigest === jsonDigest(rows) &&
815
- projection.savedEvidenceDigest ===
816
- jsonDigest(submissionFindings(prior.requests))))
817
- return;
818
- // The released SDK hashed report-free rows as an array and boxed scores as {}.
819
- const originalRows = original?.parsed.findings ?? rows;
820
- if (hasBoxedScalars &&
821
- legacyParsed.report !== undefined &&
822
- legacyKey === scopeKey(originalRows, canonicalJson) &&
823
- projection.inputDigest ===
824
- jsonDigest(originalRows, canonicalJson) &&
825
- projection.savedEvidenceDigest ===
826
- jsonDigest(originalRows.map(wireFinding), canonicalJson) &&
827
- sameFindingSelection(submissionFindings(readSaved(prior).requests), rows))
828
- return;
829
- }
830
- const content = validateSaved(serialized === undefined ? JSON.parse(captured) : prior, true);
831
- if (!sameFindingSelection(legacyParsed.findings, parsed.findings))
832
- throw new CodexSecurityError("The saved import selects different findings from this input. Resume the original selection before publishing this selection.");
833
- await retireIfReset(content, async () => {
834
- if (serialized !== undefined)
835
- await rm(legacyPath);
836
- });
837
- const savedEvidenceDigest = content.inputProjection?.savedEvidenceDigest ??
838
- jsonDigest(legacyParsed.findings);
839
- const rebindProjection = content.inputProjection !== undefined ||
840
- savedEvidenceDigest !== inputDigest;
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
- }
851
- recovered = content;
852
- if (serialized === undefined)
853
- return;
854
- if (!rebindProjection) {
855
- await rename(legacyPath, canonicalPath);
856
- }
857
- else {
858
- // Bind the new input projection to the original immutable evidence,
859
- // so later inventory reordering still resumes these exact requests.
860
- await writeAtomicJson(canonicalPath, content);
861
- await rm(legacyPath);
862
- }
863
- });
864
- }
865
- }
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
- }
817
+ async function readCompletedPublication(requests) {
818
+ const bytes = await readOptionalFile(join(state, key + ".result.json"));
819
+ if (bytes === undefined)
820
+ return;
821
+ const result = JSON.parse(bytes);
822
+ if ((result.status === "complete" || result.status === "partial") &&
823
+ result.receipts.length === requests.length &&
824
+ result.receipts.every((r, i) => r.id === requests[i].request_id))
825
+ return {
826
+ receipts: result.receipts.map((r, i) => validatePublicationReceipt(r, requests[i])),
827
+ verified: result.verified,
828
+ };
1008
829
  }
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
- }
830
+ await validatePreparedOutputDir(state, undefined, true).catch(nullIfMissingFile);
831
+ const saved = await readPending();
832
+ if (saved && wasReset(saved)) {
833
+ await underPublishLock(async () => {
834
+ const current = await readPending();
835
+ if (current && sameSubmission(current, saved)) {
836
+ await confirmReset();
837
+ if (record && current.receipts?.length) {
838
+ await record({
839
+ cloudApiUrl,
840
+ accountId: credentials.account_id,
841
+ destination,
842
+ requests: current.requests,
843
+ findings: [],
844
+ }, acknowledgedReceipts(current));
1073
845
  }
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);
1100
- });
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);
846
+ await removePending();
1116
847
  }
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
848
  });
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;
849
+ 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.");
1148
850
  }
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);
851
+ if (saved?.inspectorProjectMapRequired && !inspectorProjectMap)
852
+ throw new CodexSecurityError("Supply the verified Inspector project binding again to resume.");
853
+ // Existing repository submissions keep their original request bodies and receipts.
854
+ const privatePath = join(state, `${key}.private-import.json`);
855
+ const readPrivate = async () => parsePrivateCheckpoint(await readOptionalFile(privatePath));
856
+ const privateSaved = await readPrivate();
857
+ const usePrivateImport = !saved && (canStartJob || privateSaved !== undefined);
858
+ async function preparePrivateAttempt() {
859
+ const context = await privateClient.context();
860
+ if (source.source_key.trim() !== source.source_key)
861
+ throw new CodexSecurityError("A private source key cannot have leading or trailing whitespace.");
862
+ const report = selectPrivateReport(parsed.findings, source.provider, context, privateSaved?.contextVersion === context.context_version
863
+ ? privateSaved.format
864
+ : undefined);
865
+ const binding = {
866
+ format: report.format,
867
+ apiBaseUrl,
868
+ accountId: credentials.account_id,
869
+ source,
870
+ repository: {
871
+ id: destination.id,
872
+ repo_connector_id: destination.repo_connector_id,
873
+ },
874
+ sha256: report.digest,
875
+ contextVersion: context.context_version,
876
+ };
877
+ return { context, report, binding };
1179
878
  }
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);
879
+ function acceptsJob(context, format) {
880
+ return context.can_create_import_jobs && context.formats.includes(format);
1184
881
  }
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
- });
882
+ // Use an earlier catalog URL only after the account, connector, repository,
883
+ // inbox context and exact upload digest all match the immutable checkpoint.
884
+ const privateAttempt = usePrivateImport
885
+ ? await preparePrivateAttempt()
886
+ : undefined;
887
+ const previous = privateAttempt &&
888
+ readPrivateCheckpoint(privateSaved, privateAttempt.binding);
889
+ const retryOfJobId = unsuccessfulJob(previous?.receipt) &&
890
+ canStartJob &&
891
+ privateAttempt?.context.can_upload &&
892
+ acceptsJob(privateAttempt.context, privateAttempt.report.format)
893
+ ? previous?.jobId
894
+ : undefined;
895
+ const resumed = previous !== undefined && retryOfJobId === undefined;
896
+ const privateApproval = resumed ? previous : undefined;
897
+ const privateUrl = privateApproval?.approvedRepositoryUrl;
898
+ const approvedPlatform = saved?.approvedRepositoryPlatform ??
899
+ privateApproval?.approvedRepositoryPlatform;
900
+ // The findings-only checkpoint key lets projected and native callers resume one immutable
901
+ // request. Supplemental report claims are outside that digest and must still match its source.
902
+ const approvedUrl = saved?.approvedRepositoryUrl ??
903
+ saved?.trivyRepositoryUrl ??
904
+ privateUrl ??
905
+ destination.url;
906
+ const sourceFor = (url, platform) => hostedRepositoryMatcher(platform ? { ...report, sourceRepositoryPlatform: platform } : report, options.provider, declaredUrl, { url }, readerSources);
907
+ if (readerSources.some(({ platform, url }) => {
908
+ const source = normalizeHostedGitRepositoryUrl(url, approvedUrl, platform !== undefined);
909
+ return (cloudSourceKey(source, platform) !==
910
+ cloudSourceKey(approvedUrl, platform));
911
+ }))
912
+ throw new CodexSecurityError("The reader's source repository does not match the selected Cloud repository.");
913
+ await resolved.recheckFileRepository?.(approvedUrl, approvedPlatform);
914
+ const hostedSource = hosted
915
+ ? sourceFor(approvedUrl, approvedPlatform)
916
+ : undefined;
917
+ const matchesHostedRepository = hostedSource?.matches;
918
+ function requireApprovedSource(url, platform) {
919
+ const binding = platform && hostedSource?.github
920
+ ? sourceFor(url, platform)
921
+ : hostedSource;
922
+ if (binding && !binding.matches(url))
923
+ throw new CodexSecurityError("The source project does not match the selected Cloud repository.");
1201
924
  }
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
- }));
925
+ requireApprovedSource(approvedUrl);
926
+ const trivyTarget = saved?.trivyRepositoryUrl ?? privateUrl ?? destination.url;
927
+ function requireTrivyRemote(url, id) {
928
+ if (trivyRemoteKey(url, trivyTarget) !== catalogKey(trivyTarget)) {
929
+ const label = id === undefined ? "Source report" : `Finding ${JSON.stringify(id)}`;
930
+ throw new CodexSecurityError(`${label} belongs to ${JSON.stringify(url)}, which does not match the selected Cloud repository ${JSON.stringify(trivyTarget)}. Verify the source repository mapping before publishing.`);
931
+ }
1214
932
  }
1215
- if (saved?.inspectorProjectMapRequired && !inspectorProjectMap)
1216
- throw new CodexSecurityError("Supply the verified Inspector project binding again to resume.");
1217
- function checkRepositories(findings, report) {
933
+ for (const url of trivyReportUrls ?? [])
934
+ requireTrivyRemote(url);
935
+ function checkRepositories({ findings, report }, historical = Boolean(saved)) {
936
+ const { provider } = options;
937
+ checkTrivyReportProvider(provider, report);
938
+ // A catalog casing or spelling change keeps the original source comparison target.
939
+ const approved = hosted ? saved?.approvedRepositoryUrl : undefined;
940
+ const sameCatalog = approved !== undefined &&
941
+ cloudSourceKey(approved, "github") ===
942
+ cloudSourceKey(destination.url, "github");
943
+ const comparisonUrl = trivyReportUrls
944
+ ? trivyTarget
945
+ : sameCatalog
946
+ ? approved
947
+ : (privateUrl ?? destination.url);
948
+ const key = sameCatalog
949
+ ? (url) => cloudSourceKey(url, saved?.approvedRepositoryPlatform === "other"
950
+ ? undefined
951
+ : "github")
952
+ : catalogKey;
1218
953
  function requireDestination(id, url) {
1219
954
  if (url &&
1220
- cloudRepositoryUrlKey(url) !== cloudRepositoryUrlKey(destination.url))
955
+ (matchesHostedRepository
956
+ ? !matchesHostedRepository(url) ||
957
+ !matchesHostedRepository(comparisonUrl)
958
+ : key(url) !== key(comparisonUrl)))
1221
959
  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
960
  }
961
+ if (provider === "armorcode")
962
+ requireDestination("ArmorCode source", declaredUrl);
963
+ if (report?.["format"] === guardianSelectionFormat)
964
+ requireDestination("Guardian source", declaredScannerRepositoryUrl(report["repositoryUrl"], "Guardian"));
965
+ // An immutable retry may refer to an earlier repository name. Additional
966
+ // file declarations outside its saved findings authorize no new writes.
967
+ if (!historical)
968
+ for (const { url } of fileRepositories?.() ?? [])
969
+ requireDestination("Source report", provider === "snyk"
970
+ ? normalizeHostedGitRepositoryUrl(url, destination.url, true)
971
+ : url);
1223
972
  // Historical checkpoints already fixed the provider and request bytes.
1224
973
  // 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"))
974
+ const checkNative = (...providers) => !historical ||
975
+ trivyReportUrls !== undefined ||
976
+ providers.includes(provider);
977
+ if (checkNative("sonar")) {
978
+ for (const url of sonarRepositoryUrls(provider, findings, report, "cloud"))
1228
979
  requireDestination("Sonar source", url);
1229
980
  }
1230
- const repositories = replay
1231
- ? undefined
1232
- : blackDuckReportRepositories(options.provider, findings, "cloud");
1233
- for (const [provider, repositories] of [
981
+ const checkmarx = checkmarxSelection(provider, findings, report, "cloud", historical);
982
+ requireDestination("Checkmarx source", checkmarx.reportUrl);
983
+ const blackDuck = checkNative("blackduck")
984
+ ? blackDuckReportRepositories(provider, findings, "cloud")
985
+ : undefined;
986
+ const checkmarxRepository = checkmarx.repositories();
987
+ for (const [scanner, repositories] of [
1234
988
  ["veracode", veracodeReportRepositories],
1235
989
  ["continuum", continuumReportRepositories],
1236
990
  ]) {
1237
- if (saved && options.provider !== provider)
991
+ if (!checkNative(scanner))
1238
992
  continue;
1239
- for (const [id, source] of repositories(options.provider, findings, "cloud"))
993
+ for (const [id, source] of repositories(provider, findings, "cloud"))
1240
994
  requireDestination(id, source.url);
1241
995
  }
1242
- if (!replay ||
1243
- options.provider === "inspector" ||
1244
- options.provider === "veracode") {
1245
- const binding = inspectorReportRepository(options.provider, findings, "cloud", inspectorProjectMap);
996
+ if (checkNative("blackduck", "inspector", "veracode")) {
997
+ const binding = inspectorReportRepository(provider, findings, "cloud", inspectorProjectMap);
1246
998
  if (binding)
1247
999
  requireDestination(binding.findingId, binding.url);
1248
1000
  }
1001
+ const checkOnlyProjected = !checkNative("armorcode", "blackduck", "inspector", "veracode", "checkmarx", "continuum", "sonar");
1249
1002
  for (const finding of findings) {
1250
1003
  const { source_finding_id: id, evidence } = finding;
1251
- const projected = repositories
1252
- ? repositories.get(id)?.url
1004
+ const guardian = guardianFindingRepository(finding, "cloud");
1005
+ if (guardian) {
1006
+ requireDestination(id, guardian);
1007
+ requireDestination(id, evidence.details?.repository?.url);
1008
+ }
1009
+ checkRetainedTrivyProvider(provider, finding);
1010
+ if (trivyReportUrls) {
1011
+ const remote = trivyRepositoryUrl(retainedTrivyReport(finding), trivyTarget);
1012
+ if (remote)
1013
+ requireTrivyRemote(remote, id);
1014
+ }
1015
+ const blackDuckUrl = blackDuck
1016
+ ? blackDuck.get(id)?.url
1253
1017
  : projectedBlackDuckRepositoryUrl(finding, "cloud");
1254
- if (replay &&
1255
- !projected &&
1256
- options.provider !== "inspector" &&
1257
- options.provider !== "veracode" &&
1258
- options.provider !== "continuum" &&
1259
- options.provider !== "sonar")
1018
+ const hasProjection = !!blackDuckUrl || checkmarx.projections.has(finding);
1019
+ if (checkOnlyProjected && !hasProjection)
1260
1020
  continue;
1261
- const urls = [
1262
- projected,
1021
+ let urls = [
1022
+ blackDuckUrl,
1023
+ checkmarxRepository(finding)?.url,
1263
1024
  evidence.details?.repository?.url,
1264
- options.provider === "wiz"
1265
- ? projected
1266
- ? wizFindingRepository(evidence, "cloud")?.url
1267
- : wizCsvRepositoryUrl(evidence)
1268
- : undefined,
1269
- ...(options.provider === "snyk"
1270
- ? snykRepositoryUrls(evidence.source_data, {
1271
- cloudRepositoryUrl: destination.url,
1272
- })
1273
- : []),
1274
- ...(options.provider === "endor"
1275
- ? endorRepositoryUrls(evidence.source_data)
1276
- : []),
1277
- options.provider === "github"
1278
- ? dependabotRepository(finding)
1279
- : undefined,
1280
1025
  ];
1026
+ if (provider === "armorcode") {
1027
+ urls.push(armorCodeRepositoryUrl(evidence.source_data, "cloud"));
1028
+ }
1029
+ else if (provider === "wiz") {
1030
+ const native = hasProjection ||
1031
+ (!historical && isWizPackageFinding(evidence.source_data));
1032
+ urls.push(native
1033
+ ? wizFindingRepository(evidence, "cloud")?.url
1034
+ : wizCsvRepositoryUrl(evidence));
1035
+ }
1036
+ else if (provider === "snyk") {
1037
+ urls = urls.concat(snykRepositoryUrls(evidence.source_data, {
1038
+ cloudRepositoryUrl: destination.url,
1039
+ }));
1040
+ }
1041
+ else if (provider === "endor") {
1042
+ urls = urls.concat(endorRepositoryUrls(evidence.source_data, !blackDuck && !blackDuckUrl));
1043
+ }
1044
+ else if (provider === "github") {
1045
+ if (!historical)
1046
+ urls.push(retainedGitHubRepository(finding));
1047
+ urls.push(dependabotRepository(finding));
1048
+ }
1281
1049
  for (const url of urls)
1282
1050
  requireDestination(id, url);
1283
1051
  }
1284
1052
  }
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);
1053
+ if (!saved) {
1054
+ for (const finding of parsed.findings) {
1055
+ if (options.provider !== "semgrep" &&
1056
+ isSemgrepPlatformEvidence(finding, options.provider))
1057
+ throw new CodexSecurityError("Semgrep Platform evidence requires the semgrep provider.");
1058
+ if (options.provider !== "snyk" && isSnykRestFinding(finding))
1059
+ throw new CodexSecurityError("Snyk REST evidence requires the snyk provider.");
1321
1060
  }
1322
- const previous = checkpointForContext(privateSaved);
1323
- function requireAccess(value, savedFile, savedJob) {
1061
+ }
1062
+ if (!privateAttempt)
1063
+ checkRepositories(parsed);
1064
+ if (privateAttempt) {
1065
+ const client = privateClient;
1066
+ const { context, report, binding } = privateAttempt;
1067
+ function requireAccess(value, saved) {
1324
1068
  if (!value.can_read ||
1325
- (!savedJob &&
1326
- ((!savedFile && !value.can_upload) ||
1327
- !value.can_create_import_jobs ||
1328
- !value.formats.includes(report.format))))
1069
+ (!saved && !canStartJob) ||
1070
+ (!saved?.jobId &&
1071
+ ((!saved?.fileId && !value.can_upload) ||
1072
+ !acceptsJob(value, report.format))))
1329
1073
  throw new CodexSecurityError("Private findings import is unavailable for this account or report format.");
1330
1074
  }
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;
1075
+ checkRepositories(parsed, resumed);
1076
+ requireAccess(context, previous);
1338
1077
  const privateImport = {
1339
1078
  format: report.format,
1340
1079
  contextVersion: binding.contextVersion,
@@ -1348,27 +1087,32 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
1348
1087
  accountId: credentials.account_id,
1349
1088
  destination,
1350
1089
  source,
1351
- resumed: previous !== undefined && !retryOfJobId,
1090
+ resumed,
1352
1091
  requests: [],
1353
1092
  privateImport,
1354
1093
  };
1355
- return {
1094
+ return withReceipts(preview, {
1356
1095
  preview: structuredClone(preview),
1357
- publish: () => underLocks(lockKeys, async () => {
1096
+ publish: () => underPublishLock(async () => {
1358
1097
  const current = await client.context();
1359
1098
  if (current.context_version !== binding.contextVersion)
1360
1099
  throw new CodexSecurityError("The private inbox changed. Run the command again to review the new destination.");
1361
- const stored = checkpointForContext(await readOptionalFile(privatePath));
1100
+ const stored = readPrivateCheckpoint(await readPrivate(), binding);
1101
+ if (previous && !stored)
1102
+ throw new CodexSecurityError("The saved private import changed. Run the command again to preview this report.");
1362
1103
  // Only a preview of this terminal job can start a new attempt.
1363
1104
  const retrying = retryOfJobId !== undefined && stored?.receipt?.id === retryOfJobId;
1364
1105
  const active = retrying ? undefined : stored;
1365
- requireAccess(current, !!active?.fileId, !!active?.jobId);
1366
- if (await selectedCheckpoint())
1106
+ requireAccess(current, active);
1107
+ if (await readPending())
1367
1108
  throw new CodexSecurityError("A repository submission is pending. Review its saved request before continuing.");
1368
1109
  const checkpoint = active ?? {
1369
1110
  ...binding,
1111
+ approvedRepositoryUrl: destination.url,
1112
+ approvedRepositoryPlatform: hostedSource?.github === false ? "other" : undefined,
1370
1113
  uploadRequestId: randomUUID(),
1371
1114
  jobRequestId: randomUUID(),
1115
+ jobApiVersion: retrying ? stored?.jobApiVersion : jobApiVersion,
1372
1116
  fileId: undefined,
1373
1117
  jobId: undefined,
1374
1118
  receipt: undefined,
@@ -1433,6 +1177,7 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
1433
1177
  source,
1434
1178
  repository: binding.repository,
1435
1179
  jobId: checkpoint.jobId,
1180
+ jobApiVersion: checkpoint.jobApiVersion,
1436
1181
  }, async (id) => {
1437
1182
  checkpoint.jobId = id;
1438
1183
  await writeAtomicJson(privatePath, checkpoint);
@@ -1451,17 +1196,22 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
1451
1196
  throw new ExternalPublicationError(result.error, result, error);
1452
1197
  }
1453
1198
  }),
1454
- };
1199
+ });
1455
1200
  }
1201
+ // The original selection and request bytes govern a retry even if discovery changes.
1202
+ const severities = saved ? saved.repositorySeverities : repositorySeverities;
1203
+ const direct = repositorySelection(parsed, severities, resolved.sourcePositions);
1204
+ if (!direct.findings.length)
1205
+ throw new NoPublishableFindingsError(direct);
1456
1206
  let requests = saved?.requests;
1457
1207
  if (!requests) {
1458
1208
  let preparedCount = 0;
1459
1209
  progress({
1460
1210
  phase: "preparing",
1461
1211
  completed: 0,
1462
- total: parsed.findings.length,
1212
+ total: direct.findings.length,
1463
1213
  });
1464
- const previousReports = await readInBatches(parsed.findings, async (finding) => {
1214
+ const previousReports = await readInBatches(direct.findings, async (finding) => {
1465
1215
  const query = new URLSearchParams({
1466
1216
  provider: source.provider,
1467
1217
  source_key: source.source_key,
@@ -1482,14 +1232,14 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
1482
1232
  progress({
1483
1233
  phase: "preparing",
1484
1234
  completed: ++preparedCount,
1485
- total: parsed.findings.length,
1235
+ total: direct.findings.length,
1486
1236
  });
1487
1237
  return previous;
1488
1238
  });
1489
1239
  requests = [];
1490
1240
  let current;
1491
1241
  let currentBytes = 0;
1492
- for (const [index, finding] of parsed.findings.entries()) {
1242
+ for (const [index, finding] of direct.findings.entries()) {
1493
1243
  const previous = previousReports[index];
1494
1244
  const environmentId = previous?.environment_id ?? destination.import_environment_id;
1495
1245
  if (environmentId == null)
@@ -1531,22 +1281,29 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
1531
1281
  if (current)
1532
1282
  requests.push(validateImportRequest(current));
1533
1283
  }
1284
+ const bindSemgrepScope = !saved &&
1285
+ options.provider === "semgrep" &&
1286
+ report.findings.some((finding) => !isSemgrepPlatformEvidence(finding, "semgrep") &&
1287
+ (scopedSemgrepReport ||
1288
+ hasRetainedSemgrepScope(finding.evidence.source_data?.["finding"])));
1534
1289
  const submission = {
1535
1290
  accountId: credentials.account_id,
1291
+ ...(trivyReportUrls && { trivyRepositoryUrl: trivyTarget }),
1536
1292
  requests,
1537
1293
  receipts: saved?.receipts,
1538
- legacyApproval: saved?.legacyApproval,
1539
- wireKey: saved?.wireKey ?? (fileKey === key ? undefined : key),
1540
- inputProjection: saved?.inputProjection ?? {
1541
- identityFormat: saved ? undefined : 2,
1542
- inputDigest,
1543
- savedEvidenceDigest: jsonDigest(submissionFindings(requests)),
1544
- },
1294
+ repositorySeverities: severities,
1295
+ semgrepPartialScopeDigest: bindSemgrepScope
1296
+ ? suppliedSemgrepScopeDigest()
1297
+ : saved?.semgrepPartialScopeDigest,
1298
+ approvedRepositoryUrl: hosted ? approvedUrl : undefined,
1545
1299
  };
1300
+ if (saved?.approvedRepositoryPlatform ||
1301
+ (hostedSource && !hostedSource.github))
1302
+ submission.approvedRepositoryPlatform = "other";
1546
1303
  if (inspectorProjectMap)
1547
1304
  submission.inspectorProjectMapRequired = true;
1548
1305
  const preview = {
1549
- ...parsed,
1306
+ ...direct,
1550
1307
  findings: submissionFindings(requests),
1551
1308
  cloudApiUrl,
1552
1309
  cloudUrl: cloudFindingsUrl(environment, destination.id),
@@ -1556,9 +1313,9 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
1556
1313
  resumed: saved !== undefined,
1557
1314
  requests,
1558
1315
  };
1559
- return {
1316
+ return withReceipts(preview, {
1560
1317
  preview: cloneImportPreview(preview),
1561
- async publish() {
1318
+ async publish(beforeRetire) {
1562
1319
  progress({ phase: "waiting", completed: 0 });
1563
1320
  const result = (acknowledged, verified) => {
1564
1321
  const counts = { created: 0, updated: 0, unchanged: 0, error: 0 };
@@ -1580,17 +1337,17 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
1580
1337
  return {
1581
1338
  cloudApiUrl,
1582
1339
  cloudUrl: preview.cloudUrl,
1583
- status: parsed.excluded.length || counts.error ? "partial" : "complete",
1340
+ status: direct.excluded.length || counts.error ? "partial" : "complete",
1584
1341
  read: parsed.read,
1585
1342
  ...(parsed.report === undefined
1586
1343
  ? {}
1587
1344
  : snapshotNormalizedFindings({ report: parsed.report })),
1588
- ready: parsed.findings.length,
1589
- excluded: parsed.excluded,
1345
+ ready: direct.findings.length,
1346
+ excluded: direct.excluded,
1590
1347
  receipts: acknowledged,
1591
1348
  counts,
1592
1349
  failures,
1593
- unacknowledged: parsed.findings.length -
1350
+ unacknowledged: direct.findings.length -
1594
1351
  Object.values(counts).reduce((sum, count) => sum + count, 0),
1595
1352
  verified,
1596
1353
  };
@@ -1598,100 +1355,88 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
1598
1355
  function interruptedPreview(message, cause, savedSubmission) {
1599
1356
  // The expected file is gone or another publisher owns the lock. Use only
1600
1357
  // 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
1358
  return new ExternalPublicationError(message, {
1606
- ...result(acknowledged, 0),
1359
+ ...result(acknowledgedReceipts(submission), 0),
1607
1360
  status: "interrupted",
1608
1361
  error: message,
1609
1362
  ...(savedSubmission === undefined ? {} : { savedSubmission }),
1610
1363
  }, cause);
1611
1364
  }
1612
1365
  let acquired = false;
1613
- return await underLocks(lockKeys, async () => {
1366
+ return await underPublishLock(async () => {
1614
1367
  acquired = true;
1615
1368
  // Re-read under the lock to keep checkpoints from another publisher.
1616
- const selected = await selectedCheckpoint(saved ? fileKey : undefined).catch((error) => {
1369
+ let pending = await readPending().catch((error) => {
1617
1370
  if (!submission.receipts?.length)
1618
1371
  throw error;
1619
1372
  throw interruptedPreview(errorMessage(error), error);
1620
1373
  });
1621
- const pending = selected?.content;
1622
1374
  const current = pending ?? submission;
1623
1375
  const adoptProjectMap = submission.inspectorProjectMapRequired &&
1624
1376
  current.inspectorProjectMapRequired === undefined;
1625
1377
  if (adoptProjectMap)
1626
1378
  current.inspectorProjectMapRequired = true;
1627
- if (!selected ||
1628
- selected.fileKey !== fileKey ||
1629
- selected.historical ||
1630
- !sameSubmission(current, submission)) {
1631
- const message = selected
1379
+ if (current.approvedRepositoryUrl)
1380
+ requireApprovedSource(current.approvedRepositoryUrl, current.approvedRepositoryPlatform);
1381
+ if (!pending || !sameSubmission(current, submission)) {
1382
+ const message = pending
1632
1383
  ? "Another publication prepared this input. Run the command again to review and resume that saved request."
1633
1384
  : "The saved publication changed after preview. Review its result before publishing again.";
1634
1385
  if (saved) {
1635
- const completed = await readCompletedPublication(requests, fileKey);
1386
+ const completed = await readCompletedPublication(requests);
1636
1387
  if (completed)
1637
1388
  return result(completed.receipts, completed.verified);
1638
1389
  throw interruptedPreview(message);
1639
1390
  }
1640
- if (selected)
1391
+ if (pending)
1641
1392
  throw new CodexSecurityError(message);
1642
1393
  if ((await readOptionalFile(privatePath)) !== undefined)
1643
1394
  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
1395
  }
1647
- const receipts = (current.receipts ?? []).map(validateImportReceipt);
1648
- if (receipts.length > requests.length)
1396
+ // A preview can pin the catalog URL. Tighten its SCM comparison only
1397
+ // once the caller confirms publication, including when resuming a request.
1398
+ const adoptStrictRepository = submission.approvedRepositoryPlatform === "other" &&
1399
+ !current.approvedRepositoryPlatform;
1400
+ if (adoptStrictRepository)
1401
+ current.approvedRepositoryPlatform = "other";
1402
+ const cached = (current.receipts ?? []).map(validateImportReceipt);
1403
+ if (cached.length > requests.length)
1649
1404
  throw new CodexSecurityError("Saved publication has more receipts than requests.");
1650
1405
  const acknowledged = [];
1651
1406
  let verified = 0;
1652
- let persisted = pending !== undefined;
1653
1407
  try {
1654
- if (selected?.checkedCanonical)
1655
- current.canonicalChecked = true;
1656
- if (pending === undefined || adoptProjectMap) {
1408
+ if (!pending || adoptProjectMap || adoptStrictRepository) {
1657
1409
  await writeAtomicJson(pendingPath, current);
1658
- persisted = true;
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);
1410
+ pending = current;
1663
1411
  }
1664
- await rememberFile();
1665
1412
  progress({
1666
1413
  phase: "uploading",
1667
1414
  completed: 0,
1668
- total: parsed.findings.length,
1415
+ total: direct.findings.length,
1669
1416
  });
1670
1417
  let acknowledgedCount = 0;
1671
1418
  for (const [batchIndex, batch] of requests.entries()) {
1672
- const receipt = validatePublicationReceipt(receipts[batchIndex] ??
1419
+ const receipt = validatePublicationReceipt(cached[batchIndex] ??
1673
1420
  (await request(`${importPrefix}/finding_imports`, batch)), batch);
1674
1421
  acknowledged.push(receipt);
1675
1422
  acknowledgedCount += receipt.item_count;
1676
1423
  progress({
1677
1424
  phase: "uploading",
1678
1425
  completed: acknowledgedCount,
1679
- total: parsed.findings.length,
1426
+ total: direct.findings.length,
1680
1427
  });
1681
- if (batchIndex === receipts.length) {
1682
- receipts.push(receipt);
1428
+ if (batchIndex >= cached.length) {
1683
1429
  // Acknowledged batches must not consume another POST quota on retry.
1684
1430
  // Readback can still resume after this checkpoint without replaying writes.
1685
1431
  await writeAtomicJson(pendingPath, {
1686
- ...submission,
1687
- canonicalChecked: current.canonicalChecked,
1688
- receipts,
1432
+ ...current,
1433
+ receipts: acknowledged,
1689
1434
  });
1690
1435
  }
1691
1436
  }
1692
1437
  // Verify readable source records without mistaking a newer concurrent
1693
1438
  // observation for failure of the original, immutable import receipt.
1694
- const readbacks = receipts.flatMap((receipt, batchIndex) => {
1439
+ const readbacks = acknowledged.flatMap((receipt, batchIndex) => {
1695
1440
  const batch = requests[batchIndex];
1696
1441
  const batchItems = new Map(batch.items.map((item) => [item.client_id, item]));
1697
1442
  return receipt.results
@@ -1714,12 +1459,12 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
1714
1459
  report.version < item.version ||
1715
1460
  (report.version === item.version &&
1716
1461
  (report.observation_id !== item.observation_id ||
1717
- wireJson(report.evidence) !== wireJson(expected.evidence))) ||
1462
+ !matchesRepositoryEvidence(expected.evidence, report.evidence))) ||
1718
1463
  report.source_finding_id !== expected.source_finding_id ||
1719
1464
  report.repo_id !== destination.id ||
1720
1465
  report.repo_connector_id !== destination.repo_connector_id ||
1721
1466
  report.environment_id !== batch.repository.environment_id ||
1722
- wireJson(report.source) !== wireJson(source))
1467
+ canonicalJson(report.source) !== canonicalJson(source))
1723
1468
  throw new CodexSecurityError("Cloud readback did not match the saved finding identity.");
1724
1469
  progress({
1725
1470
  phase: "verifying",
@@ -1732,20 +1477,28 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
1732
1477
  ...completed,
1733
1478
  report: reportForJson(completed.report),
1734
1479
  });
1480
+ await beforeRetire?.(completed);
1735
1481
  await removePending();
1736
1482
  return completed;
1737
1483
  }
1738
1484
  catch (error) {
1739
1485
  if (error instanceof CloudImportError && error.status === 409) {
1740
- await removePending();
1741
- persisted = false;
1486
+ try {
1487
+ await beforeRetire?.(result(acknowledged, verified));
1488
+ await removePending();
1489
+ pending = undefined;
1490
+ }
1491
+ catch (recordError) {
1492
+ // Keep acknowledged batches resumable when their receipts are not durable.
1493
+ error = recordError;
1494
+ }
1742
1495
  }
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."}`;
1496
+ const message = `${error instanceof Error ? error.message : String(error)} ${error instanceof CloudImportError && error.status === 409 ? "The old submission was retired. Rediscover and approve a fresh publication." : pending ? `Repeat the same command to resume the saved request. Saved submission: ${pendingPath}` : "No resumable request was saved. Correct the error and review the input before retrying."}`;
1744
1497
  throw new ExternalPublicationError(message, {
1745
1498
  ...result(acknowledged, verified),
1746
1499
  status: "interrupted",
1747
1500
  error: message,
1748
- ...(persisted ? { savedSubmission: pendingPath } : {}),
1501
+ ...(pending ? { savedSubmission: pendingPath } : {}),
1749
1502
  }, error);
1750
1503
  }
1751
1504
  }).catch((error) => {
@@ -1757,6 +1510,6 @@ async function preparePublicationInput(input, options, dependencies, nativePath)
1757
1510
  throw interruptedPreview(message, error, saved ? pendingPath : undefined);
1758
1511
  });
1759
1512
  },
1760
- };
1513
+ });
1761
1514
  }
1762
1515
  //# sourceMappingURL=external-findings-publish.js.map