@openai/codex-security 0.9.1 → 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 (387) hide show
  1. package/README.md +37 -788
  2. package/_bundled_plugin/.codex-plugin/plugin.json +2 -0
  3. package/_bundled_plugin/.mcp.json +2 -0
  4. package/_bundled_plugin/mcp/helpers.mjs.br +0 -0
  5. package/_bundled_plugin/mcp/native/darwin-arm64/unix.node +0 -0
  6. package/_bundled_plugin/mcp/native/darwin-x64/unix.node +0 -0
  7. package/_bundled_plugin/mcp/native/linux-arm64-gnu/unix.node +0 -0
  8. package/_bundled_plugin/mcp/native/linux-arm64-musl/unix.node +0 -0
  9. package/_bundled_plugin/mcp/native/linux-x64-gnu/unix.node +0 -0
  10. package/_bundled_plugin/mcp/native/linux-x64-musl/unix.node +0 -0
  11. package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
  12. package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
  13. package/_bundled_plugin/mcp/server.mjs.br +0 -0
  14. package/_bundled_plugin/references/artifact-storage.md +1 -1
  15. package/_bundled_plugin/references/baseline-auditor.md +18 -0
  16. package/_bundled_plugin/references/core-scan.md +5 -42
  17. package/_bundled_plugin/references/final-report.md +1 -1
  18. package/_bundled_plugin/references/focused-investigator.md +13 -0
  19. package/_bundled_plugin/references/scan-prologue.md +2 -2
  20. package/_bundled_plugin/references/scan-runtime.md +4 -2
  21. package/_bundled_plugin/references/security-guidance.md +14 -1
  22. package/_bundled_plugin/references/validate-scan-findings.md +3 -0
  23. package/_bundled_plugin/scripts/deep_scan_workbench.py +29 -12
  24. package/_bundled_plugin/scripts/dispatch_inventory_tool +7 -0
  25. package/_bundled_plugin/scripts/finalize_scan_contract.py +29 -3
  26. package/_bundled_plugin/scripts/launch_codex_security_mcp +54 -16
  27. package/_bundled_plugin/scripts/posix_scan_local_files.py +26 -1
  28. package/_bundled_plugin/scripts/snapshot_sqlite.py +8 -2
  29. package/_bundled_plugin/scripts/windows_scan_local_files.py +216 -25
  30. package/_bundled_plugin/scripts/workbench/handoff.py +55 -25
  31. package/_bundled_plugin/scripts/workbench/storage.py +25 -5
  32. package/_bundled_plugin/scripts/workbench_cli.py +24 -2
  33. package/_bundled_plugin/scripts/workbench_constants.py +2 -0
  34. package/_bundled_plugin/scripts/workbench_db.py +244 -217
  35. package/_bundled_plugin/scripts/workbench_feedback.py +49 -4
  36. package/_bundled_plugin/scripts/workbench_native_indexes.py +226 -29
  37. package/_bundled_plugin/scripts/workbench_progress.py +159 -3
  38. package/_bundled_plugin/scripts/workbench_publication.py +2 -3
  39. package/_bundled_plugin/scripts/workbench_remediation.py +142 -2
  40. package/_bundled_plugin/scripts/workbench_saved_results.py +48 -8
  41. package/_bundled_plugin/scripts/workbench_scan_history.py +281 -119
  42. package/_bundled_plugin/scripts/workbench_scan_start.py +143 -9
  43. package/_bundled_plugin/scripts/workbench_scan_usage.py +36 -10
  44. package/_bundled_plugin/scripts/workbench_schema.py +220 -14
  45. package/_bundled_plugin/scripts/workbench_setup.py +1 -1
  46. package/_bundled_plugin/scripts/workbench_target.py +471 -84
  47. package/_bundled_plugin/scripts/workbench_target_state.py +1108 -22
  48. package/_bundled_plugin/shared/workbench-history-repairs.json +60 -14
  49. package/_bundled_plugin/shared/workbench-migrations.json +66 -0
  50. package/_bundled_plugin/skills/fix-finding/SKILL.md +1 -0
  51. package/_bundled_plugin/skills/publish-findings/SKILL.md +24 -160
  52. package/_bundled_plugin/skills/publish-findings/agents/openai.yaml +2 -2
  53. package/_bundled_plugin/skills/publish-findings/references/checkmarx-import.md +2 -2
  54. package/_bundled_plugin/skills/publish-findings/scripts/collect_checkmarx.py +3 -58
  55. package/_bundled_plugin/skills/publish-findings/scripts/collect_continuum.py +224 -0
  56. package/_bundled_plugin/skills/publish-findings/scripts/collector_paths.py +63 -0
  57. package/_bundled_plugin/skills/security-scan/SKILL.md +9 -46
  58. package/_bundled_plugin/skills/security-scan/agents/openai.yaml +2 -2
  59. package/_bundled_plugin/skills/security-scan/references/desktop-scan.md +3 -21
  60. package/_bundled_plugin/skills/security-scan/references/managed-scan.md +11 -0
  61. package/_bundled_plugin/skills/security-scan/references/scan-results.md +15 -0
  62. package/_bundled_plugin/skills/security-scan/references/scoped-paths.md +23 -0
  63. package/_bundled_plugin/skills/security-scan/references/sdk-files.md +24 -0
  64. package/bin/codex-security.mjs +2 -2
  65. package/dist/api.d.ts +6 -1
  66. package/dist/api.d.ts.map +1 -1
  67. package/dist/api.js +437 -154
  68. package/dist/api.js.map +1 -1
  69. package/dist/armorcode-findings.d.ts +9 -0
  70. package/dist/armorcode-findings.d.ts.map +1 -0
  71. package/dist/armorcode-findings.js +145 -0
  72. package/dist/armorcode-findings.js.map +1 -0
  73. package/dist/artifact-export.d.ts +2 -0
  74. package/dist/artifact-export.d.ts.map +1 -1
  75. package/dist/artifact-export.js +29 -13
  76. package/dist/artifact-export.js.map +1 -1
  77. package/dist/auth.d.ts +6 -2
  78. package/dist/auth.d.ts.map +1 -1
  79. package/dist/auth.js +33 -12
  80. package/dist/auth.js.map +1 -1
  81. package/dist/batch-cost.d.ts +18 -0
  82. package/dist/batch-cost.d.ts.map +1 -0
  83. package/dist/batch-cost.js +41 -0
  84. package/dist/batch-cost.js.map +1 -0
  85. package/dist/blackduck-findings.d.ts +1 -1
  86. package/dist/blackduck-findings.d.ts.map +1 -1
  87. package/dist/blackduck-findings.js +5 -18
  88. package/dist/blackduck-findings.js.map +1 -1
  89. package/dist/bundled-plugin.d.ts +4 -0
  90. package/dist/bundled-plugin.d.ts.map +1 -0
  91. package/dist/bundled-plugin.js +23 -0
  92. package/dist/bundled-plugin.js.map +1 -0
  93. package/dist/classify-scan-severity.d.ts +1 -1
  94. package/dist/classify-scan-severity.d.ts.map +1 -1
  95. package/dist/classify-scan-severity.js +11 -3
  96. package/dist/classify-scan-severity.js.map +1 -1
  97. package/dist/classify-severity.d.ts.map +1 -1
  98. package/dist/classify-severity.js +9 -6
  99. package/dist/classify-severity.js.map +1 -1
  100. package/dist/cli-argument.d.ts +4 -0
  101. package/dist/cli-argument.d.ts.map +1 -0
  102. package/dist/cli-argument.js +17 -0
  103. package/dist/cli-argument.js.map +1 -0
  104. package/dist/cli-help.d.ts +1 -1
  105. package/dist/cli-help.d.ts.map +1 -1
  106. package/dist/cli-help.js +71 -29
  107. package/dist/cli-help.js.map +1 -1
  108. package/dist/cli-hook.d.ts +3 -0
  109. package/dist/cli-hook.d.ts.map +1 -0
  110. package/dist/cli-hook.js +87 -0
  111. package/dist/cli-hook.js.map +1 -0
  112. package/dist/cli-integrations.d.ts +13 -0
  113. package/dist/cli-integrations.d.ts.map +1 -0
  114. package/dist/cli-integrations.js +333 -0
  115. package/dist/cli-integrations.js.map +1 -0
  116. package/dist/cli-mcp.d.ts +15 -0
  117. package/dist/cli-mcp.d.ts.map +1 -0
  118. package/dist/cli-mcp.js +178 -0
  119. package/dist/cli-mcp.js.map +1 -0
  120. package/dist/cli-token-view.d.ts +10 -0
  121. package/dist/cli-token-view.d.ts.map +1 -0
  122. package/dist/cli-token-view.js +41 -0
  123. package/dist/cli-token-view.js.map +1 -0
  124. package/dist/cli.d.ts +5 -3
  125. package/dist/cli.d.ts.map +1 -1
  126. package/dist/cli.js +1763 -894
  127. package/dist/cli.js.map +1 -1
  128. package/dist/cloud-publish.d.ts.map +1 -1
  129. package/dist/cloud-publish.js +33 -31
  130. package/dist/cloud-publish.js.map +1 -1
  131. package/dist/codex-sdk-environment.d.ts +6 -0
  132. package/dist/codex-sdk-environment.d.ts.map +1 -1
  133. package/dist/codex-sdk-environment.js +19 -0
  134. package/dist/codex-sdk-environment.js.map +1 -1
  135. package/dist/command-words.d.ts +3 -0
  136. package/dist/command-words.d.ts.map +1 -0
  137. package/dist/command-words.js +46 -0
  138. package/dist/command-words.js.map +1 -0
  139. package/dist/component-scan.d.ts +3 -1
  140. package/dist/component-scan.d.ts.map +1 -1
  141. package/dist/component-scan.js +40 -7
  142. package/dist/component-scan.js.map +1 -1
  143. package/dist/config.d.ts +1 -1
  144. package/dist/config.d.ts.map +1 -1
  145. package/dist/config.js +30 -1
  146. package/dist/config.js.map +1 -1
  147. package/dist/continuum-findings.d.ts +1 -1
  148. package/dist/continuum-findings.d.ts.map +1 -1
  149. package/dist/continuum-findings.js +93 -31
  150. package/dist/continuum-findings.js.map +1 -1
  151. package/dist/cost.d.ts.map +1 -1
  152. package/dist/cost.js +1 -45
  153. package/dist/cost.js.map +1 -1
  154. package/dist/custom-validation-prompt.d.ts +1 -1
  155. package/dist/custom-validation-prompt.d.ts.map +1 -1
  156. package/dist/custom-validation-prompt.js +29 -19
  157. package/dist/custom-validation-prompt.js.map +1 -1
  158. package/dist/deduplication/local.d.ts +3 -1
  159. package/dist/deduplication/local.d.ts.map +1 -1
  160. package/dist/deduplication/local.js +8 -1
  161. package/dist/deduplication/local.js.map +1 -1
  162. package/dist/deduplication/refusal.d.ts.map +1 -1
  163. package/dist/deduplication/refusal.js +2 -1
  164. package/dist/deduplication/refusal.js.map +1 -1
  165. package/dist/deduplication/scan.d.ts +2 -0
  166. package/dist/deduplication/scan.d.ts.map +1 -1
  167. package/dist/deduplication/scan.js +24 -7
  168. package/dist/deduplication/scan.js.map +1 -1
  169. package/dist/deep-scan.d.ts.map +1 -1
  170. package/dist/deep-scan.js +16 -15
  171. package/dist/deep-scan.js.map +1 -1
  172. package/dist/endor-findings.d.ts +2 -2
  173. package/dist/endor-findings.d.ts.map +1 -1
  174. package/dist/endor-findings.js +6 -23
  175. package/dist/endor-findings.js.map +1 -1
  176. package/dist/errors.d.ts +3 -1
  177. package/dist/errors.d.ts.map +1 -1
  178. package/dist/errors.js +7 -3
  179. package/dist/errors.js.map +1 -1
  180. package/dist/executable-path.d.ts +3 -0
  181. package/dist/executable-path.d.ts.map +1 -1
  182. package/dist/executable-path.js +26 -0
  183. package/dist/executable-path.js.map +1 -1
  184. package/dist/external-findings-import.d.ts.map +1 -1
  185. package/dist/external-findings-import.js +12 -1
  186. package/dist/external-findings-import.js.map +1 -1
  187. package/dist/external-findings-local.d.ts +66 -4
  188. package/dist/external-findings-local.d.ts.map +1 -1
  189. package/dist/external-findings-local.js +234 -53
  190. package/dist/external-findings-local.js.map +1 -1
  191. package/dist/external-findings-publish.d.ts +6 -1
  192. package/dist/external-findings-publish.d.ts.map +1 -1
  193. package/dist/external-findings-publish.js +326 -1633
  194. package/dist/external-findings-publish.js.map +1 -1
  195. package/dist/external-findings-read.d.ts +1 -0
  196. package/dist/external-findings-read.d.ts.map +1 -1
  197. package/dist/external-findings-read.js +31 -51
  198. package/dist/external-findings-read.js.map +1 -1
  199. package/dist/external-findings.d.ts +1 -1
  200. package/dist/external-findings.d.ts.map +1 -1
  201. package/dist/external-findings.js +12 -1
  202. package/dist/external-findings.js.map +1 -1
  203. package/dist/external-import-contract.d.ts +3 -11
  204. package/dist/external-import-contract.d.ts.map +1 -1
  205. package/dist/external-import-contract.js +29 -176
  206. package/dist/external-import-contract.js.map +1 -1
  207. package/dist/external-import-models.d.ts +1 -1
  208. package/dist/external-import-models.d.ts.map +1 -1
  209. package/dist/external-import-models.js.map +1 -1
  210. package/dist/finding-prompt.d.ts.map +1 -1
  211. package/dist/finding-prompt.js +2 -2
  212. package/dist/finding-prompt.js.map +1 -1
  213. package/dist/github-dependabot-findings.d.ts.map +1 -1
  214. package/dist/github-dependabot-findings.js +3 -4
  215. package/dist/github-dependabot-findings.js.map +1 -1
  216. package/dist/github-findings.d.ts.map +1 -1
  217. package/dist/github-findings.js +19 -287
  218. package/dist/github-findings.js.map +1 -1
  219. package/dist/hosted-remediation.d.ts +1 -0
  220. package/dist/hosted-remediation.d.ts.map +1 -1
  221. package/dist/hosted-remediation.js +23 -3
  222. package/dist/hosted-remediation.js.map +1 -1
  223. package/dist/index.d.ts +3 -2
  224. package/dist/index.d.ts.map +1 -1
  225. package/dist/index.js +2 -1
  226. package/dist/index.js.map +1 -1
  227. package/dist/inspector-findings.d.ts.map +1 -1
  228. package/dist/inspector-findings.js +19 -35
  229. package/dist/inspector-findings.js.map +1 -1
  230. package/dist/knowledge-base.d.ts +1 -1
  231. package/dist/knowledge-base.d.ts.map +1 -1
  232. package/dist/knowledge-base.js +34 -27
  233. package/dist/knowledge-base.js.map +1 -1
  234. package/dist/linear.d.ts +11 -0
  235. package/dist/linear.d.ts.map +1 -1
  236. package/dist/linear.js.map +1 -1
  237. package/dist/local-git-metadata.d.ts +5 -2
  238. package/dist/local-git-metadata.d.ts.map +1 -1
  239. package/dist/local-git-metadata.js +8 -4
  240. package/dist/local-git-metadata.js.map +1 -1
  241. package/dist/multiscan.d.ts +4 -0
  242. package/dist/multiscan.d.ts.map +1 -1
  243. package/dist/multiscan.js +79 -37
  244. package/dist/multiscan.js.map +1 -1
  245. package/dist/opengrep-findings.d.ts +4 -2
  246. package/dist/opengrep-findings.d.ts.map +1 -1
  247. package/dist/opengrep-findings.js +39 -22
  248. package/dist/opengrep-findings.js.map +1 -1
  249. package/dist/private-finding-import.d.ts +8 -13
  250. package/dist/private-finding-import.d.ts.map +1 -1
  251. package/dist/private-finding-import.js +69 -129
  252. package/dist/private-finding-import.js.map +1 -1
  253. package/dist/project-config.d.ts.map +1 -1
  254. package/dist/project-config.js +53 -13
  255. package/dist/project-config.js.map +1 -1
  256. package/dist/prompt-files.d.ts +7 -2
  257. package/dist/prompt-files.d.ts.map +1 -1
  258. package/dist/prompt-files.js +42 -20
  259. package/dist/prompt-files.js.map +1 -1
  260. package/dist/provider-profile.d.ts.map +1 -1
  261. package/dist/provider-profile.js +4 -9
  262. package/dist/provider-profile.js.map +1 -1
  263. package/dist/publication.d.ts +1 -1
  264. package/dist/publication.d.ts.map +1 -1
  265. package/dist/publication.js.map +1 -1
  266. package/dist/record.d.ts +1 -5
  267. package/dist/record.d.ts.map +1 -1
  268. package/dist/record.js +3 -51
  269. package/dist/record.js.map +1 -1
  270. package/dist/remediation.d.ts.map +1 -1
  271. package/dist/remediation.js +3 -1
  272. package/dist/remediation.js.map +1 -1
  273. package/dist/runtime.d.ts +35 -7
  274. package/dist/runtime.d.ts.map +1 -1
  275. package/dist/runtime.js +778 -153
  276. package/dist/runtime.js.map +1 -1
  277. package/dist/saved-scan-bootstrap.d.ts +6 -1
  278. package/dist/saved-scan-bootstrap.d.ts.map +1 -1
  279. package/dist/saved-scan-bootstrap.js +267 -115
  280. package/dist/saved-scan-bootstrap.js.map +1 -1
  281. package/dist/scan-archive-path.d.ts +2 -0
  282. package/dist/scan-archive-path.d.ts.map +1 -0
  283. package/dist/scan-archive-path.js +5 -0
  284. package/dist/scan-archive-path.js.map +1 -0
  285. package/dist/scan-comparison.d.ts.map +1 -1
  286. package/dist/scan-comparison.js +6 -2
  287. package/dist/scan-comparison.js.map +1 -1
  288. package/dist/scan-dashboard.d.ts +1 -0
  289. package/dist/scan-dashboard.d.ts.map +1 -1
  290. package/dist/scan-dashboard.js +18 -2
  291. package/dist/scan-dashboard.js.map +1 -1
  292. package/dist/scan-executor.d.ts.map +1 -1
  293. package/dist/scan-executor.js +4 -2
  294. package/dist/scan-executor.js.map +1 -1
  295. package/dist/scan-history-renderer.d.ts +1 -0
  296. package/dist/scan-history-renderer.d.ts.map +1 -1
  297. package/dist/scan-history-renderer.js +25 -9
  298. package/dist/scan-history-renderer.js.map +1 -1
  299. package/dist/scan-logs.d.ts.map +1 -1
  300. package/dist/scan-logs.js +28 -4
  301. package/dist/scan-logs.js.map +1 -1
  302. package/dist/scanner-import.d.ts +5 -11
  303. package/dist/scanner-import.d.ts.map +1 -1
  304. package/dist/scanner-import.js +16 -89
  305. package/dist/scanner-import.js.map +1 -1
  306. package/dist/security-policy.d.ts.map +1 -1
  307. package/dist/security-policy.js +26 -6
  308. package/dist/security-policy.js.map +1 -1
  309. package/dist/semgrep-findings.d.ts.map +1 -1
  310. package/dist/semgrep-findings.js +14 -48
  311. package/dist/semgrep-findings.js.map +1 -1
  312. package/dist/semgrep-platform-findings.d.ts +20 -70
  313. package/dist/semgrep-platform-findings.d.ts.map +1 -1
  314. package/dist/semgrep-platform-findings.js +116 -333
  315. package/dist/semgrep-platform-findings.js.map +1 -1
  316. package/dist/snyk-findings.d.ts +2 -0
  317. package/dist/snyk-findings.d.ts.map +1 -1
  318. package/dist/snyk-findings.js +5 -8
  319. package/dist/snyk-findings.js.map +1 -1
  320. package/dist/snyk-rest-findings.d.ts +3 -20
  321. package/dist/snyk-rest-findings.d.ts.map +1 -1
  322. package/dist/snyk-rest-findings.js +45 -217
  323. package/dist/snyk-rest-findings.js.map +1 -1
  324. package/dist/sonar-findings.d.ts +1 -1
  325. package/dist/sonar-findings.d.ts.map +1 -1
  326. package/dist/sonar-findings.js +2 -4
  327. package/dist/sonar-findings.js.map +1 -1
  328. package/dist/sonar-import.d.ts +3 -0
  329. package/dist/sonar-import.d.ts.map +1 -1
  330. package/dist/sonar-import.js +28 -30
  331. package/dist/sonar-import.js.map +1 -1
  332. package/dist/stored-finding-projection.d.ts +6 -0
  333. package/dist/stored-finding-projection.d.ts.map +1 -0
  334. package/dist/stored-finding-projection.js +104 -0
  335. package/dist/stored-finding-projection.js.map +1 -0
  336. package/dist/stored-findings-publication.d.ts +23 -0
  337. package/dist/stored-findings-publication.d.ts.map +1 -0
  338. package/dist/stored-findings-publication.js +169 -0
  339. package/dist/stored-findings-publication.js.map +1 -0
  340. package/dist/targets.d.ts +6 -1
  341. package/dist/targets.d.ts.map +1 -1
  342. package/dist/targets.js +76 -23
  343. package/dist/targets.js.map +1 -1
  344. package/dist/tenable-findings.d.ts +1 -4
  345. package/dist/tenable-findings.d.ts.map +1 -1
  346. package/dist/tenable-findings.js.map +1 -1
  347. package/dist/trivy-findings.d.ts.map +1 -1
  348. package/dist/trivy-findings.js +0 -11
  349. package/dist/trivy-findings.js.map +1 -1
  350. package/dist/trusted-executable.d.ts +6 -0
  351. package/dist/trusted-executable.d.ts.map +1 -1
  352. package/dist/trusted-executable.js +140 -37
  353. package/dist/trusted-executable.js.map +1 -1
  354. package/dist/veracode-findings.d.ts +1 -3
  355. package/dist/veracode-findings.d.ts.map +1 -1
  356. package/dist/veracode-findings.js +13 -38
  357. package/dist/veracode-findings.js.map +1 -1
  358. package/dist/version.d.ts +3 -1
  359. package/dist/version.d.ts.map +1 -1
  360. package/dist/version.js +184 -32
  361. package/dist/version.js.map +1 -1
  362. package/dist/wiz-findings.d.ts +2 -2
  363. package/dist/wiz-findings.d.ts.map +1 -1
  364. package/dist/wiz-findings.js +14 -92
  365. package/dist/wiz-findings.js.map +1 -1
  366. package/docs/cli.md +373 -106
  367. package/docs/findings-service.md +22 -9
  368. package/docs/hosted-remediation.md +13 -4
  369. package/docs/sdk.md +903 -0
  370. package/package.json +10 -6
  371. package/schemas/external-findings.schema.json +2 -1
  372. package/dist/hosted-scanner-v1.d.ts +0 -6
  373. package/dist/hosted-scanner-v1.d.ts.map +0 -1
  374. package/dist/hosted-scanner-v1.js +0 -60
  375. package/dist/hosted-scanner-v1.js.map +0 -1
  376. package/dist/reader-selection.d.ts +0 -15
  377. package/dist/reader-selection.d.ts.map +0 -1
  378. package/dist/reader-selection.js +0 -89
  379. package/dist/reader-selection.js.map +0 -1
  380. package/dist/semgrep-platform-v1.d.ts +0 -6
  381. package/dist/semgrep-platform-v1.d.ts.map +0 -1
  382. package/dist/semgrep-platform-v1.js +0 -83
  383. package/dist/semgrep-platform-v1.js.map +0 -1
  384. package/dist/snyk-rest-v1.d.ts +0 -28
  385. package/dist/snyk-rest-v1.d.ts.map +0 -1
  386. package/dist/snyk-rest-v1.js +0 -132
  387. package/dist/snyk-rest-v1.js.map +0 -1
