@openai/codex-security 0.8.2 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (375) hide show
  1. package/README.md +43 -61
  2. package/_bundled_plugin/.app.json +4 -0
  3. package/_bundled_plugin/.mcp.json +1 -0
  4. package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
  5. package/_bundled_plugin/mcp/helpers.mjs.br.part-001 +0 -0
  6. package/_bundled_plugin/mcp/native/darwin-arm64/unix.node +0 -0
  7. package/_bundled_plugin/mcp/native/darwin-x64/unix.node +0 -0
  8. package/_bundled_plugin/mcp/native/linux-arm64-gnu/unix.node +0 -0
  9. package/_bundled_plugin/mcp/native/linux-arm64-musl/unix.node +0 -0
  10. package/_bundled_plugin/mcp/native/linux-x64-gnu/unix.node +0 -0
  11. package/_bundled_plugin/mcp/native/linux-x64-musl/unix.node +0 -0
  12. package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
  13. package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
  14. package/_bundled_plugin/mcp/permission-profile-preflight.mjs +46 -12
  15. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  16. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  17. package/_bundled_plugin/mcp/server.mjs.br.part-002 +0 -0
  18. package/_bundled_plugin/references/artifact-storage.md +1 -1
  19. package/_bundled_plugin/references/core-scan.md +14 -5
  20. package/_bundled_plugin/references/default-severity-rubric.md +28 -0
  21. package/_bundled_plugin/references/finding-detail-fields.md +1 -0
  22. package/_bundled_plugin/references/threat-model.md +1 -0
  23. package/_bundled_plugin/scripts/deep_scan_workbench.py +23 -112
  24. package/_bundled_plugin/scripts/finalize_scan_contract.py +167 -153
  25. package/_bundled_plugin/scripts/finding_preview.py +5 -29
  26. package/_bundled_plugin/scripts/posix_scan_local_files.py +187 -0
  27. package/_bundled_plugin/scripts/report_projection.py +87 -136
  28. package/_bundled_plugin/scripts/validate_scan_contract.py +2 -6
  29. package/_bundled_plugin/scripts/windows_scan_local_files.py +212 -55
  30. package/_bundled_plugin/scripts/workbench/retained_coverage.py +8 -24
  31. package/_bundled_plugin/scripts/workbench/saved_coverage.py +143 -231
  32. package/_bundled_plugin/scripts/workbench_cli.py +102 -13
  33. package/_bundled_plugin/scripts/workbench_constants.py +9 -0
  34. package/_bundled_plugin/scripts/workbench_db.py +186 -360
  35. package/_bundled_plugin/scripts/workbench_finding_workflows.py +86 -4
  36. package/_bundled_plugin/scripts/workbench_native_indexes.py +17 -21
  37. package/_bundled_plugin/scripts/workbench_remediation.py +495 -11
  38. package/_bundled_plugin/scripts/workbench_scan_history.py +22 -25
  39. package/_bundled_plugin/scripts/workbench_scan_start.py +1 -1
  40. package/_bundled_plugin/scripts/workbench_scan_usage.py +25 -52
  41. package/_bundled_plugin/scripts/workbench_schema.py +1 -1
  42. package/_bundled_plugin/scripts/workbench_setup.py +239 -0
  43. package/_bundled_plugin/scripts/workbench_source_scopes.py +15 -23
  44. package/_bundled_plugin/scripts/workbench_target.py +213 -20
  45. package/_bundled_plugin/scripts/workbench_validation.py +1 -9
  46. package/_bundled_plugin/shared/workbench-history-repairs.json +7 -0
  47. package/_bundled_plugin/shared/workbench-migrations.json +19 -0
  48. package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +1 -1
  49. package/_bundled_plugin/skills/attack-path-analysis/references/severity-policy.md +4 -68
  50. package/_bundled_plugin/skills/check-skills/SKILL.md +43 -0
  51. package/_bundled_plugin/skills/check-skills/agents/openai.yaml +4 -0
  52. package/_bundled_plugin/skills/cloud-findings/SKILL.md +2 -0
  53. package/_bundled_plugin/skills/fix-finding/SKILL.md +18 -1
  54. package/_bundled_plugin/skills/publish-findings/SKILL.md +64 -35
  55. package/_bundled_plugin/skills/publish-findings/references/wiz-import.md +2 -21
  56. package/_bundled_plugin/skills/threat-model-visualize/scripts/build_explorer.py +14 -8
  57. package/_bundled_plugin/skills/track-findings/SKILL.md +5 -3
  58. package/_bundled_plugin/skills/triage-finding/references/github-rest-intake.md +22 -68
  59. package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +6 -4
  60. package/dist/api.d.ts +6 -1
  61. package/dist/api.d.ts.map +1 -1
  62. package/dist/api.js +411 -160
  63. package/dist/api.js.map +1 -1
  64. package/dist/auth.js +6 -1
  65. package/dist/auth.js.map +1 -1
  66. package/dist/blackduck-findings.d.ts +8 -0
  67. package/dist/blackduck-findings.d.ts.map +1 -0
  68. package/dist/blackduck-findings.js +305 -0
  69. package/dist/blackduck-findings.js.map +1 -0
  70. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  71. package/dist/bulk-scan-discovery.js +51 -36
  72. package/dist/bulk-scan-discovery.js.map +1 -1
  73. package/dist/classify-severity-records.d.ts +5 -0
  74. package/dist/classify-severity-records.d.ts.map +1 -1
  75. package/dist/classify-severity-records.js +6 -8
  76. package/dist/classify-severity-records.js.map +1 -1
  77. package/dist/classify-severity.d.ts +4 -1
  78. package/dist/classify-severity.d.ts.map +1 -1
  79. package/dist/classify-severity.js +28 -9
  80. package/dist/classify-severity.js.map +1 -1
  81. package/dist/cli.d.ts +3 -0
  82. package/dist/cli.d.ts.map +1 -1
  83. package/dist/cli.js +608 -258
  84. package/dist/cli.js.map +1 -1
  85. package/dist/codex-prompt.d.ts +2 -0
  86. package/dist/codex-prompt.d.ts.map +1 -1
  87. package/dist/codex-prompt.js +6 -0
  88. package/dist/codex-prompt.js.map +1 -1
  89. package/dist/component-scan.d.ts.map +1 -1
  90. package/dist/component-scan.js +6 -5
  91. package/dist/component-scan.js.map +1 -1
  92. package/dist/config.d.ts +2 -1
  93. package/dist/config.d.ts.map +1 -1
  94. package/dist/config.js +162 -40
  95. package/dist/config.js.map +1 -1
  96. package/dist/continuum-findings.d.ts +7 -0
  97. package/dist/continuum-findings.d.ts.map +1 -0
  98. package/dist/continuum-findings.js +157 -0
  99. package/dist/continuum-findings.js.map +1 -0
  100. package/dist/contract-values.d.ts +28 -0
  101. package/dist/contract-values.d.ts.map +1 -0
  102. package/dist/contract-values.js +75 -0
  103. package/dist/contract-values.js.map +1 -0
  104. package/dist/contract.d.ts +2 -20
  105. package/dist/contract.d.ts.map +1 -1
  106. package/dist/contract.js +58 -669
  107. package/dist/contract.js.map +1 -1
  108. package/dist/cost-model.d.ts.map +1 -1
  109. package/dist/cost-model.js +1 -4
  110. package/dist/cost-model.js.map +1 -1
  111. package/dist/cost.d.ts +19 -1
  112. package/dist/cost.d.ts.map +1 -1
  113. package/dist/cost.js +2120 -39
  114. package/dist/cost.js.map +1 -1
  115. package/dist/custom-validation-prompt.d.ts +1 -1
  116. package/dist/custom-validation-prompt.d.ts.map +1 -1
  117. package/dist/custom-validation-prompt.js +5 -0
  118. package/dist/custom-validation-prompt.js.map +1 -1
  119. package/dist/custom-validation.d.ts +2 -0
  120. package/dist/custom-validation.d.ts.map +1 -1
  121. package/dist/custom-validation.js +4 -0
  122. package/dist/custom-validation.js.map +1 -1
  123. package/dist/deduplication/deduplication-prompts.d.ts +4 -4
  124. package/dist/deduplication/deduplication-prompts.d.ts.map +1 -1
  125. package/dist/deduplication/deduplication-prompts.js +2 -2
  126. package/dist/deduplication/deduplication-prompts.js.map +1 -1
  127. package/dist/deduplication/deduplication-reviewer.d.ts +14 -11
  128. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
  129. package/dist/deduplication/deduplication-reviewer.js +11 -4
  130. package/dist/deduplication/deduplication-reviewer.js.map +1 -1
  131. package/dist/deduplication/deduplication.js.map +1 -1
  132. package/dist/deduplication/finding-schema.d.ts +19 -1
  133. package/dist/deduplication/finding-schema.d.ts.map +1 -1
  134. package/dist/deduplication/finding-schema.js +25 -0
  135. package/dist/deduplication/finding-schema.js.map +1 -1
  136. package/dist/deduplication/local.d.ts.map +1 -1
  137. package/dist/deduplication/local.js +3 -8
  138. package/dist/deduplication/local.js.map +1 -1
  139. package/dist/deduplication/records.d.ts +8 -2
  140. package/dist/deduplication/records.d.ts.map +1 -1
  141. package/dist/deduplication/records.js +36 -8
  142. package/dist/deduplication/records.js.map +1 -1
  143. package/dist/deep-scan.d.ts +11 -0
  144. package/dist/deep-scan.d.ts.map +1 -1
  145. package/dist/deep-scan.js +235 -4
  146. package/dist/deep-scan.js.map +1 -1
  147. package/dist/default-severity-rubric.d.ts +1 -2
  148. package/dist/default-severity-rubric.d.ts.map +1 -1
  149. package/dist/default-severity-rubric.js +2 -66
  150. package/dist/default-severity-rubric.js.map +1 -1
  151. package/dist/endor-findings.d.ts.map +1 -1
  152. package/dist/endor-findings.js +7 -37
  153. package/dist/endor-findings.js.map +1 -1
  154. package/dist/errors.js +1 -1
  155. package/dist/errors.js.map +1 -1
  156. package/dist/external-findings-import.d.ts.map +1 -1
  157. package/dist/external-findings-import.js +5 -11
  158. package/dist/external-findings-import.js.map +1 -1
  159. package/dist/external-findings-local.d.ts +9 -0
  160. package/dist/external-findings-local.d.ts.map +1 -1
  161. package/dist/external-findings-local.js +325 -179
  162. package/dist/external-findings-local.js.map +1 -1
  163. package/dist/external-findings-publish.d.ts +12 -0
  164. package/dist/external-findings-publish.d.ts.map +1 -1
  165. package/dist/external-findings-publish.js +1202 -214
  166. package/dist/external-findings-publish.js.map +1 -1
  167. package/dist/external-findings-read.d.ts +8 -0
  168. package/dist/external-findings-read.d.ts.map +1 -1
  169. package/dist/external-findings-read.js +65 -54
  170. package/dist/external-findings-read.js.map +1 -1
  171. package/dist/external-findings.d.ts +2 -0
  172. package/dist/external-findings.d.ts.map +1 -1
  173. package/dist/external-findings.js +111 -0
  174. package/dist/external-findings.js.map +1 -1
  175. package/dist/external-import-contract.d.ts +29 -1
  176. package/dist/external-import-contract.d.ts.map +1 -1
  177. package/dist/external-import-contract.js +314 -44
  178. package/dist/external-import-contract.js.map +1 -1
  179. package/dist/external-import-models.d.ts +1 -1
  180. package/dist/external-import-models.d.ts.map +1 -1
  181. package/dist/external-import-models.js.map +1 -1
  182. package/dist/finding-normalization.d.ts +1 -1
  183. package/dist/finding-normalization.d.ts.map +1 -1
  184. package/dist/finding-normalization.js +46 -11
  185. package/dist/finding-normalization.js.map +1 -1
  186. package/dist/finding-retrieval.d.ts +3 -3
  187. package/dist/finding-retrieval.d.ts.map +1 -1
  188. package/dist/finding-retrieval.js.map +1 -1
  189. package/dist/finding-workflow.d.ts +6 -1
  190. package/dist/finding-workflow.d.ts.map +1 -1
  191. package/dist/finding-workflow.js +2 -2
  192. package/dist/finding-workflow.js.map +1 -1
  193. package/dist/git-remote.d.ts +4 -0
  194. package/dist/git-remote.d.ts.map +1 -1
  195. package/dist/git-remote.js +20 -0
  196. package/dist/git-remote.js.map +1 -1
  197. package/dist/github-advisories.d.ts +3 -12
  198. package/dist/github-advisories.d.ts.map +1 -1
  199. package/dist/github-advisories.js +9 -29
  200. package/dist/github-advisories.js.map +1 -1
  201. package/dist/github-dependabot-findings.d.ts.map +1 -1
  202. package/dist/github-dependabot-findings.js +7 -16
  203. package/dist/github-dependabot-findings.js.map +1 -1
  204. package/dist/github-findings.d.ts.map +1 -1
  205. package/dist/github-findings.js +344 -56
  206. package/dist/github-findings.js.map +1 -1
  207. package/dist/github.d.ts +17 -7
  208. package/dist/github.d.ts.map +1 -1
  209. package/dist/github.js +43 -30
  210. package/dist/github.js.map +1 -1
  211. package/dist/host-protocol.d.ts +2 -2
  212. package/dist/host-protocol.d.ts.map +1 -1
  213. package/dist/host-protocol.js.map +1 -1
  214. package/dist/host-records-protocol.d.ts +10 -11
  215. package/dist/host-records-protocol.d.ts.map +1 -1
  216. package/dist/host-records-protocol.js +69 -9
  217. package/dist/host-records-protocol.js.map +1 -1
  218. package/dist/hosted-scan.d.ts +15 -0
  219. package/dist/hosted-scan.d.ts.map +1 -1
  220. package/dist/hosted-scan.js +55 -33
  221. package/dist/hosted-scan.js.map +1 -1
  222. package/dist/index.d.ts +7 -0
  223. package/dist/index.d.ts.map +1 -1
  224. package/dist/index.js +6 -0
  225. package/dist/index.js.map +1 -1
  226. package/dist/inspector-findings.d.ts.map +1 -1
  227. package/dist/inspector-findings.js +13 -18
  228. package/dist/inspector-findings.js.map +1 -1
  229. package/dist/knowledge-base.js +18 -5
  230. package/dist/knowledge-base.js.map +1 -1
  231. package/dist/local-git-metadata.d.ts +5 -0
  232. package/dist/local-git-metadata.d.ts.map +1 -0
  233. package/dist/local-git-metadata.js +131 -0
  234. package/dist/local-git-metadata.js.map +1 -0
  235. package/dist/multiscan.d.ts.map +1 -1
  236. package/dist/multiscan.js +22 -12
  237. package/dist/multiscan.js.map +1 -1
  238. package/dist/opengrep-findings.d.ts.map +1 -1
  239. package/dist/opengrep-findings.js +34 -20
  240. package/dist/opengrep-findings.js.map +1 -1
  241. package/dist/patch-tui.d.ts.map +1 -1
  242. package/dist/patch-tui.js +90 -35
  243. package/dist/patch-tui.js.map +1 -1
  244. package/dist/private-finding-import.d.ts +246 -0
  245. package/dist/private-finding-import.d.ts.map +1 -0
  246. package/dist/private-finding-import.js +387 -0
  247. package/dist/private-finding-import.js.map +1 -0
  248. package/dist/project-config.d.ts.map +1 -1
  249. package/dist/project-config.js +2 -2
  250. package/dist/project-config.js.map +1 -1
  251. package/dist/provider-profile.d.ts +10 -2
  252. package/dist/provider-profile.d.ts.map +1 -1
  253. package/dist/provider-profile.js +72 -10
  254. package/dist/provider-profile.js.map +1 -1
  255. package/dist/publication.js +7 -1
  256. package/dist/publication.js.map +1 -1
  257. package/dist/reader-selection.d.ts +13 -0
  258. package/dist/reader-selection.d.ts.map +1 -0
  259. package/dist/reader-selection.js +81 -0
  260. package/dist/reader-selection.js.map +1 -0
  261. package/dist/record.d.ts +6 -1
  262. package/dist/record.d.ts.map +1 -1
  263. package/dist/record.js +53 -2
  264. package/dist/record.js.map +1 -1
  265. package/dist/remediation.d.ts +5 -0
  266. package/dist/remediation.d.ts.map +1 -1
  267. package/dist/remediation.js +30 -0
  268. package/dist/remediation.js.map +1 -1
  269. package/dist/result.d.ts +2 -0
  270. package/dist/result.d.ts.map +1 -1
  271. package/dist/result.js +3 -1
  272. package/dist/result.js.map +1 -1
  273. package/dist/runtime.d.ts +2 -1
  274. package/dist/runtime.d.ts.map +1 -1
  275. package/dist/runtime.js +95 -52
  276. package/dist/runtime.js.map +1 -1
  277. package/dist/scan-activity.js +40 -17
  278. package/dist/scan-activity.js.map +1 -1
  279. package/dist/scan-comparison.d.ts +1 -1
  280. package/dist/scan-comparison.d.ts.map +1 -1
  281. package/dist/scan-comparison.js +11 -13
  282. package/dist/scan-comparison.js.map +1 -1
  283. package/dist/scan-dashboard.d.ts +1 -0
  284. package/dist/scan-dashboard.d.ts.map +1 -1
  285. package/dist/scan-dashboard.js +16 -37
  286. package/dist/scan-dashboard.js.map +1 -1
  287. package/dist/scan-executor.d.ts +2 -0
  288. package/dist/scan-executor.d.ts.map +1 -1
  289. package/dist/scan-executor.js.map +1 -1
  290. package/dist/scan-history-renderer.d.ts.map +1 -1
  291. package/dist/scan-history-renderer.js +22 -9
  292. package/dist/scan-history-renderer.js.map +1 -1
  293. package/dist/scan-logs.d.ts.map +1 -1
  294. package/dist/scan-logs.js +5 -5
  295. package/dist/scan-logs.js.map +1 -1
  296. package/dist/scanner-import.d.ts +46 -6
  297. package/dist/scanner-import.d.ts.map +1 -1
  298. package/dist/scanner-import.js +208 -42
  299. package/dist/scanner-import.js.map +1 -1
  300. package/dist/scanner-repository-url.d.ts +14 -0
  301. package/dist/scanner-repository-url.d.ts.map +1 -0
  302. package/dist/scanner-repository-url.js +67 -0
  303. package/dist/scanner-repository-url.js.map +1 -0
  304. package/dist/semgrep-findings.d.ts.map +1 -1
  305. package/dist/semgrep-findings.js +20 -48
  306. package/dist/semgrep-findings.js.map +1 -1
  307. package/dist/semgrep-platform-findings.d.ts +12 -0
  308. package/dist/semgrep-platform-findings.d.ts.map +1 -0
  309. package/dist/semgrep-platform-findings.js +198 -0
  310. package/dist/semgrep-platform-findings.js.map +1 -0
  311. package/dist/severity-rubric.d.ts +20 -0
  312. package/dist/severity-rubric.d.ts.map +1 -0
  313. package/dist/severity-rubric.js +73 -0
  314. package/dist/severity-rubric.js.map +1 -0
  315. package/dist/severity-store.js +15 -5
  316. package/dist/severity-store.js.map +1 -1
  317. package/dist/snyk-findings.d.ts.map +1 -1
  318. package/dist/snyk-findings.js +21 -20
  319. package/dist/snyk-findings.js.map +1 -1
  320. package/dist/sonar-collector.d.ts +16 -0
  321. package/dist/sonar-collector.d.ts.map +1 -0
  322. package/dist/sonar-collector.js +383 -0
  323. package/dist/sonar-collector.js.map +1 -0
  324. package/dist/sonar-findings.d.ts +49 -0
  325. package/dist/sonar-findings.d.ts.map +1 -0
  326. package/dist/sonar-findings.js +288 -0
  327. package/dist/sonar-findings.js.map +1 -0
  328. package/dist/sonar-import.d.ts +111 -0
  329. package/dist/sonar-import.d.ts.map +1 -0
  330. package/dist/sonar-import.js +279 -0
  331. package/dist/sonar-import.js.map +1 -0
  332. package/dist/suggest-owners.d.ts.map +1 -1
  333. package/dist/suggest-owners.js +2 -1
  334. package/dist/suggest-owners.js.map +1 -1
  335. package/dist/targets.d.ts +3 -0
  336. package/dist/targets.d.ts.map +1 -1
  337. package/dist/targets.js +18 -12
  338. package/dist/targets.js.map +1 -1
  339. package/dist/terminal-columns.d.ts +4 -0
  340. package/dist/terminal-columns.d.ts.map +1 -0
  341. package/dist/terminal-columns.js +19 -0
  342. package/dist/terminal-columns.js.map +1 -0
  343. package/dist/validation-records.d.ts +0 -3
  344. package/dist/validation-records.d.ts.map +1 -1
  345. package/dist/validation-records.js +0 -16
  346. package/dist/validation-records.js.map +1 -1
  347. package/dist/veracode-findings.d.ts +7 -0
  348. package/dist/veracode-findings.d.ts.map +1 -0
  349. package/dist/veracode-findings.js +371 -0
  350. package/dist/veracode-findings.js.map +1 -0
  351. package/dist/windows-path.d.ts.map +1 -1
  352. package/dist/windows-path.js +1 -0
  353. package/dist/windows-path.js.map +1 -1
  354. package/dist/wiz-csv.d.ts +2 -2
  355. package/dist/wiz-csv.d.ts.map +1 -1
  356. package/dist/wiz-csv.js +1 -3
  357. package/dist/wiz-csv.js.map +1 -1
  358. package/dist/wiz-findings.d.ts +10 -2
  359. package/dist/wiz-findings.d.ts.map +1 -1
  360. package/dist/wiz-findings.js +88 -49
  361. package/dist/wiz-findings.js.map +1 -1
  362. package/docs/classify-severity-records.md +1 -1
  363. package/docs/cli.md +310 -477
  364. package/docs/dedupe-records.md +10 -0
  365. package/docs/findings-service.md +21 -8
  366. package/docs/hosted-scans.md +16 -1
  367. package/package.json +8 -9
  368. package/schemas/external-findings.schema.json +12 -1
  369. package/schemas/hosted-scan-v2.fixture.json +16 -1
  370. package/_bundled_plugin/scripts/validate_tracking_source.py +0 -82
  371. package/_bundled_plugin/scripts/workbench_local_dedupe.py +0 -136
  372. package/dist/deduplication/records-protocol.d.ts +0 -4
  373. package/dist/deduplication/records-protocol.d.ts.map +0 -1
  374. package/dist/deduplication/records-protocol.js +0 -49
  375. package/dist/deduplication/records-protocol.js.map +0 -1
