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: fall back when Windows hardlinks are unavailable (#498)

faizan-oai2e940ca29ecf
diff --git a/sdk/typescript/_bundled_plugin/scripts/deep_scan_workbench.py b/sdk/typescript/_bundled_plugin/scripts/deep_scan_workbench.py
index f548db39e..6a486ba72 100644
--- a/sdk/typescript/_bundled_plugin/scripts/deep_scan_workbench.py
+++ b/sdk/typescript/_bundled_plugin/scripts/deep_scan_workbench.py
@@ -5,6 +5,7 @@ from __future__ import annotations
import argparse
import json
import os
+import shutil
import sqlite3
import sys
import tempfile
@@ -328,6 +329,31 @@ def finish_staged_file(promotion: tuple[Path, Path, Path | None]) -> None:
backup.unlink(missing_ok=True)
+def create_publication_copy(source: str | Path, destination: str | Path) -> None:
+ try:
+ os.link(source, destination)
+ except OSError:
+ shutil.copy2(source, destination)
+
+
+def publication_matches_snapshot(publication: Path, snapshot: Path) -> bool:
+ try:
+ if publication.samefile(snapshot):
+ return True
+ if publication.stat().st_size != snapshot.stat().st_size:
+ return False
+ with publication.open("rb") as published, snapshot.open("rb") as source:
+ while True:
+ published_chunk = published.read(1024 * 1024)
+ source_chunk = source.read(1024 * 1024)
+ if published_chunk != source_chunk:
+ return False
+ if not published_chunk:
+ return True
+ except OSError:
+ return False
+
+
def canonical_discovery_artifacts(scan: sqlite3.Row) -> dict[str, str]:
discovery_dir = Path(scan["scan_dir"]) / "artifacts" / "02_discovery"
artifacts = {
@@ -1075,7 +1101,7 @@ def recover_candidate_ledger_publication(connection: sqlite3.Connection, scan_id
snapshot = Path(reducer["artifact_dir"]) / "canonical" / ledger.name
if not snapshot.exists():
continue
- published = ledger.exists() and ledger.samefile(snapshot)
+ published = publication_matches_snapshot(ledger, snapshot)
interrupted = reducer["status"] != "succeeded"
if not published and not (interrupted and backups and not ledger.exists()):
continue
@@ -1577,7 +1603,7 @@ def commit_deep_scan_dedup_locked(
publication_copy = canonical_path.with_name(
f".{canonical_path.name}.{uuid.uuid4()}.publish"
)
- os.link(candidate_ledger_path, publication_copy)
+ create_publication_copy(candidate_ledger_path, publication_copy)
promotion = promote_staged_file(
str(publication_copy),
canonical_candidate_ledger_path,
diff --git a/sdk/typescript/tests-ts/deep-scan-workbench.test.ts b/sdk/typescript/tests-ts/deep-scan-workbench.test.ts
index b4a0469c8..5b00da0af 100644
--- a/sdk/typescript/tests-ts/deep-scan-workbench.test.ts
+++ b/sdk/typescript/tests-ts/deep-scan-workbench.test.ts
@@ -92,6 +92,100 @@ function runOwnershipProbe(probe: OwnershipProbe): Record<string, unknown> {
>;
}
+test("copies a Deep Scan publication when the filesystem rejects hardlinks", async () => {
+ const root = await realpath(
+ await mkdtemp(join(tmpdir(), "codex-security-deep-publication-")),
+ );
+ temporaryDirectories.push(root);
+ const source = join(root, "source.jsonl");
+ const destination = join(root, "destination.jsonl");
+ await writeFile(source, '{"finding":"synthetic"}\n');
+ const python =
+ process.env["PYTHON"] ?? Bun.which("python3") ?? Bun.which("python");
+ expect(python).not.toBeNull();
+
+ const result = Bun.spawnSync(
+ [
+ python!,
+ "-I",
+ "-B",
+ "-c",
+ [
+ "import errno, sys",
+ "from pathlib import Path",
+ "sys.path.insert(0, sys.argv[1])",
+ "import deep_scan_workbench as deep_scan",
+ "def reject_hardlink(source, destination):",
+ " raise OSError(errno.ENOTSUP, 'hardlinks are unavailable')",
+ "deep_scan.os.link = reject_hardlink",
+ "deep_scan.create_publication_copy(Path(sys.argv[2]), Path(sys.argv[3]))",
+ ].join("\n"),
+ join(PLUGIN_ROOT, "scripts"),
+ source,
+ destination,
+ ],
+ { stdout: "pipe", stderr: "pipe" },
+ );
+
+ expect(new TextDecoder().decode(result.stderr)).toBe("");
+ expect(result.exitCode).toBe(0);
+ expect(await readFile(destination, "utf8")).toBe('{"finding":"synthetic"}\n');
+});
+
+test("recovers an interrupted copied Deep Scan publication", async () => {
+ const root = await realpath(
+ await mkdtemp(join(tmpdir(), "codex-security-deep-copy-recovery-")),
+ );
+ temporaryDirectories.push(root);
+ const python =
+ process.env["PYTHON"] ?? Bun.which("python3") ?? Bun.which("python");
+ expect(python).not.toBeNull();
+
+ const result = Bun.spawnSync(
+ [
+ python!,
+ "-I",
+ "-B",
+ "-c",
+ [
+ "import json, shutil, sqlite3, sys",
+ "from pathlib import Path",
+ "sys.path.insert(0, sys.argv[1])",
+ "import deep_scan_workbench as deep_scan",
+ "root = Path(sys.argv[2])",
+ "scan_dir = root / 'scan'",
+ "ledger = scan_dir / 'artifacts' / '02_discovery' / 'candidate_ledger.jsonl'",
+ "snapshot = root / 'worker' / 'canonical' / ledger.name",
+ "backup = ledger.with_name(f'.{ledger.name}.fixture.backup')",
+ "ledger.parent.mkdir(parents=True)",
+ "snapshot.parent.mkdir(parents=True)",
+ "ledger.write_text('old ledger\\n', encoding='utf-8')",
+ "shutil.copy2(ledger, backup)",
+ "snapshot.write_text('new ledger\\n', encoding='utf-8')",
+ "shutil.copy2(snapshot, ledger)",
+ "connection = sqlite3.connect(':memory:')",
+ "connection.row_factory = sqlite3.Row",
+ "connection.execute('CREATE TABLE scans (id TEXT PRIMARY KEY, scan_dir TEXT)')",
+ "connection.execute('CREATE TABLE deep_scan_workers (scan_id TEXT, kind TEXT, status TEXT, artifact_dir TEXT, updated_at TEXT)')",
+ "connection.execute('INSERT INTO scans VALUES (?, ?)', ('scan', str(scan_dir)))",
+ "connection.execute('INSERT INTO deep_scan_workers VALUES (?, ?, ?, ?, ?)', ('scan', 'dedup', 'running', str(snapshot.parent.parent), 'now'))",
+ "deep_scan.require_scan = lambda database, value: database.execute('SELECT * FROM scans WHERE id = ?', (value,)).fetchone()",
+ "deep_scan.recover_candidate_ledger_publication(connection, 'scan')",
+ "print(json.dumps({'ledger': ledger.read_text(encoding='utf-8'), 'backup': backup.exists()}))",
+ ].join("\n"),
+ join(PLUGIN_ROOT, "scripts"),
+ root,
+ ],
+ { stdout: "pipe", stderr: "pipe" },
+ );
+
+ expect(result.exitCode, new TextDecoder().decode(result.stderr)).toBe(0);
+ expect(JSON.parse(new TextDecoder().decode(result.stdout))).toEqual({
+ ledger: "old ledger\n",
+ backup: false,
+ });
+});
+
describe("deep scan workbench ownership", () => {
test("rejects completion before an SDK-created Deep Scan finishes", async () => {
const root = await realpath(