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: terminate signal-owned publication process trees (#625)
mldangelo-oaiebd7cd11b937
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex 4d9cce33b..9da4da0b4 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -102,6 +102,7 @@ import { componentPlanSchema, planComponents } from "./component-plan.js";import { runComponentScans } from "./component-scan.js";import {checkScanPublication,+ forceTerminatePublicationProcesses as terminatePublishers,publishScan,type CheckScanPublicationOptions,type PublishScanProgress,@@ -1074,6 +1075,7 @@ interface CliDependencies {addSignalListener(signal: SignalName, listener: () => void): void;removeSignalListener(signal: SignalName, listener: () => void): void;writeSynchronously(stream: Writable, value: string): void;+ terminatePublishers?(): void;forceExit(signal: SignalName): void;exportFindings(arguments_: ExportArguments,@@ -2042,7 +2044,6 @@ export async function main(async run({ args, format, formatExplicit, options }) {const controller = new AbortController();let presentation: PublicationProgressPresenter | undefined;- let directApiPublication = false;let firstSignalAt = 0;let observingSignals = false;let cloudBatch:@@ -2056,7 +2057,8 @@ export async function main(presentation?.stop();if (controller.signal.aborted) {if (- !directApiPublication ||+ options.to !== "linear" ||+ options.dryRun ||(controller.signal.reason === signal &&dependencies.now() - firstSignalAt < 500)) {@@ -2068,6 +2070,7 @@ export async function main("codex-security: Publication force-stopped; reconcile retained Linear publication evidence before retrying.\n",);} catch {}+ (dependencies.terminatePublishers ?? terminatePublishers)();removeSignalListeners();dependencies.forceExit(signal);return;@@ -2145,9 +2148,6 @@ export async function main(dependencies.environment,): undefined;- directApiPublication =- !options.dryRun && destination?.linearApiKey !== undefined;-if (options.to === "cloud") {dependencies.addSignalListener("SIGINT", onInterrupt);dependencies.addSignalListener("SIGTERM", onTerminate);diff --git a/sdk/typescript/src/publish.ts b/sdk/typescript/src/publish.tsindex 00b922c34..eb8321ce9 100644--- a/sdk/typescript/src/publish.ts+++ b/sdk/typescript/src/publish.ts@@ -1,4 +1,8 @@-import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";+import {+ spawn,+ spawnSync,+ type ChildProcessWithoutNullStreams,+} from "node:child_process";import { createHash, randomUUID } from "node:crypto";import {appendFile,@@ -9,7 +13,7 @@ import {rm,writeFile,} from "node:fs/promises";-import { join } from "node:path";+import { join, win32 } from "node:path";import {InternalLinearError,NetworkLinearError,@@ -1494,6 +1498,25 @@ function codexFailureMessage(stderr: string, exitCode: number): string {: `Codex exited with status ${exitCode}; sign in to Codex and connect the Linear app before publishing.`;}+const activePublicationProcesses = new Set<ChildProcessWithoutNullStreams>();++interface ForceTerminationDependencies {+ platform?: NodeJS.Platform;+ systemRoot?: string;+ runTaskkill?: (+ command: string,+ args: readonly string[],+ ) => { error?: Error; status: number | null };+}++export function forceTerminatePublicationProcesses(+ dependencies: ForceTerminationDependencies = {},+): void {+ for (const child of activePublicationProcesses) {+ forceTerminatePublicationProcess(child, dependencies);+ }+}+async function runPublicationCodex(command: CodexCommand,args: readonly string[],@@ -1508,8 +1531,9 @@ async function runPublicationCodex(env: environment,stdio: ["pipe", "pipe", "pipe"],windowsHide: true,- detached: process.platform !== "win32",+ detached: process.platform !== "win32" && signal !== undefined,});+ if (signal !== undefined) activePublicationProcesses.add(child);let stdout = "";let stderr = "";let partialLine = "";@@ -1527,7 +1551,11 @@ async function runPublicationCodex(};const cleanup = (): void => {signal?.removeEventListener("abort", onAbort);- if (forcedTermination !== undefined) clearTimeout(forcedTermination);+ activePublicationProcesses.delete(child);+ if (forcedTermination !== undefined) {+ clearTimeout(forcedTermination);+ forcedTermination = undefined;+ }};signal?.addEventListener("abort", onAbort, { once: true });if (signal?.aborted === true) onAbort();@@ -1598,7 +1626,7 @@ function terminatePublicationProcess(}return new Promise((resolve) => {- const command = join(+ const command = win32.join(process.env["SystemRoot"] ?? "C:\\Windows","System32","taskkill.exe",@@ -1618,6 +1646,26 @@ function terminatePublicationProcess(});}+function forceTerminatePublicationProcess(+ child: ChildProcessWithoutNullStreams,+ dependencies: ForceTerminationDependencies,+): void {+ if (child.pid === undefined) return;+ if ((dependencies.platform ?? process.platform) === "win32") {+ const command = win32.join(+ dependencies.systemRoot ?? process.env["SystemRoot"] ?? "C:\\Windows",+ "System32",+ "taskkill.exe",+ );+ const args = ["/PID", String(child.pid), "/T", "/F"];+ const taskkill =+ dependencies.runTaskkill?.(command, args) ??+ spawnSync(command, args, { stdio: "ignore", windowsHide: true });+ if (taskkill.error === undefined && taskkill.status === 0) return;+ }+ terminatePublicationProcessGroup(child, "SIGKILL");+}+function terminatePublicationProcessGroup(child: ChildProcessWithoutNullStreams,signal: NodeJS.Signals,diff --git a/sdk/typescript/tests-ts/publication-integration.test.ts b/sdk/typescript/tests-ts/publication-integration.test.tsindex 09a204797..8f8f7f415 100644--- a/sdk/typescript/tests-ts/publication-integration.test.ts+++ b/sdk/typescript/tests-ts/publication-integration.test.ts@@ -26,6 +26,7 @@ import type {} from "../src/models.js";import {checkScanPublicationInternal,+ forceTerminatePublicationProcesses,publishScanInternal,type PublishScanDependencies,type PublishScanProgress,@@ -43,6 +44,10 @@ const OPTIONS = {teamId: "team-example",projectId: "project-example",} as const;++const NODE_EXECUTABLE = execFileSync("node", ["-p", "process.execPath"], {+ encoding: "utf8",+}).trim();const temporaryDirectories: string[] = [];interface PublicationFixture {@@ -251,6 +256,42 @@ async function artifactDigests();}+async function waitForProcessExit(pid: number): Promise<void> {+ if (!Number.isSafeInteger(pid) || pid < 1) {+ throw new Error(`Invalid test process ID: ${pid}`);+ }+ for (let attempt = 0; attempt < 250; attempt += 1) {+ try {+ process.kill(pid, 0);+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code === "ESRCH") return;+ throw error;+ }+ if (process.platform === "linux") {+ const state = await readFile(`/proc/${pid}/stat`, "utf8").catch(+ () => undefined,+ );+ if (state === undefined || /\) Z /u.test(state)) return;+ }+ await Bun.sleep(20);+ }+ throw new Error(`Test process ${pid} did not exit.`);+}++async function readProcessId(path: string): Promise<number> {+ return Number(await readFile(path, "utf8").catch(() => "0"));+}++function killTestProcess(pid: number): void {+ if (!Number.isSafeInteger(pid) || Math.abs(pid) < 2) return;+ if (process.platform === "win32" && pid < 0) return;+ try {+ process.kill(pid, "SIGKILL");+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code !== "ESRCH") throw error;+ }+}+function storedPublications(fixture: PublicationFixture): StoredPublication[] {const script = ["import json, sqlite3, sys",@@ -996,6 +1037,188 @@ describe("database-backed Linear publication integration", () => {},);+ test.skipIf(process.platform === "win32")(+ "keeps no-signal SDK publication in the host process group",+ async () => {+ const completed = await fixture(1);+ const root = dirname(completed.scanDirectory);+ const preload = join(root, "sdk-publication-child.cjs");+ const publisherPidFile = join(root, "sdk-publication-child.pid");+ await writeFile(+ preload,+ 'const fs = require("node:fs");fs.readFileSync(0, "utf8");fs.writeFileSync(process.env.CODEX_PUBLICATION_PARENT_PID, String(process.pid));const waiter = new Int32Array(new SharedArrayBuffer(4));for (;;) Atomics.wait(waiter, 0, 0, 1000);',+ { mode: 0o600 },+ );+ const publishing = publishScanInternal(completed.scanDirectory, OPTIONS, {+ environment: {+ PATH: completed.environment["PATH"],+ PYTHON: completed.python,+ CODEX_SECURITY_STATE_DIR: completed.stateDirectory,+ NODE_OPTIONS: `--require=${JSON.stringify(preload)}`,+ CODEX_PUBLICATION_PARENT_PID: publisherPidFile,+ },+ resolveCodex: () => ({ command: NODE_EXECUTABLE }),+ });++ try {+ let publisherPid = await readProcessId(publisherPidFile);+ for (let attempt = 0; publisherPid < 2 && attempt < 250; attempt += 1) {+ await Bun.sleep(20);+ publisherPid = await readProcessId(publisherPidFile);+ }+ expect(publisherPid).toBeGreaterThan(1);+ const groups = execFileSync(+ "ps",+ ["-o", "pgid=", "-p", `${process.pid},${publisherPid}`],+ { encoding: "utf8" },+ )+ .trim()+ .split(/\s+/u);+ expect(groups).toHaveLength(2);+ expect(new Set(groups).size).toBe(1);+ forceTerminatePublicationProcesses({+ platform: "win32",+ runTaskkill: () => {+ throw new Error("No-signal publication entered force registry.");+ },+ });+ } finally {+ const publisherPid = await readProcessId(publisherPidFile);+ killTestProcess(publisherPid);+ await publishing.catch(() => undefined);+ }+ },+ 30_000,+ );++ test("applies connected-publication signal escalation without delivery duplicates", async () => {+ for (const [secondSignal, elapsed, expectedForced] of [+ ["SIGINT", 0, []],+ ["SIGTERM", 0, ["1:terminated", "0:SIGTERM"]],+ ["SIGINT", 500, ["1:terminated", "0:SIGINT"]],+ ] as const) {+ const signals = new FakeSignals();+ const forced: string[] = [];+ let now = 0;+ const cli = dependencies({ signals });+ cli.environment["CODEX_SECURITY_LINEAR_TEAM"] = OPTIONS.teamId;+ cli.now = () => now;+ const record = (value: string): number =>+ forced.push(`${signals.listeners.get("SIGINT")?.size}:${value}`);+ cli.terminatePublishers = () => record("terminated");+ cli.forceExit = record;+ cli.publishScan = async () => {+ signals.emit("SIGINT");+ now = elapsed;+ signals.emit(secondSignal);+ throw new Error("Synthetic interrupted connected publication.");+ };++ expect(+ await main(+ ["publish", "scan", "completed-scan", "--to", "linear"],+ capture().stream,+ capture().stream,+ cli,+ ),+ ).toBe(130);+ expect(forced).toEqual([...expectedForced]);+ }+ });++ test("kills connected publication descendants before a later signal forces exit", async () => {+ const completed = await fixture(1);+ const root = dirname(completed.scanDirectory);+ const preload = join(root, "publisher.cjs");+ const publisherPidFile = join(root, "publisher.pid");+ const descendantPidFile = join(root, "descendant.pid");+ const descendant =+ 'const fs = require("node:fs");process.on("SIGINT", () => {});process.on("SIGTERM", () => {});fs.writeFileSync(process.env.CODEX_PUBLICATION_DESCENDANT_PID, String(process.pid));setInterval(() => {}, 1000);';+ await writeFile(+ preload,+ `const fs = require("node:fs"); const { spawn } = require("node:child_process");+const prompt = fs.readFileSync(0, "utf8"); const payload = JSON.parse(prompt.split("BEGIN UNTRUSTED PUBLICATION DATA\\n")[1].split("\\nEND UNTRUSTED PUBLICATION DATA")[0]); const finding = JSON.parse(fs.readFileSync(payload.publicationFile, "utf8")).batches[0][0];+fs.appendFileSync(payload.handoffFile, JSON.stringify({ scanId: payload.scanId, findingId: finding.findingId, occurrenceId: finding.occurrenceId, error: "Synthetic connected publication may have completed.", possibleMutation: true, arguments: finding.arguments }) + "\\n"); fs.writeFileSync(process.env.CODEX_PUBLICATION_PARENT_PID, String(process.pid));+spawn(process.execPath, ["-e", ${JSON.stringify(descendant)}], { env: { ...(process.platform === "win32" ? { SystemRoot: process.env.SystemRoot } : {}), CODEX_PUBLICATION_DESCENDANT_PID: process.env.CODEX_PUBLICATION_DESCENDANT_PID }, stdio: "ignore" });+const waiter = new Int32Array(new SharedArrayBuffer(4)); for (let attempts = 0; !fs.existsSync(process.env.CODEX_PUBLICATION_DESCENDANT_PID); attempts += 1) { if (attempts === 500) process.exit(3); Atomics.wait(waiter, 0, 0, 10); }+process.on("SIGINT", () => {}); process.on("SIGTERM", () => {}); fs.writeSync(1, '{"type":"synthetic.publisher_ready"}\\n');+for (;;) Atomics.wait(waiter, 0, 0, 1000);`,+ { mode: 0o600 },+ );+ const environment = {+ ...(process.platform === "win32"+ ? { SystemRoot: process.env["SystemRoot"] }+ : {}),+ PATH: completed.environment["PATH"],+ PYTHON: completed.python,+ CODEX_SECURITY_STATE_DIR: completed.stateDirectory,+ CODEX_SECURITY_LINEAR_TEAM: OPTIONS.teamId,+ NODE_OPTIONS: `--require=${JSON.stringify(preload)}`,+ CODEX_PUBLICATION_PARENT_PID: publisherPidFile,+ CODEX_PUBLICATION_DESCENDANT_PID: descendantPidFile,+ };+ const signals = new FakeSignals();+ const cli = dependencies({+ currentDirectory: completed.scanDirectory,+ environment,+ signals,+ });+ cli.forceExit = () => undefined;+ cli.publishScan = async (directory, options) =>+ await publishScanInternal(+ directory,+ {+ ...options,+ onProgress: (event) => {+ options.onProgress?.(event);+ if (event.type !== "codex_event") return;+ signals.emit("SIGINT");+ signals.emit("SIGTERM");+ },+ },+ {+ environment,+ resolveCodex: () => ({ command: NODE_EXECUTABLE }),+ },+ );++ try {+ expect(+ await Promise.race([+ main(+ ["publish", "scan", completed.scanDirectory, "--to", "linear"],+ capture().stream,+ capture().stream,+ cli,+ ),+ Bun.sleep(20_000).then(() => -1),+ ]),+ ).toBe(130);+ const publisherPid = await readProcessId(publisherPidFile);+ const descendantPid = await readProcessId(descendantPidFile);+ await Promise.all([+ waitForProcessExit(publisherPid),+ waitForProcessExit(descendantPid),+ ]);+ const handoffRoot = join(root, "state/publications/linear/handoffs");+ const handoffs = await readdir(handoffRoot);+ expect(handoffs).toHaveLength(1);+ const handoff = join(handoffRoot, handoffs[0]!);+ expect(await readFile(join(handoff, "issues.jsonl"), "utf8")).toContain(+ '"possibleMutation":true',+ );+ expect(+ await readFile(join(handoff, "publication.json"), "utf8"),+ ).toContain(completed.findings[0]!.findingId);+ } finally {+ const publisherPid = await readProcessId(publisherPidFile);+ const descendantPid = await readProcessId(descendantPidFile);+ killTestProcess(-publisherPid);+ killTestProcess(publisherPid);+ killTestProcess(descendantPid);+ }+ }, 30_000);+test("recovers verified SQLite publications before an interrupted CLI exits", async () => {const completed = await fixture(3);const sealed = await artifactDigests(completed.scanDirectory);diff --git a/sdk/typescript/tests-ts/publish.test.ts b/sdk/typescript/tests-ts/publish.test.tsindex a276e27f1..62e653ab1 100644--- a/sdk/typescript/tests-ts/publish.test.ts+++ b/sdk/typescript/tests-ts/publish.test.ts@@ -16,8 +16,9 @@ import {NetworkLinearError,UnknownLinearError,} from "@linear/sdk";-import { afterEach, describe, expect, test } from "bun:test";+import { afterEach, describe, expect, spyOn, test } from "bun:test";import {+ forceTerminatePublicationProcesses,publishScanInternal,type PublicationCodexResult,type PublishScanDependencies,@@ -563,6 +564,23 @@ describe("skip-recorded publication", () => {).rejects.toThrow("History is unavailable.");});});+async function cleanupPublicationProcesses(...paths: string[]): Promise<void> {+ const pids = await Promise.all(+ paths.map(async (path) =>+ Number(await readFile(path, "utf8").catch(() => "0")),+ ),+ );+ for (const pid of process.platform === "win32"+ ? pids+ : [-pids[0]!, ...pids]) {+ if (!Number.isSafeInteger(pid) || Math.abs(pid) < 2) continue;+ try {+ process.kill(pid, "SIGKILL");+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code !== "ESRCH") throw error;+ }+ }+}describe("direct Linear API publication", () => {test("leaves issues unassigned unless an email or user ID is selected", async () => {@@ -3097,12 +3115,12 @@ describe("connected Linear publication", () => {});test.each([- ["a promptly exiting parent", false, "SIGTERM"],- ["a parent that ignores termination", true, "SIGTERM"],- ["a Ctrl-C-interrupted parent", false, "SIGINT"],+ ["a promptly exiting parent", false, "SIGTERM", true],+ ["a parent that ignores termination", true, "SIGTERM", false],+ ["a Ctrl-C-interrupted parent", false, "SIGINT", false],] as const)("cancellation stops %s and its signal-resistant Codex descendants",- async (_description, ignoreTermination, terminationSignal) => {+ async (_name, ignoresSignal, signal, verifyTaskkill) => {const directory = await mkdtemp(join(tmpdir(), "codex-security-publication-cancel-"),);@@ -3118,8 +3136,6 @@ describe("connected Linear publication", () => {'const { spawn } = require("node:child_process");','fs.readFileSync(0, "utf8");',"fs.writeFileSync(process.env.CODEX_PUBLICATION_PARENT_PID, String(process.pid));",- "const environment = { ...process.env };",- "delete environment.NODE_OPTIONS;","const descendant = [",' "const fs = require(\\"node:fs\\");",',' "process.on(\\"SIGTERM\\", () => {});",',@@ -3127,7 +3143,7 @@ describe("connected Linear publication", () => {' "fs.writeFileSync(process.env.CODEX_PUBLICATION_DESCENDANT_PID, String(process.pid));",',' "setInterval(() => {}, 1000);",','].join("");',- 'spawn(process.execPath, ["-e", descendant], { env: environment, stdio: "ignore" });',+ 'spawn(process.execPath, ["-e", descendant], { env: { CODEX_PUBLICATION_DESCENDANT_PID: process.env.CODEX_PUBLICATION_DESCENDANT_PID }, stdio: "ignore" });',"const waiter = new Int32Array(new SharedArrayBuffer(4));","for (let attempts = 0; !fs.existsSync(process.env.CODEX_PUBLICATION_DESCENDANT_PID); attempts += 1) {"," if (attempts === 1000) process.exit(3);",@@ -3143,20 +3159,24 @@ describe("connected Linear publication", () => {);const controller = new AbortController();const reason =- terminationSignal === "SIGINT"+ signal === "SIGINT"? "SIGINT": new Error("Publication was interrupted.");+ let taskkill: [string, readonly string[]] | undefined;+ let posixKill: unknown;const injected = dependencies(publication,{},{environment: {- ...process.env,+ ...(process.platform === "win32"+ ? { SystemRoot: process.env["SystemRoot"] }+ : {}),CODEX_SECURITY_STATE_DIR: join(directory, "state"),NODE_OPTIONS: `--require=${JSON.stringify(preload)}`,CODEX_PUBLICATION_PARENT_PID: parentPath,CODEX_PUBLICATION_DESCENDANT_PID: descendantPath,- CODEX_PUBLICATION_IGNORE_TERMINATION: ignoreTermination ? "1" : "0",+ CODEX_PUBLICATION_IGNORE_TERMINATION: ignoresSignal ? "1" : "0",CODEX_PUBLICATION_EVENT: issueEvent(publication.issues[0]!),},resolveCodex: () => ({@@ -3169,41 +3189,77 @@ describe("connected Linear publication", () => {delete injected.runCodex;delete injected.writeReceipt;- await expect(- publishScanInternal(- publication.scanDirectory,- {- ...OPTIONS,- signal: controller.signal,- onProgress: (event) => {- if (event.type === "codex_event") controller.abort(reason);+ try {+ await expect(+ publishScanInternal(+ publication.scanDirectory,+ {+ ...OPTIONS,+ signal: controller.signal,+ onProgress: (event) => {+ if (event.type !== "codex_event") return;+ if (verifyTaskkill) {+ forceTerminatePublicationProcesses({+ platform: "win32",+ systemRoot: "C:\\Synthetic",+ runTaskkill: (command, args) => {+ taskkill = [command, [...args]];+ return { status: 0 };+ },+ });+ }+ controller.abort(reason);+ if (verifyTaskkill && process.platform !== "win32") {+ const kill = spyOn(process, "kill");+ try {+ forceTerminatePublicationProcesses();+ posixKill = kill.mock.calls.at(-1)?.slice();+ } finally {+ kill.mockRestore();+ }+ }+ },},- },- injected,- ),- ).rejects.toThrow(- /Linear publication was interrupted\. The publication handoff remains at .*; recover it before retrying to avoid creating duplicate issues\./u,- );+ injected,+ ),+ ).rejects.toThrow(+ /Linear publication was interrupted\. The publication handoff remains at .*; recover it before retrying to avoid creating duplicate issues\./u,+ );- const parent = Number(await readFile(parentPath, "utf8"));- const descendant = Number(await readFile(descendantPath, "utf8"));- expect(await processHasExited(parent)).toBe(true);- expect(await processHasExited(descendant)).toBe(true);- const receipt = join(- directory,- "state",- "publications",- "linear",- `${createHash("sha256").update(publication.scanId).digest("hex")}.json`,- );- const persisted = JSON.parse(await readFile(receipt, "utf8")) as {- created: Array<{ issueIdentifier: string }>;- counts: { findings: number; created: number; failed: number };- };- expect(persisted.created.map((issue) => issue.issueIdentifier)).toEqual([- "SEC-1",- ]);- expect(persisted.counts).toEqual({ findings: 2, created: 1, failed: 1 });+ const parent = Number(await readFile(parentPath, "utf8"));+ const descendant = Number(await readFile(descendantPath, "utf8"));+ if (verifyTaskkill) {+ expect(taskkill).toEqual([+ "C:\\Synthetic\\System32\\taskkill.exe",+ ["/PID", String(parent), "/T", "/F"],+ ]);+ if (process.platform !== "win32")+ expect(posixKill).toEqual([-parent, "SIGKILL"]);+ }+ expect(await processHasExited(parent)).toBe(true);+ expect(await processHasExited(descendant)).toBe(true);+ const receipt = join(+ directory,+ "state",+ "publications",+ "linear",+ `${createHash("sha256").update(publication.scanId).digest("hex")}.json`,+ );+ const persisted = JSON.parse(await readFile(receipt, "utf8")) as {+ created: Array<{ issueIdentifier: string }>;+ counts: { findings: number; created: number; failed: number };+ };+ expect(persisted.created.map((issue) => issue.issueIdentifier)).toEqual(+ ["SEC-1"],+ );+ expect(persisted.counts).toEqual({+ findings: 2,+ created: 1,+ failed: 1,+ });+ } finally {+ await cleanupPublicationProcesses(parentPath, descendantPath);+ }},30_000,);