@@ -3,16 +3,36 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import argparse
6
+ import codecs
7
+ import hashlib
8
+ import io
9
+ import os
10
+ import re
6
11
  import sqlite3
7
12
  import sys
13
+ from collections.abc import Callable
14
+ from contextlib import ExitStack, suppress
8
15
  from datetime import datetime, timedelta, timezone
9
16
  from pathlib import Path
10
- from typing import Any
17
+ from typing import Any, BinaryIO
11
18
 
12
19
  # Some plugin hosts launch Python with safe-path isolation enabled.
13
20
  sys.path.insert(0, str(Path(__file__).resolve().parent))
21
+ from finalize_scan_contract import (
22
+ ContractError,
23
+ MissingScanLocalFileError,
24
+ _open_scan_local_directory,
25
+ _open_verified_scan_directory,
26
+ _read_scan_local_json_bytes,
27
+ _read_sealed_scan,
28
+ scan_local_file_version,
29
+ scan_root_identity,
30
+ stage_scan_local_bytes,
31
+ )
32
+ from posix_scan_local_files import managed_fd
14
33
  from workbench_constants import CLAIM_LEASE_SECONDS, DELIVERED_ACTION_LEASE_SECONDS
15
- from workbench_validation import require_occurrence, require_uuid
34
+ from workbench_target import git_command, git_output
35
+ from workbench_validation import optional_text, require_occurrence, require_uuid
16
36
 
