OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(plugin): save generated patches for completed scans (#2033)
Michael D'Angelo3fa6215fc102
diff --git a/plugins/codex-security/mcp-app/server.ts b/plugins/codex-security/mcp-app/server.tsindex 1c04b86ac..ddc39ba53 100644--- a/plugins/codex-security/mcp-app/server.ts+++ b/plugins/codex-security/mcp-app/server.ts@@ -411,6 +411,20 @@ const findingRemediationSchema = {baseRevision: requiredText.max(512).optional(),expectedVersion: positiveInteger,occurrenceId: occurrenceIdSchema,+ patch: z+ .union([+ z+ .string()+ .refine(+ (value) => value.isWellFormed(),+ "Patch text contains an unpaired Unicode surrogate. Send the original bytes as base64.",+ ),+ z.object({ encoding: z.literal("base64"), content: z.base64() }),+ ])+ .optional()+ .describe(+ "For state generated, supply the exact unified diff as UTF-8 text or as base64 of its original bytes. The workbench saves it and computes its digest. For an existing scan-local file, supply patchPath and patchDigest without patch.",+ ),patchDigest: z.string().regex(/^sha256:[a-f0-9]{64}$/)@@ -1837,36 +1851,17 @@ export function createCodexSecurityServer(): McpServer {annotations: nonIdempotentWritingAnnotations,_meta: modelActionMeta,},- async ({- occurrenceId,- requestId,- actionToken,- expectedVersion,- state,- summary,- patchPath,- patchDigest,- baseRevision,- verificationSummary,- }) =>+ async ({ patch, ...fields }) =>scanActionResult(- await runWorkbench([- "set-finding-remediation",- "--occurrence-id",- occurrenceId,- "--request-id",- requestId,- ...optionalArg("--action-token", actionToken),- "--expected-version",- String(expectedVersion),- "--state",- state,- ...definedArg("--summary", summary),- ...definedArg("--patch-path", patchPath),- ...definedArg("--patch-digest", patchDigest),- ...definedArg("--base-revision", baseRevision),- ...definedArg("--verification-summary", verificationSummary),- ]),+ await runWorkbench(+ workbenchArgs("set-finding-remediation", {+ ...fields,+ patchStdin: patch !== undefined,+ }),+ typeof patch === "object"+ ? Buffer.from(patch.content, patch.encoding)+ : patch,+ ),"Updated the local Codex Security finding remediation state.",),);diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.ts b/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.tsindex ff54a6734..990f287d4 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.ts@@ -519,8 +519,8 @@ from pathlib import Pathsys.path.insert(0, str(Path(sys.argv[1]).parent))import finalize_scan_contract as finalizeroriginal_open = finalizer._open_scan_local_directory-def open_then_replace(root_fd, parts, *, create):- descriptor = original_open(root_fd, parts, create=create)+def open_then_replace(root_fd, parts, *, create, **options):+ descriptor = original_open(root_fd, parts, create=create, **options)if not create and parts == ("artifacts", "nested"):if ${JSON.stringify(replacement)} == "parent":os.rename(${JSON.stringify(parent)}, ${JSON.stringify(moved)})@@ -869,8 +869,8 @@ from pathlib import Pathsys.path.insert(0, str(Path(sys.argv[1]).parent))import finalize_scan_contract as finalizeroriginal_open = finalizer._open_scan_local_directory-def open_then_replace(root_fd, parts, *, create):- descriptor = original_open(root_fd, parts, create=create)+def open_then_replace(root_fd, parts, **options):+ descriptor = original_open(root_fd, parts, **options)if parts == ("artifacts", "nested"):os.rename(${JSON.stringify(parent)}, ${JSON.stringify(moved)})os.symlink(${JSON.stringify(outside)}, ${JSON.stringify(parent)})diff --git a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.tsindex 0c4b55e76..631301220 100644--- a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts+++ b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts@@ -10,6 +10,7 @@ import { hash, randomUUID } from "node:crypto";import {chmod,mkdir,+ readdir,readFile,realpath,rm,@@ -2693,7 +2694,11 @@ try {rejectedPendingClose.result.content[0].text,/pending remediation operation/,);- const remediationPatchPath = " remediation.patch";+ const manifestPath = path.join(initializedScanDir, "scan-manifest.json");+ const sealedManifest = await readFile(manifestPath);+ const filesBeforeGeneration = await readdir(initializedScanDir, {+ recursive: true,+ });const remediationPatch = `diff --git a/src/a.py b/src/a.py--- a/src/a.py+++ b/src/a.py@@ -2701,14 +2706,6 @@ try {-vulnerable+fixed`;- await writeFile(- path.join(initializedScanDir, remediationPatchPath),- remediationPatch,- );- await writeFile(- path.join(initializedScanDir, remediationPatchPath.trim()),- "different patch contents\n",- );const generatedPatch = await testServer.callTool(62, {name: "set_codex_security_finding_remediation",arguments: {@@ -2717,27 +2714,34 @@ try {requestId: remediationRequestId,expectedVersion: 1,state: "generated",- patchPath: remediationPatchPath,- patchDigest: `sha256:${hash("sha256", remediationPatch)}`,+ patch: remediationPatch,summary: "Contain archive extraction under the output root.",},});assertNoError(generatedPatch);+ const generatedFinding =+ generatedPatch.result.structuredContent.scan.findings[0];+ const remediation = generatedFinding.remediationState;+ const savedPatchPath = path.join(initializedScanDir, remediation.patchPath);+ assert.equal(remediation.state, "generated");+ assert.equal(remediation.patch, remediationPatch);assert.equal(- generatedPatch.result.structuredContent.scan.findings[0].remediationState- .state,- "generated",+ remediation.patchDigest,+ `sha256:${hash("sha256", remediationPatch)}`,);- assert.equal(- generatedPatch.result.structuredContent.scan.findings[0].remediationState- .patch,- remediationPatch,+ assert.ok(+ !filesBeforeGeneration.includes(path.normalize(remediation.patchPath)),);+ assert.deepEqual(+ await readFile(savedPatchPath),+ Buffer.from(remediationPatch),+ );+ assert.deepEqual(await readFile(manifestPath), sealedManifest);assert.equal(- generatedPatch.result.structuredContent.scan.findings[0].remediationState- .patchPath,- remediationPatchPath,+ await readFile(path.join(target, "src/a.py"), "utf8"),+ "vulnerable\n",);+ assert.equal(generatedFinding.triage.status, "open");const applyActionToken = randomUUID();const requestedApply = await testServer.callTool(65, {@@ -2774,16 +2778,10 @@ try {markedApplyDelivered.result.structuredContent.scan.findings[0].remediationState.actionDeliveredAt,);- execFileSync(- "git",- [- "apply",- "--no-index",- path.join(initializedScanDir, remediationPatchPath),- ],- {- cwd: target,- },+ execFileSync("git", ["apply", "--no-index", savedPatchPath], { cwd: target });+ assert.match(+ await readFile(path.join(target, "src/a.py"), "utf8"),+ /^fixed\r?\n$/,);const appliedPatch = await testServer.callTool(66, {@@ -2795,9 +2793,7 @@ try {occurrenceId,requestId: remediationRequestId,state: "applied",- patchPath:- generatedPatch.result.structuredContent.scan.findings[0]- .remediationState.patchPath,+ patchPath: remediation.patchPath,},});assertNoError(appliedPatch);@@ -2882,6 +2878,55 @@ try {"Focused remediation checks passed.",);+ const latin1Source = path.join(target, "src/info.py");+ await writeFile(latin1Source, "café\n", "latin1");+ const latin1Patch = Buffer.from(+ "--- a/src/info.py\n+++ b/src/info.py\n@@ -1 +1 @@\n-café\n+résolu\n",+ "latin1",+ );+ const latin1Request = {+ occurrenceId: results.findings[1].occurrenceId,+ actionToken: randomUUID(),+ requestId: randomUUID(),+ };+ assertNoError(+ await testServer.callTool(174, {+ name: "request_codex_security_finding_remediation",+ arguments: latin1Request,+ }),+ );+ const latin1Generation = {+ ...latin1Request,+ expectedVersion: 1,+ state: "generated",+ };+ const malformedPatch = await testServer.callTool(175, {+ name: "set_codex_security_finding_remediation",+ arguments: { ...latin1Generation, patch: "bad\ud800" },+ });+ assert.equal(malformedPatch.result.isError, true);+ assert.match(+ malformedPatch.result.content[0].text,+ /unpaired Unicode surrogate.*base64/,+ );+ const generatedLatin1 = await testServer.callTool(176, {+ name: "set_codex_security_finding_remediation",+ arguments: {+ ...latin1Generation,+ patch: { encoding: "base64", content: latin1Patch.toString("base64") },+ },+ });+ assertNoError(generatedLatin1);+ const { patchDigest, patchPath } =+ generatedLatin1.result.structuredContent.scan.findings[1].remediationState;+ const latin1PatchPath = path.join(initializedScanDir, patchPath);+ assert.equal(patchDigest, `sha256:${hash("sha256", latin1Patch)}`);+ assert.deepEqual(await readFile(latin1PatchPath), latin1Patch);+ execFileSync("git", ["apply", "--no-index", latin1PatchPath], {+ cwd: target,+ });+ assert.match(await readFile(latin1Source, "latin1"), /^résolu\r?\n$/);+const findingsPage = await testServer.callTool(67, {name: "list_codex_security_findings",arguments: { scanId, offset: 0, limit: 1 },diff --git a/plugins/codex-security/plugin-files.json b/plugins/codex-security/plugin-files.jsonindex 8f3f31885..65ddc8422 100644--- a/plugins/codex-security/plugin-files.json+++ b/plugins/codex-security/plugin-files.json@@ -71,6 +71,7 @@"scripts/finding_preview.py","scripts/launch_codex_security_mcp","scripts/launch_codex_security_mcp.cmd",+ "scripts/posix_scan_local_files.py","scripts/report_projection.py","scripts/review_state.py","scripts/reserved_artifact_paths.json",diff --git a/plugins/codex-security/references/artifact-storage.md b/plugins/codex-security/references/artifact-storage.mdindex 6cc7cb194..5c6401505 100644--- a/plugins/codex-security/references/artifact-storage.md+++ b/plugins/codex-security/references/artifact-storage.md@@ -53,4 +53,4 @@ End each shared threat model with these two lines:- `Repository: <stable target identity from scan-contract.md>`- `Version: <revision for an immutable Git tree; snapshot digest otherwise>`-Completed/sealed scan files cannot be edited through the save tool. For later write-ups or hardening requests, use the standalone target collection and link those returned files separately. Preserve the original result and its references. Temporary cleanup must not remove retained files or recovery checkpoints. The save tool publishes running-scan files under the same completion lock as finalization; surface a stopped/sealed-scan rejection and preserve existing output.+Completed/sealed scan files cannot be edited through the save tool. For a workbench Generate request, save or register the patch with `set_codex_security_finding_remediation` using `state: "generated"`. For later write-ups or hardening requests, use the standalone target collection and link those returned files separately. Preserve the original result and its references. Temporary cleanup must not remove retained files or recovery checkpoints. The save tool publishes running-scan files under the same completion lock as finalization; surface a stopped/sealed-scan rejection and preserve existing output.diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.pyindex 8f2d3d722..babe102f5 100644--- a/plugins/codex-security/scripts/finalize_scan_contract.py+++ b/plugins/codex-security/scripts/finalize_scan_contract.py@@ -14,10 +14,11 @@ import jsonimport mathimport osimport re-import secretsimport statimport structimport sys+from collections.abc import Callable, Iterator+from contextlib import contextmanagerfrom datetime import datetime, timezonefrom pathlib import Path, PurePosixPathfrom typing import Any, NamedTuple, TextIO@@ -28,6 +29,10 @@ sys.path.insert(0, str(Path(__file__).resolve().parent))import report_projectionimport servicenow_avitimport threat_model_projection+from posix_scan_local_files import (+ descriptor_relative_writes_available as _descriptor_relative_writes_available,+)+from posix_scan_local_files import managed_fd, stage_writefrom review_state import checkpoint_review_state, review_state, review_summarySCHEMA_VERSION = "1.0"@@ -500,15 +505,6 @@ def _is_windows() -> bool:return os.name == "nt"-def _descriptor_relative_writes_available() -> bool:- # os.replace accepts src_dir_fd/dst_dir_fd wherever descriptor-relative- # os.rename is supported, but Python lists only os.rename in supports_dir_fd.- required_operations = (os.mkdir, os.open, os.rename, os.stat, os.unlink)- return hasattr(os, "O_NOFOLLOW") and all(- operation in os.supports_dir_fd for operation in required_operations- )--def _windows_scan_local_files() -> Any:"""Load the Win32 backend and shared stream comparison lazily."""import windows_scan_local_files@@ -534,13 +530,12 @@ def _open_verified_scan_directory(raise _scan_local_directory_error("scan directory: expected an existing non-symlink directory", exc) from exc- opened = os.fstat(descriptor)- opened_identity = (opened.st_dev, opened.st_ino)- if opened_identity != observed_identity or (- expected_root_identity is not None and opened_identity != expected_root_identity- ):+ try:+ if not os.path.samestat(expected, os.fstat(descriptor)):+ raise ContractError("scan directory: changed while it was being opened")+ except BaseException:os.close(descriptor)- raise ContractError("scan directory: changed while it was being opened")+ raisereturn descriptor@@ -560,23 +555,41 @@ def scan_root_identity(scan_dir: Path) -> tuple[Path, tuple[int, int]]:os.close(descriptor)-def _open_scan_local_directory(root_fd: int, parts: tuple[str, ...], *, create: bool) -> int:+def _open_scan_local_directory(+ root_fd: int,+ parts: tuple[str, ...],+ *,+ create: bool,+ owner_read_write: bool = False,+ sync_parents: bool = False,+) -> int:descriptor = os.dup(root_fd)try:for part in parts:+ if sync_parents:+ os.fsync(descriptor)if create:try:os.mkdir(part, mode=0o700, dir_fd=descriptor)except FileExistsError:# The open below verifies that an existing entry is a real directory.pass- next_descriptor = os.open(+ else:+ if owner_read_write:+ mode = os.stat(part, dir_fd=descriptor, follow_symlinks=False).st_mode+ if mode & stat.S_IRWXU != stat.S_IRWXU:+ os.rmdir(part, dir_fd=descriptor)+ raise ScanLocalIOError(+ f"{part}: process umask prevents owner access to the new directory"+ )+ parent_descriptor = descriptor+ descriptor = os.open(part,os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0),- dir_fd=descriptor,+ dir_fd=parent_descriptor,)- os.close(descriptor)- descriptor = next_descriptor+ # If closing the parent fails, cleanup now owns the child.+ os.close(parent_descriptor)return descriptorexcept BaseException:os.close(descriptor)@@ -766,6 +779,40 @@ def _sha256_scan_local_file(scan_dir: Path, relative_path: str, context: str) ->return digest.hexdigest()+def scan_local_file_version(descriptor: int) -> tuple[os.stat_result, int]:+ metadata = os.fstat(descriptor)+ changed = (+ _windows_scan_local_files().file_change_time(descriptor)+ if _is_windows()+ else metadata.st_ctime_ns+ )+ return metadata, changed+++def _scan_local_write_path(+ scan_dir: Path, relative_path: str, external_name: bool+) -> tuple[Path, str]:+ scan_dir = _require_scan_directory(scan_dir)+ if external_name:+ if relative_path in {"", ".", ".."} or "/" in relative_path or "\0" in relative_path:+ raise ContractError("external output path: expected a safe file name")+ else:+ relative_path = _require_portable_relative_path(relative_path, "scan-local output path")+ return scan_dir, relative_path+++@contextmanager+def _windows_write_errors(backend: Any, relative_path: str) -> Iterator[None]:+ try:+ yield+ except FileExistsError:+ raise+ except backend.WindowsScanLocalPathError as exc:+ raise ContractError(f"{relative_path}: {exc}") from exc+ except OSError as exc:+ raise ScanLocalIOError(f"{relative_path}: {exc}") from exc++def write_scan_local_bytes(scan_dir: Path,relative_path: str,@@ -774,111 +821,96 @@ def write_scan_local_bytes(external_name: bool = False,expected_root_identity: tuple[int, int] | None = None,owner_read_write: bool = False,+ replace_existing: bool = True,) -> None:- scan_dir = _require_scan_directory(scan_dir)- if external_name:- if relative_path in {"", ".", ".."} or "/" in relative_path or "\0" in relative_path:- raise ContractError("external output path: expected a safe file name")- else:- relative_path = _require_portable_relative_path(relative_path, "scan-local output path")- path = scan_dir / relative_path+ if _is_windows() and not _descriptor_relative_writes_available():+ scan_dir, relative_path = _scan_local_write_path(scan_dir, relative_path, external_name)+ backend = _windows_scan_local_files()+ with _windows_write_errors(backend, relative_path):+ backend.atomic_write(+ scan_dir,+ relative_path,+ payload,+ expected_root_identity=expected_root_identity,+ replace_existing=replace_existing,+ )+ return+ with stage_scan_local_bytes(+ scan_dir,+ relative_path,+ payload,+ external_name=external_name,+ expected_root_identity=expected_root_identity,+ owner_read_write=owner_read_write,+ replace_existing=replace_existing,+ ) as publish:+ publish()+++@contextmanager+def stage_scan_local_bytes(+ scan_dir: Path,+ relative_path: str,+ payload: bytes,+ *,+ external_name: bool = False,+ expected_root_identity: tuple[int, int] | None = None,+ owner_read_write: bool = False,+ replace_existing: bool = False,+) -> Iterator[Callable[..., tuple[os.stat_result, int]]]:+ scan_dir, relative_path = _scan_local_write_path(scan_dir, relative_path, external_name)if not _descriptor_relative_writes_available():if not _is_windows():raise ContractError("scan-local output requires descriptor-relative file operations")backend = _windows_scan_local_files()- try:- backend.atomic_write(+ with _windows_write_errors(backend, relative_path):+ with backend.stage_write(scan_dir,relative_path,payload,expected_root_identity=expected_root_identity,- )- except backend.WindowsScanLocalPathError as exc:- raise ContractError(f"{relative_path}: {exc}") from exc- except OSError as exc:- raise ScanLocalIOError(f"{relative_path}: {exc}") from exc+ replace_existing=replace_existing,+ ) as publish:+ yield publishreturn- root_fd: int | None = None- parent_fd: int | None = None- temp_name: str | None = None- try:- root_fd = _open_verified_scan_directory(scan_dir, expected_root_identity)+ with managed_fd(_open_verified_scan_directory(scan_dir, expected_root_identity)) as root_fd:parts = PurePosixPath(relative_path).partstry:- parent_fd = _open_scan_local_directory(root_fd, parts[:-1], create=True)+ parent_fd = _open_scan_local_directory(+ root_fd, parts[:-1], create=True, owner_read_write=owner_read_write+ )except OSError as exc:raise _scan_local_directory_error(f"{relative_path}: expected a path inside the scan directory", exc) from exc- # The held descriptor is the authority for the validated parent. A- # concurrent rename cannot redirect later operations through a- # replacement path or link.- try:- metadata = os.stat(parts[-1], dir_fd=parent_fd, follow_symlinks=False)- except FileNotFoundError:- pass- else:- if not stat.S_ISREG(metadata.st_mode):- raise ContractError(f"{relative_path}: expected a regular non-symlink file")- try:- existing_fd = os.open(- parts[-1],- os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0),- dir_fd=parent_fd,- )- except OSError as exc:- if exc.errno not in {errno.ENOENT, errno.EACCES, errno.EPERM}:- raise- else:- try:- opened = os.fstat(existing_fd)- if not stat.S_ISREG(opened.st_mode):- raise ContractError(f"{relative_path}: expected a regular non-symlink file")- if (opened.st_dev, opened.st_ino) != (- metadata.st_dev,- metadata.st_ino,- ):- raise ContractError(f"{relative_path}: changed while it was being opened")- if expected_root_identity is not None and opened.st_size == len(payload):- try:- with os.fdopen(existing_fd, "rb") as handle:- existing_fd = -1- if _windows_scan_local_files().stream_matches_payload(- handle, payload- ):- return- except OSError:- # A failed content comparison still allows an atomic replacement.- pass- finally:- if existing_fd >= 0:- os.close(existing_fd)- temp_name = f".{path.name}.{secrets.token_hex(8)}.tmp"- temp_fd = os.open(temp_name, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600, dir_fd=parent_fd)- with os.fdopen(temp_fd, "wb") as handle:- if owner_read_write:- # Generated Markdown must remain editable under a restrictive umask.- os.fchmod(handle.fileno(), 0o600)- handle.write(payload)- handle.flush()- os.fsync(handle.fileno())- os.replace(temp_name, parts[-1], src_dir_fd=parent_fd, dst_dir_fd=parent_fd)- temp_name = None- finally:- try:- if temp_name is not None and parent_fd is not None:- try:- os.unlink(temp_name, dir_fd=parent_fd)- except FileNotFoundError:- # The temporary file is already gone, so cleanup is complete.- pass- finally:- try:- if parent_fd is not None:- os.close(parent_fd)- finally:- if root_fd is not None:- os.close(root_fd)++ with managed_fd(parent_fd):++ def check_parent(directory: Path) -> None:+ # Admission can follow an archive while the staged parent stays open.+ root = os.fstat(root_fd)+ with (+ managed_fd(+ _open_verified_scan_directory(directory, (root.st_dev, root.st_ino))+ ) as current,+ managed_fd(+ _open_scan_local_directory(current, parts[:-1], create=False)+ ) as parent,+ ):+ if not os.path.samestat(os.fstat(parent_fd), os.fstat(parent)):+ raise ContractError(f"{relative_path}: parent changed before publication")++ with stage_write(+ parent_fd,+ relative_path,+ payload,+ owner_read_write=owner_read_write,+ replace_existing=replace_existing,+ compare_existing=expected_root_identity is not None,+ check_parent=check_parent,+ contract_error=ContractError,+ ) as publish:+ yield publishdef _remove_scan_local_file_if_exists(scan_dir: Path, relative_path: str) -> None:diff --git a/plugins/codex-security/scripts/posix_scan_local_files.py b/plugins/codex-security/scripts/posix_scan_local_files.pynew file mode 100644index 000000000..021eaa2a4--- /dev/null+++ b/plugins/codex-security/scripts/posix_scan_local_files.py@@ -0,0 +1,187 @@+"""Stage and publish POSIX scan-local files through verified directory descriptors."""++from __future__ import annotations++import argparse+import errno+import os+import secrets+import stat+import sys+from collections.abc import Callable, Iterator+from contextlib import contextmanager+from pathlib import Path, PurePosixPath+++def descriptor_relative_writes_available() -> bool:+ # os.replace accepts src_dir_fd/dst_dir_fd wherever descriptor-relative+ # os.rename is supported, but Python lists only os.rename in supports_dir_fd.+ required_operations = (os.mkdir, os.open, os.rename, os.stat, os.unlink)+ return hasattr(os, "O_NOFOLLOW") and all(+ operation in os.supports_dir_fd for operation in required_operations+ )+++@contextmanager+def managed_fd(descriptor: int) -> Iterator[int]:+ try:+ yield descriptor+ finally:+ os.close(descriptor)+++@contextmanager+def stage_file(+ parent_fd: int, filename: str, payload: bytes, owner_read_write: bool+) -> Iterator[int]:+ descriptor = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600, dir_fd=parent_fd)+ try:+ with os.fdopen(descriptor, "wb", closefd=False) as handle:+ if owner_read_write:+ os.fchmod(descriptor, 0o600)+ handle.write(payload)+ handle.flush()+ os.fsync(descriptor)+ yield descriptor+ finally:+ try:+ entry = os.stat(filename, dir_fd=parent_fd, follow_symlinks=False)+ if os.path.samestat(os.fstat(descriptor), entry):+ os.unlink(filename, dir_fd=parent_fd)+ except FileNotFoundError:+ # Successful publication has already removed the temporary name.+ pass+ finally:+ os.close(descriptor)+++def _rename_exclusive(parent_fd: int, source: str, destination: str) -> None:+ import ctypes++ libc = ctypes.CDLL(None, use_errno=True)+ if sys.platform == "linux":+ rename = getattr(libc, "renameat2", None)+ flag = 1 # RENAME_NOREPLACE+ elif sys.platform == "darwin":+ rename = getattr(libc, "renameatx_np", None)+ flag = 4 # RENAME_EXCL+ else:+ rename = None+ code = errno.ENOSYS+ if rename is not None:+ rename.argtypes = [+ ctypes.c_int,+ ctypes.c_char_p,+ ctypes.c_int,+ ctypes.c_char_p,+ ctypes.c_uint,+ ]+ rename.restype = ctypes.c_int+ if rename(parent_fd, os.fsencode(source), parent_fd, os.fsencode(destination), flag) == 0:+ return+ code = ctypes.get_errno()+ detail = os.strerror(code)+ if code in {errno.ENOSYS, errno.ENOTSUP, errno.EOPNOTSUPP, errno.EINVAL}:+ detail = f"cannot publish a new file atomically without hard links or exclusive rename ({detail})"+ raise OSError(code, detail, destination)+++def publish_new_file(parent_fd: int, source: str, destination: str) -> None:+ try:+ os.link(+ source, destination, src_dir_fd=parent_fd, dst_dir_fd=parent_fd, follow_symlinks=False+ )+ except OSError as exc:+ if exc.errno not in {+ errno.EPERM,+ errno.ENOTSUP,+ errno.EOPNOTSUPP,+ errno.ENOSYS,+ errno.EXDEV,+ errno.EMLINK,+ }:+ raise+ _rename_exclusive(parent_fd, source, destination)+ else:+ os.unlink(source, dir_fd=parent_fd)+++@contextmanager+def stage_write(+ parent_fd: int,+ relative_path: str,+ payload: bytes,+ *,+ owner_read_write: bool,+ replace_existing: bool,+ compare_existing: bool,+ check_parent: Callable[[Path], None],+ contract_error: type[ValueError],+) -> Iterator[Callable[..., tuple[os.stat_result, int]]]:+ name = PurePosixPath(relative_path).name+ # Stage through the held parent so a replacement path cannot redirect the write.+ try:+ metadata = os.stat(name, dir_fd=parent_fd, follow_symlinks=False)+ except FileNotFoundError:+ pass+ else:+ if not replace_existing:+ raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST), relative_path)+ if not stat.S_ISREG(metadata.st_mode):+ raise contract_error(f"{relative_path}: expected a regular non-symlink file")+ try:+ existing_fd = os.open(+ name,+ os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0),+ dir_fd=parent_fd,+ )+ except OSError as exc:+ if exc.errno not in {errno.ENOENT, errno.EACCES, errno.EPERM}:+ raise+ else:+ with managed_fd(existing_fd):+ opened = os.fstat(existing_fd)+ if not stat.S_ISREG(opened.st_mode):+ raise contract_error(f"{relative_path}: expected a regular non-symlink file")+ if not os.path.samestat(opened, metadata):+ raise contract_error(f"{relative_path}: changed while it was being opened")+ if compare_existing and opened.st_size == len(payload):+ from windows_scan_local_files import stream_matches_payload++ try:+ with os.fdopen(existing_fd, "rb", closefd=False) as handle:+ matches = stream_matches_payload(handle, payload)+ except OSError:+ # A failed content comparison still allows an atomic replacement.+ matches = False+ if matches:+ yield lambda _directory=None: (opened, opened.st_ctime_ns)+ return+ candidate = f".{name}.{secrets.token_hex(8)}.tmp"++ with stage_file(parent_fd, candidate, payload, owner_read_write) as staged_fd:+ staged = os.fstat(staged_fd)++ def publish(directory: Path | None = None) -> tuple[os.stat_result, int]:+ if directory is not None:+ check_parent(directory)+ if replace_existing:+ os.replace(candidate, name, src_dir_fd=parent_fd, dst_dir_fd=parent_fd)+ return staged, staged.st_ctime_ns+ named = os.stat(candidate, dir_fd=parent_fd, follow_symlinks=False)+ if not os.path.samestat(staged, named) or staged.st_ctime_ns != named.st_ctime_ns:+ raise contract_error(f"{relative_path}: staged file changed before publication")+ publish_new_file(parent_fd, candidate, name)+ published = os.fstat(staged_fd)+ if (staged.st_size, staged.st_mtime_ns) != (+ published.st_size,+ published.st_mtime_ns,+ ):+ raise contract_error(f"{relative_path}: staged contents changed during publication")+ return published, published.st_ctime_ns++ yield publish+++if __name__ == "__main__":+ argparse.ArgumentParser(description=__doc__).parse_args()diff --git a/plugins/codex-security/scripts/windows_scan_local_files.py b/plugins/codex-security/scripts/windows_scan_local_files.pyindex 273391b55..800c45442 100644--- a/plugins/codex-security/scripts/windows_scan_local_files.py+++ b/plugins/codex-security/scripts/windows_scan_local_files.py@@ -29,7 +29,7 @@ import importlibimport ntpathimport osimport secrets-from collections.abc import Iterator+from collections.abc import Callable, Iteratorfrom pathlib import Path, PurePosixPathfrom typing import BinaryIO@@ -100,6 +100,16 @@ _RESERVED_DEVICE_NAMES = {}+class _FileBasicInfo(ctypes.Structure):+ _fields_ = [+ ("CreationTime", ctypes.c_int64),+ ("LastAccessTime", ctypes.c_int64),+ ("LastWriteTime", ctypes.c_int64),+ ("ChangeTime", ctypes.c_int64),+ ("FileAttributes", wintypes.DWORD),+ ]++class _FileAttributeTagInfo(ctypes.Structure):_fields_ = [("FileAttributes", wintypes.DWORD),@@ -182,6 +192,10 @@ if os.name == "nt":]_CreateFileW.restype = wintypes.HANDLE+ _ReOpenFile = _kernel32.ReOpenFile+ _ReOpenFile.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD]+ _ReOpenFile.restype = wintypes.HANDLE+_CreateDirectoryW = _kernel32.CreateDirectoryW_CreateDirectoryW.argtypes = [wintypes.LPCWSTR, wintypes.LPVOID]_CreateDirectoryW.restype = wintypes.BOOL@@ -443,13 +457,17 @@ def _canonical_scan_directory(scan_dir: Path) -> tuple[Path, tuple[int, int]]:def _open_directory(- path: Path, *, missing_ok: bool = False, missing_is_path_error: bool = True+ path: Path,+ *,+ missing_ok: bool = False,+ missing_is_path_error: bool = True,+ movable: bool = False,) -> _OwnedHandle | None:try:handle = _create_file(path,access=_FILE_READ_ATTRIBUTES,- share=_FILE_SHARE_READ | _FILE_SHARE_WRITE,+ share=_FILE_SHARE_READ | _FILE_SHARE_WRITE | (_FILE_SHARE_DELETE if movable else 0),disposition=_OPEN_EXISTING,flags=_FILE_FLAG_BACKUP_SEMANTICS | _FILE_FLAG_OPEN_REPARSE_POINT,missing_ok=missing_ok,@@ -606,6 +624,17 @@ def open_read_fd_with_path(scan_dir: Path, relative_path: str, context: str) ->) from exc+def file_change_time(descriptor: int) -> int:+ """Return the filesystem change time. Python's Windows st_ctime is creation time."""+ assert _msvcrt is not None+ info = _FileBasicInfo()+ if not _GetFileInformationByHandleEx(+ _msvcrt.get_osfhandle(descriptor), 0, ctypes.byref(info), ctypes.sizeof(info)+ ):+ _raise_last_error("GetFileInformationByHandleEx(FileBasicInfo)")+ return info.ChangeTime++def stream_matches_payload(stream: BinaryIO, payload: bytes) -> bool:offset = 0while offset < len(payload):@@ -632,7 +661,7 @@ def _write_all(handle: int, payload: bytes) -> None:_raise_last_error("FlushFileBuffers")-def _rename_handle(handle: int, destination_path: Path) -> None:+def _rename_handle(handle: int, destination_path: Path, replace_existing: bool) -> None:encoded_name = _extended_path(destination_path).encode("utf-16-le")file_name_offset = _FileRenameInfo.FileName.offsetbuffer_size = max(@@ -641,7 +670,7 @@ def _rename_handle(handle: int, destination_path: Path) -> None:)buffer = ctypes.create_string_buffer(buffer_size)info = ctypes.cast(buffer, ctypes.POINTER(_FileRenameInfo)).contents- info.FlagsOrReplaceIfExists = 1+ info.FlagsOrReplaceIfExists = int(replace_existing)# The fully qualified destination uses a null RootDirectory. Verified# directory handles remain open to prevent ancestor replacement.info.RootDirectory = None@@ -653,6 +682,8 @@ def _rename_handle(handle: int, destination_path: Path) -> None:buffer,buffer_size,):+ if not replace_existing and ctypes.get_last_error() in _COLLISION_ERRORS:+ raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST), str(destination_path))_raise_last_error("SetFileInformationByHandle(FileRenameInfo)")@@ -667,7 +698,7 @@ def _mark_handle_for_deletion(handle: int) -> None:_raise_last_error("SetFileInformationByHandle(FileDispositionInfo)")-def _validate_existing_output(path: Path) -> None:+def _validate_existing_output(path: Path) -> bool:handle = _create_file(path,access=_FILE_READ_ATTRIBUTES,@@ -677,10 +708,11 @@ def _validate_existing_output(path: Path) -> None:missing_ok=True,)if handle is None:- return+ return Falsewith handle:assert handle.value is not None_verify_regular_file(handle.value, path)+ return Truedef _existing_output_matches(path: Path, payload: bytes) -> bool:@@ -723,64 +755,189 @@ def _existing_output_matches(path: Path, payload: bytes) -> bool:return False+def _written_temp(destination: Path, payload: bytes) -> Iterator[tuple[Path, _OwnedHandle]]:+ for _ in range(16):+ path = destination.with_name(f".{destination.name}.{secrets.token_hex(8)}.tmp")+ try:+ handle = _create_file(+ path,+ access=_GENERIC_WRITE | _DELETE | _FILE_READ_ATTRIBUTES,+ share=0,+ disposition=_CREATE_NEW,+ flags=_FILE_ATTRIBUTE_NORMAL,+ )+ except WindowsScanLocalFileError as exc:+ if exc.errno in _COLLISION_ERRORS:+ continue+ raise+ assert handle is not None and handle.value is not None+ with handle:+ try:+ _verify_regular_file(handle.value, path)+ _write_all(handle.value, payload)+ yield path, handle+ except BaseException:+ if handle.value is not None:+ with contextlib.suppress(OSError):+ _mark_handle_for_deletion(handle.value)+ raise+ return+ raise WindowsScanLocalFileError(errno.EEXIST, "could not allocate a unique temp file")+++def _stage_metadata(+ handle: _OwnedHandle, *, close_writer: bool = False+) -> tuple[os.stat_result, int]:+ # Windows can update LastWriteTime on last writer close. Observe the same+ # file across that close, independently of whatever is now at the name.+ assert handle.value is not None and _msvcrt is not None+ raw = _ReOpenFile(+ handle.value,+ _FILE_READ_ATTRIBUTES,+ _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,+ 0,+ )+ if raw is None or raw == _INVALID_HANDLE_VALUE:+ _raise_last_error("ReOpenFile")+ with _OwnedHandle(raw) as observer:+ descriptor = _msvcrt.open_osfhandle(raw, os.O_RDONLY | os.O_BINARY)+ observer.detach()+ try:+ if close_writer:+ handle.close()+ try:+ return os.fstat(descriptor), file_change_time(descriptor)+ except BaseException:+ if close_writer:+ # The observer still identifies the staged file after the writer closed.+ cleanup = _ReOpenFile(+ raw, _DELETE, _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE, 0+ )+ if cleanup and cleanup != _INVALID_HANDLE_VALUE:+ with _OwnedHandle(cleanup), contextlib.suppress(OSError):+ _mark_handle_for_deletion(cleanup)+ raise+ finally:+ os.close(descriptor)+++def stage_write(+ scan_dir: Path,+ relative_path: str,+ payload: bytes,+ *,+ expected_root_identity: tuple[int, int] | None = None,+ replace_existing: bool = False,+) -> Iterator[Callable[..., tuple[os.stat_result, int]]]:+ """Flush a patch and keep only a movable handle to its root across admission."""+ if expected_root_identity is None:+ root = scan_dir.lstat()+ expected_root_identity = (root.st_dev, root.st_ino)+ with _locked_parent(+ scan_dir, relative_path, create=True, expected_root_identity=expected_root_identity+ ) as (parent, leaf):+ destination = parent / leaf+ if _validate_existing_output(destination) and not replace_existing:+ raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST), str(destination))+ root_handle = _open_directory(scan_dir, movable=True)+ assert root_handle is not None+ try:+ with _written_temp(destination, payload) as (temp, writer):+ staged, change_time = _stage_metadata(writer, close_writer=True)+ except BaseException:+ root_handle.close()+ raise++ location = scan_dir+ published = False++ @contextlib.contextmanager+ def open_stage() -> Iterator[tuple[_OwnedHandle, Path]]:+ with _locked_parent(+ location, relative_path, create=False, expected_root_identity=expected_root_identity+ ) as (parent, leaf):+ path = parent / temp.name+ handle = _create_file(+ path,+ access=_DELETE | _FILE_READ_ATTRIBUTES,+ share=0,+ disposition=_OPEN_EXISTING,+ flags=_FILE_FLAG_OPEN_REPARSE_POINT | _FILE_FLAG_BACKUP_SEMANTICS,+ )+ assert handle is not None and handle.value is not None+ with handle:+ _verify_regular_file(handle.value, path)+ current, changed = _stage_metadata(handle)+ if not (+ os.path.samestat(staged, current)+ and staged.st_size == current.st_size+ and staged.st_mtime_ns == current.st_mtime_ns+ and change_time == changed+ ):+ raise _invalid_path(path, "staged patch changed before publication")+ yield handle, parent / leaf++ def publish(directory: Path | None = None) -> tuple[os.stat_result, int]:+ nonlocal location, published+ if directory is not None:+ location = directory+ with open_stage() as (handle, destination):+ assert handle.value is not None+ _rename_handle(handle.value, destination, replace_existing)+ published = True+ _verify_regular_file(handle.value, destination)+ return _stage_metadata(handle)++ with root_handle:+ try:+ yield publish+ finally:+ if not published:+ # A different workbench database may own the archive move.+ assert root_handle.value is not None+ with contextlib.suppress(OSError):+ location = Path(_final_path(root_handle.value))+ while True:+ try:+ with open_stage() as (handle, _):+ assert handle.value is not None+ _mark_handle_for_deletion(handle.value)+ break+ except OSError:+ # A move can land between locating the root and fixing its parents.+ moved_to = Path(_final_path(root_handle.value))+ if moved_to == location:+ break+ location = moved_to++def atomic_write(scan_dir: Path,relative_path: str,payload: bytes,*,expected_root_identity: tuple[int, int] | None = None,+ replace_existing: bool = True,) -> None:- """Atomically replace a scan-local regular file with ``payload``."""-+ """Atomically publish ``payload`` to a scan-local regular file."""with _locked_parent(- scan_dir,- relative_path,- create=True,- expected_root_identity=expected_root_identity,- ) as (parent_path, leaf_name):- destination_path = parent_path / leaf_name- _validate_existing_output(destination_path)- if expected_root_identity is not None and _existing_output_matches(- destination_path, payload+ scan_dir, relative_path, create=True, expected_root_identity=expected_root_identity+ ) as (parent, leaf):+ destination = parent / leaf+ if _validate_existing_output(destination) and not replace_existing:+ raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST), str(destination))+ if (+ replace_existing+ and expected_root_identity is not None+ and _existing_output_matches(destination, payload)):return-- temp_handle: _OwnedHandle | None = None- temp_path: Path | None = None- for _ in range(16):- temp_path = parent_path / f".{leaf_name}.{secrets.token_hex(8)}.tmp"- try:- temp_handle = _create_file(- temp_path,- access=_GENERIC_WRITE | _DELETE | _FILE_READ_ATTRIBUTES,- share=0,- disposition=_CREATE_NEW,- flags=_FILE_ATTRIBUTE_NORMAL,- )- except WindowsScanLocalFileError as exc:- if exc.errno in _COLLISION_ERRORS:- continue- raise- break- if temp_handle is None or temp_path is None:- raise WindowsScanLocalFileError(errno.EEXIST, "could not allocate a unique temp file")-- with temp_handle:- assert temp_handle.value is not None- try:- _verify_regular_file(temp_handle.value, temp_path)- _write_all(temp_handle.value, payload)- _rename_handle(temp_handle.value, destination_path)- _verify_regular_file(temp_handle.value, destination_path)- except BaseException:- # Deleting by handle removes the exact temp/output file and cannot- # be redirected through a swapped path or reparse point.- try:- _mark_handle_for_deletion(temp_handle.value)- except OSError:- # Cleanup must not replace the original write or rename failure.- pass- raise+ with _written_temp(destination, payload) as (_, handle):+ assert handle.value is not None+ _rename_handle(handle.value, destination, replace_existing)+ _verify_regular_file(handle.value, destination)def unlink_if_exists(scan_dir: Path, relative_path: str) -> None:diff --git a/plugins/codex-security/scripts/workbench_cli.py b/plugins/codex-security/scripts/workbench_cli.pyindex be1a26391..08b7636a1 100644--- a/plugins/codex-security/scripts/workbench_cli.py+++ b/plugins/codex-security/scripts/workbench_cli.py@@ -384,6 +384,7 @@ def parse_args(description: str, *, inspection_only: bool = False) -> argparse.N"--state", choices=REMEDIATION_UPDATE_STATES, required=True)set_finding_remediation.add_argument("--summary")+ set_finding_remediation.add_argument("--patch-stdin", action="store_true")set_finding_remediation.add_argument("--patch-path")set_finding_remediation.add_argument("--patch-digest")set_finding_remediation.add_argument("--base-revision")diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.pyindex b6bb5a96d..e8b36ad18 100644--- a/plugins/codex-security/scripts/workbench_db.py+++ b/plugins/codex-security/scripts/workbench_db.py@@ -17,7 +17,7 @@ import tempfileimport timeimport uuidfrom collections.abc import Callable-from contextlib import closing, contextmanager, nullcontext+from contextlib import ExitStack, closing, contextmanager, nullcontextfrom datetime import datetime, timedelta, timezonefrom functools import partialfrom pathlib import Path, PurePosixPath@@ -1488,18 +1488,43 @@ def register_cli_scan(connection.execute("BEGIN IMMEDIATE")try:- if scan_dir is None:- # Host archiving temporarily removes the output directory. Wait for- # the existing writer before inspecting its replacement.- scan_dir = require_cli_scan_directory(repository, args)- with archive_scan(- connection,- args,- scan_dir,- timestamp,- require_canonical_scan_directory,- before_archive=before_archive,- ) as archived_scan_dir:+ with ExitStack() as archive:+ deadline = None+ while True:+ if scan_dir is None:+ # Wait for any previous archive to finish before inspecting its replacement.+ scan_dir = require_cli_scan_directory(repository, args)+ try:+ archived_scan_dir = archive.enter_context(+ archive_scan(+ connection,+ args,+ scan_dir,+ timestamp,+ require_canonical_scan_directory,+ before_archive=before_archive,+ )+ )+ break+ except PermissionError as exc:+ # A Windows patch writer can temporarily pin the source of this rename.+ if (+ getattr(exc, "winerror", None) not in {5, 32}+ or exc.filename != str(scan_dir)+ or not exc.filename2+ ):+ raise+ if deadline is None:+ timeout = connection.execute("PRAGMA busy_timeout").fetchone()[0]+ deadline = time.monotonic() + timeout / 1000+ remaining = deadline - time.monotonic()+ if remaining <= 0:+ raise+ connection.rollback()+ before_archive = None # The host already acknowledged this registration.+ scan_dir = None # Revalidate the directory and its owner under the next lock.+ time.sleep(min(0.05, remaining))+ connection.execute("BEGIN IMMEDIATE")target_id = ensure_security_target(connection, str(repository))if parent_scan_id is not None:parent = require_scan(connection, parent_scan_id)@@ -2051,27 +2076,22 @@ def set_finding_remediation(raise SystemExit("This remediation host request is owned by a different action token.")remediation_state.require_transition(current["state"], args.state)remediation_state.require_pending_action(current, args.state)- patch_path = current["patch_path"]- if args.patch_path is not None:- requested_patch_path = require_scan_relative_file(scan, args.patch_path)- if patch_path is not None and requested_patch_path != patch_path:- raise SystemExit("A remediation attempt cannot replace its reviewed patch path.")- patch_path = requested_patch_path- patch_digest = current["patch_digest"]- if args.patch_digest is not None:- requested_patch_digest = require_sha256_digest(args.patch_digest, "patch-digest")- if patch_digest is not None and requested_patch_digest != patch_digest:- raise SystemExit("A remediation attempt cannot replace its reviewed patch digest.")- patch_digest = requested_patch_digest+ patch = sys.stdin.buffer.read() if args.patch_stdin else None+ patch_path, patch_digest = remediation_state.patch_metadata(+ current, args, patch, partial(require_scan_relative_file, scan)+ )base_revision = optional_text(args.base_revision, maximum=512)if args.state in {"generated", "applied", "verifying", "verified"}:if patch_path is None or patch_digest is None:raise SystemExit("Generated remediation states require a scan-local patch path and digest.")- require_matching_patch_digest(scan, patch_path, patch_digest)+ if patch is None:+ require_matching_patch_digest(scan, patch_path, patch_digest)if args.state == "generated":require_remediation_checkout_unchanged(scan, current, require_base_content=True)+ if patch is not None:+ remediation_state.require_valid_patch(Path(scan["target_path"]), patch)if args.state in {"applied", "verifying", "verified"}:if base_revision != current["base_revision"]:raise SystemExit(@@ -2092,59 +2112,80 @@ def set_finding_remediation(current,patch_path,)- connection.execute("BEGIN IMMEDIATE")- timestamp = now()- occurrence = require_occurrence(connection, args.occurrence_id)- require_finding_open(connection, occurrence["id"])- replace_failure_summary = current["state"] == "failed" and args.state != "failed"- updated = connection.execute(- """- UPDATE finding_remediation_attempts- SET state = ?, version = version + 1, patch_path = ?, patch_digest = ?,- applied_content_digest = ?,- pending_action = CASE- WHEN ? IN ('verifying', 'failed') THEN pending_action ELSE NULL- END,- pending_action_claimed_at = CASE- WHEN ? = 'verifying' THEN pending_action_claimed_at ELSE NULL- END,- pending_action_claim_token = CASE- WHEN ? = 'verifying' THEN pending_action_claim_token ELSE NULL- END,- pending_action_delivered_at = CASE- WHEN ? = 'verifying' THEN pending_action_delivered_at ELSE NULL- END,- summary = CASE WHEN ? THEN ? ELSE COALESCE(?, summary) END,- verification_summary = COALESCE(?, verification_summary),- updated_at = ?- WHERE request_id = ? AND occurrence_id = ? AND version = ?- AND pending_action_claim_token = ?- """,- (- args.state,- patch_path,- patch_digest,- applied_content_digest,- args.state,- args.state,- args.state,- args.state,- replace_failure_summary,- summary,- summary,- verification_summary,- timestamp,- request_id,- occurrence["id"],- args.expected_version,- action_token,- ),- )- if updated.rowcount != 1:- raise SystemExit(- "This remediation request changed. Refresh it before recording an update."++ def record_update() -> None:+ connection.execute("BEGIN IMMEDIATE")+ timestamp = now()+ occurrence = require_occurrence(connection, args.occurrence_id)+ require_finding_open(connection, occurrence["id"])+ replace_failure_summary = current["state"] == "failed" and args.state != "failed"+ updated = connection.execute(+ """+ UPDATE finding_remediation_attempts+ SET state = ?, version = version + 1, patch_path = ?, patch_digest = ?,+ applied_content_digest = ?,+ pending_action = CASE+ WHEN ? IN ('verifying', 'failed') THEN pending_action ELSE NULL+ END,+ pending_action_claimed_at = CASE+ WHEN ? = 'verifying' THEN pending_action_claimed_at ELSE NULL+ END,+ pending_action_claim_token = CASE+ WHEN ? = 'verifying' THEN pending_action_claim_token ELSE NULL+ END,+ pending_action_delivered_at = CASE+ WHEN ? = 'verifying' THEN pending_action_delivered_at ELSE NULL+ END,+ summary = CASE WHEN ? THEN ? ELSE COALESCE(?, summary) END,+ verification_summary = COALESCE(?, verification_summary),+ updated_at = ?+ WHERE request_id = ? AND occurrence_id = ? AND version = ?+ AND pending_action_claim_token = ?+ """,+ (+ args.state,+ patch_path,+ patch_digest,+ applied_content_digest,+ args.state,+ args.state,+ args.state,+ args.state,+ replace_failure_summary,+ summary,+ summary,+ verification_summary,+ timestamp,+ request_id,+ occurrence["id"],+ args.expected_version,+ action_token,+ ),)- return scan_context(connection, occurrence["scan_id"])+ if updated.rowcount != 1:+ raise SystemExit(+ "This remediation request changed. Refresh it before recording an update."+ )++ if patch is None:+ record_update()+ else:+ try:+ remediation_state.save_patch(+ _WORKBENCH_DB_CONTEXT,+ connection,+ scan["id"],+ current,+ patch_path,+ patch_digest,+ patch,+ record_update,+ )+ except (ContractError, OSError, SystemExit) as exc:+ raise SystemExit(f"Could not save the remediation patch: {exc}") from exc+ return scan_context(+ connection, occurrence["scan_id"], occurrence["id"] if patch is not None else None+ )def require_reviewed_patch_applied(@@ -2258,20 +2299,6 @@ def require_remediation_checkout_unchanged()-def require_sha256_digest(value: str, label: str) -> str:- normalized = optional_text(value, maximum=71)- if (- normalized is None- or not normalized.startswith("sha256:")- or len(normalized) != 71- or any(- character not in "0123456789abcdef" for character in normalized.removeprefix("sha256:")- )- ):- raise SystemExit(f"{label} must use sha256:<64 lowercase hex characters>.")- return normalized--def require_scan_relative_file(scan: sqlite3.Row, value: str) -> str:if len(value) > 4096:raise SystemExit("Text value must be no longer than 4096 characters.")@@ -2285,11 +2312,7 @@ def require_scan_relative_file(scan: sqlite3.Row, value: str) -> str:def require_matching_patch_digest(scan: sqlite3.Row, patch_path: str, patch_digest: str) -> None:- digest = hashlib.sha256()- with open_scan_local_file(Path(scan["scan_dir"]), patch_path) as patch:- while chunk := patch.read(1024 * 1024):- digest.update(chunk)- if f"sha256:{digest.hexdigest()}" != patch_digest:+ if scan_local_file_digest(Path(scan["scan_dir"]), patch_path) != patch_digest:raise SystemExit("Patch digest does not match the scan-local patch file.")@@ -2398,7 +2421,11 @@ def scan_context() -> dict[str, Any]:scan = require_scan(connection, scan_id)result = scan_result(connection, scan, occurrence_id=occurrence_id)- workspace_result = result if occurrence_id is None else scan_result(connection, scan)+ workspace_result = result+ if len(result["findings"]) > FINDINGS_RESULT_LIMIT:+ workspace_result = dict(+ result, findings=result["findings"][:FINDINGS_RESULT_LIMIT], findingsTruncated=True+ )workspace = workspace_state(connection,scan["workspace_id"],@@ -2500,11 +2527,12 @@ def scan_result(occurrence_rows = scan_history.finding_occurrence_rows(connection, scan["id"], offset=0, limit=FINDINGS_RESULT_LIMIT)- if occurrence_id is not None and all(row["id"] != occurrence_id for row in occurrence_rows):+ if occurrence_id is not None:occurrence = require_occurrence(connection, occurrence_id)if occurrence["scan_id"] != scan["id"]:raise SystemExit("This finding does not belong to the selected scan.")- occurrence_rows.append(occurrence)+ if all(row["id"] != occurrence["id"] for row in occurrence_rows):+ occurrence_rows.append(occurrence)finding_count = connection.execute("SELECT COUNT(*) FROM finding_occurrences WHERE scan_id = ?", (scan["id"],)).fetchone()[0]diff --git a/plugins/codex-security/scripts/workbench_remediation.py b/plugins/codex-security/scripts/workbench_remediation.pyindex d26d8d9da..45b521e3f 100644--- a/plugins/codex-security/scripts/workbench_remediation.py+++ b/plugins/codex-security/scripts/workbench_remediation.py@@ -3,16 +3,36 @@from __future__ import annotationsimport argparse+import codecs+import hashlib+import io+import os+import reimport sqlite3import sys+from collections.abc import Callable+from contextlib import ExitStack, suppressfrom datetime import datetime, timedelta, timezonefrom pathlib import Path-from typing import Any+from typing import Any, BinaryIO# Some plugin hosts launch Python with safe-path isolation enabled.sys.path.insert(0, str(Path(__file__).resolve().parent))+from finalize_scan_contract import (+ ContractError,+ MissingScanLocalFileError,+ _open_scan_local_directory,+ _open_verified_scan_directory,+ _read_scan_local_json_bytes,+ _read_sealed_scan,+ scan_local_file_version,+ scan_root_identity,+ stage_scan_local_bytes,+)+from posix_scan_local_files import managed_fdfrom workbench_constants import CLAIM_LEASE_SECONDS, DELIVERED_ACTION_LEASE_SECONDS-from workbench_validation import require_occurrence, require_uuid+from workbench_target import git_command, git_output+from workbench_validation import optional_text, require_occurrence, require_uuidCOMMANDS = {"request-finding-remediation",@@ -58,6 +78,466 @@ def latest_attempt(connection: sqlite3.Connection, occurrence_id: str) -> sqlite).fetchone()+_HUNK_HEADER = re.compile(rb"@@ -[0-9]+(?:,([0-9]+))? \+[0-9]+(?:,([0-9]+))? @@")+_QUOTED_GIT_PATH = rb'"((?:[^"\\]|\\(?:[abfnrtv"\\]|[0-3][0-7]{2}))*)"'+_SUBMODULE_SUMMARY_END = re.compile(+ rb" (?:contains (?:modified|untracked) content|"+ rb"[0-9a-f]+\.{2,3}[0-9a-f]+(?: \([a-z ]+\):?|:))$"+)+_SUBMODULE_ERROR = (+ "A remediation patch cannot update Git submodules. Regenerate without gitlink changes."+)+++def _git_path(field: bytes) -> bytes:+ if quoted := re.match(_QUOTED_GIT_PATH + rb"(?:\t|$)", field):+ return codecs.escape_decode(quoted[1])[0]+ return field.partition(b"\t")[0]+++def _matches_git_paths(header: bytes, old_field: bytes, new_field: bytes) -> bool:+ old, new = _git_path(old_field), _git_path(new_field)+ # Git strips one component from each path. Unquoted paths can contain+ # spaces, so use the known preimage to locate the separator.+ if quoted := re.match(_QUOTED_GIT_PATH + rb"(?= )", header):+ split = quoted.end()+ elif quoted := re.search(rb" " + _QUOTED_GIT_PATH + rb"$", header):+ split = quoted.start()+ elif old == b"/dev/null" and header.endswith(b" " + new):+ split = len(header) - len(new) - 1+ elif header.count(b" ") == 1:+ split = header.index(b" ")+ else:+ # On a creation Git repeats the destination on the left of the header.+ expected = new if old == b"/dev/null" else old+ prefix, slash, suffix = expected.partition(b"/")+ split = header.find(b"/") + 1 + len(suffix) if slash else len(prefix)+ if split < 0 or header[split : split + 1] != b" ":+ return False+ for path, field in ((header[:split], old), (header[split + 1 :], new)):+ if field != b"/dev/null" and _git_path(path).split(b"/", 1)[-1] != field.split(b"/", 1)[-1]:+ return False+ return True+++def _patch_file_sections(patch: bytes, incomplete: str) -> tuple[bytes, int]:+ mail = re.match(rb"From (?:[0-9a-f]{40}|[0-9a-f]{64}) Mon Sep 17 00:00:00 2001\r?\n", patch)+ if mail:+ first = re.search(rb"(?m)^diff --git [^\n]+\n", patch)+ if first:+ start = first.start()+ # A commit message can quote a Git header. Look for format-patch's+ # diffstat before the first hunk, where a deleted "--" can also+ # look like the separator. Each run of indented lines is scanned once.+ hunk = re.compile(rb"(?m)^@@ -").search(patch, start)+ diffstat = re.compile(+ rb"(?m)^---\r?\n(?: [^\n]*\n)+\r?\n(?=diff --git |Submodule )"+ ).search(patch, 0, hunk.start() if hunk else len(patch))+ if diffstat:+ start = diffstat.end()+ else:+ # With --no-stat Git can retain a quoted header as the preimage of+ # the first change. Ignore it only for the same in-place file edit.+ file = re.compile(+ rb"(?m)^diff --git [^\n]+\n(?=index |--- |@@ |old mode|new mode|new file|deleted file"+ rb"|(?:dis)?similarity index|(?:rename|copy) |GIT binary patch|Binary files |Files |Only in )"+ ).search(patch, start)+ if (+ file+ and file[0] == first[0]+ and patch.endswith((b"\n\n", b"\n\r\n"), 0, file.start())+ and re.compile(+ rb"(?:index [0-9a-f]+\.\.[0-9a-f]+ 100(?:644|755)|"+ rb"old mode 100(?:644|755)\r?\nnew mode 100(?:644|755)\r?\n"+ rb"index [0-9a-f]+\.\.[0-9a-f]+)\r?\n(?:--- |GIT binary patch)"+ ).match(patch, file.end())+ ):+ start = file.start()+ elif not patch.endswith((b"\n\n", b"\n\r\n"), 0, start):+ # A leading submodule summary runs directly into the next file header.+ summary = re.compile(rb"(?m)^Submodule ").search(patch, 0, start)+ if summary:+ start = summary.start()+ patch = patch[start:]++ sections = old_lines = new_lines = 0+ git_paths = b""+ pending_paths = None+ submodule_summary = False+ expect_hunk = False+ stream = io.BytesIO(patch)+ for raw_line in stream:+ line = raw_line.removesuffix(b"\n").removesuffix(b"\r")+ if old_lines or new_lines:+ if line.startswith(b"\\ "): # No newline at end of file+ continue+ prefix = line[:1]+ if prefix in (b"", b" ", b"-"):+ old_lines -= 1+ if prefix in (b"", b" ", b"+"):+ new_lines -= 1+ if prefix not in (b"", b" ", b"+", b"-") or old_lines < 0 or new_lines < 0:+ raise SystemExit(incomplete)+ expect_hunk = True+ continue+ if expect_hunk and line.startswith(b"\\ "): # No newline at end of file+ continue+ hunk = _HUNK_HEADER.match(line)+ if expect_hunk and not hunk and re.match(rb"@@ -(?:[0-9]|$)", line):+ raise SystemExit(incomplete)+ expect_hunk = bool(sections) and line.startswith(b"+++ ")+ if mail and sections and line == b"-- ":+ patch = patch[: stream.tell() - len(raw_line)]+ break+ # Git writes the raw submodule name (possibly including newlines) before the+ # status or object range. Neither log nor inline-diff headers can be replayed.+ if line.startswith(b"Submodule "):+ submodule_summary = True+ if submodule_summary and _SUBMODULE_SUMMARY_END.search(line):+ raise SystemExit(_SUBMODULE_ERROR)+ if pending_paths:+ if line.startswith(b"+++ ") and not _matches_git_paths(*pending_paths, line[4:]):+ sections += 1+ pending_paths = None+ if line.startswith(b"diff --git "):+ sections += 1+ submodule_summary = False+ git_paths = line.removeprefix(b"diff --git ")+ continue+ if line.startswith(b"--- "):+ submodule_summary = False+ # A Git header can introduce the same unified path pair. An+ # unrelated pair starts a separate file even if Git ignores one.+ if git_paths:+ pending_paths = (git_paths, line[4:])+ else:+ sections += 1+ git_paths = b""+ continue+ if git_paths:+ if line.startswith(+ (b"index ", b"old mode ", b"new mode ", b"new file mode ", b"deleted file mode ")+ ):+ if re.search(rb" 0*16[0-7]{4}(?:\s|$)", line):+ raise SystemExit(_SUBMODULE_ERROR)+ if not line.startswith(b"index ") and not re.fullmatch(rb".* [0-7]{6}[ \t]*", line):+ raise SystemExit(incomplete)+ continue+ if line.startswith(+ (+ b"similarity index ",+ b"dissimilarity index ",+ b"rename from ",+ b"rename to ",+ b"copy from ",+ b"copy to ",+ )+ ):+ continue+ git_paths = b""+ if hunk:+ old_lines = int(hunk[1] or b"1")+ new_lines = int(hunk[2] or b"1")+ elif (+ line.startswith((b"Binary files ", b"Files "))+ and line.endswith(b" differ")+ and b" and " in line+ ):+ raise SystemExit(incomplete)+ elif line.startswith(b"Only in ") and b": " in line:+ raise SystemExit(incomplete)+ if old_lines or new_lines:+ raise SystemExit(incomplete)+ return patch, sections+++def require_valid_patch(target: Path, patch: bytes) -> None:+ """Reject incomplete patches before recording the submitted bytes."""+ incomplete = (+ "The remediation patch omits file content. Generate a complete binary diff "+ "with Git's --binary option."+ )+ # Git's numstat skips target-relative paths when run below the repository root.+ root = git_output(target, "rev-parse", "--show-toplevel", offline=True)+ directory = ("-C", root) if root else ()++ def git(data: bytes, *arguments: str):+ result = git_command(+ target,+ *directory,+ *arguments,+ text=False,+ input_data=data,+ pathspec_environment={"LC_ALL": "C"},+ offline=True,+ )+ if result.returncode == 127:+ raise SystemExit(+ "Validating a new remediation patch requires a trusted Git executable."+ )+ # Git may report a corrupt trailing binary block but return zero for an earlier file.+ if result.returncode or re.search(rb"(?m)^error: ", result.stderr):+ reason = result.stderr.decode(errors="replace").strip()+ raise SystemExit(f"Could not parse the remediation patch: {reason}")+ return result.stdout++ # Parse every header and hunk, then skip checkout reads and repository clean filters.+ apply = ("apply", "--no-index", "--whitespace=nowarn")+ body, sections = _patch_file_sections(patch, incomplete)+ git(patch, *apply, "--check", "--exclude=*", "-")+ # Git silently ignores some incomplete sections when another file is valid.+ # NUL termination gives one record per file, even for unusual pathnames.+ if git(patch, *apply, "--numstat", "-z", "-").count(b"\0") != sections:+ raise SystemExit(incomplete)++ binary_oid_width = None+ for header in re.finditer(+ rb"(?m)^diff --git [^\n]+\n((?:(?!diff )[a-z][^\n]*(?:\n|\Z))*)",+ body,+ ):+ metadata = header.group(1)+ index = re.search(rb"(?m)^index ([^\s.]+)\.\.([^\s.]+)(?: [0-7]+[ \t]*)?\r?$", metadata)+ binary = body.startswith((b"GIT binary patch\n", b"GIT binary patch\r\n"), header.end())+ if not binary:+ if index is None or re.compile(rb"(?:--- [^\n]*\n\+\+\+ [^\n]*\n)?@@ ").match(+ body, header.end()+ ):+ continue+ old, new = index.groups()+ if metadata.startswith(b"deleted file mode ") and not new.strip(b"0"):+ content_id = old+ elif metadata.startswith(b"new file mode ") and not old.strip(b"0"):+ content_id = new+ else:+ # Mode changes and renames can omit hunks only if+ # their advertised blob contents are unchanged.+ old, new = old.lower(), new.lower()+ if not old.startswith(new) and not new.startswith(old):+ raise SystemExit(incomplete)+ continue+ # Only an empty file can be added or deleted without a hunk.+ empty_blobs = (+ hashlib.sha1(b"blob 0\0", usedforsecurity=False).hexdigest().encode(),+ hashlib.sha256(b"blob 0\0").hexdigest().encode(),+ )+ if not any(digest.startswith(content_id.lower()) for digest in empty_blobs):+ raise SystemExit(incomplete)+ continue+ if index is None:+ raise SystemExit("A binary patch requires an index line with full object IDs.")+ old, new = index.groups()+ if binary_oid_width is None:+ binary_oid_width = len(git(b"", "hash-object", "--no-filters", "--stdin").strip())+ if (+ len(old) != binary_oid_width+ or len(new) != binary_oid_width+ or not re.fullmatch(rb"[0-9a-fA-F]+", old + new)+ ):+ raise SystemExit(+ "A binary patch requires full object IDs in the selected target's Git object format."+ )+++def patch_metadata(+ current: sqlite3.Row,+ args: argparse.Namespace,+ patch: bytes | None,+ resolve_path: Callable[[str], str],+) -> tuple[str | None, str | None]:+ path = args.patch_path+ digest = args.patch_digest+ if patch is not None:+ if args.state != "generated" or path is not None or digest is not None:+ raise SystemExit("Supply patch only for generation, without a patch path or digest.")+ path = current["patch_path"] or (+ f"artifacts/remediation/{current['occurrence_id']}/{current['request_id']}/"+ f"{current['pending_action_claim_token']}.patch"+ )+ digest = f"sha256:{hashlib.sha256(patch).hexdigest()}"+ else:+ if path is not None:+ path = resolve_path(path)+ if digest is not None:+ digest = optional_text(digest, maximum=71)+ if not re.fullmatch(r"sha256:[0-9a-f]{64}", digest or ""):+ raise SystemExit("patch-digest must use sha256:<64 lowercase hex characters>.")+ if path is not None and current["patch_path"] not in (None, path):+ raise SystemExit("A remediation attempt cannot replace its reviewed patch path.")+ if digest is not None and current["patch_digest"] not in (None, digest):+ raise SystemExit("A remediation attempt cannot replace its reviewed patch digest.")+ return path or current["patch_path"], digest or current["patch_digest"]+++def same_file_version(+ original: tuple[os.stat_result, int], current: tuple[os.stat_result, int]+) -> bool:+ before, before_change = original+ after, after_change = current+ return (+ os.path.samestat(before, after)+ and before.st_size == after.st_size+ and before.st_mtime_ns == after.st_mtime_ns+ and before_change == after_change+ )+++def file_digest_and_version(source: BinaryIO, label: str) -> tuple[str, tuple[os.stat_result, int]]:+ before = scan_local_file_version(source.fileno())+ digest = hashlib.sha256()+ for chunk in iter(lambda: source.read(1024 * 1024), b""):+ digest.update(chunk)+ after = scan_local_file_version(source.fileno())+ if not same_file_version(before, after):+ raise SystemExit(f"The {label} changed while it was being verified.")+ return f"sha256:{digest.hexdigest()}", after+++def matching_patch_metadata(source: BinaryIO, expected_digest: str) -> tuple[os.stat_result, int]:+ digest, version = file_digest_and_version(source, "scan-local patch")+ if digest != expected_digest:+ raise SystemExit("Patch digest does not match the scan-local patch file.")+ return version+++def patch_manifest_version(+ db: Any, scan: sqlite3.Row, scan_dir: Path+) -> tuple[str, tuple[os.stat_result, int]]:+ manifest_path = db.ARTIFACTS["manifest"]+ expected = scan["seal_manifest_digest"]+ if expected is None:+ # Historical manifests can have different JSON formatting. Bind their original bytes.+ saved, raw = _read_scan_local_json_bytes(scan_dir, manifest_path, manifest_path)+ manifest, _, _, _ = _read_sealed_scan(scan_dir, None, "remediation")+ if saved != manifest:+ raise SystemExit("The sealed scan manifest changed during validation.")+ db.verify_manifest_binding(scan, manifest)+ expected = f"sha256:{hashlib.sha256(raw).hexdigest()}"+ with db.open_scan_local_file(scan_dir, manifest_path) as source:+ digest, version = file_digest_and_version(source, "sealed scan manifest")+ if digest != expected:+ raise SystemExit("The sealed scan manifest changed after completion.")+ return digest, version+++def save_patch(+ db: Any,+ connection: sqlite3.Connection,+ scan_id: str,+ current: sqlite3.Row,+ path: str,+ digest: str,+ patch: bytes,+ record_update: Callable[[], None],+) -> None:+ def admit() -> Path:+ record_update()+ latest = db.require_scan(connection, scan_id)+ directory = db.require_canonical_scan_directory(Path(latest["scan_dir"]))+ if scan_root_identity(directory)[1] != identity:+ raise SystemExit("The scan directory changed after seal validation.")+ with db.open_scan_local_file(directory, db.ARTIFACTS["manifest"]) as named:+ current_version = scan_local_file_version(named.fileno())+ if latest["seal_manifest_digest"] not in (None, manifest_digest) or not same_file_version(+ manifest_version, current_version+ ):+ raise SystemExit("The sealed scan manifest changed after completion.")+ return directory++ def prepare_saved_patch(directory: Path) -> tuple[os.stat_result, int]:+ with db.open_scan_local_file(directory, path) as named:+ version = matching_patch_metadata(named, digest)+ if os.name == "posix":+ # Persist the file and every directory entry before SQLite commits its path.+ os.fsync(named.fileno())+ with (+ managed_fd(_open_verified_scan_directory(directory, identity)) as root,+ managed_fd(+ _open_scan_local_directory(+ root, Path(path).parts[:-1], create=False, sync_parents=True+ )+ ) as parent,+ ):+ os.fsync(parent)+ return version++ def verify_name(directory: Path, expected: tuple[os.stat_result, int]) -> None:+ with db.open_scan_local_file(directory, path) as named:+ if not same_file_version(expected, scan_local_file_version(named.fileno())):+ raise SystemExit("The scan-local patch changed during generation.")+ if scan_root_identity(directory)[1] != identity:+ raise SystemExit("The scan directory changed while saving the patch.")++ existing = current["patch_path"] is not None+ retried_directory = None+ identity = None+ while True:+ scan = db.require_scan(connection, scan_id)+ root_changed_during_validation = False+ with ExitStack() as staging:+ try:+ scan_dir = db.require_canonical_scan_directory(Path(scan["scan_dir"]))+ if identity is None:+ scan_dir, root_identity = scan_root_identity(scan_dir)+ manifest_digest, manifest_version = patch_manifest_version(db, scan, scan_dir)+ root_changed_during_validation = (+ scan_root_identity(scan_dir)[1] != root_identity+ )+ if root_changed_during_validation:+ raise ContractError("The scan directory changed during seal validation.")+ identity = root_identity+ publish = None+ if not existing:+ with suppress(FileExistsError):+ publish = staging.enter_context(+ stage_scan_local_bytes(+ scan_dir,+ path,+ patch,+ expected_root_identity=identity,+ owner_read_write=True,+ )+ )+ if publish is None:+ metadata = prepare_saved_patch(scan_dir)+ except (ContractError, OSError, SystemExit) as exc:+ # Archiving moves the directory before committing its new location.+ with connection:+ connection.execute("BEGIN IMMEDIATE")+ directory = db.require_scan(connection, scan_id)["scan_dir"]+ if directory == scan["scan_dir"]:+ if directory == retried_directory:+ raise+ # Rollback can restore a missing file or the verified staging root.+ cause: BaseException | None = exc+ while cause and not isinstance(+ cause, (FileNotFoundError, MissingScanLocalFileError)+ ):+ cause = cause.__cause__+ if (+ cause is None+ and not root_changed_during_validation+ and not (+ isinstance(exc, ContractError)+ and identity is not None+ and scan_root_identity(Path(directory))[1] == identity+ )+ ):+ raise+ retried_directory = directory+ continue+ directory = admit()+ if publish is not None:+ with suppress(FileExistsError):+ publish(directory)+ # Keep the token's file, including a competing winner, if cancellation+ # or interruption wins before final admission. Verify it outside SQLite.+ connection.rollback()+ existing = True+ continue+ verify_name(directory, metadata)+ return++def remediation_claim_is_active(remediation: sqlite3.Row) -> bool:if remediation["pending_action_claim_token"] is None:return Falsediff --git a/plugins/codex-security/scripts/workbench_source_scopes.py b/plugins/codex-security/scripts/workbench_source_scopes.pyindex 3a233ba7d..d858b10ec 100644--- a/plugins/codex-security/scripts/workbench_source_scopes.py+++ b/plugins/codex-security/scripts/workbench_source_scopes.py@@ -17,7 +17,7 @@ from unicodedata import normalizesys.path.insert(0, str(Path(__file__).resolve().parent))from workbench_target import (clean_worktree_content_digest,- git_bytes,+ git_command,git_worktree_context,)from workbench_validation import reject_non_finite_json@@ -74,19 +74,8 @@ def safe_source_path(target: Path, value: str) -> Path | None:def offline_git_bytes(repository: Path, *arguments: str) -> bytes | None:- names = ("GIT_NO_LAZY_FETCH", "GIT_ALLOW_PROTOCOL", "GIT_NO_REPLACE_OBJECTS")- previous = {name: os.environ.get(name) for name in names}- os.environ["GIT_NO_LAZY_FETCH"] = "1"- os.environ["GIT_ALLOW_PROTOCOL"] = ""- os.environ["GIT_NO_REPLACE_OBJECTS"] = "1"- try:- return git_bytes(repository, *arguments)- finally:- for name, value in previous.items():- if value is None:- os.environ.pop(name, None)- else:- os.environ[name] = value+ result = git_command(repository, *arguments, text=False, offline=True)+ return result.stdout if result.returncode == 0 else None@cachediff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.pyindex 4785d9ac9..6179a1f6c 100644--- a/plugins/codex-security/scripts/workbench_target.py+++ b/plugins/codex-security/scripts/workbench_target.py@@ -63,8 +63,11 @@ def git_output(*args: str,git_dir: Path | None = None,work_tree: Path | None = None,+ offline: bool = False,) -> str | None:- completed = git_command(target, *args, text=False, git_dir=git_dir, work_tree=work_tree)+ completed = git_command(+ target, *args, text=False, git_dir=git_dir, work_tree=work_tree, offline=offline+ )output = os.fsdecode(completed.stdout)if sys.platform == "win32" and output.endswith("\r\n"):output = output[:-2]@@ -160,6 +163,7 @@ def git_command(work_tree: Path | None = None,stdout_file: BinaryIO | None = None,pathspec_environment: dict[str, str] | None = None,+ offline: bool = False,) -> subprocess.CompletedProcess[str] | subprocess.CompletedProcess[bytes]:if (git_dir is None) != (work_tree is None):raise ValueError("git_dir and work_tree must be provided together")@@ -169,6 +173,9 @@ def git_command(environment["GIT_LITERAL_PATHSPECS"] = "1"if pathspec_environment is not None:environment.update(pathspec_environment)+ if offline:+ # Deny transports even on Git versions that do not honor GIT_NO_LAZY_FETCH.+ environment.update(GIT_NO_LAZY_FETCH="1", GIT_ALLOW_PROTOCOL="", GIT_NO_REPLACE_OBJECTS="1")executable = trusted_git_executable(target)# Repository-local config is untrusted; fsmonitor may name an executable hook.command = [diff --git a/plugins/codex-security/skills/fix-finding/SKILL.md b/plugins/codex-security/skills/fix-finding/SKILL.mdindex 5363a4917..9ff694a95 100644--- a/plugins/codex-security/skills/fix-finding/SKILL.md+++ b/plugins/codex-security/skills/fix-finding/SKILL.md@@ -55,10 +55,27 @@ Confirm review concerns against source or, in standard mode, focused execution bWhen a Codex Security workbench request includes a scan ID, occurrence ID, remediation request ID, action token, and expected version, follow only the requested remediation stage. The stage boundary changes when code may be written, but it does not weaken the validation requirements above.-- **Generate**: Keep the selected target checkout unchanged. Use an isolated worktree or temporary copy when edits are needed to develop or test the fix. Apply the patch contract and verification requirements above, write one canonical unified diff containing the complete source and regression-test change, then record `generated` or `failed` using the supplied workbench identity.+Before creating or entering any work copy, select Python and Git from a controlled empty directory outside all checkouts. Verify that each trusted tool's absolute invocation path, resolved parent directory, and resolved executable are outside the selected target, its enclosing checkout, and all capture and rehearsal workspaces. Use the host-selected Python (`CODEX_SECURITY_PYTHON_COMMAND` in SDK runs). If absent, use a trusted absolute `PYTHON`, a host-managed runtime, or a verified system interpreter. Invoke its quoted absolute path with `-I` to import helpers from the installed plugin's scripts directory. From the same directory call `workbench_target.trusted_git_executable(target)`, which honors `CODEX_SECURITY_GIT` or discovers Git when that variable is absent. Pin the verified absolute invocation path it returns in the helper process's `CODEX_SECURITY_GIT` environment and pass it to child helpers. Stop if either tool cannot be verified. For workbench Generate and Apply Git commands, pass `offline=True` and the real target to `workbench_target.git_command`, use `-C` for the working directory, and set `-c core.hooksPath=<empty private directory>`.++Disable external Git filters for inline capture and any rehearsal in either verification mode. Identify drivers with a nonempty `clean`, `smudge`, or `process` command in the effective Git configuration of the real target and each private command's working directory. Write the overrides with `git config --file <file> <key> <value>` using separate arguments. Clear each driver's `clean`, `smudge`, and `process` commands. For staging and capture diffs, set `filter.<driver>.required` to true only if the driver was already required or had a `clean` or `process` command. For a rehearsal that writes files, use a separate override that also requires drivers with a `smudge` command. Pass `-c include.path=<absolute private config>` for the operation's overrides. Git will fail if it needs the driver, including after edits to `.gitattributes`. If capture requires external conversion, register an existing reviewed scan-local patch by path and digest when available, or record the failure.++In static mode the workbench setter's checkout snapshot and reverse check must also preserve source-only verification. Rule out executable filters in the applicable working-tree and index attributes, including any attribute changes in the reviewed patch, before requesting such an operation. Stop before the operation if that cannot be established.++- **Generate.** Before developing or editing the fix in either verification mode, confine filesystem writes by the worker and its child processes to the isolated worktree or temporary workspace, even through filesystem links. Develop the source and regression-test changes and run any permitted tests inside that boundary. Preserve links as link entries. To edit a referent, use a verified path inside the copy. If writes cannot be confined, record `failed` before editing or running repository commands. Call `set_codex_security_finding_remediation` with the supplied workbench identity and `state: "generated"`.+ - For a Git working tree, isolate the writable private repository from system and user Git configuration starting before `git init`. In the private helper process, remove inherited `GIT_CONFIG` and `GIT_CONFIG_*` variables, use empty private `HOME` and `XDG_CONFIG_HOME` directories, set `GIT_CONFIG_NOSYSTEM=1` and point `GIT_CONFIG_GLOBAL` to an empty private file. Initialize a fresh repository with `git init --template=` and the target's object format (`git rev-parse --show-object-format` where supported). Copy the request's working-tree baseline, including local edits and intended untracked files, preserving file modes, symlinks, the nested target location and applicable in-repository `.gitattributes` files. Exclude source Git metadata, config and hooks. Set only the needed non-executable values such as `core.autocrlf`, `core.eol`, `core.fileMode` and `core.symlinks`. Compare the intended paths' effective attributes using `git check-attr -z --all --stdin` in both repositories. Reproduce needed built-in attributes and selected filter names as data so the private filter overrides apply. Never copy executable filter configuration from the target.+ - Seed the private index with the target's stage-zero blob entries for the intended existing files from `git ls-files --stage -z` at the repository root. Run this inventory with `LC_ALL=C` and stop on an `error:` or `fatal:` diagnostic even if Git exits successfully. Read each blob with `workbench_source_scopes.offline_git_bytes(target, "cat-file", "blob", oid)`. Stop if any blob is unavailable locally. Write the returned bytes with private `git hash-object -w --stdin`, verify its OID, and pass the selected entries to private `git update-index -z --index-info`. Git uses prior blob contents for automatic line-ending conversion.+ - Use `git ls-files --cached --others --exclude-standard -z` in the real target to check that the workbench captures every intended existing preimage, including files to delete. Stop if a required preimage is ignored and untracked. Stage the intended existing paths in the copy with `git add -- <paths>` before editing and record `git write-tree` as `BASE`. Before final staging, query the same inventory from the edited copy's selected directory using the original target environment, Git directory and index, with the copy root as `work_tree`. This uses the reviewed `.gitignore` changes and the target's repository and user excludes. Stop if any required surviving file is absent. Stage just the intended paths, including additions, deletions and new regression tests, with `git add -A -- <paths>`. From the selected directory in the copy, capture `git diff-index --cached --binary --relative --no-ext-diff --no-textconv --no-renames --submodule=short --no-color --unified=3 --src-prefix=a/ --dst-prefix=b/ BASE -- .` as bytes.+ - For a target outside Git, copy its baseline to sibling directories `a` and `b` outside every Git repository. Preserve file modes and symlinks and make only the reviewed changes in `b`. Compare their raw contents from the parent in the isolated private Git environment described above, with `GIT_ATTR_NOSYSTEM=1` and no target conversion settings. Capture `git -c core.autocrlf=false diff --no-index --binary --no-prefix --no-renames --submodule=short --no-ext-diff --no-textconv --no-color --unified=3 -- a b` as bytes. Exit 1 means the copies differ.+ - Inspect the entire diff against the intended source changes. For any rehearsal, use a separate disposable baseline with the same nested location and native conversion settings, and confine all writes to that copy even through links. If native conversion could affect a non-Git change, rehearse before registration. Compare the actual forward result under the intended Apply settings to `b`, then reverse under the workbench's native settings and compare the restored tree to `a`, including raw file bytes. Record `failed` if the reviewed result or baseline cannot be reproduced. Preserve the selected verification mode.+ - Send the exact unified diff in `patch`, preserving any final newline. Use a string for UTF-8 text. If the diff contains non-UTF-8 bytes, base64-encode the original bytes and send `patch: { encoding: "base64", content: "..." }`. The workbench checks that Git can parse the diff before saving it and returning its path and digest.+ - For an existing scan-local patch, pass `patchPath` and `patchDigest` and omit `patch`. Reuse the recorded path and digest when retrying a failed Generate request that already has a reviewed patch.- **Apply**: Verify the recorded base revision and patch digest, then apply exactly that patch to the selected working tree without unrelated edits. Record `applied` or `failed`. Do not verify or close the finding in this stage.+ - For example, consider a private checkout with no executable hooks or filters. Its selected directory is `components/api`. The whole reviewed patch consists of one content hunk editing the existing tracked regular file `handler.py` in place. The checkout uses `core.autocrlf=false`, and no attributes convert `handler.py`'s contents. Both headers name that file, and its ancestors are ordinary source directories. Confirm the current file bytes match the reviewed preimage. With `root, prefix = git_worktree_context(target)`, call `git_command(target, "-C", str(root), "-c", f"core.hooksPath={empty_hooks}", "apply", "--no-index", "--check", "--whitespace=nowarn", f"--directory={prefix}", "-", input_data=patch_bytes, text=False, offline=True)`. If it succeeds, repeat that call with the same digest-verified bytes and omit `--check`. Confirm that `components/api/handler.py` contains exactly the reviewed result. Running from inside `components/api` can exit successfully while skipping the change.+ - For an equivalent authorized Apply, obtain the root and prefix from the selected checkout. Read the registered scan-local file once with `workbench_db.open_scan_local_file` and match its SHA-256 against the recorded digest. Compare **all** its bytes with the in-place diff for the reviewed before and after contents, and confirm the actual target paths and Git settings meet the example's conditions before either Git command. Other patches use the existing stage's scope and verification requirements. If the worker cannot establish how to apply the reviewed changes safely in the selected mode, record `failed` before writing.- **Verify**: Do not modify source. Apply the selected mode's verification gates above against the recorded patch. Record `verified` only when evidence establishes that the original vulnerable path is closed, legitimate behavior remains intact, and the required checks pass; preserve exact evidence and any commands run in the verification summary. Otherwise record `failed` and state the failing gate or proof gap. Do not close the finding.+If a stage fails, record `failed` with the failing step and error in `summary`. Include the setter's error when registration fails.+When a parent thread delegates a remediation stage, the worker owns that stage through its terminal workbench update. The parent remains an orchestrator and must not duplicate the worker's edits or treat a chat response as completion.## Outcome and Output Contractdiff --git a/plugins/codex-security/tests/test_deep_scan_publication_authority.py b/plugins/codex-security/tests/test_deep_scan_publication_authority.pyindex e20683c25..91ef1b079 100644--- a/plugins/codex-security/tests/test_deep_scan_publication_authority.py+++ b/plugins/codex-security/tests/test_deep_scan_publication_authority.py@@ -277,6 +277,7 @@ def emulate_windows_atomic_write(monkeypatch, finalizer, files, *, reparse_pointbackend = finalizer._windows_scan_local_files()paths = {}streams = {}+ movable_directories = {}pending_deletions = set()next_directory_handle = -1@@ -296,16 +297,25 @@ def emulate_windows_atomic_write(monkeypatch, finalizer, files, *, reparse_pointstream.close()raisepaths[descriptor] = path+ if path.is_dir() and kwargs["share"] & backend._FILE_SHARE_DELETE:+ movable_directories[descriptor] = path.stat()return backend._OwnedHandle(descriptor)- def rename_handle(handle, destination):- os.replace(paths[handle], destination)+ def rename_handle(handle, destination, replace_existing):+ if replace_existing:+ os.replace(paths[handle], destination)+ else:+ os.link(paths[handle], destination)+ paths[handle].unlink()paths[handle] = destinationdef close_handle(handle):- if handle >= 0:- streams[handle].close()+ if handle in streams:+ streams.pop(handle).close()+ elif handle >= 0:+ os.close(handle)if handle in pending_deletions:+ pending_deletions.remove(handle)paths[handle].unlink()def attributes(handle):@@ -320,12 +330,39 @@ def emulate_windows_atomic_write(monkeypatch, finalizer, files, *, reparse_pointmonkeypatch.setattr(backend, "_require_windows", lambda: None)monkeypatch.setattr(backend, "_validate_existing_output", lambda path: None)monkeypatch.setattr(backend, "_create_file", create_file)++ def reopen_file(handle, *_):+ fd = os.open(paths[handle], os.O_RDONLY) if handle < 0 else os.dup(handle)+ paths[fd] = paths[handle]+ return fd++ monkeypatch.setattr(backend, "_ReOpenFile", reopen_file, raising=False)+ monkeypatch.setattr(backend, "file_change_time", lambda fd: os.fstat(fd).st_ctime_ns)+ monkeypatch.setattr(+ backend,+ "_msvcrt",+ SimpleNamespace(+ open_osfhandle=lambda handle, flags: (+ os.open(paths[handle], flags) if handle < 0 else handle+ )+ ),+ )+ monkeypatch.setattr(os, "O_BINARY", getattr(os, "O_BINARY", 0), raising=False)monkeypatch.setattr(backend, "_close_handle", close_handle)monkeypatch.setattr(backend, "_attributes", attributes)monkeypatch.setattr(backend, "_GetFileType", lambda handle: backend._FILE_TYPE_DISK, raising=False)- monkeypatch.setattr(backend, "_final_path", lambda handle, **kwargs: str(paths[handle]))++ def final_path(handle, **kwargs):+ path = paths[handle]+ if handle in movable_directories:+ for sibling in path.parent.iterdir():+ if os.path.samestat(movable_directories[handle], sibling.lstat()):+ return str(sibling)+ return str(path)++ monkeypatch.setattr(backend, "_final_path", final_path)monkeypatch.setattr(backend, "_write_all", os.write)monkeypatch.setattr(backend, "_rename_handle", rename_handle)monkeypatch.setattr(backend, "_mark_handle_for_deletion", pending_deletions.add)diff --git a/plugins/codex-security/tests/test_scan_local_write_cleanup.py b/plugins/codex-security/tests/test_scan_local_write_cleanup.pyindex 9b72f632d..35ebb5434 100644--- a/plugins/codex-security/tests/test_scan_local_write_cleanup.py+++ b/plugins/codex-security/tests/test_scan_local_write_cleanup.py@@ -1,7 +1,10 @@from __future__ import annotations+import ctypesimport errnoimport os+import secrets+from contextlib import ExitStackfrom pathlib import Pathfrom unittest import mock@@ -18,6 +21,54 @@ pytestmark = []+def test_scan_directory_closes_descriptor_when_metadata_read_fails(tmp_path: Path) -> None:+ failure = OSError(errno.EIO, "could not read directory metadata")+ with (+ mock.patch.object(os, "fstat", side_effect=failure) as fstat,+ pytest.raises(OSError) as raised,+ ):+ FINALIZER._open_verified_scan_directory(tmp_path)+ assert raised.value is failure+ descriptor = fstat.call_args.args[0]+ try:+ os.fstat(descriptor)+ except OSError as closed:+ assert closed.errno == errno.EBADF+ else:+ os.close(descriptor)+ pytest.fail("metadata read failure left the directory open")+++def test_directory_walk_closes_child_when_closing_parent_fails() -> None:+ failure = OSError(errno.EIO, "could not close parent directory")+ parent_fd, child_fd = 11, 12+ with (+ mock.patch.object(os, "dup", return_value=parent_fd),+ mock.patch.object(os, "open", return_value=child_fd),+ mock.patch.object(os, "close", side_effect=[failure, None]) as close,+ pytest.raises(OSError) as raised,+ ):+ FINALIZER._open_scan_local_directory(10, ("child",), create=False)+ assert raised.value is failure+ assert close.call_args_list == [mock.call(parent_fd), mock.call(child_fd)]+++def test_write_retries_after_umask_denies_owner_write(tmp_path: Path) -> None:+ relative_path = "remediation/request/generated.patch"+ patch = b"--- a/source.txt\n+++ b/source.txt\n@@ -1 +1 @@\n-old\n+new\n"+ previous_umask = os.umask(0o200)+ try:+ with pytest.raises(FINALIZER.ScanLocalIOError):+ FINALIZER.write_scan_local_bytes(tmp_path, relative_path, patch, owner_read_write=True)+ assert not (tmp_path / "remediation").exists()++ os.umask(0o022)+ FINALIZER.write_scan_local_bytes(tmp_path, relative_path, patch, owner_read_write=True)+ finally:+ os.umask(previous_umask)+ assert (tmp_path / relative_path).read_bytes() == patch++@pytest.mark.parametrize("failure", [None, "write", "write_and_unlink", "parent_close"])def test_atomic_write_closes_directories_after_cleanup_failures(tmp_path: Path, failure: str | None@@ -102,3 +153,101 @@ def test_atomic_write_closes_directories_after_cleanup_failures(raiseelse:close(descriptor)++[email protected]("outcome", ["saved", "collision", "no-rename", "link-out-of-space"])+def test_new_patch_on_a_filesystem_without_hardlinks(tmp_path, monkeypatch, outcome):+ destination = tmp_path / "generated.patch"+ patch = b"--- a/source.txt\n+++ b/source.txt\n@@ -1 +1 @@\n-old\n+new\n"+ winner = b"concurrent patch"+ with ExitStack() as files:+ staged = None++ def unavailable(source, _destination, *, src_dir_fd, **_kwargs):+ nonlocal staged+ # Hold the flushed inode so a direct copy cannot reuse its inode number.+ staged = files.enter_context(+ open(+ source, "rb", opener=lambda name, flags: os.open(name, flags, dir_fd=src_dir_fd)+ )+ )+ if outcome == "link-out-of-space":+ raise OSError(errno.ENOSPC, "hardlink failed for a storage error")+ if outcome == "collision":+ destination.write_bytes(winner)+ raise OSError(errno.ENOTSUP, "hardlink unavailable")++ monkeypatch.setattr(os, "link", unavailable)+ if outcome == "no-rename":+ monkeypatch.setattr(ctypes, "CDLL", lambda *args, **kwargs: object())+ if outcome == "saved":+ FINALIZER.write_scan_local_bytes(+ tmp_path, destination.name, patch, replace_existing=False+ )+ assert os.path.samestat(os.fstat(staged.fileno()), destination.stat())+ assert destination.read_bytes() == patch+ else:+ with pytest.raises(OSError) as failed:+ FINALIZER.write_scan_local_bytes(+ tmp_path, destination.name, patch, replace_existing=False+ )+ if outcome == "collision":+ assert isinstance(failed.value, FileExistsError)+ assert destination.read_bytes() == winner+ else:+ assert not destination.exists()+ if outcome == "no-rename":+ assert "exclusive rename" in str(failed.value)+ else:+ assert failed.value.errno == errno.ENOSPC+ assert not list(tmp_path.glob(".*.tmp"))++[email protected]("moved", ["parent", "scan"])+def test_new_patch_rejects_directory_moved_outside_the_admitted_scan(tmp_path, moved):+ scan = tmp_path / "scan"+ scan.mkdir()+ path = "artifacts/remediation/request/generated.patch"+ destination = scan / path+ outside = tmp_path / "outside"+ with FINALIZER.stage_scan_local_bytes(scan, path, b"patch") as publish:+ directory = destination.parent if moved == "parent" else scan+ directory.rename(outside)+ directory.mkdir()+ with pytest.raises(FINALIZER.ContractError, match="changed"):+ publish(scan)+ assert not destination.exists()+ assert not list(outside.rglob("*.patch"))+ assert not list(outside.rglob("*.tmp"))+++def test_new_patch_rejects_same_size_edit_during_publication(tmp_path, monkeypatch):+ destination = tmp_path / "generated.patch"+ link = os.link++ def edit_after_publication(*args, **kwargs):+ link(*args, **kwargs)+ before = destination.stat()+ destination.write_bytes(b"other")+ # Make the modification visible even on filesystems with coarse timestamps.+ os.utime(destination, ns=(before.st_atime_ns, before.st_mtime_ns + 2_000_000_000))++ monkeypatch.setattr(os, "link", edit_after_publication)+ with pytest.raises(FINALIZER.ContractError, match="staged contents changed during publication"):+ FINALIZER.write_scan_local_bytes(+ tmp_path, destination.name, b"first", replace_existing=False+ )+ assert destination.read_bytes() == b"other"+ assert list(tmp_path.iterdir()) == [destination]+++def test_existing_temporary_file_survives_a_name_collision(tmp_path, monkeypatch):+ monkeypatch.setattr(secrets, "token_hex", lambda _: "collision")+ existing = tmp_path / ".generated.patch.collision.tmp"+ existing.write_bytes(b"another writer")+ with pytest.raises(FileExistsError):+ FINALIZER.write_scan_local_bytes(+ tmp_path, "generated.patch", b"new", replace_existing=False+ )+ assert existing.read_bytes() == b"another writer"+ assert not (tmp_path / "generated.patch").exists()diff --git a/plugins/codex-security/tests/test_windows_scan_local_files.py b/plugins/codex-security/tests/test_windows_scan_local_files.pyindex 60eb71a75..9dc5f1889 100644--- a/plugins/codex-security/tests/test_windows_scan_local_files.py+++ b/plugins/codex-security/tests/test_windows_scan_local_files.py@@ -1,11 +1,22 @@from __future__ import annotations+import argparse+import errnoimport osimport shutil+import sqlite3import subprocess+import sys+from concurrent.futures import ThreadPoolExecutor+from contextlib import ExitStack, contextmanagerfrom pathlib import Path+from threading import Event+from unittest.mock import Mockimport pytest+from test_deep_scan_publication_authority import emulate_windows_atomic_write+from test_workbench_archive import recipe, register+from test_workbench_remediation_retry import pending_generation as pending_generationfrom workbench_test_support import load_scriptpytestmark = pytest.mark.native_windows@@ -86,6 +97,216 @@ def test_native_windows_backend_writes_reads_replaces_and_deletes(tmp_path: PathWINDOWS_FILES.unlink_if_exists(scan_dir, "exports/results.sarif")[email protected](os.name != "nt", reason="requires native Win32 file APIs")+def test_create_only_write_reports_existing_patch_when_disk_is_full(+ tmp_path: Path, monkeypatch+) -> None:+ destination = tmp_path / "reviewed.patch"+ patch = b"previously saved patch"+ destination.write_bytes(patch)++ def disk_full(*args):+ raise OSError(errno.ENOSPC, "synthetic disk full")++ monkeypatch.setattr(WINDOWS_FILES, "_write_all", disk_full)+ with pytest.raises(FileExistsError):+ WINDOWS_FILES.atomic_write(tmp_path, destination.name, patch, replace_existing=False)+ assert destination.read_bytes() == patch++[email protected]("outcome", ["publish", "archive", "abandon", "collision", "substitution"])+def test_staged_patch_is_visible_only_after_publication(tmp_path, monkeypatch, outcome):+ scan_dir = tmp_path / "scan"+ scan_dir.mkdir()+ destination = scan_dir / "reviewed.patch"+ patch = b"generated patch\n"+ with ExitStack() as files:+ backend = WINDOWS_FILES+ if os.name != "nt":+ backend = emulate_windows_atomic_write(+ monkeypatch, load_script("finalize_scan_contract"), files+ )+ with backend.stage_write(scan_dir, destination.name, patch) as publish:+ assert not destination.exists()+ if outcome in {"publish", "archive"}:+ if outcome == "archive":+ archived = tmp_path / "archived-scan"+ scan_dir.rename(archived)+ scan_dir.mkdir()+ destination = archived / destination.name+ saved, change = publish(destination.parent)+ assert destination.read_bytes() == patch+ with destination.open("rb") as current:+ metadata = os.fstat(current.fileno())+ assert os.path.samestat(saved, metadata)+ assert (saved.st_size, saved.st_mtime_ns) == (+ metadata.st_size,+ metadata.st_mtime_ns,+ )+ assert change == backend.file_change_time(current.fileno())+ elif outcome == "collision":+ destination.write_bytes(b"competing patch\n")+ with pytest.raises(FileExistsError):+ publish()+ elif outcome == "substitution":+ (temp,) = scan_dir.glob(".*.tmp")+ # Keep the original inode alive. Match the temp size and timestamp.+ original = temp.rename(scan_dir / "original")+ temp.write_bytes(b"different bytes\n")+ original_stat = original.stat()+ os.utime(temp, ns=(original_stat.st_atime_ns, original_stat.st_mtime_ns))+ with pytest.raises(backend.WindowsScanLocalPathError, match="staged patch changed"):+ publish()+ if outcome == "substitution":+ assert temp.read_bytes() == b"different bytes\n"+ assert not destination.exists()+ else:+ assert not list(destination.parent.glob(".*.tmp"))+ if outcome == "abandon":+ assert not destination.exists()+ elif outcome == "collision":+ assert destination.read_bytes() == b"competing patch\n"+ elif outcome == "archive":+ assert not list(scan_dir.iterdir())++[email protected]("other_state", [False, True])+def test_canceled_generation_removes_its_staged_patch_after_archive(+ pending_generation, workbench_api, tmp_path, monkeypatch, other_state+):+ database, scan_dir, _, args, _ = pending_generation+ remediation = workbench_api["remediation_state"]+ stage = remediation.stage_scan_local_bytes+ archived = None+ replacement = None++ @contextmanager+ def archive_and_cancel(*stage_args, **options):+ nonlocal archived, replacement+ with ExitStack() as files:+ if os.name != "nt":+ emulation = files.enter_context(monkeypatch.context())+ backend = emulate_windows_atomic_write(+ emulation, sys.modules[stage.__module__], files+ )+ emulation.setattr(backend, "_create_directory", Path.mkdir)+ with stage(*stage_args, **options) as publish:+ registered = register(+ tmp_path / "other-state" if other_state else database.parent,+ tmp_path / "target",+ scan_dir,+ "--archive-existing",+ )+ archived = Path(registered["archivedScanDir"])+ (temp,) = archived.rglob("*.tmp")+ replacement = scan_dir / temp.relative_to(archived)+ replacement.parent.mkdir(parents=True)+ replacement.write_bytes(b"new scan's file")+ with sqlite3.connect(database) as other:+ other.row_factory = sqlite3.Row+ remediation.cancel_finding_remediation_request(other, args)+ yield publish++ monkeypatch.setattr(remediation, "stage_scan_local_bytes", archive_and_cancel)+ with sqlite3.connect(database) as connection:+ connection.row_factory = sqlite3.Row+ with pytest.raises(SystemExit, match="remediation request changed"):+ workbench_api["set_finding_remediation"](connection, args)+ assert not list(archived.rglob("*.tmp"))+ assert not list(archived.rglob("*.patch"))+ assert replacement.read_bytes() == b"new scan's file"+++def test_staging_metadata_failure_removes_the_flushed_file(tmp_path, monkeypatch):+ with ExitStack() as files:+ backend = WINDOWS_FILES+ if os.name != "nt":+ backend = emulate_windows_atomic_write(+ monkeypatch, load_script("finalize_scan_contract"), files+ )++ def fail_metadata(_):+ raise OSError(errno.EIO, "metadata unavailable")++ monkeypatch.setattr(backend, "file_change_time", fail_metadata)+ with pytest.raises(OSError, match="metadata unavailable"):+ with backend.stage_write(tmp_path, "reviewed.patch", b"reviewed bytes"):+ pytest.fail("staging should have failed")+ assert not list(tmp_path.iterdir())++[email protected](os.name != "nt", reason="requires native Win32 archive sharing semantics")[email protected]("timeout", [5, 0], ids=["retry", "no-wait"])+def test_archive_contention_during_windows_patch_write(+ pending_generation, workbench_api, tmp_path, monkeypatch, timeout+):+ database, scan_dir, _, args, patch = pending_generation+ backend = sys.modules["windows_scan_local_files"]+ write_all = backend._write_all+ rename = Path.rename+ writing, rename_denied = Event(), Event()+ before_archive = Mock()++ def write_while_archiving(handle, data):+ if data == patch:+ writing.set()+ assert rename_denied.wait(10)+ # A different writer can commit before the patch releases its open handles.+ with sqlite3.connect(database) as other:+ other.execute("BEGIN IMMEDIATE")+ other.execute("CREATE TABLE unrelated_write (value INTEGER)")+ return write_all(handle, data)++ def observe_rename(source, destination):+ try:+ return rename(source, destination)+ except PermissionError as exc:+ if source == scan_dir and exc.winerror in {5, 32}:+ rename_denied.set()+ raise++ def archive():+ assert writing.wait(10)+ target = tmp_path / "target"+ registration = argparse.Namespace(+ repository=str(target),+ scan_dir=str(scan_dir),+ archive_existing=True,+ archived_scan_dir=None,+ recipe_json=recipe(target),+ recipe_json_stdin=False,+ registration_json_stdin=False,+ parent_scan_id=None,+ )+ with sqlite3.connect(database, timeout=timeout) as connection:+ connection.row_factory = sqlite3.Row+ return Path(+ workbench_api["register_cli_scan"](+ connection, registration, before_archive=before_archive+ )["archivedScanDir"]+ )++ monkeypatch.setattr(backend, "_write_all", write_while_archiving)+ monkeypatch.setattr(Path, "rename", observe_rename)+ with ThreadPoolExecutor(max_workers=1) as pool, sqlite3.connect(database) as connection:+ future = pool.submit(archive)+ connection.row_factory = sqlite3.Row+ result = workbench_api["set_finding_remediation"](connection, args)+ if timeout:+ archived = future.result(timeout=10)+ else:+ with pytest.raises(PermissionError) as denied:+ future.result(timeout=10)+ assert denied.value.winerror in {5, 32}+ assert not list(scan_dir.parent.glob(f"{scan_dir.name}.previous-*"))+ archived = scan_dir+ before_archive.assert_called_once_with()+ recorded = result["scan"]["findings"][0]["remediationState"]["patchPath"]+ assert (archived / recorded).read_bytes() == patch+ if timeout:+ assert not (scan_dir / recorded).exists()++@pytest.mark.skipif(os.name != "nt", reason="requires native Win32 reparse-point behavior")def test_native_windows_backend_rejects_symlink_ancestor(tmp_path: Path) -> None:scan_dir = tmp_path / "scan"diff --git a/plugins/codex-security/tests/test_workbench_archive.py b/plugins/codex-security/tests/test_workbench_archive.pyindex 3e5456ce7..43614e831 100644--- a/plugins/codex-security/tests/test_workbench_archive.py+++ b/plugins/codex-security/tests/test_workbench_archive.py@@ -1,18 +1,29 @@from __future__ import annotationsimport argparse+import ioimport json+import osimport runpyimport signalimport sqlite3import subprocess+import sysimport uuidfrom concurrent.futures import ThreadPoolExecutor+from contextlib import contextmanagerfrom pathlib import Pathfrom threading import Event+from unittest.mock import Mockimport pytest-from workbench_test_support import SCRIPT, run_workbench+from workbench_test_support import (+ SCRIPT,+ RemediationRequest,+ run_workbench,+ scan_command,+ write_completed_contract,+)def recipe(target: Path) -> str:@@ -135,6 +146,293 @@ def test_only_one_concurrent_registration_archives_previous_output(workflow_scan)+def pending_patch(previous_scan, workbench_api, monkeypatch):+ state, target, output, scan_id = previous_scan+ write_completed_contract(output, scan_id, target, relative_path="fixture.py")+ completed = scan_command(state, "complete-scan", scan_id)["scan"]+ occurrence = completed["findings"][0]["occurrenceId"]+ request = RemediationRequest(state, occurrence)+ token = str(uuid.uuid4())+ request.start(token)+ patch = b"--- a/fixture.py\n+++ b/fixture.py\n@@ -1 +1 @@\n-value = 1\n+value = 2\n"+ monkeypatch.setenv("CODEX_SECURITY_STATE_DIR", str(state))+ monkeypatch.setattr(sys, "stdin", Mock(buffer=io.BytesIO(patch)))+ monkeypatch.setattr(+ sys,+ "argv",+ [+ str(SCRIPT),+ "set-finding-remediation",+ "--occurrence-id",+ occurrence,+ "--request-id",+ request.id,+ "--action-token",+ token,+ "--expected-version",+ "1",+ "--state",+ "generated",+ "--patch-stdin",+ ],+ )+ return workbench_api["parse_args"](""), patch++[email protected]_platform+ ("archive", "legacy"),+ [+ ("same-state", False),+ ("same-state", True),+ ("same-state-before-stage", False),+ ("same-state-staged", False),+ ("same-state-before-digest", False),+ ("other-state", False),+ ("other-state", True),+ ("before-write", False),+ ("after-write", False),+ ("after-digest", False),+ ],+)+def test_generate_patch_keeps_completed_scan_identity_during_archive(+ previous_scan, pending_patch, workbench_api, monkeypatch, capsys, archive, legacy+):+ state, target, output, scan_id = previous_scan+ args, patch = pending_patch+ occurrence, request, token = args.occurrence_id, args.request_id, args.action_token+ archived = False++ def archive_output():+ nonlocal archived+ if archived:+ return+ owner = state if archive.startswith("same-state") else state.with_name("other-state")+ replacement = register(owner, target, output, "--archive-existing")+ archived = True+ if owner != state:+ write_completed_contract(+ output, replacement["scanId"], target, relative_path="fixture.py"+ )+ scan_command(owner, "complete-scan", replacement["scanId"])++ def archive_while_reading_patch():+ if archive in {"same-state", "other-state"}:+ archive_output()+ return patch++ if legacy:+ with sqlite3.connect(state / "workbench.sqlite3") as connection:+ connection.execute(+ "UPDATE scans SET seal_manifest_digest = NULL WHERE id = ?", (scan_id,)+ )+ if archive in {"before-write", "after-write", "same-state-before-stage", "same-state-staged"}:+ remediation = workbench_api["remediation_state"]+ stage = remediation.stage_scan_local_bytes++ @contextmanager+ def stage_with_archive(*args, **kwargs):+ if archive in {"before-write", "same-state-before-stage"}:+ archive_output()+ with stage(*args, **kwargs) as publish:+ if archive == "same-state-staged":+ archive_output()+ yield publish+ if archive == "after-write":+ archive_output()++ monkeypatch.setattr(remediation, "stage_scan_local_bytes", stage_with_archive)+ elif archive in {"same-state-before-digest", "after-digest"}:+ patch_path = output / "artifacts" / "remediation" / occurrence / request / f"{token}.patch"+ patch_path.parent.mkdir(parents=True)+ patch_path.write_bytes(patch)+ db = workbench_api["_WORKBENCH_DB_CONTEXT"]+ open_file = db.open_scan_local_file++ @contextmanager+ def open_with_archive(directory, relative, **options):+ is_patch = Path(relative).suffix == ".patch"+ if is_patch and archive == "same-state-before-digest":+ archive_output()+ with open_file(directory, relative, **options) as source:+ yield source+ if is_patch and archive == "after-digest":+ archive_output()++ monkeypatch.setattr(db, "open_scan_local_file", open_with_archive)++ monkeypatch.setattr(sys, "stdin", Mock(buffer=Mock(read=archive_while_reading_patch)))+ if not archive.startswith("same-state"):+ with pytest.raises(SystemExit, match="Could not save the remediation patch"):+ workbench_api["main"]()+ assert not list(output.rglob("*.patch"))+ with sqlite3.connect(state / "workbench.sqlite3") as connection:+ assert connection.execute(+ "SELECT state, version, patch_path FROM finding_remediation_attempts WHERE request_id = ?",+ (request,),+ ).fetchone() == ("requested", 1, None)+ return+ workbench_api["main"]()+ result = json.loads(capsys.readouterr().out)["scan"]+ patch_path = result["findings"][0]["remediationState"]["patchPath"]+ assert not (output / patch_path).exists()+ assert (Path(result["scanDir"]) / patch_path).read_bytes() == patch++[email protected]_platform[email protected]("rollback", [False, True], ids=["commit", "rollback"])[email protected]("phase", ["before-seal", "after-root-sample", "before-stage"])+def test_generate_waits_for_the_archive_transaction(+ previous_scan, pending_patch, workbench_api, monkeypatch, rollback, phase+):+ state, target, output, _ = previous_scan+ args, patch = pending_patch+ database = state / "workbench.sqlite3"+ start_archive, moved, finish_archive = Event(), Event(), Event()++ class PausedArchive(sqlite3.Connection):+ def execute(self, sql, *args):+ result = super().execute(sql, *args)+ if sql.startswith("UPDATE scans SET scan_dir"):+ moved.set()+ assert finish_archive.wait(10)+ return result++ def archive():+ assert start_archive.wait(10)+ with sqlite3.connect(database, factory=PausedArchive) as connection:+ connection.row_factory = sqlite3.Row+ args = argparse.Namespace(+ repository=str(target),+ scan_dir=str(output),+ archive_existing=True,+ archived_scan_dir=None,+ recipe_json=recipe(target),+ recipe_json_stdin=False,+ registration_json_stdin=False,+ parent_scan_id=str(uuid.uuid4()) if rollback else None,+ )+ if rollback:+ with pytest.raises(SystemExit, match="not found"):+ workbench_api["register_cli_scan"](connection, args)+ return output+ return Path(workbench_api["register_cli_scan"](connection, args)["archivedScanDir"])++ def pause_for_archive():+ start_archive.set()+ assert moved.wait(10)++ if phase == "before-stage":+ remediation = workbench_api["remediation_state"]+ stage = remediation.stage_scan_local_bytes++ def stage_during_archive(*args, **kwargs):+ pause_for_archive()+ return stage(*args, **kwargs)++ monkeypatch.setattr(remediation, "stage_scan_local_bytes", stage_during_archive)+ else:++ def read_patch():+ pause_for_archive()+ return patch++ monkeypatch.setattr(sys, "stdin", Mock(buffer=Mock(read=read_patch)))+ with ThreadPoolExecutor(max_workers=1) as pool, sqlite3.connect(database) as connection:+ connection.row_factory = sqlite3.Row+ connection.set_trace_callback(+ lambda sql: (+ finish_archive.set() if sql == "BEGIN IMMEDIATE" and moved.is_set() else None+ )+ )+ registered = pool.submit(archive)+ if phase == "after-root-sample":+ remediation = workbench_api["remediation_state"]+ sample = remediation.scan_root_identity++ def sample_then_finish_archive(directory):+ observed = sample(directory)+ if not finish_archive.is_set():+ assert moved.is_set()+ finish_archive.set()+ registered.result(timeout=10)+ return observed++ monkeypatch.setattr(remediation, "scan_root_identity", sample_then_finish_archive)+ try:+ result = workbench_api["set_finding_remediation"](connection, args)["scan"]+ finally:+ finish_archive.set()+ destination = registered.result()+ relative = result["findings"][0]["remediationState"]["patchPath"]+ assert Path(result["scanDir"]) == destination+ assert (destination / relative).read_bytes() == patch+ if not rollback:+ assert not (output / relative).exists()++[email protected]_platform[email protected]("replace", [False, True])+def test_generate_hashes_and_rechecks_the_manifest(+ previous_scan, pending_patch, workbench_api, monkeypatch, replace+):+ state, _, output, _ = previous_scan+ args, patch = pending_patch+ database = state / "workbench.sqlite3"+ manifest = output / "scan-manifest.json"+ original = manifest.stat()+ opened = os.fdopen+ reads = []++ def check_manifest_reads(fd, *options, **kwargs):+ stream = opened(fd, *options, **kwargs)+ if os.path.samestat(original, os.fstat(fd)):+ read = stream.read++ def read_without_writer(*args):+ with sqlite3.connect(database, timeout=0) as writer:+ writer.execute("BEGIN IMMEDIATE")+ reads.append(True)+ return read(*args)++ stream.read = read_without_writer+ return stream++ monkeypatch.setattr(os, "fdopen", check_manifest_reads)+ if replace:+ remediation = workbench_api["remediation_state"]+ stage = remediation.stage_scan_local_bytes++ @contextmanager+ def stage_and_replace(*args, **kwargs):+ with stage(*args, **kwargs) as publish:+ replacement = manifest.with_suffix(".new")+ replacement.write_bytes(manifest.read_bytes())+ os.utime(replacement, ns=(original.st_atime_ns, original.st_mtime_ns))+ replacement.replace(manifest)+ yield publish++ monkeypatch.setattr(remediation, "stage_scan_local_bytes", stage_and_replace)+ with sqlite3.connect(database) as connection:+ connection.row_factory = sqlite3.Row+ generate = workbench_api["set_finding_remediation"]+ if replace:+ with pytest.raises(SystemExit, match="sealed scan manifest changed"):+ generate(connection, args)+ row = connection.execute(+ "SELECT state, version, patch_path FROM finding_remediation_attempts WHERE request_id = ?",+ (args.request_id,),+ ).fetchone()+ assert tuple(row) == ("requested", 1, None)+ assert not list(output.rglob("*.patch"))+ else:+ result = generate(connection, args)["scan"]+ relative = result["findings"][0]["remediationState"]["patchPath"]+ assert (output / relative).read_bytes() == patch+ assert reads++@pytest.mark.parametrize("result", [None, {}, {"sarifPath": None}, {"sarifPath": "unrelated"}])def test_archiving_preserves_other_workflow_bindings_and_result_metadata(previous_scan, result):state, target, output, scan_id = previous_scandiff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.pyindex 6342a6b12..bad47bfee 100644--- a/plugins/codex-security/tests/test_workbench_db.py+++ b/plugins/codex-security/tests/test_workbench_db.py@@ -1066,6 +1066,10 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_auassert generated["findings"][0]["remediationState"]["summary"] == ("Contained archive extraction under the selected output directory.")+ selected = get_scan(state_dir, scan_id, "--occurrence-id", f" {occurrence_id}\t")+ for result in (selected["scan"], selected["workspace"]["results"]):+ assert result["findingCount"] == 1+ assert result["findings"] == generated["findings"]apply_token = str(uuid.uuid4())apply_requested = request.request_action("2", "apply", apply_token)["scan"]assert apply_requested["findings"][0]["remediationState"]["pendingAction"] == "apply"@@ -3060,6 +3064,29 @@ def test_workbench_hides_missing_artifact_on_reopen(tmp_path: Path) -> None:assert "markdownReport" not in reopened["scan"]["artifacts"][email protected](os.name != "posix", reason="changes permissions on an open patch")+def test_patch_digest_accepts_file_becoming_read_only(tmp_path: Path, monkeypatch) -> None:+ path = tmp_path / "reviewed.patch"+ patch = b"reviewed content\n"+ path.write_bytes(patch)+ path.chmod(0o600)+ db = load_script("workbench_db")+ with db.open_scan_local_file(tmp_path, path.name) as source:+ read = source.read++ def read_and_protect(size):+ chunk = read(size)+ os.fchmod(source.fileno(), 0o400)+ return chunk++ monkeypatch.setattr(source, "read", read_and_protect)+ monkeypatch.setattr(db, "open_scan_local_file", lambda *_: source)+ db.require_matching_patch_digest(+ {"scan_dir": str(tmp_path)}, path.name, "sha256:" + hashlib.sha256(patch).hexdigest()+ )+ assert path.stat().st_mode & 0o777 == 0o400++@pytest.mark.cross_platformdef test_large_patch_preview_preserves_digest_checks(tmp_path: Path, capfd: pytest.CaptureFixture[str]diff --git a/plugins/codex-security/tests/test_workbench_db_exports.py b/plugins/codex-security/tests/test_workbench_db_exports.pyindex 10f298816..f98b9cc66 100644--- a/plugins/codex-security/tests/test_workbench_db_exports.py+++ b/plugins/codex-security/tests/test_workbench_db_exports.py@@ -870,6 +870,26 @@ def test_completed_findings_are_returned_in_bounded_pages(tmp_path: Path) -> Nonfinding["occurrenceId"] == off_prefix_occurrence_id for finding in selected["findings"])+ request = RemediationRequest(state_dir, off_prefix_occurrence_id)+ action_token = str(uuid.uuid4())+ request.start(action_token)+ patch = "--- /dev/null\n+++ b/contained.txt\n@@ -0,0 +1 @@\n+safe\n"+ generated = request.update(+ action_token,+ "1",+ "generated",+ "--patch-stdin",+ input_text=patch,+ )+ findings = {finding["occurrenceId"]: finding for finding in generated["scan"]["findings"]}+ remediation = findings[off_prefix_occurrence_id]["remediationState"]+ assert remediation["state"] == "generated"+ patch_path = scan_dir / remediation["patchPath"]+ assert patch_path.read_text(encoding="utf-8") == patch+ digest = hashlib.sha256(patch_path.read_bytes()).hexdigest()+ assert remediation["patchDigest"] == f"sha256:{digest}"+ assert generated["workspace"]["results"]["findings"] == completed["findings"]+def test_embedded_and_paged_findings_bound_large_stored_fields(tmp_path: Path) -> None:state_dir, target, scan_id, scan_dir = empty_target_scan(tmp_path)diff --git a/plugins/codex-security/tests/test_workbench_remediation_retry.py b/plugins/codex-security/tests/test_workbench_remediation_retry.pyindex aa2e41ad1..4bb0e9b14 100644--- a/plugins/codex-security/tests/test_workbench_remediation_retry.py+++ b/plugins/codex-security/tests/test_workbench_remediation_retry.py@@ -1,15 +1,23 @@from __future__ import annotations-import hashlib+import argparse+import errno+import io+import os+import secretsimport sqlite3import subprocess+import sysimport uuid+from contextlib import contextmanagerfrom pathlib import Pathfrom typing import Any+import pytestfrom workbench_test_support import (RemediationRequest,empty_target_paths,+ initialize_git_repository,scan_command,set_triage,start_saved_scan,@@ -25,7 +33,699 @@ def update_remediation(return result["scan"]["findings"][0]["remediationState"]-def test_failed_remediation_steps_can_retry_or_regenerate(tmp_path: Path) -> None:[email protected]_platform+def test_generated_patch_does_not_fetch_missing_git_objects(tmp_path, workbench_api, monkeypatch):+ from workbench_source_scopes import offline_git_bytes++ repo = tmp_path / "repo"+ revision = initialize_git_repository(repo)+ missing = "e" * len(revision)+ marker = tmp_path / "remote-called"+ monkeypatch.setenv("TEST_REMOTE_MARKER", marker.as_posix())+ monkeypatch.setenv("GIT_ALLOW_PROTOCOL", "file")+ monkeypatch.delenv("GIT_NO_LAZY_FETCH", raising=False)+ for key, value in {+ "attr.tree": missing,+ "core.repositoryformatversion": "1",+ "extensions.partialclone": "origin",+ "remote.origin.promisor": "true",+ "remote.origin.partialclonefilter": "blob:none",+ "remote.origin.url": repo.as_uri(),+ "remote.origin.uploadpack": 'echo invoked > "$TEST_REMOTE_MARKER"; exit 1;',+ }.items():+ subprocess.run(["git", "config", key, value], cwd=repo, check=True)++ patch = b"--- /dev/null\n+++ b/added\n@@ -0,0 +1 @@\n+new\n"+ workbench_api["remediation_state"].require_valid_patch(repo, patch)+ assert offline_git_bytes(repo, "cat-file", "blob", missing) is None+ assert not marker.exists()+ # An explicit transport can reach the same helper through the caller's environment.+ assert subprocess.run(["git", "ls-remote", "origin"], cwd=repo).returncode != 0+ assert marker.exists()++[email protected]_platform+def test_generated_patch_requires_binary_contents(+ tmp_path: Path, workbench_api, monkeypatch+) -> None:+ repository = tmp_path / "repo"+ initialize_git_repository(repository)+ target = repository / "nested" / "scan" / "target"+ target.mkdir(parents=True)+ asset = target / "asset.bin"+ asset.write_bytes(b"\0before")+ subprocess.run(["git", "add", "asset.bin"], cwd=target, check=True)+ diff = ["git", "diff", "--relative"]+ added = subprocess.check_output([*diff, "--cached", "--binary"], cwd=target)+ subprocess.run(["git", "commit", "-qm", "Add asset"], cwd=target, check=True)+ asset.write_bytes(b"\0after")+ summary = subprocess.check_output(diff, cwd=target)+ binary = subprocess.check_output([*diff, "--binary"], cwd=target)+ notice = summary.splitlines(keepends=True)[-1]+ prose = (+ notice.decode()+ + "Only in target: new-file\nindex 1234567..89abcde 100644\nGIT binary patch"+ )+ subprocess.run(["git", "commit", "-qam", "Update asset", "-m", prose], cwd=target, check=True)+ email = ["git", "format-patch", "-1", "--stdout", "--relative", "--signature", prose]+ mail = subprocess.check_output(email, cwd=target)+ mail_summary = subprocess.check_output([*email, "--no-binary"], cwd=target)+ asset.write_bytes(b"\0before")++ validate = workbench_api["remediation_state"].require_valid_patch+ hunk = b"--- a/note\n+++ b/note\n@@ -1 +1 @@\n-old\n+Binary files a/a and b/a differ\n"+ with pytest.raises(SystemExit, match="Could not parse the remediation patch"):+ validate(target, summary.removesuffix(notice))+ for incomplete in (+ summary,+ summary.rstrip(b"\n"),+ hunk + summary,+ mail_summary,+ notice + hunk,+ hunk + notice,+ hunk + b"Only in tree: new-file\n",+ ):+ with pytest.raises(SystemExit, match="--binary option"):+ validate(target, incomplete)++ data_line = binary.split(b"GIT binary patch\n", 1)[1].splitlines()[1]+ corrupt = binary.replace(data_line, b"!", 1)+ monkeypatch.setenv("GIT_TRACE", "1")+ with pytest.raises(SystemExit, match="error: corrupt binary patch"):+ validate(target, hunk + corrupt)+ validate(target, hunk + binary)+ padded = binary.replace(b" 100644\nGIT binary patch", b" 100644 \t\nGIT binary patch")+ assert padded != binary+ validate(target, padded)+ validate(target, mail)+ near_miss = b"---\n" + b" 1 file changed\n" * 10000 + b"end of message\n"+ validate(target, mail.replace(b"\n---\n", b"\n" + near_miss + b"---\n", 1))+ # --no-stat with diff.suppressBlankEmpty can look like a diffstat between two files.+ deleted_dash = b"diff --git a/a b/a\n--- a/a\n+++ b/a\n@@ -1,3 +1,2 @@\n---\n stays\n\n"+ validate(target, mail.partition(b"\n---\n")[0] + b"\n\n" + deleted_dash + binary)+ validate(target, mail + b"diff --git a/signature b/signature\n")+ message, signature = mail.rsplit(b"\n-- \n", 1)+ with pytest.raises(SystemExit, match="omits file content"):+ validate(target, message + b"\ndiff --git a/test b/test\n-- \n" + signature)+ validate(target, b"Files changed: note\n" + hunk)+ validate(target, added.partition(b"\n\n")[0] + b"\n\n")+ index_line = next(+ line for line in added.splitlines(keepends=True) if line.startswith(b"index ")+ )+ with pytest.raises(SystemExit, match="full object IDs"):+ validate(target, added.replace(index_line, b""))+ assert asset.read_bytes() == b"\0before"++[email protected]_platform+ "old,new,make_executable",+ [+ (b"old\n", b"new\n", False),+ (b"\0old", b"\0new", False),+ (b"", None, False),+ (b"old\n", b"new\n", True),+ ],+)+def test_generated_patch_with_quoted_no_stat_header(+ tmp_path: Path, workbench_api, old: bytes, new: bytes | None, make_executable: bool+) -> None:+ repo = tmp_path / "repo"+ initialize_git_repository(repo)+ subprocess.run(["git", "config", "core.autocrlf", "false"], cwd=repo, check=True)++ def git(*args: str) -> bytes:+ return subprocess.check_output(["git", *args], cwd=repo)++ path = repo / "file"+ path.write_bytes(old)+ git("add", "file")+ git("commit", "-qm", "Baseline")+ if new is None:+ path.unlink()+ else:+ path.write_bytes(new)+ git("add", "-A", "--", "file")+ if make_executable:+ git("update-index", "--chmod=+x", "file")+ git("commit", "-qm", "Change", "-m", "diff --git a/file b/file\nThis header is an example.")+ mail = git("format-patch", "--no-stat", "--binary", "-1", "--stdout")+ target = tmp_path / "target"+ target.mkdir()+ restored = target / "file"+ restored.write_bytes(old)+ validate = workbench_api["remediation_state"].require_valid_patch+ if new is None:+ # Git retains a preimage for this empty deletion and cannot reverse it.+ with pytest.raises(SystemExit, match="omits file content"):+ validate(target, mail)+ return+ validate(target, mail)+ for flags, expected in (([], new), (["--reverse"], old)):+ subprocess.run(+ ["git", "-c", "core.autocrlf=false", "apply", "--no-index", *flags, "-"],+ cwd=target,+ input=mail,+ check=True,+ )+ assert restored.read_bytes() == expected+ if make_executable and os.name != "nt":+ assert bool(restored.stat().st_mode & 0o100) == (not flags)++[email protected]_platform+def test_generated_patch_compares_decoded_git_paths(tmp_path: Path, workbench_api) -> None:+ (tmp_path / "source file").write_bytes(b"old\n")+ raw = (b"a/source file", b"b/source file")+ quoted = (b'"a/source file"', rb'"b/\163ource\040file"')+ mixed = (rb'"original/\163ource\040file"', b"updated/source file")+ for git_paths, (old, new) in ((raw, quoted), (quoted, raw), (raw, mixed), (quoted, mixed)):+ patch = b"diff --git %s %s\n--- %s\n+++ %s\n@@ -1 +1 @@\n-old\n+new\n" % (+ *git_paths,+ old,+ new,+ )+ subprocess.run(+ ["git", "apply", "--no-index", "--check", "-"], input=patch, cwd=tmp_path, check=True+ )+ workbench_api["remediation_state"].require_valid_patch(tmp_path, patch)++ # Git uses the unified paths even if the Git header names another file.+ # A suffix following a space in a directory name is still a different target.+ for header in (b"a/other b/source file", b"a/source file b/other dir/source file"):+ unrelated = patch.replace(patch.splitlines()[0], b"diff --git " + header)+ numstat = subprocess.check_output(+ ["git", "apply", "--numstat", "-z", "-"], input=unrelated, cwd=tmp_path+ )+ assert numstat.endswith(b"\tsource file\0")+ with pytest.raises(SystemExit, match="omits file content"):+ workbench_api["remediation_state"].require_valid_patch(tmp_path, unrelated)++[email protected]_platform+def test_generated_patch_rejects_truncated_hunk_header(tmp_path: Path, workbench_api) -> None:+ repo = tmp_path / "repo"+ initialize_git_repository(repo)+ path = repo / "source"+ path.write_bytes(b"before first\n" + b"context\n" * 12 + b"before last\n")+ subprocess.run(["git", "add", "source"], cwd=repo, check=True)+ path.write_bytes(path.read_bytes().replace(b"before", b"after"))+ patch = subprocess.check_output(["git", "diff", "--", "source"], cwd=repo)+ first, last = patch.rsplit(b"\n@@ -", 1)+ assert b"@@ -" in first and b"after last" in last+ validate = workbench_api["remediation_state"].require_valid_patch+ validate(repo, patch)+ truncated = first + b"\n@@ -"+ numstat = subprocess.check_output(+ ["git", "apply", "--numstat", "-z", "-"], input=truncated, cwd=repo+ )+ assert numstat == b"1\t1\tsource\0"+ for tail in (b"", b"12,2 +", b"12,2 +12,2 @\n"):+ with pytest.raises(SystemExit, match="omits file content"):+ validate(repo, truncated + tail)+ validate(repo, b"@@ - a quoted header\n" + patch + b"\nPostscript\n@@ -")+ no_newline = b"--- a/one\n+++ b/one\n@@ -1 +1 @@\n-@@ -\n+@@ -\n\\ No newline at end of file\n"+ validate(repo, no_newline)+ with pytest.raises(SystemExit, match="omits file content"):+ validate(repo, no_newline + b"@@ -")++[email protected]_platform+def test_generated_patch_requires_complete_file_sections(tmp_path: Path, workbench_api) -> None:+ validate = workbench_api["remediation_state"].require_valid_patch+ hunk = b"--- a/one\n+++ b/one\n@@ -1 +1 @@\n-old\n+new\n"+ validate(tmp_path, b"Binary files " + b"path and " * 10000 + b"other text\n" + hunk)+ added = (+ b"diff --git a/added b/added\nnew file mode 100644 \t\n"+ b"--- /dev/null\n+++ b/added\n@@ -0,0 +1 @@\n+contents\n"+ )+ validate(tmp_path, added)+ with pytest.raises(SystemExit, match="omits file content"):+ validate(tmp_path, added.replace(b"100644", b"10064"))+ third = hunk.replace(b"one", b"three")+ for header in (+ b"diff --git a/two b/two",+ b"diff --git a/two b/two\nnew file m",+ b"diff --git a/two b/two\nnew file mode 100",+ b"--- a/two",+ b"--- a/two\n+++ b/two",+ ):+ for ending in (+ b"",+ b"\n",+ b"\r\n\n",+ b"\n" + third,+ b"\ndiff --git a/three b/three\n" + third,+ ):+ with pytest.raises(SystemExit, match="omits file content|Could not parse"):+ validate(tmp_path, hunk + header + ending)+ validate(tmp_path, hunk + b"--- a/two\n+++ b/two\n@@ -1 +1 @@\n--- a/three\n+++ b/three\n")+ validate(tmp_path, b"--- a/one\n+++ b/one\n@@ -1 +0,0 @@\n--- a/two\n")++ for section in (+ b"diff --git a/two b/two\nold mode 100644\nnew mode 100755\n",+ b"diff --git a/two b/three\nsimilarity index 100%\nrename from two\nrename to three\n",+ ):+ validate(tmp_path, hunk + section)+ validate(tmp_path, hunk + section + b"index 1234567..1234567\n")+ with pytest.raises(SystemExit, match="omits file content"):+ validate(tmp_path, hunk + section + b"index 1234567..7654321\n")++ copy = b"diff --git a/two b/three\nsimilarity index 100%\ncopy from two\ncopy to three\n"+ validate(tmp_path, hunk + copy)+ (tmp_path / "one").write_bytes(b"old\n")+ (tmp_path / "two").write_bytes(b"original\n")+ subprocess.run(+ ["git", "-c", "core.autocrlf=false", "apply", "--no-index", "-"],+ cwd=tmp_path,+ input=hunk + copy,+ check=True,+ )+ assert (tmp_path / "three").read_bytes() == (tmp_path / "two").read_bytes() == b"original\n"++[email protected]_platform+def test_generated_patch_rejects_gitlink_changes(tmp_path: Path, workbench_api) -> None:+ target = tmp_path / "repo"+ before = initialize_git_repository(target)++ def git(*args: str) -> bytes:+ return subprocess.check_output(["git", *args], cwd=target)++ git("update-index", "--add", "--cacheinfo", f"160000,{before},dependency")+ added = git("diff", "--cached")+ git("commit", "-qm", "Add gitlink")+ after = git("rev-parse", "HEAD").decode().strip()+ git("update-index", "--cacheinfo", f"160000,{after},dependency")+ modified = git("diff", "--cached")+ git("update-index", "--force-remove", "dependency")+ deleted = git("diff", "--cached")+ for patch in (added, modified, deleted, modified.replace(b" 160000\n", b" 0160000 \n")):+ with pytest.raises(SystemExit, match="cannot update Git submodules"):+ workbench_api["remediation_state"].require_valid_patch(target, patch)++[email protected]_platform[email protected]("style", ["log", "diff"])+def test_generated_patch_rejects_submodule_summaries(+ tmp_path: Path, workbench_api, style: str+) -> None:+ root = tmp_path / "repo"+ initialize_git_repository(root)+ target = root / "nested"+ target.mkdir()+ dep = target / "a dependency"+ initialize_git_repository(dep)++ def git(*args: str) -> bytes:+ return subprocess.check_output(["git", *args], cwd=target)++ validate = workbench_api["remediation_state"].require_valid_patch+ note = target / "note"+ prose = "Submodule dep 1234567..89abcde (rewind):"+ note.write_bytes(prose.encode() + b"\n")+ git("add", "note")+ git("commit", "-qm", "Add note", "-m", prose)+ # A commit message, signature and changed file can all quote a real summary.+ email = ("format-patch", "-1", "--stdout", "--relative", "--signature", prose)+ validate(target, git(*email))+ validate(target, git(*email, "--no-stat"))++ git("config", "diff.submodule", style)+ note.write_bytes(b"updated\n")+ git("add", "note", "a dependency")+ added = git("diff", "--cached", "--relative")+ assert b"(new submodule)" in added+ git("commit", "-qm", "Add dependency")+ mail = git(*email, f"--submodule={style}")+ mail_no_stat = git(*email, f"--submodule={style}", "--no-stat")+ assert mail.index(b"Submodule nested/a dependency") < mail.index(b"diff --git ")++ (dep / "README.md").write_bytes(b"new version\n")+ git("-C", str(dep), "commit", "-qam", "Update dependency")+ note.write_bytes(b"another update\n")+ moved = git("diff", "--relative")+ if style == "diff":+ assert b"diff --git a/nested/a dependency/README.md" in moved+ git("add", "a dependency")+ (dep / "README.md").write_bytes(b"worktree change\n")+ dirty = git("diff", "--relative")+ assert b"contains modified content" in dirty+ for patch in (added, mail, mail_no_stat, moved, dirty):+ with pytest.raises(SystemExit, match="cannot update Git submodules"):+ validate(target, patch)++[email protected]_platform[email protected]("object_format", ["sha1", "sha256"])+def test_generated_patch_preserves_deletions_and_uses_target_binary_ids(+ tmp_path: Path, workbench_api, object_format: str+) -> None:+ repo = tmp_path / object_format+ other_format = "sha256" if object_format == "sha1" else "sha1"+ other = tmp_path / other_format+ for directory in (repo, other):+ subprocess.run(+ ["git", "init", "-q", f"--object-format={directory.name}", directory], check=True+ )++ def git(*args: str) -> bytes:+ return subprocess.check_output(["git", *args], cwd=repo)++ (repo / "text").write_bytes(b"old\n")+ (repo / "empty").touch()+ (repo / "asset").write_bytes(b"\0old")+ git("add", ".")+ added = git("diff", "--cached", "--", "text")+ added_empty = git("diff", "--cached", "--abbrev=4", "--", "empty")+ (repo / "text").unlink()+ (repo / "empty").unlink()+ (repo / "asset").write_bytes(b"\0new")+ validate = workbench_api["remediation_state"].require_valid_patch+ for marker in (b"--- /dev/null", b"+++ b/text", b"@@ "):+ with pytest.raises(SystemExit, match="omits file content"):+ validate(repo, added.partition(marker)[0])+ with pytest.raises(SystemExit, match="omits file content"):+ validate(repo, git("diff", "--irreversible-delete", "--", "text"))+ text = git("diff", "--full-index", "--", "text")+ empty = git("diff", "--abbrev=4", "--", "empty")+ for target in (repo, other, tmp_path):+ validate(target, text)+ validate(target, empty)+ validate(target, added)+ validate(target, added_empty)++ binary = git("diff", "--binary", "--", "asset")+ forward_only = binary.partition(b"\n\n")[0] + b"\n\n"+ validate(repo, forward_only)+ (repo / "asset").write_bytes(b"\0old")+ subprocess.run(["git", "apply", "--no-index", "-"], cwd=repo, input=forward_only, check=True)+ assert (repo / "asset").read_bytes() == b"\0new"+ index = next(line for line in binary.splitlines() if line.startswith(b"index "))+ with pytest.raises(SystemExit, match="full object IDs"):+ validate(repo, binary.replace(index, b"index 1234567..89abcde 100644"))+ with pytest.raises(SystemExit, match="Git object format"):+ validate(other, binary)+ if object_format == "sha1":+ validate(tmp_path, binary)+ else:+ with pytest.raises(SystemExit, match="Git object format"):+ validate(tmp_path, binary)+++def pending_generation(tmp_path, monkeypatch):+ state, target = tmp_path / "state", tmp_path / "target"+ target.mkdir()+ (target / "source.txt").write_text("before\n")+ scan_id, scan_dir = start_saved_scan(state, target, tmp_path / "scans")+ write_completed_contract(scan_dir, scan_id, target, relative_path="source.txt")+ completed = scan_command(state, "complete-scan", scan_id)["scan"]+ occurrence = completed["findings"][0]["occurrenceId"]+ request = RemediationRequest(state, occurrence)+ token = str(uuid.uuid4())+ request.start(token)+ database = state / "workbench.sqlite3"+ patch = b"--- a/source.txt\n+++ b/source.txt\n@@ -1 +1 @@\n-before\n+after\n"+ monkeypatch.setattr(sys, "stdin", io.TextIOWrapper(io.BytesIO(patch)))+ args = argparse.Namespace(+ occurrence_id=occurrence,+ request_id=request.id,+ action_token=token,+ expected_version=1,+ state="generated",+ patch_stdin=True,+ patch_path=None,+ patch_digest=None,+ base_revision=None,+ summary=None,+ verification_summary=None,+ )+ return database, scan_dir, scan_id, args, patch++[email protected]_platform[email protected]("change_manifest", [False, True])+def test_generate_legacy_patch_validates_seal_before_taking_writer_lock(+ pending_generation, workbench_api, monkeypatch, change_manifest: bool+) -> None:+ database, scan_dir, scan_id, args, patch = pending_generation+ with sqlite3.connect(database) as connection:+ connection.execute("UPDATE scans SET seal_manifest_digest = NULL WHERE id = ?", (scan_id,))++ manifest = scan_dir / "scan-manifest.json"+ manifest.write_bytes(manifest.read_bytes() + b"\n")+ remediation = workbench_api["remediation_state"]+ read_seal = remediation._read_sealed_scan+ validated = []++ def read_without_writer_lock(*args):+ with sqlite3.connect(database, timeout=0) as writer:+ writer.execute("BEGIN IMMEDIATE")+ result = read_seal(*args)+ validated.append(args[0])+ if change_manifest:+ manifest.write_bytes(manifest.read_bytes() + b"\n")+ return result++ monkeypatch.setattr(remediation, "_read_sealed_scan", read_without_writer_lock)+ with sqlite3.connect(database) as connection:+ connection.row_factory = sqlite3.Row+ if change_manifest:+ with pytest.raises(SystemExit, match="sealed scan manifest changed"):+ workbench_api["set_finding_remediation"](connection, args)+ row = connection.execute(+ "SELECT state, version, patch_path FROM finding_remediation_attempts WHERE request_id = ?",+ (args.request_id,),+ ).fetchone()+ assert tuple(row) == ("requested", 1, None)+ assert not list(scan_dir.rglob("*.patch"))+ else:+ result = workbench_api["set_finding_remediation"](connection, args)+ relative = result["scan"]["findings"][0]["remediationState"]["patchPath"]+ assert (scan_dir / relative).read_bytes() == patch+ assert validated == [scan_dir]++[email protected]_platform[email protected]("scenario", ["new", "existing", "canceled", "canceled-after-publication"])+def test_generate_keeps_the_database_writer_available_during_patch_io(+ pending_generation, workbench_api, monkeypatch, scenario+):+ database, scan_dir, _, args, patch = pending_generation+ destination = (+ scan_dir+ / f"artifacts/remediation/{args.occurrence_id}/{args.request_id}/{args.action_token}.patch"+ )+ remediation = workbench_api["remediation_state"]+ acquired = []+ if scenario == "existing":+ destination.parent.mkdir(parents=True)+ destination.write_bytes(patch)+ if scenario in {"existing", "canceled-after-publication"}:+ verify = remediation.matching_patch_metadata++ def verify_with_writer(*verify_args):+ with sqlite3.connect(database, timeout=0) as other:+ other.row_factory = sqlite3.Row+ other.execute("BEGIN IMMEDIATE")+ acquired.append(scenario)+ result = verify(*verify_args)+ other.commit()+ if scenario == "canceled-after-publication":+ remediation.cancel_finding_remediation_request(other, args)+ return result++ monkeypatch.setattr(remediation, "matching_patch_metadata", verify_with_writer)+ else:+ stage = remediation.stage_scan_local_bytes++ @contextmanager+ def stage_with_writer(*stage_args, **options):+ with sqlite3.connect(database, timeout=0) as other:+ other.row_factory = sqlite3.Row+ other.execute("BEGIN IMMEDIATE")+ with stage(*stage_args, **options) as publish:+ acquired.append(scenario)+ other.commit()+ if scenario == "canceled":+ remediation.cancel_finding_remediation_request(other, args)+ yield publish++ monkeypatch.setattr(remediation, "stage_scan_local_bytes", stage_with_writer)+ with sqlite3.connect(database) as connection:+ connection.row_factory = sqlite3.Row+ if scenario.startswith("canceled"):+ with pytest.raises(SystemExit, match="remediation request changed"):+ workbench_api["set_finding_remediation"](connection, args)+ if scenario == "canceled":+ assert not destination.exists()+ else:+ assert destination.read_bytes() == patch+ assert not list(destination.parent.glob("*.tmp"))+ assert (+ connection.execute(+ "SELECT 1 FROM finding_remediation_attempts WHERE request_id = ?",+ (args.request_id,),+ ).fetchone()+ is None+ )+ else:+ result = workbench_api["set_finding_remediation"](connection, args)+ assert result["scan"]["findings"][0]["remediationState"]["state"] == "generated"+ assert destination.read_bytes() == patch+ assert acquired == [scenario]++[email protected]_platform[email protected]("matching", [False, True], ids=["different-patch", "identical-patch"])+def test_generate_preserves_patch_published_by_another_writer(+ pending_generation, workbench_api, monkeypatch, matching+):+ database, scan_dir, _, args, patch = pending_generation+ destination = (+ scan_dir+ / f"artifacts/remediation/{args.occurrence_id}/{args.request_id}/{args.action_token}.patch"+ )+ winner = patch if matching else patch.replace(b"+after", b"+another")+ token_hex = secrets.token_hex+ created = []++ def publish_before_temp_file(size):+ # Both backends allocate the temp name after checking the destination.+ with destination.open("xb") as output:+ output.write(winner)+ created.append(destination.stat())+ return token_hex(size)++ monkeypatch.setattr(secrets, "token_hex", publish_before_temp_file)+ with sqlite3.connect(database) as connection:+ connection.row_factory = sqlite3.Row+ if matching:+ result = workbench_api["set_finding_remediation"](connection, args)+ assert result["scan"]["findings"][0]["remediationState"]["state"] == "generated"+ else:+ with pytest.raises(SystemExit, match="Patch digest does not match"):+ workbench_api["set_finding_remediation"](connection, args)+ row = connection.execute(+ "SELECT state, version, patch_path FROM finding_remediation_attempts WHERE request_id = ?",+ (args.request_id,),+ ).fetchone()+ assert tuple(row) == ("requested", 1, None)+ assert len(created) == 1 and os.path.samestat(created[0], destination.stat())+ assert destination.read_bytes() == winner+ assert list(destination.parent.iterdir()) == [destination]++[email protected]_macos[email protected](os.name != "posix", reason="requires POSIX hard-link publication")+def test_generate_rejects_changed_patch_bytes_even_with_the_original_mtime(+ pending_generation, workbench_api, monkeypatch+):+ database, scan_dir, _, args, patch = pending_generation+ destination = (+ scan_dir+ / f"artifacts/remediation/{args.occurrence_id}/{args.request_id}/{args.action_token}.patch"+ )+ changed = patch.replace(b"+after", b"+other")+ link = os.link++ def overwrite_after_link(*args, **kwargs):+ link(*args, **kwargs)+ published = destination.stat()+ destination.write_bytes(changed)+ os.utime(destination, ns=(published.st_atime_ns, published.st_mtime_ns))++ monkeypatch.setattr(os, "link", overwrite_after_link)+ with sqlite3.connect(database) as connection:+ connection.row_factory = sqlite3.Row+ with pytest.raises(SystemExit, match="Patch digest does not match"):+ workbench_api["set_finding_remediation"](connection, args)+ row = connection.execute(+ "SELECT state, version, patch_path FROM finding_remediation_attempts WHERE request_id = ?",+ (args.request_id,),+ ).fetchone()+ assert tuple(row) == ("requested", 1, None)+ assert destination.read_bytes() == changed++[email protected]_macos[email protected](os.name != "posix", reason="POSIX directory fsync")+def test_generate_retries_syncing_patch_and_new_directories_before_commit(+ pending_generation, workbench_api, monkeypatch+):+ database, scan_dir, _, args, patch = pending_generation+ parent = scan_dir / "artifacts" / "remediation" / args.occurrence_id / args.request_id+ destination = parent / f"{args.action_token}.patch"+ fsync = os.fsync+ synced = []++ def fail_directory_sync(fd):+ metadata = os.fstat(fd)+ if destination.exists() and os.path.samestat(metadata, parent.stat()):+ raise OSError(errno.ENOSPC, "patch directory sync failed")+ fsync(fd)++ def record_sync(fd):+ fsync(fd)+ synced.append(os.fstat(fd))++ with sqlite3.connect(database) as connection:+ connection.row_factory = sqlite3.Row+ monkeypatch.setattr(os, "fsync", fail_directory_sync)+ with pytest.raises(SystemExit, match="patch directory sync failed"):+ workbench_api["set_finding_remediation"](connection, args)+ row = connection.execute(+ "SELECT state, version, patch_path FROM finding_remediation_attempts WHERE request_id = ?",+ (args.request_id,),+ ).fetchone()+ assert tuple(row) == ("requested", 1, None)+ assert destination.read_bytes() == patch++ monkeypatch.setattr(os, "fsync", record_sync)+ monkeypatch.setattr(sys, "stdin", io.TextIOWrapper(io.BytesIO(patch)))+ result = workbench_api["set_finding_remediation"](connection, args)+ assert result["scan"]["findings"][0]["remediationState"]["state"] == "generated"+ entry = destination+ while entry != scan_dir.parent:+ assert any(os.path.samestat(entry.stat(), info) for info in synced), entry+ entry = entry.parent++[email protected]_platform+def test_generate_requires_the_published_patch_at_its_recorded_path(+ pending_generation, workbench_api, monkeypatch+):+ database, scan_dir, _, args, patch = pending_generation+ remediation = workbench_api["remediation_state"]+ stage = remediation.stage_scan_local_bytes+ request_dir = scan_dir / "artifacts" / "remediation" / args.occurrence_id / args.request_id+ moved = request_dir.with_name("moved-request")++ @contextmanager+ def move_published_patch(*args, **kwargs):+ with stage(*args, **kwargs) as publish:+ yield publish+ request_dir.rename(moved)++ monkeypatch.setattr(remediation, "stage_scan_local_bytes", move_published_patch)+ with sqlite3.connect(database) as connection:+ connection.row_factory = sqlite3.Row+ with pytest.raises(SystemExit, match="scan-local regular file"):+ workbench_api["set_finding_remediation"](connection, args)+ row = connection.execute(+ "SELECT state, version, patch_path FROM finding_remediation_attempts WHERE request_id = ?",+ (args.request_id,),+ ).fetchone()+ assert tuple(row) == ("requested", 1, None)+ assert not request_dir.exists()+ assert (moved / f"{args.action_token}.patch").read_bytes() == patch++[email protected]("wrote_patch", [False, True], ids=["before-apply", "after-apply"])+def test_failed_remediation_steps_can_retry_or_regenerate(+ tmp_path: Path, wrote_patch: bool+) -> None:state_dir, target = empty_target_paths(tmp_path)source = target / "source.txt"source.write_text("vulnerable\n")@@ -38,14 +738,34 @@ def test_failed_remediation_steps_can_retry_or_regenerate(tmp_path: Path) -> Nonaction_token = str(uuid.uuid4())request.start(action_token)request.mark_delivered(action_token)+ destination = (+ scan_dir / f"artifacts/remediation/{occurrence_id}/{request.id}/{action_token}.patch"+ )+ generation = (action_token, "1", "generated", "--patch-stdin")+ proposed_patch = "--- a/source.txt\n+++ b/source.txt\n@@ -1 +1 @@\n-vulnerable\n+updated\n"+ for invalid_patch in ("", " \t\n", "Updated the source.\n", proposed_patch.removesuffix("\n")):+ rejected = request.update(*generation, input_text=invalid_patch, check=False)+ assert rejected["returncode"] != 0+ assert "Could not parse the remediation patch" in rejected["stderr"]+ assert not destination.exists()++ destination.parent.mkdir(parents=True, exist_ok=True)+ write_remediation_patch(destination)+ existing_patch = destination.read_bytes()+ rejected = request.update(*generation, input_text=proposed_patch, check=False)+ assert rejected["returncode"] != 0+ save_error = rejected["stderr"].strip()+ assert "Could not save the remediation patch" in save_error+ assert destination.read_bytes() == existing_patchfailed = update_remediation(request,action_token,1,"failed","--summary",- "Patch generation could not access the dependency registry.",+ save_error,)+ assert failed["summary"] == save_errorassert failed["pendingAction"] == "generate"assert failed["actionClaimToken"] is Noneassert failed["actionDeliveredAt"] is None@@ -94,34 +814,36 @@ def test_failed_remediation_steps_can_retry_or_regenerate(tmp_path: Path) -> Nonregenerated = request.start(action_token)["scan"]["findings"][0]["remediationState"]assert regenerated["state"] == "requested"- patch_path = scan_dir / "remediation.patch"- write_remediation_patch(patch_path)- patch_digest = f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}"- update_remediation(- request,+ no_newline_patch = proposed_patch + r"\ No newline at end of file"+ generated = request.update(action_token,- 1,+ "1","generated",- "--patch-path",- patch_path.name,- "--patch-digest",- patch_digest,- )+ "--patch-stdin",+ input_text=no_newline_patch,+ )["scan"]["findings"][0]["remediationState"]+ patch_path = scan_dir / generated["patchPath"]+ assert patch_path.read_text(encoding="utf-8") == no_newline_patchapply_token = str(uuid.uuid4())request.request_action("2", "apply", apply_token)- subprocess.run(["git", "apply", "--no-index", str(patch_path)], cwd=target, check=True)+ apply_command = ["git", "apply", "--no-index", str(patch_path)]+ if wrote_patch:+ subprocess.run(apply_command, cwd=target, check=True)failed = update_remediation(request,apply_token,3,"failed","--summary",- "Patch application was interrupted after changing the checkout.",+ "The Apply worker was interrupted.",)assert failed["pendingAction"] == "apply"apply_retry_token = str(uuid.uuid4())request.claim_resend(apply_retry_token)+ if not wrote_patch:+ assert source.read_text() == "vulnerable\n"+ subprocess.run(apply_command, cwd=target, check=True)applied = update_remediation(request, apply_retry_token, 4, "applied", "--base-revision", "unversioned")diff --git a/plugins/codex-security/tests/workbench_test_support.py b/plugins/codex-security/tests/workbench_test_support.pyindex c721c952d..3c8e1dd80 100644--- a/plugins/codex-security/tests/workbench_test_support.py+++ b/plugins/codex-security/tests/workbench_test_support.py@@ -522,7 +522,9 @@ class RemediationRequest:occurrence_id: strid: str = field(default_factory=lambda: str(uuid.uuid4()))- def _run(self, command: str, *args: str, check: bool = True) -> dict[str, object]:+ def _run(+ self, command: str, *args: str, check: bool = True, input_text: str | None = None+ ) -> dict[str, object]:return run_workbench(self.state_dir,command,@@ -532,13 +534,20 @@ class RemediationRequest:self.id,*args,check=check,+ input_text=input_text,)def start(self, token: str, *, check: bool = True) -> dict[str, object]:return self._run("request-finding-remediation", "--action-token", token, check=check)def update(- self, token: str, version: str, state: str, *args: str, check: bool = True+ self,+ token: str,+ version: str,+ state: str,+ *args: str,+ check: bool = True,+ input_text: str | None = None,) -> dict[str, object]:return self._run("set-finding-remediation",@@ -550,6 +559,7 @@ class RemediationRequest:state,*args,check=check,+ input_text=input_text,)def generate(@@ -953,10 +963,12 @@ def windows_file_backend() -> mock.Mock:payload: bytes,*,expected_root_identity: tuple[int, int] | None = None,+ replace_existing: bool = True,) -> None:path = scan_dir / relative_pathpath.parent.mkdir(parents=True, exist_ok=True)- path.write_bytes(payload)+ with path.open("wb" if replace_existing else "xb") as handle:+ handle.write(payload)def unlink_if_exists(scan_dir: Path, relative_path: str) -> None:(scan_dir / relative_path).unlink(missing_ok=True)diff --git a/sdk/typescript/tests-ts/fixtures/source_scopes.py b/sdk/typescript/tests-ts/fixtures/source_scopes.pyindex 9a99f9b85..ddf66cebc 100644--- a/sdk/typescript/tests-ts/fixtures/source_scopes.py+++ b/sdk/typescript/tests-ts/fixtures/source_scopes.py@@ -564,22 +564,17 @@ def boundaries(repository: Path) -> dict:result["removedDirectory"] = excerpt(selected, repository, "src/nested/child.py", ["src"])- calls = []- original = scopes.git_bytes-- def observed(target: Path, *arguments: str):- calls.append(- (- os.environ.get("GIT_NO_LAZY_FETCH"),- os.environ.get("GIT_ALLOW_PROTOCOL"),- os.environ.get("GIT_NO_REPLACE_OBJECTS"),- )- )- return original(target, *arguments)-- with patch.object(scopes, "git_bytes", observed):+ scopes.tree_entries.cache_clear()+ with patch.object(subprocess, "run", wraps=subprocess.run) as run:result["offline"] = excerpt(selected, repository, "src/public.py", ["src"])- assert calls and all(call == ("1", "", "1") for call in calls)+ for command in ("ls-tree", "cat-file"):+ reads = [call for call in run.call_args_list if command in call.args[0]]+ assert reads, command+ for call in reads:+ env = call.kwargs["env"]+ assert env["GIT_NO_LAZY_FETCH"] == "1"+ assert env["GIT_ALLOW_PROTOCOL"] == ""+ assert env["GIT_NO_REPLACE_OBJECTS"] == "1"with patch.object(excerpts, "offline_git_bytes", return_value=None):result["missingObject"] = excerpt(selected, repository, "src/public.py", ["src"]diff --git a/sdk/typescript/tests-ts/scan-recovery.test.ts b/sdk/typescript/tests-ts/scan-recovery.test.tsindex a1f8f6f22..54be7699d 100644--- a/sdk/typescript/tests-ts/scan-recovery.test.ts+++ b/sdk/typescript/tests-ts/scan-recovery.test.ts@@ -383,7 +383,7 @@ describe("scan publication and artifact recovery", () => {});}, 30_000);- test("builds each ordinary scan context once without losing selected findings", async () => {+ test("builds each scan context once without losing selected findings", async () => {const fixture = await startDraftScan();const findingsPath = join(fixture.scanDir, "findings.json");const document = await readJson<FindingsDocument>(findingsPath);@@ -429,7 +429,13 @@ describe("scan publication and artifact recovery", () => {" ordinary_calls = len(calls)"," calls.clear()"," selected = workbench.scan_context(connection, sys.argv[2], sys.argv[3])",- "print(json.dumps({'ordinaryCalls': ordinary_calls, 'selectedCalls': len(calls), 'ordinaryCount': len(ordinary['scan']['findings']), 'selectedCount': len(selected['scan']['findings']), 'workspaceCount': len(selected['workspace']['results']['findings']), 'selectedIncluded': any(finding['occurrenceId'] == sys.argv[3] for finding in selected['scan']['findings'])}))",+ "workspace_page = selected['workspace']['results']['findings']",+ "print(json.dumps({",+ " 'ordinaryCalls': ordinary_calls, 'selectedCalls': len(calls),",+ " 'ordinaryCount': len(ordinary['scan']['findings']), 'selectedCount': len(selected['scan']['findings']),",+ " 'workspaceCount': len(workspace_page), 'sameWorkspacePage': workspace_page == ordinary['workspace']['results']['findings'],",+ " 'selectedIncluded': any(finding['occurrenceId'] == sys.argv[3] for finding in selected['scan']['findings']),",+ "}))",].join("\n"),join(PLUGIN_ROOT, "scripts"),fixture.scanId,@@ -446,10 +452,11 @@ describe("scan publication and artifact recovery", () => {expect(probe.status, probe.stderr).toBe(0);expect(JSON.parse(probe.stdout)).toEqual({ordinaryCalls: 1,- selectedCalls: 2,+ selectedCalls: 1,ordinaryCount: 20,selectedCount: 21,workspaceCount: 20,+ sameWorkspacePage: true,selectedIncluded: true,});}, 30_000);