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(runtime): recover malformed credential-home lock owners (#448)
mldangelo-oai54232ac46f63
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.tsindex 1a18cfbcb..3569c11b4 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -76,6 +76,7 @@ const CREDENTIAL_LOCK_NAME = ".codex-security-scan.lock";const CREDENTIAL_LOGOUT_MARKER = ".codex-security-logged-out";const CREDENTIAL_LOCK_POLL_MILLISECONDS = 25;const INCOMPLETE_CREDENTIAL_LOCK_MILLISECONDS = 30_000;+const MAX_PROCESS_ID = 2_147_483_647;const MAX_WINDOWS_CREDENTIAL_ACL_STDERR = 64 * 1024;export interface PluginInstall {@@ -1140,9 +1141,17 @@ async function recoverStaleCredentialHomeLock(lock: string): Promise<boolean> {}}- if (isRecord(owner) && typeof owner["pid"] === "number") {+ // Only positive signed-32-bit PIDs identify an owner. Other values can name+ // process groups or fail argument validation, so use the stale-age check.+ const ownerPid = isRecord(owner) ? owner["pid"] : undefined;+ if (+ typeof ownerPid === "number" &&+ Number.isInteger(ownerPid) &&+ ownerPid > 0 &&+ ownerPid <= MAX_PROCESS_ID+ ) {try {- process.kill(owner["pid"], 0);+ process.kill(ownerPid, 0);return false;} catch (error) {if (nodeErrorCode(error) !== "ESRCH") {diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.tsindex 34a120b29..d27a21162 100644--- a/sdk/typescript/tests-ts/runtime.test.ts+++ b/sdk/typescript/tests-ts/runtime.test.ts@@ -14,6 +14,7 @@ import {rm,stat,symlink,+ utimes,writeFile,} from "node:fs/promises";import * as fsPromises from "node:fs/promises";@@ -2360,6 +2361,39 @@ describe("runtime directories and plugin Python boundary", () => {expect(existsSync(lock)).toBe(false);});+ test("recovers credential-home locks whose owner names no process", async () => {+ const root = await temporaryDirectory();+ const home = await prepareCodexSecurityCredentialHome({+ CODEX_SECURITY_STATE_DIR: join(root, "state"),+ });+ const lock = join(home, ".codex-security-scan.lock");+ for (const pid of [0, -1, 0.5, 2 ** 31, 2 ** 53]) {+ await mkdir(lock, { mode: 0o700 });+ await writeFile(+ join(lock, "owner.json"),+ `${JSON.stringify({ pid, token: "unidentifiable-owner" })}\n`,+ { mode: 0o600 },+ );+ const aged = new Date(Date.now() - 10 * 60_000);+ await utimes(lock, aged, aged);++ // Bound acquisition so a false live-owner result cannot hang the test.+ const abort = new AbortController();+ const timer = setTimeout(() => abort.abort(), 5_000);+ try {+ const release = await acquireCodexSecurityCredentialHomeLock(+ home,+ abort.signal,+ );+ expect(existsSync(lock)).toBe(true);+ await release();+ } finally {+ clearTimeout(timer);+ }+ expect(existsSync(lock)).toBe(false);+ }+ });+test("prevents ambient credential imports after an explicit logout", async () => {const root = await temporaryDirectory();const home = await prepareCodexSecurityCredentialHome({