@@ -5,6 +5,7 @@ from __future__ import annotations
5
5
  import argparse
6
6
  import codecs
7
7
  import hashlib
8
+ import json
8
9
  import os
9
10
  import re
10
11
  import shutil
@@ -14,18 +15,22 @@ import subprocess
14
15
  import sys
15
16
  import tempfile
16
17
  import unicodedata
18
+ from collections.abc import Iterable, Iterator
17
19
  from concurrent.futures import ThreadPoolExecutor
20
+ from contextlib import ExitStack, contextmanager
21
+ from contextvars import ContextVar
18
22
  from pathlib import Path, PurePosixPath
19
23
  from typing import Any, BinaryIO, Callable, cast
20
24
 
21
25
  # Some plugin hosts launch Python with safe-path isolation enabled.
22
26
  sys.path.insert(0, str(Path(__file__).resolve().parent))
23
- from filesystem_identity import stored_filesystem_identity_matches
27
+ from filesystem_identity import serialize_filesystem_identity, stored_filesystem_identity_matches
24
28
  from windows_scan_local_files import copy_directory_junction, pinned_root_git_marker
25
29
  from workbench_constants import GIT_REPOSITORY_ENVIRONMENT
26
30
 
27
31
  SOURCE_METADATA_DIRECTORIES = {".git", ".svn"}