17
37
  COMMANDS = {
18
38
  "request-finding-remediation",
@@ -46,6 +66,478 @@ def require_request(
46
66
  return current
47
67
 
48
68
 
69
+ def latest_attempt(connection: sqlite3.Connection, occurrence_id: str) -> sqlite3.Row | None:
70
+ return connection.execute(
71
+ """
72
+ SELECT * FROM finding_remediation_attempts
73
+ WHERE occurrence_id = ?
74
+ ORDER BY created_at DESC, rowid DESC
75
+ LIMIT 1
76
+ """,
77
+ (occurrence_id,),
78
+ ).fetchone()
79
+
80
+
81
+ _HUNK_HEADER = re.compile(rb"@@ -[0-9]+(?:,([0-9]+))? \+[0-9]+(?:,([0-9]+))? @@")
82
+ _QUOTED_GIT_PATH = rb'"((?:[^"\\]|\\(?:[abfnrtv"\\]|[0-3][0-7]{2}))*)"'
83
+ _SUBMODULE_SUMMARY_END = re.compile(
84
+ rb" (?:contains (?:modified|untracked) content|"
85
+ rb"[0-9a-f]+\.{2,3}[0-9a-f]+(?: \([a-z ]+\):?|:))$"
86
+ )
87
+ _SUBMODULE_ERROR = (
88
+ "A remediation patch cannot update Git submodules. Regenerate without gitlink changes."
89
+ )
90
+
91
+
92
+ def _git_path(field: bytes) -> bytes:
93
+ if quoted := re.match(_QUOTED_GIT_PATH + rb"(?:\t|$)", field):
94
+ return codecs.escape_decode(quoted[1])[0]
95
+ return field.partition(b"\t")[0]
96
+
97
+
98
+ def _matches_git_paths(header: bytes, old_field: bytes, new_field: bytes) -> bool:
99
+ old, new = _git_path(old_field), _git_path(new_field)
100
+ # Git strips one component from each path. Unquoted paths can contain
101
+ # spaces, so use the known preimage to locate the separator.
102
+ if quoted := re.match(_QUOTED_GIT_PATH + rb"(?= )", header):
103
+ split = quoted.end()
104
+ elif quoted := re.search(rb" " + _QUOTED_GIT_PATH + rb"$", header):
105
+ split = quoted.start()
106
+ elif old == b"/dev/null" and header.endswith(b" " + new):
107
+ split = len(header) - len(new) - 1
108
+ elif header.count(b" ") == 1:
109
+ split = header.index(b" ")
110
+ else:
111
+ # On a creation Git repeats the destination on the left of the header.
112
+ expected = new if old == b"/dev/null" else old
113
+ prefix, slash, suffix = expected.partition(b"/")
114
+ split = header.find(b"/") + 1 + len(suffix) if slash else len(prefix)
115
+ if split < 0 or header[split : split + 1] != b" ":
116
+ return False
117
+ for path, field in ((header[:split], old), (header[split + 1 :], new)):
118
+ if field != b"/dev/null" and _git_path(path).split(b"/", 1)[-1] != field.split(b"/", 1)[-1]:
119
+ return False
120
+ return True
121
+
122
+
123
+ def _patch_file_sections(patch: bytes, incomplete: str) -> tuple[bytes, int]:
124
+ mail = re.match(rb"From (?:[0-9a-f]{40}|[0-9a-f]{64}) Mon Sep 17 00:00:00 2001\r?\n", patch)
125
+ if mail:
126
+ first = re.search(rb"(?m)^diff --git [^\n]+\n", patch)
127
+ if first:
128
+ start = first.start()
129
+ # A commit message can quote a Git header. Look for format-patch's
130
+ # diffstat before the first hunk, where a deleted "--" can also
131
+ # look like the separator. Each run of indented lines is scanned once.
132
+ hunk = re.compile(rb"(?m)^@@ -").search(patch, start)
133
+ diffstat = re.compile(
134
+ rb"(?m)^---\r?\n(?: [^\n]*\n)+\r?\n(?=diff --git |Submodule )"
135
+ ).search(patch, 0, hunk.start() if hunk else len(patch))
136
+ if diffstat:
137
+ start = diffstat.end()
138
+ else:
139
+ # With --no-stat Git can retain a quoted header as the preimage of
140
+ # the first change. Ignore it only for the same in-place file edit.
141
+ file = re.compile(
142
+ rb"(?m)^diff --git [^\n]+\n(?=index |--- |@@ |old mode|new mode|new file|deleted file"
143
+ rb"|(?:dis)?similarity index|(?:rename|copy) |GIT binary patch|Binary files |Files |Only in )"
144
+ ).search(patch, start)
145
+ if (
146
+ file
147
+ and file[0] == first[0]
148
+ and patch.endswith((b"\n\n", b"\n\r\n"), 0, file.start())
149
+ and re.compile(
150
+ rb"(?:index [0-9a-f]+\.\.[0-9a-f]+ 100(?:644|755)|"
151
+ rb"old mode 100(?:644|755)\r?\nnew mode 100(?:644|755)\r?\n"
152
+ rb"index [0-9a-f]+\.\.[0-9a-f]+)\r?\n(?:--- |GIT binary patch)"
153
+ ).match(patch, file.end())
154
+ ):
155
+ start = file.start()
156
+ elif not patch.endswith((b"\n\n", b"\n\r\n"), 0, start):
157
+ # A leading submodule summary runs directly into the next file header.
158
+ summary = re.compile(rb"(?m)^Submodule ").search(patch, 0, start)
159
+ if summary:
160
+ start = summary.start()
161
+ patch = patch[start:]
162
+
163
+ sections = old_lines = new_lines = 0
164
+ git_paths = b""
165
+ pending_paths = None
166
+ submodule_summary = False
167
+ expect_hunk = False
168
+ stream = io.BytesIO(patch)
169
+ for raw_line in stream:
170
+ line = raw_line.removesuffix(b"\n").removesuffix(b"\r")
171
+ if old_lines or new_lines:
172
+ if line.startswith(b"\\ "): # No newline at end of file
173
+ continue
174
+ prefix = line[:1]
175
+ if prefix in (b"", b" ", b"-"):
176
+ old_lines -= 1
177
+ if prefix in (b"", b" ", b"+"):
178
+ new_lines -= 1
179
+ if prefix not in (b"", b" ", b"+", b"-") or old_lines < 0 or new_lines < 0:
180
+ raise SystemExit(incomplete)
181
+ expect_hunk = True
182
+ continue
183
+ if expect_hunk and line.startswith(b"\\ "): # No newline at end of file
184
+ continue
185
+ hunk = _HUNK_HEADER.match(line)
186
+ if expect_hunk and not hunk and re.match(rb"@@ -(?:[0-9]|$)", line):
187
+ raise SystemExit(incomplete)
188
+ expect_hunk = bool(sections) and line.startswith(b"+++ ")
189
+ if mail and sections and line == b"-- ":
190
+ patch = patch[: stream.tell() - len(raw_line)]
191
+ break
192
+ # Git writes the raw submodule name (possibly including newlines) before the
193
+ # status or object range. Neither log nor inline-diff headers can be replayed.
194
+ if line.startswith(b"Submodule "):
195
+ submodule_summary = True
196
+ if submodule_summary and _SUBMODULE_SUMMARY_END.search(line):
197
+ raise SystemExit(_SUBMODULE_ERROR)
198
+ if pending_paths:
199
+ if line.startswith(b"+++ ") and not _matches_git_paths(*pending_paths, line[4:]):
200
+ sections += 1
201
+ pending_paths = None
202
+ if line.startswith(b"diff --git "):
203
+ sections += 1
204
+ submodule_summary = False
205
+ git_paths = line.removeprefix(b"diff --git ")
206
+ continue
207
+ if line.startswith(b"--- "):
208
+ submodule_summary = False
209
+ # A Git header can introduce the same unified path pair. An
210
+ # unrelated pair starts a separate file even if Git ignores one.
211
+ if git_paths:
212
+ pending_paths = (git_paths, line[4:])
213
+ else:
214
+ sections += 1
215
+ git_paths = b""
216
+ continue
217
+ if git_paths:
218
+ if line.startswith(
219
+ (b"index ", b"old mode ", b"new mode ", b"new file mode ", b"deleted file mode ")
220
+ ):
221
+ if re.search(rb" 0*16[0-7]{4}(?:\s|$)", line):
222
+ raise SystemExit(_SUBMODULE_ERROR)
223
+ if not line.startswith(b"index ") and not re.fullmatch(rb".* [0-7]{6}[ \t]*", line):
224
+ raise SystemExit(incomplete)
225
+ continue
226
+ if line.startswith(
227
+ (
228
+ b"similarity index ",
229
+ b"dissimilarity index ",
230
+ b"rename from ",
231
+ b"rename to ",
232
+ b"copy from ",
233
+ b"copy to ",
234
+ )
235
+ ):
236
+ continue
237
+ git_paths = b""
238
+ if hunk:
239
+ old_lines = int(hunk[1] or b"1")
240
+ new_lines = int(hunk[2] or b"1")
241
+ elif (
242
+ line.startswith((b"Binary files ", b"Files "))
243
+ and line.endswith(b" differ")
244
+ and b" and " in line
245
+ ):
246
+ raise SystemExit(incomplete)
247
+ elif line.startswith(b"Only in ") and b": " in line:
248
+ raise SystemExit(incomplete)
249
+ if old_lines or new_lines:
250
+ raise SystemExit(incomplete)
251
+ return patch, sections
252
+
253
+
254
+ def require_valid_patch(target: Path, patch: bytes) -> None:
255
+ """Reject incomplete patches before recording the submitted bytes."""
256
+ incomplete = (
257
+ "The remediation patch omits file content. Generate a complete binary diff "
258
+ "with Git's --binary option."
259
+ )
260
+ # Git's numstat skips target-relative paths when run below the repository root.
261
+ root = git_output(target, "rev-parse", "--show-toplevel", offline=True)
262
+ directory = ("-C", root) if root else ()
263
+
264
+ def git(data: bytes, *arguments: str):
265
+ result = git_command(
266
+ target,
267
+ *directory,
268
+ *arguments,
269
+ text=False,
270
+ input_data=data,
271
+ environment_overrides={"LC_ALL": "C", "GIT_NO_REPLACE_OBJECTS": "1"},
272
+ offline=True,
273
+ )
274
+ if result.returncode == 127:
275
+ raise SystemExit(
276
+ "Validating a new remediation patch requires a trusted Git executable."
277
+ )
278
+ # Git may report a corrupt trailing binary block but return zero for an earlier file.
279
+ if result.returncode or re.search(rb"(?m)^error: ", result.stderr):
280
+ reason = result.stderr.decode(errors="replace").strip()
281
+ raise SystemExit(f"Could not parse the remediation patch: {reason}")
282
+ return result.stdout
283
+
284
+ # Parse every header and hunk, then skip checkout reads and repository clean filters.
285
+ apply = ("apply", "--no-index", "--whitespace=nowarn")
286
+ body, sections = _patch_file_sections(patch, incomplete)
287
+ git(patch, *apply, "--check", "--exclude=*", "-")
288
+ # Git silently ignores some incomplete sections when another file is valid.
289
+ # NUL termination gives one record per file, even for unusual pathnames.
290
+ if git(patch, *apply, "--numstat", "-z", "-").count(b"\0") != sections:
291
+ raise SystemExit(incomplete)
292
+
293
+ binary_oid_width = None
294
+ for header in re.finditer(
295
+ rb"(?m)^diff --git [^\n]+\n((?:(?!diff )[a-z][^\n]*(?:\n|\Z))*)",
296
+ body,
297
+ ):
298
+ metadata = header.group(1)
299
+ index = re.search(rb"(?m)^index ([^\s.]+)\.\.([^\s.]+)(?: [0-7]+[ \t]*)?\r?$", metadata)
300
+ binary = body.startswith((b"GIT binary patch\n", b"GIT binary patch\r\n"), header.end())
301
+ if not binary:
302
+ if index is None or re.compile(rb"(?:--- [^\n]*\n\+\+\+ [^\n]*\n)?@@ ").match(
303
+ body, header.end()
304
+ ):
305
+ continue
306
+ old, new = index.groups()
307
+ if metadata.startswith(b"deleted file mode ") and not new.strip(b"0"):
308
+ content_id = old
309
+ elif metadata.startswith(b"new file mode ") and not old.strip(b"0"):
310
+ content_id = new
311
+ else:
312
+ # Mode changes and renames can omit hunks only if
313
+ # their advertised blob contents are unchanged.
314
+ old, new = old.lower(), new.lower()
315
+ if not old.startswith(new) and not new.startswith(old):
316
+ raise SystemExit(incomplete)
317
+ continue
318
+ # Only an empty file can be added or deleted without a hunk.
319
+ empty_blobs = (
320
+ hashlib.sha1(b"blob 0\0", usedforsecurity=False).hexdigest().encode(),
321
+ hashlib.sha256(b"blob 0\0").hexdigest().encode(),
322
+ )
323
+ if not any(digest.startswith(content_id.lower()) for digest in empty_blobs):
324
+ raise SystemExit(incomplete)
325
+ continue
326
+ if index is None:
327
+ raise SystemExit("A binary patch requires an index line with full object IDs.")
328
+ old, new = index.groups()
329
+ if binary_oid_width is None:
330
+ binary_oid_width = len(git(b"", "hash-object", "--no-filters", "--stdin").strip())
331
+ if (
332
+ len(old) != binary_oid_width
333
+ or len(new) != binary_oid_width
334
+ or not re.fullmatch(rb"[0-9a-fA-F]+", old + new)
335
+ ):
336
+ raise SystemExit(
337
+ "A binary patch requires full object IDs in the selected target's Git object format."
338
+ )
339
+
340
+
341
+ def patch_metadata(
342
+ current: sqlite3.Row,
343
+ args: argparse.Namespace,
344
+ patch: bytes | None,
345
+ resolve_path: Callable[[str], str],
346
+ ) -> tuple[str | None, str | None]:
347
+ path = args.patch_path
348
+ digest = args.patch_digest
349
+ if patch is not None:
350
+ if args.state != "generated" or path is not None or digest is not None:
351
+ raise SystemExit("Supply patch only for generation, without a patch path or digest.")
352
+ path = current["patch_path"] or (
353
+ f"artifacts/remediation/{current['occurrence_id']}/{current['request_id']}/"
354
+ f"{current['pending_action_claim_token']}.patch"
355
+ )
356
+ digest = f"sha256:{hashlib.sha256(patch).hexdigest()}"
357
+ else:
358
+ if path is not None:
359
+ path = resolve_path(path)
360
+ if digest is not None:
361
+ digest = optional_text(digest, maximum=71)
362
+ if not re.fullmatch(r"sha256:[0-9a-f]{64}", digest or ""):
363
+ raise SystemExit("patch-digest must use sha256:<64 lowercase hex characters>.")
364
+ if path is not None and current["patch_path"] not in (None, path):
365
+ raise SystemExit("A remediation attempt cannot replace its reviewed patch path.")
366
+ if digest is not None and current["patch_digest"] not in (None, digest):
367
+ raise SystemExit("A remediation attempt cannot replace its reviewed patch digest.")
368
+ return path or current["patch_path"], digest or current["patch_digest"]
369
+
370
+
371
+ def same_file_version(
372
+ original: tuple[os.stat_result, int], current: tuple[os.stat_result, int]
373
+ ) -> bool:
374
+ before, before_change = original
375
+ after, after_change = current
376
+ return (
377
+ os.path.samestat(before, after)
378
+ and before.st_size == after.st_size
379
+ and before.st_mtime_ns == after.st_mtime_ns
380
+ and before_change == after_change
381
+ )
382
+
383
+
384
+ def file_digest_and_version(source: BinaryIO, label: str) -> tuple[str, tuple[os.stat_result, int]]:
385
+ before = scan_local_file_version(source.fileno())
386
+ digest = hashlib.sha256()
387
+ for chunk in iter(lambda: source.read(1024 * 1024), b""):
388
+ digest.update(chunk)
389
+ after = scan_local_file_version(source.fileno())
390
+ if not same_file_version(before, after):
391
+ raise SystemExit(f"The {label} changed while it was being verified.")
392
+ return f"sha256:{digest.hexdigest()}", after
393
+
394
+
395
+ def matching_patch_metadata(source: BinaryIO, expected_digest: str) -> tuple[os.stat_result, int]:
396
+ digest, version = file_digest_and_version(source, "scan-local patch")
397
+ if digest != expected_digest:
398
+ raise SystemExit("Patch digest does not match the scan-local patch file.")
399
+ return version
400
+
401
+
402
+ def patch_manifest_version(
403
+ db: Any, scan: sqlite3.Row, scan_dir: Path
404
+ ) -> tuple[str, tuple[os.stat_result, int]]:
405
+ manifest_path = db.ARTIFACTS["manifest"]
406
+ expected = scan["seal_manifest_digest"]
407
+ if expected is None:
408
+ # Historical manifests can have different JSON formatting. Bind their original bytes.
409
+ saved, raw = _read_scan_local_json_bytes(scan_dir, manifest_path, manifest_path)
410
+ manifest, _, _, _ = _read_sealed_scan(scan_dir, None, "remediation")
411
+ if saved != manifest:
412
+ raise SystemExit("The sealed scan manifest changed during validation.")
413
+ db.verify_manifest_binding(scan, manifest)
414
+ expected = f"sha256:{hashlib.sha256(raw).hexdigest()}"
415
+ with db.open_scan_local_file(scan_dir, manifest_path) as source:
416
+ digest, version = file_digest_and_version(source, "sealed scan manifest")
417
+ if digest != expected:
418
+ raise SystemExit("The sealed scan manifest changed after completion.")
419
+ return digest, version
420
+
421
+
422
+ def save_patch(
423
+ db: Any,
424
+ connection: sqlite3.Connection,
425
+ scan_id: str,
426
+ current: sqlite3.Row,
427
+ path: str,
428
+ digest: str,
429
+ patch: bytes,
430
+ record_update: Callable[[], None],
431
+ ) -> None:
432
+ def admit() -> Path:
433
+ record_update()
434
+ latest = db.require_scan(connection, scan_id)
435
+ directory = db.require_canonical_scan_directory(Path(latest["scan_dir"]))
436
+ if scan_root_identity(directory)[1] != identity:
437
+ raise SystemExit("The scan directory changed after seal validation.")
438
+ with db.open_scan_local_file(directory, db.ARTIFACTS["manifest"]) as named:
439
+ current_version = scan_local_file_version(named.fileno())
440
+ if latest["seal_manifest_digest"] not in (None, manifest_digest) or not same_file_version(
441
+ manifest_version, current_version
442
+ ):
443
+ raise SystemExit("The sealed scan manifest changed after completion.")
444
+ return directory
445
+
446
+ def prepare_saved_patch(directory: Path) -> tuple[os.stat_result, int]:
447
+ with db.open_scan_local_file(directory, path) as named:
448
+ version = matching_patch_metadata(named, digest)
449
+ if os.name == "posix":
450
+ # Persist the file and every directory entry before SQLite commits its path.
451
+ os.fsync(named.fileno())
452
+ with (
453
+ managed_fd(_open_verified_scan_directory(directory, identity)) as root,
454
+ managed_fd(
455
+ _open_scan_local_directory(
456
+ root, Path(path).parts[:-1], create=False, sync_parents=True
457
+ )
458
+ ) as parent,
459
+ ):
460
+ os.fsync(parent)
461
+ return version
462
+
463
+ def verify_name(directory: Path, expected: tuple[os.stat_result, int]) -> None:
464
+ with db.open_scan_local_file(directory, path) as named:
465
+ if not same_file_version(expected, scan_local_file_version(named.fileno())):
466
+ raise SystemExit("The scan-local patch changed during generation.")
467
+ if scan_root_identity(directory)[1] != identity:
468
+ raise SystemExit("The scan directory changed while saving the patch.")
469
+
470
+ existing = current["patch_path"] is not None
471
+ retried_directory = None
472
+ identity = None
473
+ while True:
474
+ scan = db.require_scan(connection, scan_id)
475
+ root_changed_during_validation = False
476
+ with ExitStack() as staging:
477
+ try:
478
+ scan_dir = db.require_canonical_scan_directory(Path(scan["scan_dir"]))
479
+ if identity is None:
480
+ scan_dir, root_identity = scan_root_identity(scan_dir)
481
+ manifest_digest, manifest_version = patch_manifest_version(db, scan, scan_dir)
482
+ root_changed_during_validation = (
483
+ scan_root_identity(scan_dir)[1] != root_identity
484
+ )
485
+ if root_changed_during_validation:
486
+ raise ContractError("The scan directory changed during seal validation.")
487
+ identity = root_identity
488
+ publish = None
489
+ if not existing:
490
+ with suppress(FileExistsError):
491
+ publish = staging.enter_context(
492
+ stage_scan_local_bytes(
493
+ scan_dir,
494
+ path,
495
+ patch,
496
+ expected_root_identity=identity,
497
+ owner_read_write=True,
498
+ )
499
+ )
500
+ if publish is None:
501
+ metadata = prepare_saved_patch(scan_dir)
502
+ except (ContractError, OSError, SystemExit) as exc:
503
+ # Archiving moves the directory before committing its new location.
504
+ with connection:
505
+ connection.execute("BEGIN IMMEDIATE")
506
+ directory = db.require_scan(connection, scan_id)["scan_dir"]
507
+ if directory == scan["scan_dir"]:
508
+ if directory == retried_directory:
509
+ raise
510
+ # Rollback can restore a missing file or the verified staging root.
511
+ cause: BaseException | None = exc
512
+ while cause and not isinstance(
513
+ cause, (FileNotFoundError, MissingScanLocalFileError)
514
+ ):
515
+ cause = cause.__cause__
516
+ if (
517
+ cause is None
518
+ and not root_changed_during_validation
519
+ and not (
520
+ isinstance(exc, ContractError)
521
+ and identity is not None
522
+ and scan_root_identity(Path(directory))[1] == identity
523
+ )
524
+ ):
525
+ raise
526
+ retried_directory = directory
527
+ continue
528
+ directory = admit()
529
+ if publish is not None:
530
+ with suppress(FileExistsError):
531
+ publish(directory)
532
+ # Keep the token's file, including a competing winner, if cancellation
533
+ # or interruption wins before final admission. Verify it outside SQLite.
534
+ connection.rollback()
535
+ existing = True
536
+ continue
537
+ verify_name(directory, metadata)
538
+ return
539
+
540
+
49
541
  def remediation_claim_is_active(remediation: sqlite3.Row) -> bool:
50
542
  if remediation["pending_action_claim_token"] is None:
51
543
  return False
@@ -173,15 +665,7 @@ def _cancel_generation(
173
665
  connection.execute(
174
666
  "DELETE FROM finding_remediation_attempts WHERE request_id = ?", (request_id,)
175
667
  )
176
- previous = connection.execute(
177
- """
178
- SELECT * FROM finding_remediation_attempts
179
- WHERE occurrence_id = ?
180
- ORDER BY created_at DESC, rowid DESC
181
- LIMIT 1
182
- """,
183
- (occurrence_id,),
184
- ).fetchone()
668
+ previous = latest_attempt(connection, occurrence_id)
185
669
  if previous is None or previous["state"] != "superseded":
186
670
  return
187
671
  restored_state = "applied" if previous["applied_content_digest"] is not None else "generated"
@@ -8,6 +8,7 @@ import os
8
8
  import sqlite3
9
9
  import sys
10
10
  from collections.abc import Iterable, Iterator
11
+ from functools import cache
11
12
  from itertools import chain
12
13
  from pathlib import Path, PurePosixPath
13
14
  from typing import Any, Callable
@@ -215,7 +216,7 @@ def _same_repository(
215
216
  before: sqlite3.Row,
216
217
  after: sqlite3.Row,
217
218
  *,
218
- after_identity: tuple[str | None, tuple[str, str] | None] | None = None,
219
+ after_git: Callable[..., str | None] | None = None,
219
220
  ) -> bool:
220
221
  if before["target_id"] is not None and before["target_id"] == after["target_id"]:
221
222
  return True
@@ -223,28 +224,23 @@ def _same_repository(
223
224
  after_target = Path(after["target_path"])
224
225
  if before_target.resolve() == after_target.resolve():
225
226
  return True
226
- before_git_dir = git_output(
227
- before_target, "rev-parse", "--path-format=absolute", "--git-common-dir"
228
- )
229
- after_git_dir = (
230
- git_output(after_target, "rev-parse", "--path-format=absolute", "--git-common-dir")
231
- if after_identity is None
232
- else after_identity[0]
233
- )
227
+ common_dir = ("rev-parse", "--path-format=absolute", "--git-common-dir")
228
+ after_git = after_git or git_output
229
+ before_git_dir = git_output(before_target, *common_dir)
234
230
  if (
235
231
  before_git_dir is not None
236
- and after_git_dir is not None
232
+ and (after_git_dir := after_git(after_target, *common_dir)) is not None
237
233
  and Path(before_git_dir).resolve() == Path(after_git_dir).resolve()
238
234
  ):
239
235
  return True
240
- before_origin = _repository_origin(before_target)
241
- return before_origin is not None and before_origin == (
242
- _repository_origin(after_target) if after_identity is None else after_identity[1]
236
+ origin = ("remote", "get-url", "origin")
237
+ before_origin = _repository_origin(git_output(before_target, *origin))
238
+ return before_origin is not None and before_origin == _repository_origin(
239
+ after_git(after_target, *origin)
243
240
  )
244
241
 
245
242
 
246
- def _repository_origin(target: Path) -> tuple[str, str] | None:
247
- remote = git_output(target, "remote", "get-url", "origin")
243
+ def _repository_origin(remote: str | None) -> tuple[str, str] | None:
248
244
  if remote is None:
249
245
  return None
250
246
  if "://" in remote:
@@ -352,16 +348,13 @@ def list_scans(
352
348
  """,
353
349
  (str(repository), str(repository)),
354
350
  ).fetchone()
355
- requested_identity = (
356
- git_output(repository, "rev-parse", "--path-format=absolute", "--git-common-dir"),
357
- _repository_origin(repository),
358
- )
351
+ requested_git = cache(git_output)
359
352
  related_target_ids = [
360
353
  target["target_id"]
361
354
  for target in connection.execute(
362
355
  "SELECT id AS target_id, current_path AS target_path FROM security_targets"
363
356
  )
364
- if _same_repository(target, requested_repository, after_identity=requested_identity)
357
+ if _same_repository(target, requested_repository, after_git=requested_git)
365
358
  ]
366
359
  repository_clauses = ["scans.target_path = ?"]
367
360
  values.append(str(repository))
@@ -452,11 +445,15 @@ def list_unmatched_scan_pairs(
452
445
  """,
453
446
  (str(repository), str(repository)),
454
447
  ).fetchone()
455
- selected = [
456
- scan
457
- for scan in connection.execute("SELECT * FROM scans WHERE status = 'complete'")
458
- if _same_repository(scan, requested)
459
- ]
448
+ selected = []
449
+ requested_git = cache(git_output)
450
+ membership: dict[tuple[str | None, str], bool] = {}
451
+ for scan in connection.execute("SELECT * FROM scans WHERE status = 'complete'"):
452
+ target = (scan["target_id"], scan["target_path"])
453
+ if target not in membership:
454
+ membership[target] = _same_repository(scan, requested, after_git=requested_git)
455
+ if membership[target]:
456
+ selected.append(scan)
460
457
 
461
458
  selected.sort(key=lambda scan: (timestamp_key(scan["started_at"]), scan["id"]))
462
459
 
@@ -233,7 +233,7 @@ def insert_running_scan(
233
233
  source_scopes: dict[str, Any],
234
234
  target_root: Path,
235
235
  target_summary: str | None,
236
- scope_file_count: int,
236
+ scope_file_count: int | None,
237
237
  timestamp: str,
238
238
  handoff_status: str = "pending",
239
239
  model: str | None = None,