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(auth): cancel inherited pipes after the login process exits (#1068)
Dipesh Babu5ebe48db0585
diff --git a/sdk/typescript/src/auth.ts b/sdk/typescript/src/auth.tsindex 718856d85..ee6f21af8 100644--- a/sdk/typescript/src/auth.ts+++ b/sdk/typescript/src/auth.ts@@ -189,8 +189,14 @@ export class CodexLoginHandle {public cancel(): void {this.#canceled = true;- if (this.#child.exitCode !== null || this.#child.signalCode !== null)+ if (this.#child.exitCode !== null || this.#child.signalCode !== null) {+ // Descendants can retain inherited pipes after the login process exits.+ // Cancellation must release those pipes so the close event can settle.+ this.#child.stdin.destroy();+ this.#child.stdout.destroy();+ this.#child.stderr.destroy();return;+ }this.#child.kill("SIGTERM");if (this.#forcedTermination !== undefined) return;this.#forcedTermination = setTimeout(() => {diff --git a/sdk/typescript/tests-ts/auth.test.ts b/sdk/typescript/tests-ts/auth.test.tsindex ad7f9e588..073a5b23b 100644--- a/sdk/typescript/tests-ts/auth.test.ts+++ b/sdk/typescript/tests-ts/auth.test.ts@@ -367,6 +367,72 @@ setInterval(() => {}, 1000);expect(observeSucceeded).not.toHaveBeenCalled();});+ test.skipIf(process.platform === "win32")(+ "cancels login after the parent exits while a descendant holds stderr",+ async () => {+ const root = await temporaryDirectory("codex-security-auth-exited-");+ const script = join(root, "login.mjs");+ const ready = join(root, "ready");+ const release = join(root, "release");+ const descendant = `+import { existsSync, writeFileSync } from "node:fs";+const [parent, ready, release] = process.argv.slice(1);+setTimeout(() => process.exit(1), 10_000);+let announced = false;+setInterval(() => {+ if (existsSync(release)) process.exit(0);+ if (announced) return;+ try { process.kill(Number(parent), 0); return; }+ catch (error) { if (error.code !== "ESRCH") process.exit(1); }+ announced = true;+ console.error("Open https://auth.example.test/device");+ console.error("User code: ABCD-EFGH");+}, 25);+writeFileSync(ready, "ready");+`;+ await writeFile(+ script,+ `+import { spawn } from "node:child_process";+import { existsSync } from "node:fs";+const child = spawn(process.execPath, ["-e", ${JSON.stringify(descendant)}, String(process.pid), ${JSON.stringify(ready)}, ${JSON.stringify(release)}], {+ stdio: ["ignore", "ignore", "inherit"], windowsHide: true,+});+child.on("error", () => process.exit(1));+setInterval(() => { if (existsSync(${JSON.stringify(ready)})) process.exit(0); }, 25);+setTimeout(() => { child.kill(); process.exit(1); }, 10_000);+`,+ );+ let succeeded = false;+ const handle = new CodexLoginHandle(+ nodeCommand(),+ [script],+ process.env,+ () => {+ succeeded = true;+ },+ );+ const deadline = new AbortController();+ try {+ await handle.waitForInstructions({ deviceCode: true });+ handle.cancel();+ await expect(+ Promise.race([+ handle.wait(),+ delay(5_000, undefined, { signal: deadline.signal }).then(() => {+ throw new Error("Canceled login waited for inherited stderr.");+ }),+ ]),+ ).resolves.toMatchObject({ success: false, exitCode: 0 });+ expect(succeeded).toBe(false);+ } finally {+ deadline.abort();+ await writeFile(release, "released");+ await handle.wait();+ }+ },+ );+test("does not report a canceled interactive login as successful", async () => {const root = await temporaryDirectory("codex-security-auth-cancel-");const script = join(root, "codex.mjs");