28
32
  _WINDOWS = os.name == "nt"
33
+ _SCOPED_GIT: ContextVar[Callable[[Path], str] | None] = ContextVar("workbench_git", default=None)
29
34
 
30
35
 
31
36
  class UnreadableLocalFile(SystemExit):
@@ -204,8 +209,10 @@ def _stream_is_binary(output: BinaryIO, size: int, *, drain: bool = False) -> bo
204
209
 
205
210
  def _protected_repository_root(target: Path) -> Path:
206
211
  root = target.resolve()
207
- if root.is_file():
208
- root = root.parent
212
+ return _outer_checkout_root(root.parent if root.is_file() else root)
213
+
214
+
215
+ def _outer_checkout_root(root: Path) -> Path:
209
216
  protected = root
210
217
  for ancestor in (root, *root.parents):
211
218
  try:
@@ -216,9 +223,67 @@ def _protected_repository_root(target: Path) -> Path:
216
223
  return protected
217
224
 
218
225
 
226
+ def rejected_checkout_roots(value: str) -> list[str] | None:
227
+ # Retain both spellings before a checkout marker or symlink can move.
228
+ try:
229
+ lexical = Path(value).expanduser().absolute()
230
+ canonical = lexical.resolve()
231
+ lexical_root = _outer_checkout_root(lexical)
232
+ roots = {
233
+ lexical,
234
+ canonical,
235
+ lexical_root,
236
+ lexical_root.resolve(),
237
+ _protected_repository_root(canonical),
238
+ }
239
+ return sorted(map(str, roots))
240
+ except (OSError, RuntimeError):
241
+ return None
242
+
243
+
244
+ def pin_scoped_git(target: Path) -> str | None:
245
+ scoped = _SCOPED_GIT.get()
246
+ return scoped(target) if scoped is not None else None
247
+
248
+
219
249
  def trusted_git_executable(protected_root: Path) -> str | None:
