Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(deep-scan): finish cancellation when preflight ignores termination (#1049)

mldangelo-oaiffc3bca75251
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts b/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts
index f228f3f35..f416ca556 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts
@@ -317,7 +317,20 @@ class AppServerPreflightClient {
this.removeAbortListener();
this.stopChild();
if (this.childClosed) return;
- await this.childClose;
+ const forcedTermination = setTimeout(() => {
+ if (this.child.exitCode === null && this.child.signalCode === null) {
+ this.child.kill("SIGKILL");
+ }
+ // Descendants can keep inherited pipes open after the child exits.
+ this.child.stdin.destroy();
+ this.child.stdout.destroy();
+ this.child.stderr.destroy();
+ }, 1_000);
+ try {
+ await this.childClose;
+ } finally {
+ clearTimeout(forcedTermination);
+ }
}
private write(message: JsonRecord): void {
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
index d6d69f9dc..c65e64550 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
@@ -9,6 +9,7 @@ interface PreflightFixture {
envPath: string;
readyPath: string;
terminatedPath: string;
+ descendantPidPath: string;
children: ChildProcess[];
}
import { temporaryDirectory } from "./support/temporary-directories.ts";
@@ -79,6 +80,7 @@ await testRuntimeFallbackWarningClassification();
await testSpawnErrorFailsClosed();
await testMissingWorkerDirectoryRemainsRetryable();
await testAbortKillsPreflightChild();
+await testAbortPreservesCallerReason();
async function testAllowedProfileAndRawArgv() {
await withFakeCodex(
@@ -702,6 +704,71 @@ async function testRuntimeFallbackWarningClassification() {
}
async function testAbortKillsPreflightChild() {
+ for (const [ignoreTermination, inheritStdio] of [
+ [false, false],
+ [true, false],
+ [false, true],
+ [true, true],
+ ]) {
+ await withFakeCodex(
+ {
+ hangAt: "config/read",
+ ignoreTermination,
+ inheritStdio,
+ },
+ async ({
+ codexPath,
+ cwd,
+ readyPath,
+ terminatedPath,
+ descendantPidPath,
+ children,
+ }) => {
+ const controller = new AbortController();
+ const running = preflightDeepScanWorkerPermissionProfile({
+ codexPath,
+ cwd,
+ configOverrides: rawOverrides,
+ expectedProfile,
+ signal: controller.signal,
+ });
+ await waitForFile(readyPath);
+ controller.abort(new DOMException("fixture aborted", "AbortError"));
+ let timeout: NodeJS.Timeout | undefined;
+ try {
+ await assert.rejects(
+ Promise.race([
+ running,
+ new Promise((_, reject) => {
+ timeout = setTimeout(
+ () => reject(new Error("Preflight did not stop")),
+ 5_000,
+ );
+ }),
+ ]),
+ (error: NodeJS.ErrnoException) => error?.name === "AbortError",
+ );
+ } finally {
+ clearTimeout(timeout);
+ }
+ assert.ok(
+ children[0].exitCode !== null || children[0].signalCode !== null,
+ );
+ if (inheritStdio) {
+ const pid = Number(await readFile(descendantPidPath, "utf8"));
+ assert.doesNotThrow(() => process.kill(pid, 0));
+ }
+ if (ignoreTermination && process.platform !== "win32") {
+ assert.equal(children[0].signalCode, "SIGKILL");
+ } else {
+ await assertPreflightStopped(children, terminatedPath);
+ }
+ },
+ );
+ }
+}
+
+async function testAbortPreservesCallerReason() {
for (const run of [
preflightDeepScanWorkerPermissionProfile,
readDeepScanRuntimeConfig,
@@ -833,6 +900,7 @@ async function withFakeCodex(
const envPath = path.join(root, "env.json");
const readyPath = path.join(root, "ready");
const terminatedPath = path.join(root, "terminated");
+ const descendantPidPath = path.join(root, "descendant-pid");
const fixture = {
...scenario,
argvPath,
@@ -841,6 +909,7 @@ async function withFakeCodex(
envPath,
readyPath,
terminatedPath,
+ descendantPidPath,
};
await writeFile(scriptPath, fakeCodexSource(fixture), "utf8");
await chmod(scriptPath, 0o755);
@@ -890,9 +959,25 @@ async function withFakeCodex(
envPath,
readyPath,
terminatedPath,
+ descendantPidPath,
children,
});
} finally {
+ const descendantPid = await readFile(descendantPidPath, "utf8").catch(
+ (error: NodeJS.ErrnoException) => {
+ if (error.code !== "ENOENT") throw error;
+ return undefined;
+ },
+ );
+ if (descendantPid !== undefined)
+ process.kill(Number(descendantPid), "SIGKILL");
+ for (const child of children) {
+ if (child.exitCode === null && child.signalCode === null) {
+ const closed = new Promise((resolve) => child.once("close", resolve));
+ child.kill("SIGKILL");
+ await closed;
+ }
+ }
childProcess.spawn = originalSpawn;
syncBuiltinESMExports();
await rm(root, { recursive: true, force: true });
@@ -901,6 +986,7 @@ async function withFakeCodex(
function fakeCodexSource(scenario: unknown) {
return `#!/usr/bin/env node
+import { spawn } from "node:child_process";
import { appendFileSync, writeFileSync } from "node:fs";
const scenario = JSON.parse(${JSON.stringify(JSON.stringify(scenario))});
@@ -911,12 +997,21 @@ writeFileSync(scenario.envPath, JSON.stringify({
sentinel: process.env.DEEP_SCAN_PREFLIGHT_ENV_SENTINEL ?? null
}));
if (scenario.stderr) process.stderr.write(scenario.stderr);
+if (scenario.inheritStdio) {
+ const descendant = spawn(process.execPath, ["-e", "setInterval(() => {}, 1_000)"], {
+ detached: true,
+ stdio: ["ignore", "inherit", "inherit"],
+ });
+ writeFileSync(scenario.descendantPidPath, String(descendant.pid));
+ descendant.unref();
+}
let buffer = "";
let catalogIndex = 0;
+if (scenario.ignoreTermination) setInterval(() => {}, 1_000);
process.on("SIGTERM", () => {
writeFileSync(scenario.terminatedPath, "SIGTERM");
- process.exit(0);
+ if (!scenario.ignoreTermination) process.exit(0);
});
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => {
@@ -934,7 +1029,7 @@ process.stdin.on("data", (chunk) => {
});
process.stdin.on("end", () => {
writeFileSync(scenario.terminatedPath, "stdin-end");
- process.exit(0);
+ if (!scenario.ignoreTermination) process.exit(0);
});
function handle(message) {