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
refactor(sdk): share the SQLite opener across history and locking (#2480)
Michael D'Angelo897f16e49906
diff --git a/sdk/typescript/src/external-findings-publish.ts b/sdk/typescript/src/external-findings-publish.tsindex d01b22cf7..ca8444220 100644--- a/sdk/typescript/src/external-findings-publish.ts+++ b/sdk/typescript/src/external-findings-publish.ts@@ -9,9 +9,7 @@ import {rename,rm,} 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,@@ -49,6 +47,8 @@ import {codexSecurityStateDirectory,createWindowsOutputDirectory,validatePreparedOutputDir,+ sqliteDatabaseOpener,+ type RuntimeDatabase,} from "./runtime.js";import {EXTERNAL_FINDINGS_PROVIDERS,@@ -598,13 +598,8 @@ async function withImportLocks<T>(await createWindowsOutputDirectory(state, true);else await mkdir(state, { recursive: true, mode: 0o700 });await validatePreparedOutputDir(state, undefined, true);- const require = createRequire(import.meta.url);- const Database = (- process.versions["bun"]- ? require("bun:sqlite").Database- : require("node:sqlite").DatabaseSync- ) as new (path: string) => { exec(sql: string): void; close(): void };- const guards: InstanceType<typeof Database>[] = [];+ const openDatabase = sqliteDatabaseOpener();+ const guards: RuntimeDatabase[] = [];try {for (const key of [...new Set(keys)].sort()) {const path = join(state, `${key}.lock.sqlite`);@@ -618,7 +613,7 @@ async function withImportLocks<T>(throw new CodexSecurityError("The publication lock is not a regular file.",);- const guard = new Database(sqliteFilename(path));+ const guard = openDatabase(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 4a1f6bdb3..faf5d9c5e 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -1280,6 +1280,42 @@ async function secureWindowsCredentialHome(path: string): Promise<void> {}}+export interface RuntimeDatabase {+ exec(sql: string): unknown;+ prepare(sql: string): {+ all(...parameters: (string | number)[]): unknown[];+ finalize?(): void;+ };+ close(): void;+}++export function sqliteDatabaseOpener(+ access: "read-existing" | "read-write" = "read-write",+) {+ const require = createRequire(import.meta.url);+ const bun = process.versions["bun"] !== undefined;+ const Database = (+ bun ? require("bun:sqlite").Database : require("node:sqlite").DatabaseSync+ ) as new (filename: string, options?: object) => RuntimeDatabase;++ return (path: string): RuntimeDatabase => {+ const filename = sqliteFilename(path);+ if (access === "read-write") return new Database(filename);+ // Apple's SQLite requires write access to initialize missing WAL sidecars.+ // The history reader sets PRAGMA query_only on this existing-file connection.+ return new Database(+ filename,+ bun+ ? {+ readonly: process.platform !== "darwin",+ readwrite: process.platform === "darwin",+ create: false,+ }+ : { readOnly: true },+ );+ };+}+export async function withCredentialHomeLock<T>(codexHome: string,operation: () => Promise<T>,@@ -1318,21 +1354,11 @@ export async function acquireCodexSecurityCredentialHomeLock(if (existingDatabaseMetadata !== null) {requireCredentialLockDatabaseFile(existingDatabaseMetadata, databasePath);}- const require = createRequire(import.meta.url);- // Both supported runtimes bundle SQLite. Keep the transaction in the process- // doing the protected work, so pausing it cannot expire its lock.- const Database = process.versions["bun"]- ? (require("bun:sqlite") as { Database: CredentialLockDatabaseConstructor })- .Database- : (- require("node:sqlite") as {- DatabaseSync: CredentialLockDatabaseConstructor;- }- ).DatabaseSync;+ const openDatabase = sqliteDatabaseOpener();// 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(sqliteFilename(databasePath));+ const database = openDatabase(databasePath);let databaseLocked = false;try {@@ -1468,13 +1494,6 @@ function requireCredentialLockDatabaseFile(}}-interface CredentialLockDatabaseConstructor {- new (path: string): {- exec(sql: string): unknown;- close(): void;- };-}-async function recoverStaleCredentialHomeLock(lock: string): Promise<boolean> {const metadata = await lstat(lock).catch(nullIfMissingFileError);if (metadata === null) return true;diff --git a/sdk/typescript/src/saved-scan-bootstrap.ts b/sdk/typescript/src/saved-scan-bootstrap.tsindex e94e09cc3..fcfe89db1 100644--- a/sdk/typescript/src/saved-scan-bootstrap.ts+++ b/sdk/typescript/src/saved-scan-bootstrap.ts@@ -3,9 +3,7 @@ import { compareTimestamps } from "./timestamps.js";import { environmentEntry } from "./auth.js";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,@@ -19,6 +17,8 @@ import {runWorkbench,workbenchEnvironment,type ProcessEnvironment,+ sqliteDatabaseOpener,+ type RuntimeDatabase,} from "./runtime.js";interface ScanTarget {@@ -27,15 +27,6 @@ interface ScanTarget {target_id?: string | null;}-interface BootstrapDatabase {- exec(sql: string): unknown;- prepare(sql: string): {- all(...parameters: (string | number)[]): unknown[];- finalize?(): void;- };- close(): void;-}-/** Read only enough history to protect Python discovery; the workbench still validates the scan. */export async function savedScanWorkbench(requestedId: string | { workflowId: string },@@ -170,33 +161,10 @@ export async function savedScanWorkbench(function openSavedScanDatabase(environment: ProcessEnvironment,-): BootstrapDatabase {- const require = createRequire(import.meta.url);- const bun = process.versions["bun"] !== undefined;- const Database = (- bun ? require("bun:sqlite").Database : require("node:sqlite").DatabaseSync- ) as new (- path: string,- options: {- readonly?: boolean;- readOnly?: boolean;- readwrite?: boolean;- create?: boolean;- },- ) => BootstrapDatabase;- return new Database(- 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.- bun- ? {- readonly: process.platform !== "darwin",- readwrite: process.platform === "darwin",- create: false,- }- : { readOnly: true },+): RuntimeDatabase {+ const openDatabase = sqliteDatabaseOpener("read-existing");+ return openDatabase(+ join(codexSecurityStateDirectory(environment), "workbench.sqlite3"),);}@@ -225,7 +193,7 @@ async function readTargets(currentDirectory: string,signal?: AbortSignal,): Promise<ScanTarget[]> {- let database: BootstrapDatabase | undefined;+ let database: RuntimeDatabase | undefined;try {database = openSavedScanDatabase(environment);database.exec("PRAGMA busy_timeout = 5000");@@ -291,7 +259,7 @@ async function readTargets(}function readRows<T>(- database: BootstrapDatabase,+ database: RuntimeDatabase,sql: string,...parameters: (string | number)[]): T[] {@@ -306,7 +274,7 @@ function readRows<T>(}async function latestTargets(- database: BootstrapDatabase,+ database: RuntimeDatabase,directory: string,environment: ProcessEnvironment,signal?: AbortSignal,