Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(deep-scan): recover publication after a lost reply (#938)

Michael D'Angelo29a45a556934
diff --git a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
index 79e25f089..bb9397b88 100644
--- a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
@@ -6,7 +6,7 @@ import {
import { isRecord as isObject } from "./record.js";
import { createHash, randomUUID } from "node:crypto";
import { promises as fs } from "node:fs";
-import { dirname, isAbsolute, join, posix, sep } from "node:path";
+import { basename, dirname, isAbsolute, join, posix, sep } from "node:path";
import { isDeepStrictEqual } from "node:util";
import * as z from "zod/v4";
import type { ArtifactContext } from "./artifact-context.js";
@@ -159,16 +159,18 @@ export async function recordCodexSecurityScanDraftViaWorkbench(
["drafts", `${randomUUID()}.json`],
"staged scan draft",
);
+ const acceptanceName = `${basename(draftPath, ".json")}.accepted.json`;
try {
const { handoffClaimToken: _claim, ...snapshot } = checkpoint;
+ const stagedDraft = {
+ ...draft,
+ ...(publication === undefined
+ ? {}
+ : { deepScanPublication: publication }),
+ };
await Promise.all([
replaceArtifactJson(checkpointPath, snapshot),
- replaceArtifactJson(draftPath, {
- ...draft,
- ...(publication === undefined
- ? {}
- : { deepScanPublication: publication }),
- }),
+ replaceArtifactJson(draftPath, stagedDraft),
]);
const arguments_ = [
"write-scan-draft",
@@ -185,22 +187,49 @@ export async function recordCodexSecurityScanDraftViaWorkbench(
if (context.handoffClaimToken) {
arguments_.push("--claim-token", context.handoffClaimToken);
}
- try {
+ const publish = async () => {
const result = await runWorkbench(arguments_);
- documentWarnings = Array.isArray(result?.warnings)
+ return Array.isArray(result?.warnings)
? result.warnings.filter(
(warning): warning is string => typeof warning === "string",
)
: undefined;
+ };
+ try {
+ documentWarnings = await publish();
} catch (error) {
- if (!workbenchScanDraftConflict(error)) throw error;
- signal?.throwIfAborted();
- continue;
+ const accepted =
+ context.mode === "deep" && publication !== undefined
+ ? await readArtifactJsonObject(
+ context,
+ ["drafts", acceptanceName],
+ "accepted scan draft",
+ ).catch(() => undefined)
+ : undefined;
+ if (
+ accepted?.status === "draft_written" &&
+ isDeepStrictEqual(
+ accepted.input,
+ JSON.parse(
+ JSON.stringify({ ...stagedDraft, checkpoint: snapshot }),
+ ),
+ )
+ ) {
+ signal?.throwIfAborted();
+ documentWarnings = await publish();
+ } else {
+ if (!workbenchScanDraftConflict(error)) throw error;
+ signal?.throwIfAborted();
+ continue;
+ }
}
} finally {
await Promise.all([
fs.rm(checkpointPath, { force: true }),
fs.rm(draftPath, { force: true }),
+ fs
+ .rm(join(dirname(draftPath), acceptanceName), { force: true })
+ .catch(() => {}),
]);
}
const warnings = [
diff --git a/plugins/codex-security/mcp-app/tests/clock_workbench.py b/plugins/codex-security/mcp-app/tests/clock_workbench.py
index 54c6e4475..5225c226d 100644
--- a/plugins/codex-security/mcp-app/tests/clock_workbench.py
+++ b/plugins/codex-security/mcp-app/tests/clock_workbench.py
@@ -1,5 +1,6 @@
"""Control the workbench clock while retaining its real commands and database writes."""
+import errno
import os
import runpy
import sys
@@ -12,4 +13,22 @@ api = runpy.run_path(str(source), run_name="test_workbench")
instant = os.environ["TEST_WORKBENCH_NOW"]
api["main"].__globals__["now"] = lambda: instant
api["_WORKBENCH_DB_CONTEXT"].now = lambda: instant
+if os.environ.get("TEST_WORKBENCH_RECEIPT_IO_FAILURE"):
+ if os.name == "nt":
+ from finalize_scan_contract import _windows_scan_local_files
+
+ backend = _windows_scan_local_files()
+ original_replace = backend._rename_handle
+ else:
+ original_replace = os.replace
+
+ def replace(source, destination, *args, **kwargs):
+ if str(destination).endswith(".accepted.json"):
+ raise OSError(errno.ENOSPC, "Synthetic receipt I/O failure", destination)
+ return original_replace(source, destination, *args, **kwargs)
+
+ if os.name == "nt":
+ backend._rename_handle = replace
+ else:
+ os.replace = replace
api["main"]()
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_publication_integration.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_publication_integration.ts
index 462e191cc..d9fd6c882 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_publication_integration.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_publication_integration.ts
@@ -739,6 +739,7 @@ async function createFixture(t: TestContext, paths: FixturePaths = {}) {
store,
runWorkbench,
instant,
+ environment,
setTime(offset: number) {
now = new Date(Date.parse(instant) + offset * 1_000).toISOString();
},
@@ -773,6 +774,7 @@ async function commitReducers(
kind: "discovery",
status: "running",
attempt: 1,
+ threadId: `session-${label}`,
...artifact,
};
await store.updateWorker(update);
@@ -799,6 +801,7 @@ async function commitReducers(
kind: "dedup",
status: "running",
attempt: 1,
+ threadId: `session-${label}`,
...artifact,
});
const resultManifestPath = path.join(artifact.artifactDir, "result.json");
@@ -895,3 +898,458 @@ function assertToolError(result: ToolResult, pattern?: RegExp) {
assert.equal(result.isError, true, JSON.stringify(result));
if (pattern) assert.match(JSON.stringify(result), pattern);
}
+
+async function publicationFixture(
+ t: TestContext,
+ workflow = "deep-scan-mcp/v1",
+) {
+ const fixture = await createFixture(t);
+ const { run, store, runWorkbench, environment } = fixture;
+ const database = path.join(
+ environment.CODEX_SECURITY_STATE_DIR,
+ "workbench.sqlite3",
+ );
+ const sql = async (script: string, ...args: string[]) =>
+ (
+ await execFileAsync(
+ python,
+ ["-c", script, database, run.scanId, ...args],
+ { env: environment },
+ )
+ ).stdout;
+ await sql(
+ `import sqlite3, sys
+with sqlite3.connect(sys.argv[1]) as c:
+ c.execute("UPDATE deep_scan_runs SET workflow_version = ? WHERE scan_id = ?", (sys.argv[3], sys.argv[2]))
+`,
+ workflow,
+ );
+ const claim = await store.claimCoordinator({
+ scanId: run.scanId,
+ threadId: owner,
+ });
+ assert.equal(claim.run.coordinatorGeneration, 2);
+ const results = await commitReducers(fixture, [1, 2], [highId, lowId]);
+ await mkdir(path.join(run.scanDir, "checkpoints"), { recursive: true });
+ const draft: ScanDraftInput = {
+ ...JSON.parse(await readFile(results[1], "utf8")),
+ coverage,
+ };
+ const context = await createScanArtifactContext(run.scanId, runWorkbench, {
+ requireRunning: true,
+ });
+ return {
+ ...fixture,
+ sql,
+ results,
+ draft,
+ publish: (runner = runWorkbench) =>
+ recordCodexSecurityScanDraftViaWorkbench(
+ context,
+ draft,
+ runner,
+ undefined,
+ { coordinatorGeneration: 2, resultPath: results[1] },
+ ),
+ workers: async (): Promise<PublicationWorkers> =>
+ JSON.parse(
+ await sql(`import json, sqlite3, sys
+c = sqlite3.connect(sys.argv[1]); c.row_factory = sqlite3.Row
+print(json.dumps({
+ "workflow": c.execute("SELECT workflow_version FROM deep_scan_runs WHERE scan_id = ?", (sys.argv[2],)).fetchone()[0],
+ "owner": dict(c.execute("SELECT workspace_id, deep_scan_owner_thread_id, continuation_thread_id, handoff_claim_token FROM scans WHERE id = ?", (sys.argv[2],)).fetchone()),
+ "workers": [dict(r) for r in c.execute("SELECT * FROM deep_scan_workers WHERE scan_id = ? ORDER BY id", (sys.argv[2],))],
+ "inputs": [dict(r) for r in c.execute("SELECT * FROM deep_scan_dedup_inputs WHERE dedup_worker_id IN (SELECT id FROM deep_scan_workers WHERE scan_id = ?) ORDER BY dedup_worker_id, discovery_worker_id", (sys.argv[2],))],
+}))`),
+ ),
+ };
+}
+
+type AcceptedPublicationFixture = Awaited<
+ ReturnType<typeof publicationFixture>
+>;
+interface PublicationPaths {
+ draftPath: string;
+ checkpointPath: string;
+}
+interface PublicationRequest {
+ args: string[];
+ input?: string | Buffer;
+ draft: string;
+ checkpoint: string;
+}
+interface PublicationHooks {
+ before?: (attempt: number, paths: PublicationPaths) => void | Promise<void>;
+ after?: (attempt: number, paths: PublicationPaths) => void | Promise<void>;
+}
+interface PublicationWorkers {
+ workflow: string;
+ owner: {
+ workspace_id: string | null;
+ deep_scan_owner_thread_id: string | null;
+ continuation_thread_id: string | null;
+ handoff_claim_token: string | null;
+ };
+ workers: Record<string, unknown>[];
+ inputs: Record<string, unknown>[];
+}
+function publicationRunner(
+ fixture: AcceptedPublicationFixture,
+ { before, after }: PublicationHooks = {},
+) {
+ const requests: PublicationRequest[] = [];
+ let writes = 0;
+ const run: RunArtifactWorkbench = async (args, input) => {
+ if (args[0] !== "write-scan-draft")
+ return fixture.runWorkbench(args, input);
+ const draftPath = args[args.indexOf("--draft-path") + 1];
+ const checkpointPath = args[args.indexOf("--checkpoint-path") + 1];
+ requests.push({
+ args: [...args],
+ input,
+ draft: await readFile(draftPath, "utf8"),
+ checkpoint: await readFile(checkpointPath, "utf8"),
+ });
+ await before?.(requests.length, { draftPath, checkpointPath });
+ const result = await fixture.runWorkbench(args, input);
+ writes++;
+ await after?.(requests.length, { draftPath, checkpointPath });
+ return result;
+ };
+ return { run, requests, writes: () => writes };
+}
+
+for (const workflow of ["deep-scan-mcp/v1", "deep-security-scan/v1"]) {
+ for (const [loseResponse, receiptIoFailure] of [
+ [false, false],
+ [true, false],
+ [false, true],
+ ] as const) {
+ test(`${workflow} finishes accepted publication (lost=${loseResponse}, receipt I/O failure=${receiptIoFailure})`, async (t) => {
+ const f = await publicationFixture(t, workflow);
+ if (receiptIoFailure)
+ f.environment.TEST_WORKBENCH_RECEIPT_IO_FAILURE = "1";
+ const before = await f.workers();
+ const sources = await Promise.all(
+ f.results.map((file) => readFile(file)),
+ );
+ let accepted: Awaited<ReturnType<typeof snapshot>> | undefined;
+ const runner = publicationRunner(f, {
+ after: async (attempt, { draftPath }) => {
+ if (attempt === 1) accepted = await snapshot(f.run);
+ else assert.deepEqual(await snapshot(f.run), accepted);
+ if (receiptIoFailure) {
+ await assert.rejects(
+ readFile(draftPath.replace(/\.json$/, ".accepted.json")),
+ { code: "ENOENT" },
+ );
+ }
+ if (loseResponse && attempt === 1) {
+ f.setTime(10);
+ throw new Error("Synthetic accepted publication response loss");
+ }
+ },
+ });
+ let publications = 0;
+ const coordinator = new DeepScanCoordinator({
+ run: await f.store.get(f.run.scanId, owner),
+ store: f.store,
+ pluginRoot,
+ executor: {
+ run() {
+ assert.fail("Publication recovery cannot dispatch workers");
+ },
+ },
+ clock: { now: () => Date.parse(f.instant), sleep: async () => {} },
+ onComplete: async (draft, signal, publication) => {
+ publications++;
+ assert.deepEqual(draft, f.draft);
+ assert.deepEqual(publication, {
+ coordinatorGeneration: 2,
+ resultPath: f.results[1],
+ });
+ const context = await createScanArtifactContext(
+ f.run.scanId,
+ runner.run,
+ { requireRunning: true },
+ );
+ await recordCodexSecurityScanDraftViaWorkbench(
+ context,
+ draft,
+ runner.run,
+ signal,
+ publication,
+ );
+ },
+ });
+ coordinator.start();
+ const terminal = await coordinator.settled();
+ assert.equal(
+ terminal.status,
+ "succeeded",
+ terminal.error ?? "publication failed",
+ );
+ assert.equal(terminal.terminalReason, "saturated");
+ assert.equal((await f.workers()).workflow, workflow);
+ assert.equal(terminal.coordinatorGeneration, 2);
+ assert.equal(publications, 1);
+ assert.equal(runner.requests.length, loseResponse ? 2 : 1);
+ assert.equal(runner.writes(), loseResponse ? 2 : 1);
+ if (loseResponse)
+ assert.deepEqual(runner.requests[1], runner.requests[0]);
+ assert.deepEqual(await f.workers(), before);
+ assert.deepEqual(
+ await Promise.all(f.results.map((file) => readFile(file))),
+ sources,
+ );
+ const parent = await f.runWorkbench([
+ "complete-scan",
+ "--scan-id",
+ f.run.scanId,
+ ]);
+ assert.equal(
+ (parent.scan as { progress: { status: string } }).progress.status,
+ "complete",
+ );
+ assert.deepEqual(await f.workers(), before);
+ assert.deepEqual(await readdir(path.join(f.run.scanDir, "drafts")), []);
+ });
+ }
+}
+
+test("accepted response recovery preserves an omitted threat model and the accepted bytes", async (t) => {
+ const f = await publicationFixture(t);
+ await f.publish();
+ f.setTime(5);
+ delete f.draft.threatModel;
+ let accepted: Awaited<ReturnType<typeof snapshot>> | undefined;
+ const runner = publicationRunner(f, {
+ async after(attempt) {
+ if (attempt === 1) {
+ accepted = await snapshot(f.run);
+ f.setTime(10);
+ throw new Error("Synthetic accepted publication response loss");
+ }
+ },
+ });
+ assert.equal((await f.publish(runner.run)).status, "draft_written");
+ assert.ok(runner.requests[0].args.includes("--expected-draft-digest"));
+ assert.equal(runner.requests.length, 2);
+ assert.deepEqual(runner.requests[1], runner.requests[0]);
+ assert.deepEqual(await snapshot(f.run), accepted);
+});
+
+test("accepted response recovery cannot replace a later publication from the same coordinator", async (t) => {
+ const f = await publicationFixture(t);
+ let newer: Awaited<ReturnType<typeof snapshot>> | undefined;
+ const runner = publicationRunner(f, {
+ async after(attempt) {
+ assert.equal(attempt, 1, "The superseded replay cannot publish");
+ f.setTime(10);
+ f.draft.threatModel = { summary: "Later accepted model" };
+ await f.publish();
+ newer = await snapshot(f.run);
+ throw new Error("Synthetic accepted publication response loss");
+ },
+ });
+ await assert.rejects(f.publish(runner.run), /canonical scan results changed/);
+ assert.equal(runner.requests.length, 2);
+ assert.deepEqual(await snapshot(f.run), newer);
+});
+
+for (const code of ["EPERM", "EACCES", "EIO"]) {
+ test(`optional receipt cleanup ${code} preserves publication success`, async (t) => {
+ const f = await publicationFixture(t);
+ const filesystem = await import("node:fs");
+ const original = filesystem.promises.rm;
+ t.mock.method(
+ filesystem.promises,
+ "rm",
+ async (...[file, options]: Parameters<typeof original>) => {
+ if (String(file).endsWith(".accepted.json"))
+ throw Object.assign(new Error(`Synthetic receipt cleanup ${code}`), {
+ code,
+ });
+ return original(file, options);
+ },
+ );
+ assert.equal((await f.publish()).status, "draft_written");
+ assert.ok(
+ JSON.parse((await snapshot(f.run)).files["scan-manifest.json"]).scan
+ .completedAt,
+ );
+ t.mock.restoreAll();
+ });
+}
+
+test("receipt I/O failure cannot authorize replay after response loss", async (t) => {
+ const f = await publicationFixture(t);
+ f.environment.TEST_WORKBENCH_RECEIPT_IO_FAILURE = "1";
+ const workers = await f.workers();
+ const lost = new Error("Synthetic accepted publication response loss");
+ let accepted: Awaited<ReturnType<typeof snapshot>> | undefined;
+ const runner = publicationRunner(f, {
+ after: async (_attempt, { draftPath }) => {
+ accepted = await snapshot(f.run);
+ await assert.rejects(
+ readFile(draftPath.replace(/\.json$/, ".accepted.json")),
+ { code: "ENOENT" },
+ );
+ throw lost;
+ },
+ });
+ await assert.rejects(f.publish(runner.run), (error) => error === lost);
+ assert.equal(runner.requests.length, 1);
+ assert.equal(runner.writes(), 1);
+ assert.deepEqual(await snapshot(f.run), accepted);
+ assert.deepEqual(await f.workers(), workers);
+ assert.deepEqual(await readdir(path.join(f.run.scanDir, "drafts")), []);
+});
+
+for (const code of ["EACCES", "ECONNRESET"]) {
+ for (const matching of [false, true]) {
+ test(`publication ${code} before acceptance is one attempt (matching=${matching})`, async (t) => {
+ const f = await publicationFixture(t);
+ if (matching) await f.publish();
+ const before = await snapshot(f.run);
+ const workers = await f.workers();
+ const original = Object.assign(new Error(`Synthetic precommit ${code}`), {
+ code,
+ });
+ const runner = publicationRunner(f, {
+ before() {
+ throw original;
+ },
+ });
+ await assert.rejects(
+ f.publish(runner.run),
+ (error) => error === original,
+ );
+ assert.equal(runner.requests.length, 1);
+ assert.equal(runner.writes(), 0);
+ assert.deepEqual(await snapshot(f.run), before);
+ assert.deepEqual(await f.workers(), workers);
+ assert.deepEqual(await readdir(path.join(f.run.scanDir, "drafts")), []);
+ });
+ }
+}
+
+for (const takeover of ["coordinator", "continuation"]) {
+ test(`accepted publication replay rejects a replacement ${takeover}`, async (t) => {
+ const f = await publicationFixture(t);
+ const workers = await f.workers();
+ let accepted: Awaited<ReturnType<typeof snapshot>> | undefined;
+ const runner = publicationRunner(f, {
+ after: async (attempt) => {
+ assert.equal(attempt, 1, "A stale replay must not commit");
+ accepted = await snapshot(f.run);
+ if (takeover === "coordinator") {
+ f.setTime(120);
+ const replacement = new WorkbenchDeepScanStore(f.runWorkbench);
+ const claim = await replacement.claimCoordinator({
+ scanId: f.run.scanId,
+ threadId: owner,
+ });
+ assert.equal(claim.acquired, true);
+ assert.equal(claim.run.coordinatorGeneration, 3);
+ } else {
+ await f.sql(`import sqlite3, sys
+with sqlite3.connect(sys.argv[1]) as c:
+ c.execute("UPDATE scans SET handoff_claim_token = ?, continuation_thread_id = ? WHERE id = ?", ("00000000-0000-4000-8000-000000000099", "new-owner", sys.argv[2]))
+`);
+ workers.owner.handoff_claim_token =
+ "00000000-0000-4000-8000-000000000099";
+ workers.owner.continuation_thread_id = "new-owner";
+ }
+ throw new Error("Synthetic response loss after takeover");
+ },
+ });
+ await assert.rejects(
+ f.publish(runner.run),
+ takeover === "coordinator" ? /newer generation/ : /another continuation/,
+ );
+ assert.equal(runner.requests.length, 2);
+ assert.equal(runner.writes(), 1);
+ assert.deepEqual(runner.requests[1], runner.requests[0]);
+ assert.deepEqual(await snapshot(f.run), accepted);
+ assert.deepEqual(await f.workers(), workers);
+ assert.deepEqual(await readdir(path.join(f.run.scanDir, "drafts")), []);
+ });
+}
+
+test("a failed accepted publication replay has no third attempt", async (t) => {
+ const f = await publicationFixture(t);
+ let accepted: Awaited<ReturnType<typeof snapshot>> | undefined;
+ const failure = new Error("Synthetic replay failure");
+ const runner = publicationRunner(f, {
+ before(attempt) {
+ if (attempt === 2) throw failure;
+ },
+ async after() {
+ accepted = await snapshot(f.run);
+ throw new Error("Synthetic accepted response loss");
+ },
+ });
+ await assert.rejects(f.publish(runner.run), (error) => error === failure);
+ assert.equal(runner.requests.length, 2);
+ assert.equal(runner.writes(), 1);
+ assert.deepEqual(runner.requests[1], runner.requests[0]);
+ assert.deepEqual(await snapshot(f.run), accepted);
+ assert.deepEqual(await readdir(path.join(f.run.scanDir, "drafts")), []);
+});
+
+test("raw publication repeats the same staged paths without changing their bytes", async (t) => {
+ const f = await publicationFixture(t);
+ const workers = await f.workers();
+ let calls = 0;
+ await f.publish(async (args, input) => {
+ assert.equal(
+ ++calls,
+ 1,
+ "Raw replay assertions must not trigger host recovery",
+ );
+ const stagedPaths = ["--draft-path", "--checkpoint-path"].map(
+ (flag) => args[args.indexOf(flag) + 1],
+ );
+ const staged = await Promise.all(stagedPaths.map((file) => readFile(file)));
+ const first = await f.runWorkbench(args, input);
+ const accepted = await snapshot(f.run);
+ assert.deepEqual(
+ await Promise.all(stagedPaths.map((file) => readFile(file))),
+ staged,
+ );
+ f.setTime(10);
+ assert.deepEqual(await f.runWorkbench(args, input), first);
+ assert.deepEqual(
+ await Promise.all(stagedPaths.map((file) => readFile(file))),
+ staged,
+ );
+ assert.deepEqual(await snapshot(f.run), accepted);
+ return first;
+ });
+ assert.deepEqual(await f.workers(), workers);
+ assert.deepEqual(await readdir(path.join(f.run.scanDir, "drafts")), []);
+});
+
+for (const changed of ["draftPath", "checkpointPath"] as const) {
+ test(`publication acknowledgment must match the original ${changed}`, async (t) => {
+ const f = await publicationFixture(t);
+ const lost = new Error(
+ "Synthetic response loss after different staged input",
+ );
+ const runner = publicationRunner(f, {
+ async before(attempt, paths) {
+ const document = JSON.parse(await readFile(paths[changed], "utf8"));
+ document.threatModel = { summary: "Different staged input" };
+ await writeFile(paths[changed], JSON.stringify(document));
+ },
+ after() {
+ throw lost;
+ },
+ });
+ await assert.rejects(f.publish(runner.run), (error) => error === lost);
+ assert.equal(runner.requests.length, 1);
+ assert.equal(runner.writes(), 1);
+ assert.deepEqual(await readdir(path.join(f.run.scanDir, "drafts")), []);
+ });
+}
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.py
index ae8fdbd34..1287f6777 100644
--- a/plugins/codex-security/scripts/finalize_scan_contract.py
+++ b/plugins/codex-security/scripts/finalize_scan_contract.py
@@ -97,6 +97,10 @@ class ContractError(ValueError):
"""Raised when a completed scan does not satisfy the additive contract."""
+class ScanLocalIOError(ContractError):
+ """Raised when scan-local storage I/O fails, distinct from path validation."""
+
+
class MissingScanLocalFileError(ContractError):
"""Raised when a scan-local file or its containing directory is absent."""
@@ -452,18 +456,28 @@ def _windows_unsafe_path_component(value: str) -> bool:
return WINDOWS_UNSAFE_PATH_COMPONENT_RE.search(value) is not None
+def _scan_local_directory_error(message: str, error: OSError) -> ContractError:
+ if error.errno in {errno.ENOENT, errno.ENOTDIR, errno.ELOOP}:
+ return ContractError(message)
+ return ScanLocalIOError(message)
+
+
def _require_scan_directory(scan_dir: Path) -> Path:
scan_dir = scan_dir.absolute()
try:
metadata = scan_dir.lstat()
except OSError as exc:
- raise ContractError("scan directory: expected an existing non-symlink directory") from exc
+ raise _scan_local_directory_error(
+ "scan directory: expected an existing non-symlink directory", exc
+ ) from exc
if not stat.S_ISDIR(metadata.st_mode):
raise ContractError("scan directory: expected an existing non-symlink directory")
try:
resolved = scan_dir.resolve(strict=True)
except OSError as exc:
- raise ContractError("scan directory: expected an existing non-symlink directory") from exc
+ raise _scan_local_directory_error(
+ "scan directory: expected an existing non-symlink directory", exc
+ ) from exc
if os.path.normcase(resolved) != os.path.normcase(scan_dir):
raise ContractError("scan directory: expected a canonical non-symlink directory")
return resolved
@@ -518,7 +532,9 @@ def _open_verified_scan_directory(
os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0),
)
except OSError as exc:
- raise ContractError("scan directory: expected an existing non-symlink directory") from exc
+ 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 (
@@ -770,15 +786,18 @@ def write_scan_local_bytes(
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:
- _windows_scan_local_files().atomic_write(
+ backend.atomic_write(
scan_dir,
relative_path,
payload,
expected_root_identity=expected_root_identity,
)
- except OSError as exc:
+ 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
return
root_fd: int | None = None
parent_fd: int | None = None
@@ -789,8 +808,8 @@ def write_scan_local_bytes(
try:
parent_fd = _open_scan_local_directory(root_fd, parts[:-1], create=True)
except OSError as exc:
- raise ContractError(
- f"{relative_path}: expected a path inside the scan directory"
+ 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
diff --git a/plugins/codex-security/scripts/windows_scan_local_files.py b/plugins/codex-security/scripts/windows_scan_local_files.py
index 0bcdca618..273391b55 100644
--- a/plugins/codex-security/scripts/windows_scan_local_files.py
+++ b/plugins/codex-security/scripts/windows_scan_local_files.py
@@ -40,6 +40,10 @@ class WindowsScanLocalFileError(OSError):
"""Raised when a scan-local operation cannot be completed securely."""
+class WindowsScanLocalPathError(WindowsScanLocalFileError):
+ """Raised when a scan-local path fails an integrity check."""
+
+
# CreateFile access and sharing flags.
_DELETE = 0x00010000
_FILE_READ_ATTRIBUTES = 0x00000080
@@ -73,6 +77,7 @@ _ERROR_LOCK_VIOLATION = 33
_ERROR_FILE_EXISTS = 80
_ERROR_INSUFFICIENT_BUFFER = 122
_ERROR_ALREADY_EXISTS = 183
+_ERROR_DIRECTORY = 267
_DACL_SECURITY_INFORMATION = 0x00000004
_MISSING_ERRORS = {_ERROR_FILE_NOT_FOUND, _ERROR_PATH_NOT_FOUND}
_COLLISION_ERRORS = {_ERROR_FILE_EXISTS, _ERROR_ALREADY_EXISTS}
@@ -261,8 +266,8 @@ def _raise_last_error(operation: str, path: Path | None = None) -> None:
raise WindowsScanLocalFileError(error, f"{operation}{target}: {detail}", str(path or ""))
-def _invalid_path(path: Path | str, reason: str) -> WindowsScanLocalFileError:
- return WindowsScanLocalFileError(errno.EINVAL, reason, str(path))
+def _invalid_path(path: Path | str, reason: str) -> WindowsScanLocalPathError:
+ return WindowsScanLocalPathError(errno.EINVAL, reason, str(path))
def _validated_parts(relative_path: str) -> tuple[str, ...]:
@@ -429,21 +434,32 @@ def _canonical_scan_directory(scan_dir: Path) -> tuple[Path, tuple[int, int]]:
expected = absolute.lstat()
canonical = absolute.resolve(strict=True)
except OSError as exc:
+ if exc.errno not in {errno.ENOENT, errno.ENOTDIR, errno.ELOOP}:
+ raise
raise _invalid_path(scan_dir, "expected an existing scan directory") from exc
if _normalized_windows_path(absolute) != _normalized_windows_path(canonical):
raise _invalid_path(scan_dir, "scan directory must be canonical and non-reparse")
return canonical, (expected.st_dev, expected.st_ino)
-def _open_directory(path: Path, *, missing_ok: bool = False) -> _OwnedHandle | None:
- handle = _create_file(
- path,
- access=_FILE_READ_ATTRIBUTES,
- share=_FILE_SHARE_READ | _FILE_SHARE_WRITE,
- disposition=_OPEN_EXISTING,
- flags=_FILE_FLAG_BACKUP_SEMANTICS | _FILE_FLAG_OPEN_REPARSE_POINT,
- missing_ok=missing_ok,
- )
+def _open_directory(
+ path: Path, *, missing_ok: bool = False, missing_is_path_error: bool = True
+) -> _OwnedHandle | None:
+ try:
+ handle = _create_file(
+ path,
+ access=_FILE_READ_ATTRIBUTES,
+ share=_FILE_SHARE_READ | _FILE_SHARE_WRITE,
+ disposition=_OPEN_EXISTING,
+ flags=_FILE_FLAG_BACKUP_SEMANTICS | _FILE_FLAG_OPEN_REPARSE_POINT,
+ missing_ok=missing_ok,
+ )
+ except WindowsScanLocalFileError as exc:
+ if exc.errno != _ERROR_DIRECTORY and not (
+ missing_is_path_error and exc.errno in _MISSING_ERRORS
+ ):
+ raise
+ raise _invalid_path(path, f"could not open scan-local directory: {exc}") from exc
if handle is not None:
try:
assert handle.value is not None
@@ -489,6 +505,7 @@ def _locked_parent(
relative_path: str,
*,
create: bool,
+ missing_is_path_error: bool = True,
expected_root_identity: tuple[int, int] | None = None,
) -> Iterator[tuple[Path, str]]:
"""Hold non-deletable handles for every directory in the absolute target path."""
@@ -507,7 +524,14 @@ def _locked_parent(
directory_handle = _open_directory(directory_path)
assert directory_handle is not None
handles.append(directory_handle)
- current_root = root_path.lstat()
+ try:
+ current_root = root_path.lstat()
+ except OSError as exc:
+ if exc.errno not in {errno.ENOENT, errno.ENOTDIR, errno.ELOOP}:
+ raise
+ raise _invalid_path(
+ scan_dir, f"scan directory changed while it was being opened: {exc}"
+ ) from exc
current_root_identity = (current_root.st_dev, current_root.st_ino)
if current_root_identity != observed_root_identity or (
expected_root_identity is not None and current_root_identity != expected_root_identity
@@ -516,7 +540,9 @@ def _locked_parent(
current_path = root_path
for component in parts[:-1]:
current_path /= component
- child_handle = _open_directory(current_path, missing_ok=create)
+ child_handle = _open_directory(
+ current_path, missing_ok=create, missing_is_path_error=missing_is_path_error
+ )
if child_handle is None:
_create_directory(current_path)
child_handle = _open_directory(current_path)
@@ -546,7 +572,10 @@ def open_read_fd_with_path(scan_dir: Path, relative_path: str, context: str) ->
"""Return the descriptor and its filename while the verified parents are held."""
try:
- with _locked_parent(scan_dir, relative_path, create=False) as (parent_path, leaf_name):
+ with _locked_parent(scan_dir, relative_path, create=False, missing_is_path_error=False) as (
+ parent_path,
+ leaf_name,
+ ):
path = parent_path / leaf_name
handle = _create_file(
path,
diff --git a/plugins/codex-security/scripts/workbench_saved_results.py b/plugins/codex-security/scripts/workbench_saved_results.py
index 825a5cf0e..6d3abc7f8 100644
--- a/plugins/codex-security/scripts/workbench_saved_results.py
+++ b/plugins/codex-security/scripts/workbench_saved_results.py
@@ -20,6 +20,7 @@ from typing import Any
sys.path.insert(0, str(Path(__file__).resolve().parent))
from finalize_scan_contract import (
ContractError,
+ ScanLocalIOError,
_finding_strength,
_populate_unsealed_artifact_envelope,
_populate_unsealed_manifest_envelope,
@@ -2895,13 +2896,6 @@ def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]:
"The scan stopped; its saved checkpoint was retained without replacing sealed results."
)
scan_dir = db.require_canonical_scan_directory(Path(scan["scan_dir"]))
- if (
- args.expected_draft_digest is not None
- and args.expected_draft_digest != _scan_draft_digest(scan_dir)
- ):
- raise SystemExit(
- "scan_draft_conflict: canonical scan results changed; reconcile the saved checkpoint again."
- )
try:
relative = Path(args.draft_path).relative_to(scan_dir).as_posix()
except ValueError as exc:
@@ -2911,6 +2905,8 @@ def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]:
if not re.fullmatch(r"drafts/[0-9a-fA-F-]+\.json", relative):
raise SystemExit("Scan draft must be inside the registered scan drafts directory.")
draft = _read_scan_local_json(scan_dir, relative, "Staged scan draft")
+ accepted_input = copy.deepcopy(draft)
+ accepted_checkpoint = None
manifest, findings, coverage = draft["manifest"], draft["findings"], draft["coverage"]
binding = db.workbench_completion_binding(scan, db.now())
# Save scan IDs without sealing the draft.
@@ -2933,6 +2929,7 @@ def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]:
checkpoint, checkpoint_contents = _read_scan_local_json_bytes(
scan_dir, checkpoint_relative, "Staged scan checkpoint"
)
+ accepted_checkpoint = checkpoint
if checkpoint.get("scanId") != scan_id:
raise SystemExit("Staged scan checkpoint belongs to another scan.")
checkpoint_only = (
@@ -2948,6 +2945,36 @@ def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]:
)
if scan["mode"] == "deep" and not checkpoint_only:
_require_current_deep_publication(db, connection, scan, draft)
+ acceptance_input = {**accepted_input, "checkpoint": accepted_checkpoint}
+ acceptance_relative = Path(relative).with_suffix(".accepted.json").as_posix()
+ acknowledge = (
+ scan["mode"] == "deep"
+ and draft.get("deepScanPublication") is not None
+ and db.deep_scan.require_deep_scan_run(connection, scan_id)["workflow_version"]
+ in {"deep-scan-mcp/v1", "deep-security-scan/v1"}
+ )
+ if acknowledge and (scan_dir / acceptance_relative).exists():
+ accepted = _read_scan_local_json(scan_dir, acceptance_relative, "Accepted scan draft")
+ if (
+ accepted.get("status") == "draft_written"
+ and accepted.get("input") == acceptance_input
+ ):
+ if accepted.get("draftDigest") != _scan_draft_digest(scan_dir):
+ raise SystemExit(
+ "scan_draft_conflict: canonical scan results changed after publication."
+ )
+ return {
+ "scanId": scan_id,
+ "status": "draft_written",
+ **({"warnings": accepted["warnings"]} if accepted.get("warnings") else {}),
+ }
+ if (
+ args.expected_draft_digest is not None
+ and args.expected_draft_digest != _scan_draft_digest(scan_dir)
+ ):
+ raise SystemExit(
+ "scan_draft_conflict: canonical scan results changed; reconcile the saved checkpoint again."
+ )
if checkpoint_contents is not None:
checkpoint_digest = hashlib.sha256(checkpoint_contents).hexdigest()
write_scan_local_bytes(
@@ -2968,16 +2995,16 @@ def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]:
write_scan_local_bytes(
scan_dir, "checkpoint-head.json", _encoded({"checkpoint": checkpoint_name})
)
- for filename, document in (
- ("findings.json", findings),
- ("coverage.json", coverage),
- ("scan-manifest.json", manifest),
- ):
- write_scan_local_bytes(
- scan_dir,
- filename,
- (json.dumps(document, allow_nan=False, indent=2) + "\n").encode(),
+ documents = {
+ filename: (json.dumps(document, allow_nan=False, indent=2) + "\n").encode()
+ for filename, document in (
+ ("findings.json", findings),
+ ("coverage.json", coverage),
+ ("scan-manifest.json", manifest),
)
+ }
+ for filename, contents in documents.items():
+ write_scan_local_bytes(scan_dir, filename, contents)
if scan["mode"] == "deep" and manifest["scan"].get("complete") is not False:
# Select the accepted final publication before releasing the write lock.
with connection:
@@ -2987,6 +3014,19 @@ def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]:
(str(scan_dir / "scan-manifest.json"), scan_id),
)
model_warning = write_threat_model_projection_if_possible(scan_dir, manifest)
+ if acknowledge:
+ # Optional recovery evidence is written only after the publication succeeds.
+ try:
+ acceptance = {
+ "status": "draft_written",
+ "input": acceptance_input,
+ "draftDigest": _scan_draft_contents_digest(documents),
+ **({"warnings": [model_warning]} if model_warning else {}),
+ }
+ write_scan_local_bytes(scan_dir, acceptance_relative, _encoded(acceptance))
+ except (OSError, ScanLocalIOError):
+ # A missing receipt prevents recovery but does not undo accepted results.
+ pass
# Accepted Standard drafts are evidence of review or report assembly,
# even when the parent omitted its explicit progress call.
model_only_checkpoint = (
@@ -3025,20 +3065,31 @@ def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]:
def _scan_draft_digest(scan_dir: Path) -> str:
- digest = hashlib.sha256()
+ documents: dict[str, bytes | None] = {}
for filename in ("scan-manifest.json", "findings.json", "coverage.json"):
- digest.update(filename.encode())
- digest.update(b"\0")
try:
(scan_dir / filename).lstat()
except FileNotFoundError:
- digest.update(b"missing\0")
+ documents[filename] = None
continue
- digest.update(b"present\0")
descriptor = open_scan_local_file_descriptor(scan_dir, filename, filename)
with os.fdopen(descriptor, "rb") as handle:
- digest.update(handle.read())
+ documents[filename] = handle.read()
+ return _scan_draft_contents_digest(documents)
+
+
+def _scan_draft_contents_digest(documents: dict[str, bytes | None]) -> str:
+ digest = hashlib.sha256()
+ for filename in ("scan-manifest.json", "findings.json", "coverage.json"):
+ digest.update(filename.encode())
digest.update(b"\0")
+ contents = documents[filename]
+ if contents is None:
+ digest.update(b"missing\0")
+ else:
+ digest.update(b"present\0")
+ digest.update(contents)
+ digest.update(b"\0")
return digest.hexdigest()
diff --git a/plugins/codex-security/tests/test_deep_scan_publication_authority.py b/plugins/codex-security/tests/test_deep_scan_publication_authority.py
index 0a8a6de62..b8ccb096c 100644
--- a/plugins/codex-security/tests/test_deep_scan_publication_authority.py
+++ b/plugins/codex-security/tests/test_deep_scan_publication_authority.py
@@ -1,9 +1,15 @@
from __future__ import annotations
import copy
+import errno
import json
+import os
+import sys
import uuid
from argparse import Namespace
+from contextlib import ExitStack
+from pathlib import Path
+from types import SimpleNamespace
import pytest
from test_deep_scan_successful_publication import add_worker, complete
@@ -212,3 +218,495 @@ def test_legacy_terminal_aggregate_survives_a_late_partial_draft_after_write_fai
json.loads((scan.scan_dir / "coverage.json").read_text())["deferred"]
== scan.coverage["deferred"]
)
+
+
+ "failure",
+ ["validation", "findings.json", "coverage.json", "scan-manifest.json"]
+ + [
+ f"windows-emulated:{name}"
+ for name in ("findings.json", "coverage.json", "scan-manifest.json")
+ ],
+)
+def test_failed_publication_does_not_acknowledge_staged_input(
+ workbench_api, workbench_db, publication_scan, monkeypatch, failure
+):
+ scan = publication_scan()
+ result = add_worker(workbench_db, scan)
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE deep_scan_runs SET workflow_version = 'deep-scan-mcp/v1', "
+ "coordinator_generation = 2 WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ workbench_db.execute(
+ "UPDATE deep_scan_workers SET kind = 'dedup', merge_state = 'none' WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ args = stage_publication(scan, generation=2, result_path=result, title="Accepted aggregate")
+ staged = {path: path.read_bytes() for path in (scan.scan_dir / "drafts").iterdir()}
+ saved_results = workbench_api["saved_results"]
+ original_write = saved_results.write_scan_local_bytes
+
+ def fail_validation(*args):
+ raise OSError("Synthetic validation failure")
+
+ def fail_write(scan_dir, filename, contents):
+ if filename == failure:
+ raise OSError("Synthetic canonical write failure")
+ if failure == f"windows-emulated:{filename}":
+ with monkeypatch.context() as patch, ExitStack() as files:
+ finalizer = sys.modules[original_write.__module__]
+ backend = emulate_windows_atomic_write(patch, finalizer, files)
+ patch.setattr(backend, "_rename_handle", fail_validation)
+ return original_write(scan_dir, filename, contents)
+ return original_write(scan_dir, filename, contents)
+
+ if failure == "validation":
+ monkeypatch.setattr(saved_results, "_validate_completion_binding", fail_validation)
+ else:
+ monkeypatch.setattr(saved_results, "write_scan_local_bytes", fail_write)
+
+ expected_error = (
+ saved_results.ContractError if failure.startswith("windows-emulated:") else OSError
+ )
+ with pytest.raises(expected_error, match="Synthetic"):
+ workbench_api["saved_results"].write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, args
+ )
+
+ assert {path: path.read_bytes() for path in (scan.scan_dir / "drafts").iterdir()} == staged
+
+
+def emulate_windows_atomic_write(monkeypatch, finalizer, files, *, reparse_point=False):
+ """Run the real Windows atomic writer with emulated handles, not native Win32 I/O."""
+ backend = finalizer._windows_scan_local_files()
+ paths = {}
+ streams = {}
+ pending_deletions = set()
+ next_directory_handle = -1
+
+ def create_file(path, **kwargs):
+ nonlocal next_directory_handle
+ if kwargs["disposition"] == backend._OPEN_EXISTING:
+ if kwargs.get("missing_ok") and not path.exists():
+ return None
+ descriptor = next_directory_handle
+ next_directory_handle -= 1
+ else:
+ stream = open(path, "xb", opener=lambda path, flags: os.open(path, flags, 0o600))
+ try:
+ descriptor = stream.fileno()
+ streams[descriptor] = files.enter_context(stream)
+ except BaseException:
+ stream.close()
+ raise
+ paths[descriptor] = path
+ return backend._OwnedHandle(descriptor)
+
+ def rename_handle(handle, destination):
+ os.replace(paths[handle], destination)
+ paths[handle] = destination
+
+ def close_handle(handle):
+ if handle >= 0:
+ streams[handle].close()
+ if handle in pending_deletions:
+ paths[handle].unlink()
+
+ def attributes(handle):
+ path = paths[handle]
+ flags = backend._FILE_ATTRIBUTE_DIRECTORY if path.is_dir() else 0
+ if path.is_symlink() or (reparse_point and not path.is_dir()):
+ flags |= backend._FILE_ATTRIBUTE_REPARSE_POINT
+ return SimpleNamespace(FileAttributes=flags)
+
+ monkeypatch.setattr(finalizer, "_descriptor_relative_writes_available", lambda: False)
+ monkeypatch.setattr(finalizer, "_is_windows", lambda: True)
+ monkeypatch.setattr(backend, "_require_windows", lambda: None)
+ monkeypatch.setattr(backend, "_validate_existing_output", lambda path: None)
+ monkeypatch.setattr(backend, "_create_file", create_file)
+ 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]))
+ monkeypatch.setattr(backend, "_write_all", os.write)
+ monkeypatch.setattr(backend, "_rename_handle", rename_handle)
+ monkeypatch.setattr(backend, "_mark_handle_for_deletion", pending_deletions.add)
+ return backend
+
+
[email protected]("workflow", ["deep-scan-mcp/v1", "deep-security-scan/v1"])
+ ("backend_kind", "failure"),
+ [("host", failure) for failure in ("write", "rename")]
+ + [("posix", failure) for failure in ("root-stat", "root-resolve", "root-open", "parent-open")]
+ + [("windows-emulated", failure) for failure in ("create", "write", "rename")],
+)
+def test_receipt_io_failure_preserves_successful_publication(
+ workbench_api, workbench_db, publication_scan, monkeypatch, workflow, backend_kind, failure
+):
+ if backend_kind == "posix" and os.name == "nt":
+ pytest.skip("POSIX descriptor-relative directory errors")
+ scan = publication_scan()
+ result = add_worker(workbench_db, scan)
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE deep_scan_runs SET workflow_version = ?, coordinator_generation = 2 "
+ "WHERE scan_id = ?",
+ (workflow, scan.scan_id),
+ )
+ workbench_db.execute(
+ "UPDATE deep_scan_workers SET kind = 'dedup', merge_state = 'none' WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ args = stage_publication(scan, generation=2, result_path=result, title="Accepted aggregate")
+ draft = json.loads(Path(args.draft_path).read_text())
+ instant = workbench_api["now"]()
+ monkeypatch.setattr(workbench_api["_WORKBENCH_DB_CONTEXT"], "now", lambda: instant)
+ scan_row = workbench_db.execute("SELECT * FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()
+ binding = workbench_api["workbench_completion_binding"](scan_row, instant)
+ saved = workbench_api["saved_results"]
+ saved._populate_unsealed_manifest_envelope(
+ draft["manifest"], draft["manifest"]["scan"], binding
+ )
+ saved._populate_unsealed_artifact_envelope(
+ draft["manifest"], draft["findings"], draft["coverage"], binding
+ )
+ staged = {path: path.read_bytes() for path in (scan.scan_dir / "drafts").iterdir()}
+ saved_results = workbench_api["saved_results"]
+ original_write = saved_results.write_scan_local_bytes
+ original_replace = os.replace
+ failures = []
+
+ def fail_receipt(filename):
+ failures.append(filename)
+ # The fault occurs only after all three canonical files have been published.
+ for name, document in (
+ ("findings.json", draft["findings"]),
+ ("coverage.json", draft["coverage"]),
+ ("scan-manifest.json", draft["manifest"]),
+ ):
+ assert json.loads((scan.scan_dir / name).read_text()) == document
+ raise OSError(
+ errno.EIO if backend_kind == "posix" else errno.ENOSPC,
+ "Synthetic receipt I/O failure",
+ filename,
+ )
+
+ def write_file(scan_dir, filename, contents):
+ if not filename.endswith(".accepted.json"):
+ return original_write(scan_dir, filename, contents)
+ with monkeypatch.context() as patch, ExitStack() as files:
+ finalizer = sys.modules[original_write.__module__]
+ if backend_kind == "posix":
+ original_open = os.open
+ original_stat = Path.lstat
+ original_resolve = Path.resolve
+
+ def open_directory(file, flags, *args, **kwargs):
+ if (failure == "root-open" and Path(file) == scan_dir) or (
+ failure == "parent-open" and file == "drafts"
+ ):
+ fail_receipt(filename)
+ return original_open(file, flags, *args, **kwargs)
+
+ def stat_root(path, *args, **kwargs):
+ if path == scan_dir:
+ fail_receipt(filename)
+ return original_stat(path, *args, **kwargs)
+
+ def resolve_root(path, *args, **kwargs):
+ if path == scan_dir:
+ fail_receipt(filename)
+ return original_resolve(path, *args, **kwargs)
+
+ if failure == "root-stat":
+ patch.setattr(Path, "lstat", stat_root)
+ elif failure == "root-resolve":
+ patch.setattr(Path, "resolve", resolve_root)
+ else:
+ patch.setattr(os, "open", open_directory)
+ patch.setattr(os, "supports_dir_fd", {*os.supports_dir_fd, open_directory})
+ return original_write(scan_dir, filename, contents)
+ if backend_kind == "windows-emulated":
+ backend = emulate_windows_atomic_write(patch, finalizer, files)
+ elif os.name == "nt":
+ backend = finalizer._windows_scan_local_files()
+ else:
+ if failure == "write":
+ fail_receipt(filename)
+ return original_write(scan_dir, filename, contents)
+
+ def fail_backend(*args, **kwargs):
+ fail_receipt(filename)
+
+ operation = {
+ "create": "_create_file",
+ "write": "_write_all",
+ "rename": "_rename_handle",
+ }
+ patch.setattr(backend, operation[failure], fail_backend)
+ return original_write(scan_dir, filename, contents)
+
+ def replace_file(source, destination, *args, **kwargs):
+ if failure == "rename" and str(destination).endswith(".accepted.json"):
+ fail_receipt(destination)
+ return original_replace(source, destination, *args, **kwargs)
+
+ monkeypatch.setattr(saved_results, "write_scan_local_bytes", write_file)
+ monkeypatch.setattr(os, "replace", replace_file)
+
+ assert workbench_api["saved_results"].write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, args
+ ) == {
+ "scanId": scan.scan_id,
+ "status": "draft_written",
+ }
+ assert len(failures) == 1
+ assert {path: path.read_bytes() for path in (scan.scan_dir / "drafts").iterdir()} == staged
+ assert len(list((scan.scan_dir / "checkpoints").glob("*.json"))) == 2
+
+
[email protected]("workflow", ["deep-scan-mcp/v1", "deep-security-scan/v1"])
+ ("operation", "failure"),
+ [
+ (operation, failure)
+ for operation in ("lstat", "resolve", "locked-lstat")
+ for failure in ("io", "handles", "denied", "missing", "not-directory", "loop")
+ ]
+ + [
+ (operation, failure)
+ for operation in ("ancestor-open", "root-open")
+ for failure in ("io", "handles", "denied", "missing", "missing-parent", "not-directory")
+ ],
+)
+def test_windows_receipt_root_probe_classifies_storage_and_path_failures(
+ workbench_api,
+ workbench_db,
+ publication_scan,
+ monkeypatch,
+ tmp_path,
+ workflow,
+ operation,
+ failure,
+):
+ scan = publication_scan()
+ result = add_worker(workbench_db, scan)
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE deep_scan_runs SET workflow_version = ?, coordinator_generation = 2 "
+ "WHERE scan_id = ?",
+ (workflow, scan.scan_id),
+ )
+ workbench_db.execute(
+ "UPDATE deep_scan_workers SET kind = 'dedup', merge_state = 'none' WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ args = stage_publication(scan, generation=2, result_path=result, title="Accepted aggregate")
+ bad_root = tmp_path / "invalid-root"
+ if failure == "not-directory":
+ bad_root.write_text("ordinary file")
+ bad_root /= "child"
+ elif failure == "loop":
+ try:
+ bad_root.symlink_to(bad_root, target_is_directory=True)
+ except OSError as exc:
+ pytest.skip(f"creating a synthetic symlink loop requires host support: {exc}")
+ bad_root /= "child"
+ saved = workbench_api["saved_results"]
+ original_write = saved.write_scan_local_bytes
+ finalizer = sys.modules[original_write.__module__]
+ published = {}
+ probes = []
+
+ def write_file(scan_dir, filename, contents):
+ if not filename.endswith(".accepted.json"):
+ return original_write(scan_dir, filename, contents)
+ published.update(
+ (path, path.read_bytes())
+ for path in scan_dir.rglob("*.json")
+ if "drafts" not in path.parts
+ )
+ assert json.loads(published[scan_dir / "findings.json"])["findings"][0]["title"] == (
+ "Accepted aggregate"
+ )
+ with monkeypatch.context() as patch, ExitStack() as files:
+ backend = emulate_windows_atomic_write(patch, finalizer, files)
+ original_probe = backend._canonical_scan_directory
+ path_operation = "lstat" if operation == "locked-lstat" else operation
+ original_operation = getattr(Path, path_operation, None)
+ original_create = backend._create_file
+ original_directory = backend._open_directory
+
+ def fail_root(path, *args, **kwargs):
+ if path == scan_dir:
+ probes.append(operation)
+ if operation == "locked-lstat":
+ patch.setattr(Path, "lstat", original_operation)
+ if failure in {"io", "handles", "denied"}:
+ code = {"io": errno.EIO, "handles": errno.EMFILE, "denied": errno.EACCES}[
+ failure
+ ]
+ raise OSError(code, "Synthetic receipt root I/O failure", str(path))
+ # Exercise actual missing, non-directory, and loop errors without
+ # changing the already accepted canonical artifacts.
+ return original_operation(bad_root, *args, **kwargs)
+ return original_operation(path, *args, **kwargs)
+
+ def probe_root(path):
+ # The common finalizer root check has succeeded. Keep the real
+ # Windows locked-parent and canonical probe in the call path.
+ with monkeypatch.context() as probe_patch:
+ probe_patch.setattr(Path, operation, fail_root)
+ return original_probe(path)
+
+ def open_directory(path, **kwargs):
+ selected = scan_dir.parent if operation == "ancestor-open" else scan_dir
+ if path == selected and kwargs["disposition"] == backend._OPEN_EXISTING:
+ probes.append(operation)
+ code = {
+ "io": 1117,
+ "handles": 4,
+ "denied": 5,
+ "missing": 2,
+ "missing-parent": 3,
+ "not-directory": 267,
+ }[failure]
+ raise backend.WindowsScanLocalFileError(
+ code, "Synthetic late directory failure", str(path)
+ )
+ return original_create(path, **kwargs)
+
+ def open_root_before_stat(path, **kwargs):
+ handle = original_directory(path, **kwargs)
+ if path == scan_dir:
+ patch.setattr(Path, "lstat", fail_root)
+ return handle
+
+ if operation == "locked-lstat":
+ patch.setattr(backend, "_open_directory", open_root_before_stat)
+ elif operation.endswith("-open"):
+ patch.setattr(backend, "_create_file", open_directory)
+ else:
+ patch.setattr(backend, "_canonical_scan_directory", probe_root)
+ return original_write(scan_dir, filename, contents)
+
+ monkeypatch.setattr(saved, "write_scan_local_bytes", write_file)
+ if failure in {"io", "handles", "denied"}:
+ assert saved.write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, args
+ ) == {"scanId": scan.scan_id, "status": "draft_written"}
+ else:
+ # Python versions before 3.13 report resolve loops as RuntimeError.
+ expected_errors = (
+ (finalizer.ContractError, RuntimeError)
+ if failure == "loop" and operation == "resolve"
+ else finalizer.ContractError
+ )
+ with pytest.raises(expected_errors) as rejected:
+ saved.write_scan_draft(workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, args)
+ assert not isinstance(rejected.value, finalizer.ScanLocalIOError)
+ if operation.endswith("-open"):
+ assert "Synthetic late directory failure" in str(rejected.value)
+ assert probes == [operation]
+ assert published
+ assert {path: path.read_bytes() for path in published} == published
+ assert not list((scan.scan_dir / "drafts").glob("*.accepted.json"))
+
+
[email protected]("failure", ["validation", "unsafe-path", "windows-reparse-emulated"])
+def test_receipt_contract_errors_still_reject_publication(
+ workbench_api, workbench_db, publication_scan, monkeypatch, failure
+):
+ scan = publication_scan()
+ result = add_worker(workbench_db, scan)
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE deep_scan_runs SET workflow_version = 'deep-scan-mcp/v1', "
+ "coordinator_generation = 2 WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ workbench_db.execute(
+ "UPDATE deep_scan_workers SET kind = 'dedup', merge_state = 'none' WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ args = stage_publication(scan, generation=2, result_path=result, title="Accepted aggregate")
+ staged = {path: path.read_bytes() for path in (scan.scan_dir / "drafts").iterdir()}
+ saved_results = workbench_api["saved_results"]
+ original_write = saved_results.write_scan_local_bytes
+ finalizer = sys.modules[original_write.__module__]
+ attempts = []
+
+ def write_file(scan_dir, filename, contents):
+ if not filename.endswith(".accepted.json"):
+ return original_write(scan_dir, filename, contents)
+ attempts.append(filename)
+ if failure == "validation":
+ raise finalizer.ContractError("Synthetic receipt validation failure")
+ if failure == "unsafe-path":
+ return original_write(scan_dir, "../outside.accepted.json", contents)
+ with monkeypatch.context() as patch, ExitStack() as files:
+ emulate_windows_atomic_write(patch, finalizer, files, reparse_point=True)
+ return original_write(scan_dir, filename, contents)
+
+ monkeypatch.setattr(saved_results, "write_scan_local_bytes", write_file)
+ with pytest.raises(finalizer.ContractError, match="validation|safe|reparse"):
+ workbench_api["saved_results"].write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, args
+ )
+ assert len(attempts) == 1
+ assert {path: path.read_bytes() for path in (scan.scan_dir / "drafts").iterdir()} == staged
+ assert not (scan.scan_dir.parent / "outside.accepted.json").exists()
+
+
+def test_accepted_publication_replay_retains_original_warning_and_bytes(
+ workbench_api, workbench_db, publication_scan, monkeypatch
+):
+ scan = publication_scan()
+ result = add_worker(workbench_db, scan)
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE deep_scan_runs SET coordinator_generation = 2 WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ workbench_db.execute(
+ "UPDATE deep_scan_workers SET kind = 'dedup', merge_state = 'none' WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ args = stage_publication(scan, generation=2, result_path=result, title="Accepted aggregate")
+ saved = workbench_api["saved_results"]
+ args.expected_draft_digest = saved._scan_draft_digest(scan.scan_dir)
+ warnings = iter(["Original projection warning", "Replay must not reproject"])
+ monkeypatch.setattr(
+ saved, "write_threat_model_projection_if_possible", lambda *_: next(warnings)
+ )
+ first = saved.write_scan_draft(workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, args)
+ published = {
+ path: path.read_bytes()
+ for path in scan.scan_dir.rglob("*.json")
+ if "drafts" not in path.parts
+ }
+ assert args.expected_draft_digest != saved._scan_draft_digest(scan.scan_dir)
+ monkeypatch.setattr(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], "now", lambda: "2099-01-01T00:00:00Z"
+ )
+
+ assert (
+ saved.write_scan_draft(workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, args)
+ == first
+ == {
+ "scanId": scan.scan_id,
+ "status": "draft_written",
+ "warnings": ["Original projection warning"],
+ }
+ )
+ assert {
+ path: path.read_bytes()
+ for path in scan.scan_dir.rglob("*.json")
+ if "drafts" not in path.parts
+ } == published
+ assert next(warnings) == "Replay must not reproject"
diff --git a/plugins/codex-security/tests/test_finalize_writeup_recovery.py b/plugins/codex-security/tests/test_finalize_writeup_recovery.py
index a75f1f59c..e2f23a8a8 100644
--- a/plugins/codex-security/tests/test_finalize_writeup_recovery.py
+++ b/plugins/codex-security/tests/test_finalize_writeup_recovery.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import copy
import os
+from unittest import mock
import pytest
from workbench_test_support import load_script, windows_file_backend
@@ -62,3 +63,33 @@ def test_windows_writeup_recovery_only_omits_missing_files(tmp_path, monkeypatch
with pytest.raises(FINALIZER.ContractError):
FINALIZER._prepare_derived_writeups(tmp_path, findings, allow_missing=True)
assert "writeup" in findings["findings"][0]
+
+
[email protected]("error_code", [2, 3])
+def test_windows_writeup_recovery_omits_missing_parent_directories(
+ tmp_path, monkeypatch, error_code
+):
+ backend = load_script("windows_scan_local_files")
+ scan_dir = tmp_path.resolve()
+ missing_parent = scan_dir / "findings" / "missing"
+
+ def open_directory(path, **_options):
+ if path == missing_parent:
+ raise backend.WindowsScanLocalFileError(
+ error_code, "Synthetic missing parent directory", str(path)
+ )
+ return mock.Mock(value=1)
+
+ monkeypatch.setattr(backend, "_require_windows", lambda: None)
+ monkeypatch.setattr(backend, "_create_file", open_directory)
+ monkeypatch.setattr(backend, "_verify_directory", lambda *_args: None)
+ monkeypatch.setattr(FINALIZER.os, "supports_dir_fd", set())
+ monkeypatch.setattr(FINALIZER, "_is_windows", lambda: True)
+ monkeypatch.setattr(FINALIZER, "_windows_scan_local_files", lambda: backend)
+ findings = {"findings": [{"writeup": {"reportPath": "findings/missing/report.md"}}]}
+
+ warnings = FINALIZER._prepare_derived_writeups(scan_dir, findings, allow_missing=True)
+
+ assert findings == {"findings": [{}]}
+ assert len(warnings) == 1
diff --git a/plugins/codex-security/tests/test_windows_scan_local_files.py b/plugins/codex-security/tests/test_windows_scan_local_files.py
index 05206cde6..60eb71a75 100644
--- a/plugins/codex-security/tests/test_windows_scan_local_files.py
+++ b/plugins/codex-security/tests/test_windows_scan_local_files.py
@@ -142,3 +142,44 @@ def test_native_copy_junction_rejects_nonempty_destination(tmp_path: Path) -> No
WINDOWS_FILES.copy_directory_junction(source, destination)
assert (destination / "keep.txt").read_text() == "placeholder contents\n"
assert not (target / "keep.txt").exists()
+
+
[email protected]("changed", ["root", "ancestor"])
[email protected]("replacement", ["missing", "file"])
+def test_native_directory_change_after_probe_remains_a_path_error(
+ tmp_path: Path, monkeypatch, changed: str, replacement: str
+) -> None:
+ finalizer = load_script("finalize_scan_contract")
+ ancestor = tmp_path / "ancestor"
+ scan_dir = ancestor / "scan"
+ scan_dir.mkdir(parents=True)
+ (scan_dir / "keep.txt").write_bytes(b"Accepted content")
+ finalizer.write_scan_local_bytes(scan_dir, "new-parent/receipt.json", b"Ordinary creation")
+ assert (scan_dir / "new-parent/receipt.json").read_bytes() == b"Ordinary creation"
+ moved = tmp_path / "retained"
+ target = scan_dir if changed == "root" else ancestor
+ retained_scan = moved if changed == "root" else moved / "scan"
+ mutated = False
+ # Windows exercises the actual CreateFileW boundary; POSIX is a native
+ # control at its existing directory-validation boundary.
+ owner = finalizer._windows_scan_local_files() if os.name == "nt" else finalizer
+ name = "_canonical_scan_directory" if os.name == "nt" else "_require_scan_directory"
+ original = getattr(owner, name)
+
+ def replace_after_probe(path):
+ nonlocal mutated
+ result = original(path)
+ if path == scan_dir and not mutated:
+ mutated = True
+ target.rename(moved)
+ if replacement == "file":
+ target.write_bytes(b"Replacement file")
+ return result
+
+ monkeypatch.setattr(owner, name, replace_after_probe)
+ with pytest.raises(finalizer.ContractError) as rejected:
+ finalizer.write_scan_local_bytes(scan_dir, "drafts/receipt.json", b"receipt")
+ assert not isinstance(rejected.value, finalizer.ScanLocalIOError)
+ assert mutated
+ assert (retained_scan / "keep.txt").read_bytes() == b"Accepted content"
+ assert not (retained_scan / "drafts/receipt.json").exists()