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(scans): archive long POSIX output directory names (#2529)
Michael D'Angelo3254a2159242
diff --git a/plugins/codex-security/scripts/workbench/storage.py b/plugins/codex-security/scripts/workbench/storage.pyindex 85e5933f2..28a31a947 100644--- a/plugins/codex-security/scripts/workbench/storage.py+++ b/plugins/codex-security/scripts/workbench/storage.py@@ -2,6 +2,7 @@from __future__ import annotations+import hashlibimport osimport sqlite3import tempfile@@ -35,18 +36,37 @@ def worker_usage_directory(scan_id: str) -> Path:return state_dir() / "codex-home" / "worker-usage" / scan_id+def short_archive_prefix(original_name: str) -> str:+ """Bind a short archive name to the complete original filename."""+ return f".previous-{hashlib.sha256(os.fsencode(original_name)).hexdigest()}-"+++def archive_source_directory(archive: Path, source: Path) -> Path | None:+ """Return the original sibling when the archived name identifies it."""+ if not source.is_relative_to(archive.parent):+ return None+ parts = source.relative_to(archive.parent).parts+ if not parts or ".." in parts:+ return None+ name = parts[0]+ if archive.name.startswith(f"{name}.previous-") or (+ archive.name.startswith(".previous-")+ and archive.name.startswith(short_archive_prefix(name))+ ):+ return archive.parent / name+ return None++def archived_deep_path(scan_dir: Path, value: str | None) -> str | None:"""Resolve old archive rows that still name the original sibling output."""if value is None or ".previous-" not in scan_dir.name:return valuepath = Path(value)- if not path.is_relative_to(scan_dir.parent):- return value- parts = path.relative_to(scan_dir.parent).parts- if not parts or ".." in parts or not scan_dir.name.startswith(f"{parts[0]}.previous-"):+ original = archive_source_directory(scan_dir, path)+ if original is None:return value# Keep missing files inside the archive too. Never follow the replacement scan's output.- return str(scan_dir.joinpath(*parts[1:]))+ return str(scan_dir / path.relative_to(original))def saved_deep_workers(connection: sqlite3.Connection, scan: sqlite3.Row) -> list[Any]:diff --git a/plugins/codex-security/scripts/workbench_scan_start.py b/plugins/codex-security/scripts/workbench_scan_start.pyindex de93d0429..075d9ef38 100644--- a/plugins/codex-security/scripts/workbench_scan_start.py+++ b/plugins/codex-security/scripts/workbench_scan_start.py@@ -3,6 +3,7 @@from __future__ import annotationsimport argparse+import errnoimport jsonimport osimport re@@ -19,7 +20,12 @@ from typing import Anysys.path.insert(0, str(Path(__file__).resolve().parent))from filesystem_identity import serialize_filesystem_identityfrom finalize_scan_contract import write_scan_local_bytes-from workbench.storage import create_private_temporary_directory, state_dir+from workbench.storage import (+ archive_source_directory,+ create_private_temporary_directory,+ short_archive_prefix,+ state_dir,+)from workbench_feedback import get_scan_feedbackfrom workbench_target import (directory_content_digest,@@ -129,7 +135,7 @@ def archive_scan(if archived_scan_dir is not None and (not args.archive_existingor archived_scan_dir.parent != scan_dir.parent- or not archived_scan_dir.name.startswith(f"{scan_dir.name}.previous-")+ or archive_source_directory(archived_scan_dir, scan_dir) != scan_dir):raise SystemExit("The archived scan must be a previous sibling of the scan directory.")@@ -179,9 +185,17 @@ def archive_scan()if before_archive is not None:before_archive()- archived_scan_dir = Path(- tempfile.mkdtemp(prefix=f"{scan_dir.name}.previous-", dir=scan_dir.parent)- ).resolve()+ try:+ reserved = tempfile.mkdtemp(+ prefix=f"{scan_dir.name}.previous-", dir=scan_dir.parent+ )+ except OSError as error:+ if os.name != "posix" or error.errno != errno.ENAMETOOLONG:+ raise+ reserved = tempfile.mkdtemp(+ prefix=short_archive_prefix(scan_dir.name), dir=scan_dir.parent+ )+ archived_scan_dir = Path(reserved).resolve()archived_scan_dir.rmdir()scan_dir.rename(archived_scan_dir)moved = Truediff --git a/plugins/codex-security/scripts/workbench_scan_usage.py b/plugins/codex-security/scripts/workbench_scan_usage.pyindex dc4b7a845..5af54fef4 100644--- a/plugins/codex-security/scripts/workbench_scan_usage.py+++ b/plugins/codex-security/scripts/workbench_scan_usage.py@@ -20,7 +20,7 @@ from typing import Any, Mappingfrom urllib.parse import quotesys.path.insert(0, str(Path(__file__).resolve().parent))-from workbench.storage import worker_usage_directory+from workbench.storage import archive_source_directory, worker_usage_directoryTOKEN_FIELDS = {"input_tokens": "inputTokens",@@ -708,10 +708,12 @@ def _discover_rollout_sessions(for path in worker_directoriesif Path(path).is_relative_to(scan_directory)}- for marker in re.finditer(r"\.previous-", scan_directory.name):- if marker.start() == 0:- continue- original = scan_directory.with_name(scan_directory.name[: marker.start()])+ originals = {+ original+ for path in bound_directories | {Path(path) for path in worker_directories}+ if (original := archive_source_directory(scan_directory, path)) is not None+ }+ for original in originals:candidates = {original / relative: path for relative, path in relatives.items()}# Older archives kept absolute discovery paths in their original directory.if (diff --git a/plugins/codex-security/tests/test_workbench_scan_archive.py b/plugins/codex-security/tests/test_workbench_scan_archive.pyindex bc6cd3fd7..93d94f552 100644--- a/plugins/codex-security/tests/test_workbench_scan_archive.py+++ b/plugins/codex-security/tests/test_workbench_scan_archive.py@@ -160,6 +160,46 @@ def test_archive_requires_previous_directory_when_artifacts_exist(tmp_path: Pathassert "archived scan directory is required" in str(rejected["stderr"])[email protected](os.name != "posix", reason="uses the filesystem's POSIX component limit")+def test_archive_keeps_output_with_a_full_length_filename(tmp_path: Path) -> None:+ limit = os.pathconf(tmp_path, "PC_NAME_MAX")+ if limit < 0:+ pytest.skip("This filesystem does not report a component limit")+ name = "a" * (limit - 1) + "b"+ state_dir = tmp_path / "state"+ repository = tmp_path / "repository"+ scan_dir = tmp_path / name+ repository.mkdir()+ scan_dir.mkdir(mode=0o700)+ previous = register_scan(state_dir, repository, scan_dir)+ fail_scan(state_dir, str(previous["scanId"]), "Interrupted fixture scan.")+ (scan_dir / "report.md").write_text("original report\n")++ current = register_scan(state_dir, repository, scan_dir, "--archive-existing")+ archived = Path(str(current["archivedScanDir"]))+ assert archived.parent == tmp_path+ assert (archived / "report.md").read_text() == "original report\n"+ assert not (scan_dir / "report.md").exists()+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ assert connection.execute(+ "SELECT scan_dir FROM scans WHERE id = ?", (previous["scanId"],)+ ).fetchone() == (str(archived),)++ other = tmp_path / (name[:-1] + "c")+ other.mkdir(mode=0o700)+ rejected = register_scan(+ state_dir,+ repository,+ other,+ "--archive-existing",+ "--archived-scan-dir",+ str(archived),+ check=False,+ )+ assert rejected["returncode"] != 0+ assert "previous sibling" in str(rejected["stderr"])++def test_archive_does_not_replace_a_running_scan(tmp_path: Path) -> None:state_dir = tmp_path / "state"repository = tmp_path / "repository"diff --git a/plugins/codex-security/tests/test_workbench_scan_usage.py b/plugins/codex-security/tests/test_workbench_scan_usage.pyindex b20457ad4..3e51d5f2a 100644--- a/plugins/codex-security/tests/test_workbench_scan_usage.py+++ b/plugins/codex-security/tests/test_workbench_scan_usage.py@@ -2,6 +2,7 @@from __future__ import annotations+import hashlibimport jsonimport sqlite3import subprocess@@ -3158,11 +3159,21 @@ def test_cancel_scan_retains_runtime_usage_on_repeated_cancellation(tmp_path: Pa@pytest.mark.parametrize(- ("worker_kind", "legacy_archive"),- [("discovery", False), ("discovery", True), ("dedup", False)],+ ("worker_kind", "legacy_archive", "short_archive"),+ [+ ("discovery", False, False),+ ("discovery", True, False),+ ("dedup", False, False),+ pytest.param(+ "discovery",+ True,+ True,+ marks=pytest.mark.skipif(sys.platform == "win32", reason="POSIX archive naming"),+ ),+ ],)def test_completion_recovers_replaced_deep_worker_threads(- tmp_path: Path, worker_kind: str, legacy_archive: bool+ tmp_path: Path, worker_kind: str, legacy_archive: bool, short_archive: bool) -> None:# Compressed variants exercise the bundled Node decoder in the SDK runtime lane.fixture = _start_scan(tmp_path, mode="deep")@@ -3307,6 +3318,13 @@ def test_completion_recovers_replaced_deep_worker_threads("threadCount": 5,}.items())+ archived_arguments = []+ if short_archive:+ digest = hashlib.sha256(fixture.scan_dir.name.encode()).hexdigest()+ archived = fixture.scan_dir.with_name(f".previous-{digest}-fixture")+ fixture.scan_dir.rename(archived)+ fixture.scan_dir.mkdir(mode=0o700)+ archived_arguments = ["--archived-scan-dir", str(archived)]run_workbench(fixture.state_dir,"register-cli-scan",@@ -3315,6 +3333,7 @@ def test_completion_recovers_replaced_deep_worker_threads("--scan-dir",str(fixture.scan_dir),"--archive-existing",+ *archived_arguments,"--recipe-json",json.dumps({diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.mdindex 4de4634e0..a7d650b0d 100644--- a/sdk/typescript/docs/cli.md+++ b/sdk/typescript/docs/cli.md@@ -538,7 +538,9 @@ through inheritance. Other users must not have permission to set reparse dataon the destination directory itself.`--archive-existing` moves earlier output to a sibling directory with a uniquesuffix, such as `results.previous-ab12cd34` for an output directory named-`results`. Use `--dry-run` to preview the move.+`results`. If adding the suffix exceeds the filesystem's filename limit on POSIX,+the archive uses a short sibling name starting with `.previous-` and a hash of the+original filename. Use `--dry-run` to preview the move.Scans also attempt to write SARIF to `<scan-dir>/exports/results.sarif`. If thatoptional export fails, the scan can still complete. Use thediff --git a/sdk/typescript/scripts/package-dist-files.mjs b/sdk/typescript/scripts/package-dist-files.mjsindex 6b7e809e8..c3cefbaf7 100644--- a/sdk/typescript/scripts/package-dist-files.mjs+++ b/sdk/typescript/scripts/package-dist-files.mjs@@ -133,6 +133,7 @@ const distModules = ["retry-windows-rename","runtime","scan-activity",+ "scan-archive-path","scan-comparison","scan-dashboard","scan-history-renderer",diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.tsindex 1c4aa5717..1b05ab886 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -16,6 +16,7 @@ import {type SpawnOptionsWithoutStdio,} from "node:child_process";import { randomUUID } from "node:crypto";+import { shortArchivePrefix } from "./scan-archive-path.js";import {chmodSync,close as closeCallback,@@ -2173,14 +2174,33 @@ export async function planOutputArchive(if (outputDirectory === null) return null;const entries = await readdir(outputDirectory).catch(nullIfMissingFileError);if (entries === null || entries.length === 0) return null;- return `${outputDirectory}${outputArchiveSuffix()}`;+ const id = randomUUID().slice(0, 8);+ const archive = `${outputDirectory}${outputArchiveSuffix(id)}`;+ if (process.platform === "win32" || !(await archiveNameTooLong(archive)))+ return archive;+ // The workbench reserves a sibling with an eight-character random suffix.+ const sibling = `${outputDirectory}.previous-${id}`;+ if (!(await archiveNameTooLong(sibling))) return sibling;+ return join(+ dirname(outputDirectory),+ shortArchivePrefix(basename(outputDirectory)) + id,+ );+}++async function archiveNameTooLong(path: string): Promise<boolean> {+ try {+ await lstat(path);+ return false;+ } catch (error) {+ return nodeErrorCode(error) === "ENAMETOOLONG";+ }}-function outputArchiveSuffix(): string {+function outputArchiveSuffix(id = randomUUID().slice(0, 8)): string {return `.previous-${new Date().toISOString().replaceAll(/[-:]/g, "")- .replace(/\.\d{3}Z$/, "")}-${randomUUID().slice(0, 8)}`;+ .replace(/\.\d{3}Z$/, "")}-${id}`;}export function requireModelSafeOutputDir(path: string): void {diff --git a/sdk/typescript/src/scan-archive-path.ts b/sdk/typescript/src/scan-archive-path.tsnew file mode 100644index 000000000..e093b2126--- /dev/null+++ b/sdk/typescript/src/scan-archive-path.ts@@ -0,0 +1,5 @@+import { createHash } from "node:crypto";++export function shortArchivePrefix(originalName: string): string {+ return `.previous-${createHash("sha256").update(originalName).digest("hex")}-`;+}diff --git a/sdk/typescript/src/scan-logs.ts b/sdk/typescript/src/scan-logs.tsindex c83a3fc3a..1fa72cc03 100644--- a/sdk/typescript/src/scan-logs.ts+++ b/sdk/typescript/src/scan-logs.ts@@ -1,4 +1,4 @@-import { basename, dirname, join, relative } from "node:path";+import { basename, dirname, isAbsolute, join, relative, sep } from "node:path";import { isDeepStrictEqual } from "node:util";import { firstNativeSessionLine, sessionLines } from "./session-lines.js";import { parseJson } from "./value.js";@@ -13,6 +13,7 @@ import {sessionStartedAt,} from "./scan-sessions.js";import { compareTimestamps } from "./timestamps.js";+import { shortArchivePrefix } from "./scan-archive-path.js";interface ScanLogOptions {scanId: string;@@ -164,12 +165,34 @@ export async function readScanLogs(options: ScanLogOptions) {if (options.scanDirectory && startedAt !== null) {const directories = [options.scanDirectory];const name = basename(options.scanDirectory);+ const parent = dirname(options.scanDirectory);+ const originals = new Set<string>();for (const marker of name.matchAll(/\.previous-/g)) {- if (!marker.index) continue;- const original = join(- dirname(options.scanDirectory),- name.slice(0, marker.index),- );+ if (marker.index)+ originals.add(join(parent, name.slice(0, marker.index)));+ }+ if (name.startsWith(".previous-")) {+ // A short archive has no readable source name. Only already-bound+ // sessions can supply a sibling whose complete filename matches its hash.+ const boundCwds = [+ root?.workingDirectory,+ ...Array.from(traversed, (id) => logs.get(id)?.[0].workingDirectory),+ ];+ for (const cwd of boundCwds) {+ if (!cwd) continue;+ const path = relative(parent, cwd);+ const sibling = path.split(sep, 1)[0];+ if (+ sibling &&+ sibling !== ".." &&+ !isAbsolute(path) &&+ name.startsWith(shortArchivePrefix(sibling))+ ) {+ originals.add(join(parent, sibling));+ }+ }+ }+ for (const original of originals) {// A Desktop owner can work outside the scan directory. Its registered// workers provide the same binding after archiving the output.if (diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.tsindex 9ddcd220b..d46e8f91c 100644--- a/sdk/typescript/tests-ts/runtime.test.ts+++ b/sdk/typescript/tests-ts/runtime.test.ts@@ -6405,6 +6405,31 @@ describe("runtime directories and plugin Python boundary", () => {).not.toThrow();});+ testPosix.each([+ [230, "a".repeat(230)],+ [250, "é".repeat(125)],+ ] as const)(+ "previews and archives a %i-byte output name",+ async (_bytes, name) => {+ const root = await temporaryDirectory();+ const output = join(root, name);+ await mkdir(output, { mode: 0o700 });+ await writeFile(join(output, "report.md"), "first report");+ const preview = await planOutputArchive(output);+ expect(dirname(preview!)).toBe(root);+ await expect(lstat(preview!)).rejects.toMatchObject({ code: "ENOENT" });+ const archived: string[] = [];+ await prepareOutputDir(output, "repo", root, undefined, true, (path) => {+ archived.push(path);+ });+ expect(archived).toHaveLength(1);+ expect(await readFile(join(archived[0]!, "report.md"), "utf8")).toBe(+ "first report",+ );+ expect(await readdir(output)).toEqual([]);+ },+ );+test("prepares an output directory without moving prior results", async () => {const root = await temporaryDirectory();const output = join(root, "scan");diff --git a/sdk/typescript/tests-ts/scan-logs.test.ts b/sdk/typescript/tests-ts/scan-logs.test.tsindex cd5bed3f4..f0b69c68d 100644--- a/sdk/typescript/tests-ts/scan-logs.test.ts+++ b/sdk/typescript/tests-ts/scan-logs.test.ts@@ -1,4 +1,5 @@import { writeJsonLines } from "./support/json.js";+import { createHash } from "node:crypto";import { mkdir, readFile, rename, writeFile } from "node:fs/promises";import { unlinkSync } from "node:fs";import { join } from "node:path";@@ -173,9 +174,14 @@ describe("saved scan logs", () => {expect(result.sessions.map(({ threadId }) => threadId)).toEqual(["parent"]);});- test.each([false, true])(- "collects parentless comparisons inside a shared Desktop scan window (archived: %p)",- async (archived) => {+ test.each([+ "original",+ "legacy archive",+ "short archive",+ "unrelated short archive",+ ])(+ "collects parentless comparisons inside a shared Desktop scan window (%s)",+ async (layout) => {const home = await temporaryHome();const scanDir = join(home, "scan");const cwd = join(scanDir, "artifacts", "deep_discovery");@@ -213,10 +219,27 @@ describe("saved scan logs", () => {"2026-08-02T01:00:00Z",cwd,);+ await writeSession(+ home,+ "unbound-sibling",+ [],+ undefined,+ "2026-08-02T01:00:00Z",+ join(home, "scan-other", "artifacts"),+ );+ const digest = createHash("sha256")+ .update(layout === "unrelated short archive" ? "scan-other" : "scan")+ .digest("hex");+ const directory =+ layout === "original"+ ? scanDir+ : layout === "legacy archive"+ ? scanDir + ".previous-test"+ : join(home, `.previous-${digest}-test`);const result = await readSavedScanLogs({scanId: "scan-1",- scanDir: archived ? scanDir + ".previous-test" : scanDir,+ scanDir: directory,mode: "deep",continuationThreadId: "owner",lifecycle: {@@ -228,11 +251,11 @@ describe("saved scan logs", () => {},home,);- expect(result.sessions.map(({ threadId }) => threadId).sort()).toEqual([- "comparison",- "owner",- "worker",- ]);+ expect(result.sessions.map(({ threadId }) => threadId).sort()).toEqual(+ layout === "unrelated short archive"+ ? ["owner", "worker"]+ : ["comparison", "owner", "worker"],+ );},);@@ -832,6 +855,11 @@ describe("saved scan logs", () => {]);expect(JSON.stringify(archivedLogs)).toContain("review archived scan");expect(JSON.stringify(archivedLogs)).not.toContain("PRIVATE REPLACEMENT");+ const digest = createHash("sha256").update("results").digest("hex");+ const shortArchive = join(home, "scans", `.previous-${digest}-a1b2c3d4`);+ expect(+ await readScanLogs({ ...options, scanDirectory: shortArchive }),+ ).toEqual(archivedLogs);for (const logCompletedAt of [null, undefined]) {const legacyLogs = await readSavedScanLogs(