220
250
  """Validate host-selected Git, or discover Git for a direct plugin invocation."""
221
251
  configured = os.environ.get("CODEX_SECURITY_GIT")
252
+ if configured is None:
253
+ configured = pin_scoped_git(protected_root)
254
+ return _resolve_git_executable(protected_root, configured)
255
+
256
+
257
+ def _historical_repository_roots(target: Path) -> Iterator[Path]:
258
+ try:
259
+ yield _protected_repository_root(target)
260
+ except (OSError, RuntimeError):
261
+ # Keep checking lexical ancestry when the saved path cannot be resolved.
262
+ pass
263
+ # A moved or unavailable descendant still excludes its lexical checkout ancestry.
264
+ root = Path(os.path.abspath(target))
265
+ protected = root
266
+ for ancestor in (root, *root.parents):
267
+ try:
268
+ (ancestor / ".git").lstat()
269
+ except (OSError, RuntimeError):
270
+ continue
271
+ protected = ancestor
272
+ yield protected
273
+ try:
274
+ yield protected.resolve()
275
+ except (OSError, RuntimeError):
276
+ # The lexical root above still excludes tools at an unavailable checkout.
277
+ pass
278
+
279
+
280
+ def _resolve_git_executable(
281
+ protected_root: Path,
282
+ configured: str | None,
283
+ *,
284
+ discovery_roots: Iterable[Path] = (),
285
+ recorded_git_roots: Iterable[Path] | None = (),
286
+ ) -> str | None:
222
287
  windows = sys.platform == "win32"
223
288
  if configured is None:
224
289
  names = ("git.exe", "git.com") if windows else ("git",)
@@ -236,7 +301,22 @@ def trusted_git_executable(protected_root: Path) -> str | None:
236
301
  candidates = iter((candidate,))
237
302
 
238
303
  try:
239
- repository = _protected_repository_root(protected_root)
304
+ repositories = [_protected_repository_root(protected_root)]
305
+ if configured is None:
306
+ # A legacy Git target has no trustworthy boundary to exclude from PATH.
307
+ if recorded_git_roots is None:
308
+ return None
309
+ saved_boundaries = tuple(recorded_git_roots)
310
+ for root in saved_boundaries:
311
+ repositories.extend(_historical_repository_roots(root))
312
+ for root in discovery_roots:
313
+ for repository in _historical_repository_roots(root):
314
+ if not repository.is_dir() and not any(
315
+ repository.is_relative_to(boundary) for boundary in saved_boundaries
316
+ ):
317
+ # Its former checkout is neither discoverable nor already excluded.
318
+ return None
319
+ repositories.append(repository)
240
320
  except (OSError, RuntimeError):
241
321
  return None
242
322
 
@@ -261,6 +341,7 @@ def trusted_git_executable(protected_root: Path) -> str | None:
261
341
  continue
262
342
  if any(
263
343
  path == repository or repository in path.parents
344
+ for repository in repositories
264
345
  for path in (lexical, invocation, canonical)
265
346
  ):
266
347
  if configured is not None:
@@ -270,6 +351,39 @@ def trusted_git_executable(protected_root: Path) -> str | None:
270
351
  return None
271
352
 
272
353
 
