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(windows): open long state paths in JavaScript SQLite connections (#2380)
Michael D'Angelodc80da644eaa
diff --git a/plugins/codex-security/mcp-app/src/windows-path.ts b/plugins/codex-security/mcp-app/src/windows-path.tsindex e176f69f8..4f956c4dd 100644--- a/plugins/codex-security/mcp-app/src/windows-path.ts+++ b/plugins/codex-security/mcp-app/src/windows-path.ts@@ -1,4 +1,38 @@-import { parse } from "node:path";+import { parse, toNamespacedPath } from "node:path";++// SQLite's default win32 VFS allows MAX_PATH * 4 UTF-8 bytes, including room for "-journal".+const SQLITE_WINDOWS_MAX_FILENAME_BYTES = 260 * 4 - 8;++export function sqliteFilename(path: string): string {+ // Ordinary paths also preserve Win32 handling of device names and trailing spaces or dots.+ if (+ process.platform !== "win32" ||+ path.length + 8 < 260 ||+ path.includes("\0")+ )+ return path;+ const root = parse(path).root;+ // Root-relative names (including ambiguous UNC spellings) must not move to the current drive.+ if (root.length < 3) return path;+ // Keep device prefixes, dot segments, and incomplete UNC roots as supplied.+ // Repeated separators below a complete root still identify the same file.+ const isDrivePath = root[1] === ":";+ const components = path.slice(isDrivePath ? root.length : 2).split(/[\\/]/u);+ if (+ (!isDrivePath && (!components[0] || !components[1])) ||+ components.some(+ (part) => part === "." || isWindowsUnsafePathComponent(part),+ )+ )+ return path;+ const namespaced = toNamespacedPath(path);+ if (+ namespaced !== path &&+ Buffer.byteLength(namespaced) > SQLITE_WINDOWS_MAX_FILENAME_BYTES+ )+ return path;+ return namespaced;+}const UNSAFE_COMPONENT = /[<>:"|?*\u0000-\u001f]|[ .]$/u;const RESERVED_COMPONENT =diff --git a/plugins/codex-security/mcp-app/src/workbench/database.ts b/plugins/codex-security/mcp-app/src/workbench/database.tsindex 49bcf82e4..e56e006f9 100644--- a/plugins/codex-security/mcp-app/src/workbench/database.ts+++ b/plugins/codex-security/mcp-app/src/workbench/database.ts@@ -8,6 +8,7 @@ import {} from "node:fs";import { dirname, isAbsolute, join, sep } from "node:path";import { DatabaseSync } from "node:sqlite";+import { sqliteFilename } from "../windows-path";import { setTimeout } from "node:timers/promises";import { applyMigrations } from "./migrations";import { decodePosixBytes, encodePosixPath } from "../helpers/posix-path";@@ -58,7 +59,7 @@ export async function openWorkbenchDatabase(try {createStateDirectory(dirname(databasePath));for (let attempt = 0; ; attempt++) {- const database = new DatabaseSync(databasePath);+ const database = new DatabaseSync(sqliteFilename(databasePath));try {database.exec("PRAGMA foreign_keys = ON; PRAGMA busy_timeout = 5000;");applyMigrations(database, undefined, !deferred || attempt > 0);diff --git a/plugins/codex-security/mcp-app/src/workbench/severity.ts b/plugins/codex-security/mcp-app/src/workbench/severity.tsindex 8f3065119..a9f09045b 100644--- a/plugins/codex-security/mcp-app/src/workbench/severity.ts+++ b/plugins/codex-security/mcp-app/src/workbench/severity.ts@@ -1,5 +1,6 @@import { DatabaseSync } from "node:sqlite";import { parseJson, stringifyJson } from "../helpers/json";+import { sqliteFilename } from "../windows-path";import { requireSqliteText } from "./database";import type { Finding } from "./findings";import { transaction } from "./transaction";@@ -201,7 +202,9 @@ export function readSeverityClassification(scanId: string,): unknown {requireSqliteText([scanId]);- const database = new DatabaseSync(databasePath, { readOnly: true });+ const database = new DatabaseSync(sqliteFilename(databasePath), {+ readOnly: true,+ });try {database.exec("PRAGMA busy_timeout = 5000");return transaction(database, "BEGIN", () => {diff --git a/plugins/codex-security/mcp-app/tests/test_helper_files.ts b/plugins/codex-security/mcp-app/tests/test_helper_files.tsindex b7353b23e..ae790d5c7 100644--- a/plugins/codex-security/mcp-app/tests/test_helper_files.ts+++ b/plugins/codex-security/mcp-app/tests/test_helper_files.ts@@ -1,22 +1,24 @@import assert from "node:assert/strict";+import { execFileSync } from "node:child_process";import { promises as fs } from "node:fs";import { mkdir, rm, symlink } from "node:fs/promises";-import { join, win32 } from "node:path";+import { join, toNamespacedPath, win32 } from "node:path";+import { DatabaseSync } from "node:sqlite";+import { fileURLToPath } from "node:url";import { after, test } from "node:test";import { importModule, importSource } from "./import-module.ts";import { createTemporaryDirectories } from "./support/temporary-directories.ts";import { registerWorkerExecutableFirstLaunchTests } from "./windows_executable_first_launch_cases.ts";+const packagedHelper = new URL(+ "../../../../sdk/typescript/_bundled_plugin/mcp/helpers.mjs",+ import.meta.url,+);const { resolvedPathText, isMissingPathError } = (await importSource("src/helpers/helper-files.ts",{define: {- "import.meta.url": JSON.stringify(- new URL(- "../../../../sdk/typescript/_bundled_plugin/mcp/helpers.mjs",- import.meta.url,- ).href,- ),+ "import.meta.url": JSON.stringify(packagedHelper.href),},},)) as typeof import("../src/helpers/helper-files.ts");@@ -26,6 +28,9 @@ const { expandHome } = (await importSource(const { windowsFileSystem } = (await importModule({entryPoints: ["../native/windows-files.mts"],})) as typeof import("../../native/windows-files.mjs");+const { sqliteFilename } = (await importSource(+ "src/windows-path.ts",+)) as typeof import("../src/windows-path.ts");const directories = createTemporaryDirectories(true);after(() => directories.cleanup());@@ -89,6 +94,109 @@ test(},);+test(+ "preserves ordinary Windows SQLite spellings for short paths and verbatim-sensitive components",+ { skip: process.platform !== "win32" },+ () => {+ const root = "C:/synthetic";+ const long = join(root, "a".repeat(128), "b".repeat(128));+ for (const filename of [+ join(root, "workbench.sqlite3"),+ join(long, "state.", "workbench.sqlite3"),+ join(long, "state ", "workbench.sqlite3"),+ `\\\\.\\C:\\..\\synthetic\\${long.slice(3)}`,+ `\\\\?\\C:\\example\\..\\synthetic\\${long.slice(3)}`,+ `\\\\first.invalid\\..\\second.invalid\\share\\${long.slice(3)}`,+ `\\\\first.invalid\\\\share\\${long.slice(3)}`,+ `\\\\first.invalid\\share.\\${long.slice(3)}`,+ "/".repeat(3) ++ "synthetic/share/" ++ "a".repeat(128) ++ "/" ++ "b".repeat(128),+ "\\".repeat(3) ++ "synthetic\\share\\" ++ "a".repeat(128) ++ "\\" ++ "b".repeat(128),+ ]) {+ assert.equal(sqliteFilename(filename), filename);+ }+ const share = `\\\\first.invalid\\share`;+ assert.equal(+ sqliteFilename(`${share}\\\\${long.slice(3)}`),+ toNamespacedPath(`${share}\\${long.slice(3)}`),+ );+ },+);++test(+ "the severity reader rejects NUL even before a parent path component",+ { skip: process.platform !== "win32" },+ async () => {+ const state = await directories.create("invalid-sqlite-name-");+ const helper = fileURLToPath(packagedHelper);+ execFileSync(process.execPath, [helper, "database-info"], {+ input: JSON.stringify(state),+ windowsHide: true,+ });+ const invalid = `${state}\\${"x".repeat(Math.max(1, 260 - state.length))}\0\\..`;+ assert.ok(invalid.length >= 260);+ const request = JSON.stringify({+ stateDirectory: invalid,+ payload: { scanId: "synthetic" },+ });+ assert.throws(() =>+ execFileSync(process.execPath, [helper, "read-severity-classification"], {+ input: request,+ stdio: ["pipe", "pipe", "pipe"],+ windowsHide: true,+ }),+ );+ },+);++test(+ "opens and reopens a workbench database beyond Windows MAX_PATH",+ { skip: process.platform !== "win32" },+ async (t) => {+ const root = await directories.create("codex-long-workbench-");+ const filename = "workbench.sqlite3";+ const padding = Math.max(0, 260 - join(root, filename).length);+ const state = join(root, "x".repeat(padding));+ const expected = join(state, filename);+ if (Buffer.byteLength(toNamespacedPath(expected)) > 1032) {+ t.skip(+ "Temporary root exceeds the SQLite filename limit for this fixture",+ );+ return;+ }+ for (const suffix of ["", "\\", "/", "\\\\", "/\\"]) {+ const output = execFileSync(+ process.execPath,+ [fileURLToPath(packagedHelper), "database-info"],+ {+ encoding: "utf8",+ input: JSON.stringify(state + suffix),+ windowsHide: true,+ },+ );+ assert.equal(JSON.parse(output).databasePath, expected);+ }+ const database = new DatabaseSync(toNamespacedPath(expected), {+ readOnly: true,+ });+ try {+ assert.equal(+ database.prepare("PRAGMA journal_mode").get()?.journal_mode,+ "wal",+ );+ } finally {+ database.close();+ }+ },+);+test("POSIX home expansion removes boundary separators while retaining literal suffixes",{ skip: process.platform === "win32" },diff --git a/sdk/typescript/scripts/smoke-package.mjs b/sdk/typescript/scripts/smoke-package.mjsindex 1cea9612c..ba0983a10 100644--- a/sdk/typescript/scripts/smoke-package.mjs+++ b/sdk/typescript/scripts/smoke-package.mjs@@ -833,12 +833,19 @@ try {/lin_api_|security@example\.test/u,);+ let credentialLockState = join(consumer, "credential-lock-state");+ if (process.platform === "win32" && !process.versions.bun) {+ // Exercise a long SQLite guard while the canonical home fits the ACL tools' path limit.+ const state = join(await realpath(consumer), "credential-lock-state");+ const padding = Math.max(0, 240 - join(state, "codex-home").length - 1);+ credentialLockState = join(state, "x".repeat(padding));+ }run(process.execPath,[join(packageRoot, "scripts", "fixtures", "credential-lock.mjs"),pathToFileURL(join(installedRoot, "dist", "runtime.js")).href,- join(consumer, "credential-lock-state"),+ credentialLockState,],{ cwd: consumer },);diff --git a/sdk/typescript/src/external-findings-publish.ts b/sdk/typescript/src/external-findings-publish.tsindex ea13f190b..3bd24af75 100644--- a/sdk/typescript/src/external-findings-publish.ts+++ b/sdk/typescript/src/external-findings-publish.ts@@ -11,6 +11,7 @@ import {} from "node:fs/promises";import { createRequire } from "node:module";import { join } from "node:path";+import { sqliteFilename } from "./windows-path.js";import { setTimeout as delay } from "node:timers/promises";import {isBoxedPrimitive,@@ -614,7 +615,7 @@ async function withImportLocks<T>(throw new CodexSecurityError("The publication lock is not a regular file.",);- const guard = new Database(path);+ const guard = new Database(sqliteFilename(path));guards.push(guard);if (process.platform !== "win32") chmodSync(path, 0o600);guard.exec("PRAGMA busy_timeout = 0");diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.tsindex d6fcc37f1..4a1f6bdb3 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -82,6 +82,7 @@ import {} from "./trusted-executable.js";import {isWindowsUnsafePathComponent,+ sqliteFilename,windowsUnsafePathComponent,} from "./windows-path.js";@@ -1331,7 +1332,7 @@ export async function acquireCodexSecurityCredentialHomeLock(// Let SQLite create a missing guard so no separate descriptor can close after// another connection acquires its process-owned POSIX lock. Keep the guard// across releases so every contender locks the same inode.- const database = new Database(databasePath);+ const database = new Database(sqliteFilename(databasePath));let databaseLocked = false;try {diff --git a/sdk/typescript/src/saved-scan-bootstrap.ts b/sdk/typescript/src/saved-scan-bootstrap.tsindex 7b1492ee6..e94e09cc3 100644--- a/sdk/typescript/src/saved-scan-bootstrap.ts+++ b/sdk/typescript/src/saved-scan-bootstrap.ts@@ -5,6 +5,7 @@ import { inspectTrustedExecutable } from "./trusted-executable.js";import { realpath } from "node:fs/promises";import { createRequire } from "node:module";import { isAbsolute, join, resolve } from "node:path";+import { sqliteFilename } from "./windows-path.js";import {gitHistoryIdentity,gitMarkerRoot,@@ -184,7 +185,9 @@ function openSavedScanDatabase(},) => BootstrapDatabase;return new Database(- join(codexSecurityStateDirectory(environment), "workbench.sqlite3"),+ sqliteFilename(+ join(codexSecurityStateDirectory(environment), "workbench.sqlite3"),+ ),// Apple's SQLite cannot initialize absent WAL sidecars through a read-only handle.// Open only an existing file there; query_only still prevents SQL data/schema writes.bundiff --git a/sdk/typescript/src/windows-path.ts b/sdk/typescript/src/windows-path.tsindex 0137b6f49..94db369ea 100644--- a/sdk/typescript/src/windows-path.ts+++ b/sdk/typescript/src/windows-path.ts@@ -1,5 +1,39 @@/* Generated from plugin windows-path.ts. Run `pnpm generate:models`. */-import { parse } from "node:path";+import { parse, toNamespacedPath } from "node:path";++// SQLite's default win32 VFS allows MAX_PATH * 4 UTF-8 bytes, including room for "-journal".+const SQLITE_WINDOWS_MAX_FILENAME_BYTES = 260 * 4 - 8;++export function sqliteFilename(path: string): string {+ // Ordinary paths also preserve Win32 handling of device names and trailing spaces or dots.+ if (+ process.platform !== "win32" ||+ path.length + 8 < 260 ||+ path.includes("\0")+ )+ return path;+ const root = parse(path).root;+ // Root-relative names (including ambiguous UNC spellings) must not move to the current drive.+ if (root.length < 3) return path;+ // Keep device prefixes, dot segments, and incomplete UNC roots as supplied.+ // Repeated separators below a complete root still identify the same file.+ const isDrivePath = root[1] === ":";+ const components = path.slice(isDrivePath ? root.length : 2).split(/[\\/]/u);+ if (+ (!isDrivePath && (!components[0] || !components[1])) ||+ components.some(+ (part) => part === "." || isWindowsUnsafePathComponent(part),+ )+ )+ return path;+ const namespaced = toNamespacedPath(path);+ if (+ namespaced !== path &&+ Buffer.byteLength(namespaced) > SQLITE_WINDOWS_MAX_FILENAME_BYTES+ )+ return path;+ return namespaced;+}const UNSAFE_COMPONENT = /[<>:"|?*\u0000-\u001f]|[ .]$/u;const RESERVED_COMPONENT =diff --git a/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts b/sdk/typescript/tests-ts/saved-scan-bootstrap.test.tsindex 67038085b..de487a7b4 100644--- a/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts+++ b/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts@@ -10,10 +10,13 @@ import {symlink,writeFile,} from "node:fs/promises";-import { delimiter, dirname, join } from "node:path";+import { delimiter, dirname, join, toNamespacedPath } from "node:path";import { afterEach, expect, mock, test } from "bun:test";import { deduplicateScanInternal } from "../src/deduplication/scan.js";-import { savedScanWorkbench } from "../src/saved-scan-bootstrap.js";+import {+ readSavedScanLifecycle,+ savedScanWorkbench,+} from "../src/saved-scan-bootstrap.js";import { inspectTrustedExecutable } from "../src/trusted-executable.js";import { resolveCompletedScan } from "../src/saved-scan.js";import { resolvePluginPython, runWorkbench } from "../src/runtime.js";@@ -27,6 +30,7 @@ import { git } from "./git-fixture.js";import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";import { createApiTestFixtures } from "./support/temporary-directories.js";import { rejecting } from "./support/errors.js";+import { runTestInSubprocess } from "./support/test-subprocess.js";import { scanRegistrationArguments } from "./support/workbench-command.js";import { runWorkbenchFixtureCommands } from "./support/workbench-fixtures.js";@@ -35,6 +39,86 @@ const { temporaryDirectory, cleanup } = createApiTestFixtures();afterEach(cleanup);+function seedSavedLifecycle(filename: string): void {+ const database = new Database(filename, { create: true });+ try {+ database.exec("CREATE TABLE scan_lifecycles (scan_id TEXT, snapshot TEXT)");+ database+ .query("INSERT INTO scan_lifecycles VALUES (?, ?)")+ .run("synthetic", JSON.stringify({ completed: true }));+ } finally {+ database.close();+ }+}++function recordSavedDatabaseOpens(rejectedFilename?: string): unknown[] {+ const opened: unknown[] = [];+ class RecordedDatabase extends Database {+ constructor(...args: ConstructorParameters<typeof Database>) {+ opened.push(args[0]);+ if (rejectedFilename !== undefined && args[0] === rejectedFilename)+ throw new Error("Ordinary long SQLite filename unavailable");+ super(...args);+ }+ }+ mock.module("bun:sqlite", () => ({ Database: RecordedDatabase }));+ return opened;+}++const legacyPathTest =+ "reads saved history when ordinary long Windows filenames are unavailable";+test.skipIf(process.platform !== "win32")(legacyPathTest, async () => {+ if (runTestInSubprocess(import.meta.path, legacyPathTest)) return;+ const root = await temporaryDirectory();+ const filename = "workbench.sqlite3";+ const padding = Math.max(0, 260 - join(root, filename).length);+ const state = join(root, "x".repeat(padding));+ const ordinary = join(state, filename);+ const namespaced = toNamespacedPath(ordinary);+ if (ordinary === namespaced || Buffer.byteLength(namespaced) > 1032) return;+ await mkdir(state, { recursive: true });+ seedSavedLifecycle(namespaced);+ const opened = recordSavedDatabaseOpens(ordinary);+ expect(+ readSavedScanLifecycle({ CODEX_SECURITY_STATE_DIR: state }, "synthetic"),+ ).toEqual({ completed: true });+ expect(opened).toEqual([namespaced]);+});++const byteLimitTest =+ "Bun reads saved history near the Win32 SQLite filename limit";+test.skipIf(process.platform !== "win32")(byteLimitTest, async () => {+ if (runTestInSubprocess(import.meta.path, byteLimitTest)) return;+ const filename = "workbench.sqlite3";+ // A 1,030-byte filename fits the default VFS until a drive namespace is added.+ const targetBytes = 1030;+ let state = await temporaryDirectory();+ if (Buffer.byteLength(join(state, filename)) > targetBytes) return;+ while (targetBytes - Buffer.byteLength(join(state, filename)) > 128)+ state = join(state, "a".repeat(64));+ const padding = targetBytes - Buffer.byteLength(join(state, filename)) - 1;+ if (padding > 0) state = join(state, "b".repeat(padding));+ await mkdir(state, { recursive: true });+ const ordinary = join(state, filename);+ try {+ seedSavedLifecycle(ordinary);+ } catch (error) {+ // Exercise the compatibility boundary only where Bun itself can open this filename.+ if (+ error instanceof Error &&+ "code" in error &&+ error.code === "SQLITE_CANTOPEN"+ )+ return;+ throw error;+ }+ const opened = recordSavedDatabaseOpens();+ expect(+ readSavedScanLifecycle({ CODEX_SECURITY_STATE_DIR: state }, "synthetic"),+ ).toEqual({ completed: true });+ expect(opened).toEqual([ordinary]);+});+function writePythonProbe(path: string) {return writeFile(path,