354
+ @contextmanager
355
+ def scoped_git_discovery(
356
+ protected_roots: Callable[[], Iterable[Path]],
357
+ *,
358
+ recorded_git_roots: Callable[[], Iterable[Path] | None] = lambda: (),
359
+ ) -> Iterator[None]:
360
+ """Pin ambient Git when an operation first needs to inspect repository targets."""
361
+ if os.environ.get("CODEX_SECURITY_GIT") is not None:
362
+ yield
363
+ return
364
+ executable: str | None = None
365
+
366
+ def resolve_once(target: Path) -> str:
367
+ nonlocal executable
368
+ if executable is None:
369
+ executable = (
370
+ _resolve_git_executable(
371
+ Path.cwd(),
372
+ None,
373
+ discovery_roots=(*protected_roots(), target),
374
+ recorded_git_roots=recorded_git_roots(),
375
+ )
376
+ or ""
377
+ )
378
+ return executable
379
+
380
+ token = _SCOPED_GIT.set(resolve_once)
381
+ try:
382
+ yield
383
+ finally:
384
+ _SCOPED_GIT.reset(token)
385
+
386
+
273
387
  def git_command(
274
388
  target: Path,
275
389
  *args: str,
@@ -279,6 +393,7 @@ def git_command(
279
393
  work_tree: Path | None = None,
280
394
  stdout_file: BinaryIO | None = None,
281
395
  environment_overrides: dict[str, str] | None = None,
396
+ inherit_git_environment: bool = True,
282
397
  offline: bool = False,
283
398
  ) -> subprocess.CompletedProcess[str] | subprocess.CompletedProcess[bytes]:
284
399
  """Run Git against target and return the completed process with its exit status.
@@ -289,7 +404,13 @@ def git_command(
289
404
  """
290
405
  if (git_dir is None) != (work_tree is None):
291
406
  raise ValueError("git_dir and work_tree must be provided together")
292
- environment = os.environ.copy()
407
+ environment = (
408
+ os.environ.copy()
409
+ if inherit_git_environment
410
+ else {
411
+ name: value for name, value in os.environ.items() if not name.upper().startswith("GIT_")
412
+ }
413
+ )
293
414
  for name in GIT_REPOSITORY_ENVIRONMENT:
294
415
  environment.pop(name, None)
295
416
  environment["GIT_LITERAL_PATHSPECS"] = "1"
@@ -575,6 +696,56 @@ def _visible_git_character(char: str) -> str:
575
696
  return ascii(char)[1:-1]
576
697
 
577
698
 
699
+ def _canonical_patch_chunks(output: BinaryIO) -> Iterator[bytes]:
700
+ """Drop Git's optional function label while preserving hunk coordinates and content."""
701
+ line_start = True
702
+ # Limit each read even when source lines are arbitrarily large.
703
+ for part in iter(lambda: output.readline(1024 * 1024), b""):
704
+ next_line_start = part.endswith(b"\n")
705
+ if line_start and part.startswith(b"@@ "):
706
+ coordinates, end, _ = part.partition(b" @@")
707
+ if end:
708
+ part = coordinates + end + b"\n"
709
+ yield part
710
+ line_start = next_line_start
711
+
712
+
713
+ @contextmanager
714
+ def _diff_driver_config(
715
+ target: Path, git_dir: Path | None, work_tree: Path | None
716
+ ) -> Iterator[tuple[str, ...]]:
717
+ names = git_command(
718
+ target,
719
+ "config",
720
+ "--name-only",
721
+ "--null",
722
+ "--get-regexp",
723
+ r"^diff\..+\.binary$",
724
+ text=False,
725
+ git_dir=git_dir,
726
+ work_tree=work_tree,
727
+ environment_overrides={"LC_ALL": "C"},
728
+ )
729
+ if names.returncode == 1: # Git returns 1 when there are no matching keys.
730
+ yield ()
731
+ return
732
+ if names.returncode:
733
+ raise SystemExit(
734
+ os.fsdecode(names.stderr) or "Could not inspect Git diff-driver configuration."
735
+ )
736
+ # A private command-scope include preserves case and arbitrary driver-name bytes
737
+ # without putting an unbounded number of keys on the command line.
738
+ with tempfile.TemporaryDirectory(prefix="codex-security-diff-") as directory:
739
+ config = Path(directory) / "drivers"
740
+ with config.open("wb") as stream:
741
+ for key in names.stdout.split(b"\0"):
742
+ if key:
743
+ driver = key.removeprefix(b"diff.").removesuffix(b".binary")
744
+ escaped = driver.replace(b"\\", b"\\\\").replace(b'"', b'\\"')
745
+ stream.write(b'[diff "' + escaped + b'"]\n\tbinary = auto\n')
746
+ yield "-c", f"include.path={config}"
747
+
748
+
578
749
  def _update_digest_field_from_git(
579
750
  digest: Any,
580
751
  label: bytes,
@@ -582,15 +753,24 @@ def _update_digest_field_from_git(
582
753
  *args: str,
583
754
  git_dir: Path | None = None,
584
755
  work_tree: Path | None = None,
756
+ environment_overrides: dict[str, str] | None = None,
757
+ stable_display: bool = False,
585
758
  ) -> None:
586
- with tempfile.TemporaryFile() as output:
759
+ with tempfile.TemporaryFile() as output, ExitStack() as stack:
760
+ driver_config = (
761
+ stack.enter_context(_diff_driver_config(target, git_dir, work_tree))
762
+ if stable_display
763
+ else ()
764
+ )
587
765
  completed = git_command(
588
766
  target,
767
+ *driver_config,
589
768
  *args,
590
769
  text=False,
591
770
  git_dir=git_dir,
592
771
  work_tree=work_tree,
593
772
  stdout_file=output,
773
+ environment_overrides=environment_overrides,
594
774
  )
595
775
  if completed.returncode != 0:
596
776
  detail = completed.stderr.decode("utf-8", errors="backslashreplace").replace(
@@ -600,41 +780,52 @@ def _update_digest_field_from_git(
600
780
  raise SystemExit(
601
781
  f"Could not snapshot the selected working-tree changes.\n{detail}".rstrip("\n")
602
782
  )
603
- output.seek(0, os.SEEK_END)
604
- value_size = output.tell()
783
+ if stable_display:
784
+ output.seek(0)
785
+ value_size = sum(len(part) for part in _canonical_patch_chunks(output))
786
+ else:
787
+ value_size = output.seek(0, os.SEEK_END)
605
788
  _update_digest_field_header(digest, label, value_size)
606
789
  output.seek(0)
607
- for chunk in iter(lambda: output.read(1024 * 1024), b""):
790
+ chunks = (
791
+ _canonical_patch_chunks(output)
792
+ if stable_display
793
+ else iter(lambda: output.read(1024 * 1024), b"")
794
+ )
795
+ for chunk in chunks:
608
796
  digest.update(chunk)
609
797
 
610
798
 
611
- def worktree_content_digest(target: Path) -> str:
799
+ def worktree_content_digest(target: Path, *, expected_digest: str | None = None) -> str:
612
800
  """Return the worktree digest for the selected path after checking the state of its submodules."""
613
801
  require_clean_submodule_worktrees(target)
614
802
  repository, pathspec = git_worktree_context(target)
615
- return worktree_content_digest_for_context(repository, pathspec)
803
+ return worktree_content_digest_for_context(
804
+ repository, pathspec, expected_digest=expected_digest
805
+ )
616
806
 
617
807
 
618
808
  def remediation_checkout_snapshot(
619
809
  scan: sqlite3.Row,
620
810
  *,
621
811
  expected_revision: str | None = None,
812
+ expected_digest: str | None = None,
622
813
  head_changed_message: str = (
623
814
  "Repository HEAD changed. Regenerate the remediation patch against the current checkout."
624
815
  ),
625
816
  ) -> tuple[str, str | None]:
626
817
  """Return the current revision and content digest after verifying the recorded checkout identity and required revision."""
627
- target = require_scan_target_identity(scan)
628
- revision = git_revision(target)
629
- required_revision = expected_revision or scan["target_revision"]
630
- if revision != required_revision:
631
- raise SystemExit(head_changed_message)
632
- content_digest = (
633
- worktree_content_digest(target)
634
- if revision != "unversioned"
635
- else directory_content_digest(target, excluded=(Path(scan["scan_dir"]),))
636
- )
637
- return revision, content_digest
818
+ with checked_scan_target(scan) as target:
819
+ revision = git_revision(target)
820
+ required_revision = expected_revision or scan["target_revision"]
821
+ if revision != required_revision:
822
+ raise SystemExit(head_changed_message)
823
+ content_digest = (
824
+ worktree_content_digest(target, expected_digest=expected_digest)
825
+ if revision != "unversioned"
826
+ else directory_content_digest(target, excluded=(Path(scan["scan_dir"]),))
827
+ )
828
+ return revision, content_digest
638
829
 
639
830
 
640
831
  def require_scan_target_unchanged(scan: sqlite3.Row) -> None:
@@ -646,28 +837,33 @@ def require_scan_target_unchanged(scan: sqlite3.Row) -> None:
646
837
  and scan["target_revision"] != "unversioned"
647
838
  ):
648
839
  # Legacy Git records bind only the revision, not working-tree contents.
649
- target = require_scan_target_identity(scan)
650
- if git_revision(target) != scan["target_revision"]:
651
- raise SystemExit(head_changed_message)
652
- require_clean_submodule_worktrees(target)
840
+ with checked_scan_target(scan) as target:
841
+ if git_revision(target) != scan["target_revision"]:
842
+ raise SystemExit(head_changed_message)
843
+ require_clean_submodule_worktrees(target)
653
844
  return
654
- _, current_digest = remediation_checkout_snapshot(
655
- scan, head_changed_message=head_changed_message
656
- )
657
845
  if scan["diff_target_kind"] == "working_tree":
658
846
  expected_digest = scan["diff_content_digest"]
659
847
  elif scan["diff_target_kind"] in {"commit", "range"}:
660
- tracked = git_bytes(Path(scan["target_path"]), "ls-files", "-t", "-z")
848
+ expected_digest = clean_worktree_content_digest()
849
+ else:
850
+ expected_digest = scan["target_snapshot_digest"]
851
+ _, current_digest = remediation_checkout_snapshot(
852
+ scan, expected_digest=expected_digest, head_changed_message=head_changed_message
853
+ )
854
+ if scan["diff_target_kind"] in {"commit", "range"}:
855
+ target = Path(scan["target_path"])
856
+ tracked = git_bytes(target, "ls-files", "-v", "-z")
661
857
  if tracked is None:
662
858
  raise SystemExit("Could not inspect tracked files in the scan target.")
663
- if any(entry.startswith(b"S ") for entry in tracked.split(b"\0")):
859
+ entries = tracked.split(b"\0")
860
+ if any(entry.startswith((b"S ", b"s ")) for entry in entries):
664
861
  raise SystemExit(
665
862
  "Committed-diff scans require a full repository checkout. "
666
863
  "Sparse checkouts are not supported; materialize skipped tracked files and retry."
667
864
  )
668
- expected_digest = clean_worktree_content_digest()
669
- else:
670
- expected_digest = scan["target_snapshot_digest"]
865
+ if any(entry.startswith(b"h ") for entry in entries):
866
+ verify_assumed_unchanged_files(target)
671
867
  if current_digest != expected_digest:
672
868
  raise SystemExit(
673
869
  "Scan target contents changed since the scan. "
@@ -675,12 +871,67 @@ def require_scan_target_unchanged(scan: sqlite3.Row) -> None:
675
871
  )
676
872
 
677
873
 
874
+ def verify_assumed_unchanged_files(target: Path) -> None:
875
+ index = git_output(target, "rev-parse", "--path-format=absolute", "--git-path", "index")
876
+ if index is None:
877
+ raise SystemExit("Could not locate the Git index for the scan target.")
878
+ with tempfile.TemporaryDirectory(prefix="codex-security-git-index-") as directory:
879
+ # Refresh a private index without running checkout hooks or updating its shared index.
880
+ index_copy = Path(directory) / "index"
881
+ shutil.copyfile(index, index_copy)
882
+ result = git_command(
883
+ target,
884
+ "-c",
885
+ f"core.hooksPath={directory}",
886
+ "-c",
887
+ "core.splitIndex=false",
888
+ "update-index",
889
+ "--really-refresh",
890
+ text=False,
891
+ environment_overrides={"GIT_INDEX_FILE": str(index_copy)},
892
+ # A repository's clean filter can run while Git compares working-tree bytes.
893
+ inherit_git_environment=False,
894
+ offline=True,
895
+ )
896
+ if result.returncode == 1:
897
+ raise SystemExit(
898
+ "A tracked file marked assume-unchanged changed since the scan. "
899
+ "Run a new scan before validating its findings."
900
+ )
901
+ if result.returncode != 0:
902
+ raise SystemExit(
903
+ os.fsdecode(result.stderr) or "Could not refresh the scan target's Git index."
904
+ )
905
+
906
+
678
907
  def worktree_content_digest_for_context(
679
908
  repository: Path,
680
909
  pathspec: str,
681
910
  *,
682
911
  git_dir: Path | None = None,
683
912
  work_tree: Path | None = None,
913
+ expected_digest: str | None = None,
914
+ ) -> str:
915
+ def calculate(stable_display: bool) -> str:
916
+ return _git_content_digest(repository, pathspec, git_dir, work_tree, stable_display)
917
+
918
+ current = calculate(True)
919
+ # Older sealed snapshots may have included the user's Git display settings.
920
+ if (
921
+ expected_digest is not None
922
+ and current != expected_digest
923
+ and calculate(False) == expected_digest
924
+ ):
925
+ return expected_digest
926
+ return current
927
+
928
+
929
+ def _git_content_digest(
930
+ repository: Path,
931
+ pathspec: str,
932
+ git_dir: Path | None,
933
+ work_tree: Path | None,
934
+ stable_display: bool,
684
935
  ) -> str:
685
936
  digest = hashlib.sha256()
686
937
  update_digest_field(digest, b"format", b"codex-security-snapshot/v1")
@@ -689,12 +940,44 @@ def worktree_content_digest_for_context(
689
940
  ":(top,glob,exclude)**/.svn",
690
941
  ":(top,glob,exclude)**/.svn/**",
691
942
  ]
943
+ config = (
944
+ (
945
+ "-c",
946
+ "core.quotePath=true",
947
+ "-c",
948
+ "diff.suppressBlankEmpty=false",
949
+ "-c",
950
+ "core.compression=1", # Git uses the fastest zlib level for binary patches by default.
951
+ )
952
+ if stable_display
953
+ else ()
954
+ )
955
+ display = (
956
+ (
957
+ "--no-color",
958
+ "--src-prefix=a/",
959
+ "--dst-prefix=b/",
960
+ "--unified=3",
961
+ "--diff-algorithm=myers",
962
+ "--indent-heuristic",
963
+ "--inter-hunk-context=0",
964
+ "--find-renames=50%",
965
+ "-l1000", # Git's default inexact rename limit.
966
+ "--submodule=short",
967
+ "-O",
968
+ os.devnull,
969
+ )
970
+ if stable_display
971
+ else ()
972
+ )
692
973
  _update_digest_field_from_git(
693
974
  digest,
694
975
  b"tracked-diff",
695
976
  repository,
977
+ *config,
696
978
  "--no-literal-pathspecs",
697
979
  "diff",
980
+ *display,
698
981
  "--binary",
699
982
  "--full-index",
700
983
  "--no-ext-diff",
@@ -705,6 +988,9 @@ def worktree_content_digest_for_context(
705
988
  *source_pathspecs,
706
989
  git_dir=git_dir,
707
990
  work_tree=work_tree,
991
+ # GIT_DIFF_OPTS overrides even an explicit --unified option.
992
+ environment_overrides={"GIT_DIFF_OPTS": ""} if stable_display else None,
993
+ stable_display=stable_display,
708
994
  )
709
995
  untracked = git_bytes(
710
996
  repository,
@@ -869,7 +1155,7 @@ def _windows_inventory_query(
869
1155
  input_data: bytes | None = None,
870
1156
  environment_overrides: dict[str, str] | None = None,
871
1157
  ) -> subprocess.CompletedProcess[bytes]:
872
- """Retry a root inventory's metadata length error with its verified literal marker."""
1158
+ """Retry an inventory's metadata length error with its verified worktree marker."""
873
1159
 
874
1160
  def query(
875
1161
  directory: Path, environment: dict[str, str] | None
@@ -891,7 +1177,7 @@ def _windows_inventory_query(
891
1177
  if (
892
1178
  result.returncode != 128
893
1179
  or result.stdout
894
- or str(target) != str(work_tree)
1180
+ or not target.is_relative_to(work_tree)
895
1181
  or b"GIT_DIR" not in result.stderr
896
1182
  ):
897
1183
  return result
@@ -901,7 +1187,7 @@ def _windows_inventory_query(
901
1187
  probe = query(git_dir, {**(environment_overrides or {}), "LC_ALL": "C", "LANGUAGE": "C"})
902
1188
  if probe.returncode != 128 or probe.stdout or diagnostic not in probe.stderr.splitlines():
903
1189
  return result
904
- with pinned_root_git_marker(target, git_dir) as marker:
1190
+ with pinned_root_git_marker(target, git_dir, work_tree=work_tree) as marker:
905
1191
  if marker is not None:
906
1192
  retry = query(marker, environment_overrides)
907
1193
  if retry.returncode == 0 or (retry.returncode == 1 and "check-ignore" in args):
@@ -909,11 +1195,16 @@ def _windows_inventory_query(
909
1195
  return result
910
1196
 
911
1197
 
912
- def git_directory_snapshot_paths(target: Path) -> list[Path] | None:
913
- repository_root = git_output(target, "rev-parse", "--show-toplevel")
914
- if repository_root is None:
915
- return None
916
- repository, pathspec = git_worktree_context(target)
1198
+ def git_directory_snapshot_paths(
1199
+ target: Path, *, replay_git_dir: Path | None = None
1200
+ ) -> list[Path] | None:
1201
+ if replay_git_dir is None:
1202
+ if git_output(target, "rev-parse", "--show-toplevel") is None:
1203
+ return None
1204
+ repository, pathspec = git_worktree_context(target)
1205
+ else:
1206
+ # The temporary copy of an unborn repository uses the selected Git config and index.
1207
+ repository, pathspec = target, "."
917
1208
  scope = repository / pathspec
918
1209
  scope_depth = len(Path(pathspec).parts)
919
1210
  matching_prefixes: dict[str, bool] = {}
@@ -941,6 +1232,8 @@ def git_directory_snapshot_paths(target: Path) -> list[Path] | None:
941
1232
  "-z",
942
1233
  "--",
943
1234
  inventory_pathspec,
1235
+ git_dir=replay_git_dir,
1236
+ work_tree=repository if replay_git_dir else None,
944
1237
  )
945
1238
  if listed is None:
946
1239
  raise SystemExit("Could not inspect files in the selected Git working tree.")
@@ -999,7 +1292,7 @@ def git_directory_snapshot_paths(target: Path) -> list[Path] | None:
999
1292
  if _WINDOWS:
1000
1293
  # Git omits empty untracked directories from its file listing. Inventory
1001
1294
  # junction identities independently of whether their targets contain files.
1002
- git_directory = git_output(target, "rev-parse", "--absolute-git-dir")
1295
+ git_directory = replay_git_dir or git_output(target, "rev-parse", "--absolute-git-dir")
1003
1296
  if git_directory is None:
1004
1297
  raise SystemExit("Could not inspect the selected Git working tree.")
1005
1298
  directories = _windows_inventory_query(
@@ -1116,6 +1409,7 @@ def directory_content_digest(
1116
1409
  excluded: tuple[Path, ...] = (),
1117
1410
  include_ignored: bool = False,
1118
1411
  include_git_metadata: bool = False,
1412
+ replay_git_dir: Path | None = None,
1119
1413
  ) -> str:
1120
1414
  def raise_walk_error(error: OSError) -> None:
1121
1415
  raise error
@@ -1128,7 +1422,7 @@ def directory_content_digest(
1128
1422
  target, excluded, include_git_metadata=include_git_metadata, onerror=raise_walk_error
1129
1423
  )
1130
1424
  if include_ignored
1131
- else git_directory_snapshot_paths(target)
1425
+ else git_directory_snapshot_paths(target, replay_git_dir=replay_git_dir)
1132
1426
  )
1133
1427
  if paths is None:
1134
1428
  paths = source_directory_snapshot_paths(target, excluded)
@@ -1166,19 +1460,37 @@ def directory_content_digest(
1166
1460
  return f"codex-security-snapshot/v1:sha256:{digest.hexdigest()}"
1167
1461
 
1168
1462
 
1169
- def directory_snapshot_regular_file_count(target: Path) -> int:
1463
+ def directory_snapshot_regular_files(target: Path) -> Iterator[Path]:
1170
1464
  paths = git_directory_snapshot_paths(target)
1171
1465
  if paths is None:
1172
1466
  paths = source_directory_snapshot_paths(target)
1173
- count = 0
1174
1467
  for path in paths:
1175
1468
  try:
1176
1469
  metadata = path.lstat()
1177
1470
  except OSError as exc:
1178
1471
  raise SystemExit(f"Could not inspect local file: {path.relative_to(target)}") from exc
1179
1472
  if stat.S_ISREG(metadata.st_mode):
1180
- count += 1
1181
- return count
1473
+ yield path
1474
+
1475
+
1476
+ def directory_snapshot_regular_file_count(target: Path) -> int:
1477
+ return sum(1 for _ in directory_snapshot_regular_files(target))
1478
+
1479
+
1480
+ def scoped_regular_file_count(repository: Path, scopes: list[str]) -> int:
1481
+ # Count directory entries once while retaining explicitly requested ignored files.
1482
+ names: set[str] = set()
1483
+ for scope in scopes:
1484
+ selected = (repository / scope).resolve(strict=True)
1485
+ if not selected.is_relative_to(repository):
1486
+ raise SystemExit("Requested scope must be inside the repository.")
1487
+ if not SOURCE_METADATA_DIRECTORIES.isdisjoint(selected.relative_to(repository).parts):
1488
+ continue
1489
+ if selected.is_file():
1490
+ names.add(selected.as_posix())
1491
+ elif selected.is_dir():
1492
+ names.update(path.as_posix() for path in directory_snapshot_regular_files(selected))
1493
+ return len(names)
1182
1494
 
1183
1495
 
1184
1496
  def copy_directory_excluding(
@@ -1369,7 +1681,10 @@ def require_remediation_target(value: str) -> Path:
1369
1681
  raise SystemExit("Remediation target must be an absolute local directory path.")
1370
1682
  try:
1371
1683
  resolved = stored.resolve(strict=True)
1372
- except (FileNotFoundError, OSError) as exc:
1684
+ except NotADirectoryError:
1685
+ # A component of the recorded checkout path now names a non-directory.
1686
+ resolved = None
1687
+ except OSError as exc:
1373
1688
  raise SystemExit(
1374
1689
  "Remediation is unavailable because the selected checkout is no longer accessible."
1375
1690
  ) from exc
@@ -1380,9 +1695,78 @@ def require_remediation_target(value: str) -> Path:
1380
1695
  return stored
1381
1696
 
1382
1697
 
1383
- def require_scan_target_identity(scan: sqlite3.Row) -> Path:
1384
- """Return the recorded checkout path or raise SystemExit if it no longer identifies the original directory."""
1698
+ @contextmanager
1699
+ def verified_windows_checkout(
1700
+ scan: sqlite3.Row, target: Path, *, replaced_message: str
1701
+ ) -> Iterator[None]:
1702
+ """Bind a Windows Deep checkout for the full operation, including legacy stat-width changes."""
1703
+ from windows_scan_local_files import pinned_resume_checkout
1704
+
1705
+ binding = scan["windows_resume_target_identity"]
1706
+ stored = (scan["target_device"], scan["target_inode"])
1707
+ if binding is None:
1708
+
1709
+ def stat_identity() -> tuple[int | str, int | str]:
1710
+ try:
1711
+ metadata = target.stat()
1712
+ except OSError as exc:
1713
+ raise SystemExit(f"Cannot verify the original Windows checkout: {exc}") from exc
1714
+ return serialize_filesystem_identity(metadata.st_dev), serialize_filesystem_identity(
1715
+ metadata.st_ino
1716
+ )
1717
+
1718
+ if stat_identity() == stored:
1719
+ yield
1720
+ if stat_identity() != stored:
1721
+ raise SystemExit(replaced_message)
1722
+ return
1723
+
1724
+ with pinned_resume_checkout(target) as native:
1725
+ if binding is not None:
1726
+ if native is None:
1727
+ raise SystemExit("Cannot verify the original Windows checkout identity.")
1728
+ if list(native) != json.loads(binding):
1729
+ raise SystemExit(replaced_message)
1730
+ # Legacy stat returned at most a 32-bit volume and 64-bit file index.
1731
+ elif (
1732
+ native is None
1733
+ or (native[0] < 1 << 32 and native[1] < 1 << 64)
1734
+ or tuple(map(serialize_filesystem_identity, native)) != stored
1735
+ ):
1736
+ raise SystemExit(
1737
+ "Cannot verify the saved Windows checkout identity with this Python runtime. "
1738
+ "Use its original Python runtime or start a new scan with "
1739
+ f"'codex-security scans rerun {scan['id']}'."
1740
+ )
1741
+ yield
1742
+
1743
+
1744
+ @contextmanager
1745
+ def checked_scan_target(scan: sqlite3.Row) -> Iterator[Path]:
1746
+ """Keep any recorded Windows checkout binding intact while a caller reads the target."""
1385
1747
  target = require_remediation_target(scan["target_path"])
1748
+ replaced = "Remediation is unavailable because the selected checkout path was replaced. Start a new scan."
1749
+ if (
1750
+ os.name == "nt"
1751
+ and "windows_resume_target_identity" in scan.keys()
1752
+ and (
1753
+ scan["windows_resume_target_identity"] is not None
1754
+ or (
1755
+ scan["mode"] == "deep"
1756
+ and scan["launch_kind"] == "scan"
1757
+ and scan["recipe_json"] is not None
1758
+ )
1759
+ )
1760
+ ):
1761
+ with ExitStack() as pins:
1762
+ try:
1763
+ pins.enter_context(
1764
+ verified_windows_checkout(scan, target, replaced_message=replaced)
1765
+ )
1766
+ except OSError as exc:
1767
+ raise SystemExit(f"Cannot verify the original Windows checkout: {exc}") from exc
1768
+ yield target
1769
+ return
1386
1770
  expected_inode = scan["target_inode"]
1387
1771
  if expected_inode is None:
1388
1772
  raise SystemExit(
@@ -1396,11 +1780,14 @@ def require_scan_target_identity(scan: sqlite3.Row) -> Path:
1396
1780
  "Remediation is unavailable because the selected checkout is no longer accessible."
1397
1781
  ) from exc
1398
1782
  if not stored_filesystem_identity_matches(expected_inode, metadata.st_ino):
1399
- raise SystemExit(
1400
- "Remediation is unavailable because the selected checkout path was replaced. "
1401
- "Start a new scan."
1402
- )
1403
- return target
1783
+ raise SystemExit(replaced)
1784
+ yield target
1785
+
1786
+
1787
+ def require_scan_target_identity(scan: sqlite3.Row) -> Path:
1788
+ """Return the recorded checkout path after checking its filesystem identity."""
1789
+ with checked_scan_target(scan) as target:
1790
+ return target
1404
1791
 
1405
1792
 
1406
1793
  def require_git_worktree_head(target: Path) -> str:
@@ -1414,37 +1801,37 @@ def scan_target_warning(scan: sqlite3.Row) -> str | None:
1414
1801
  if scan["diff_target_kind"] != "working_tree" and not scan["target_snapshot_digest"]:
1415
1802
  return None
1416
1803
  try:
1417
- target = require_scan_target_identity(scan)
1418
- if scan["target_revision"] == "unversioned":
1419
- if (
1420
- directory_content_digest(target, excluded=(Path(scan["scan_dir"]),))
1421
- != scan["target_snapshot_digest"]
1422
- ):
1804
+ with checked_scan_target(scan) as target:
1805
+ if scan["target_revision"] == "unversioned":
1806
+ if (
1807
+ directory_content_digest(target, excluded=(Path(scan["scan_dir"]),))
1808
+ != scan["target_snapshot_digest"]
1809
+ ):
1810
+ return (
1811
+ "Directory contents changed while the scan was running; "
1812
+ "results were saved for the original snapshot."
1813
+ )
1814
+ return None
1815
+ if git_revision(target) == "unversioned":
1423
1816
  return (
1424
- "Directory contents changed while the scan was running; "
1425
- "results were saved for the original snapshot."
1817
+ "The scanned Git repository became unavailable while the scan was running; "
1818
+ "results were saved for the original revision."
1426
1819
  )
1427
- return None
1428
- if git_revision(target) == "unversioned":
1429
- return (
1430
- "The scanned Git repository became unavailable while the scan was running; "
1431
- "results were saved for the original revision."
1432
- )
1433
- working_tree = scan["diff_target_kind"] == "working_tree"
1434
- expected_head = scan["diff_head_revision"] if working_tree else scan["target_revision"]
1435
- if require_git_worktree_head(target) != expected_head:
1436
- return (
1437
- "Repository HEAD changed while the scan was running; "
1438
- "results were saved for the original revision."
1439
- )
1440
- expected_digest = (
1441
- scan["diff_content_digest"] if working_tree else scan["target_snapshot_digest"]
1442
- )
1443
- if worktree_content_digest(target) != expected_digest:
1444
- return (
1445
- "Working-tree contents changed while the scan was running; "
1446
- "results were saved for the original snapshot."
1820
+ working_tree = scan["diff_target_kind"] == "working_tree"
1821
+ expected_head = scan["diff_head_revision"] if working_tree else scan["target_revision"]
1822
+ if require_git_worktree_head(target) != expected_head:
1823
+ return (
1824
+ "Repository HEAD changed while the scan was running; "
1825
+ "results were saved for the original revision."
1826
+ )
1827
+ expected_digest = (
1828
+ scan["diff_content_digest"] if working_tree else scan["target_snapshot_digest"]
1447
1829
  )
1830
+ if worktree_content_digest(target, expected_digest=expected_digest) != expected_digest:
1831
+ return (
1832
+ "Working-tree contents changed while the scan was running; "
1833
+ "results were saved for the original snapshot."
1834
+ )
1448
1835
  except (OSError, SystemExit) as error:
1449
1836
  return (
1450
1837
  f"Could not verify the scan target after scanning: {error}; "