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(cli): avoid success output after command failures (#1775)

Michael D'Angelo1f1e7583ae74
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 91a8abf09..2f2e19b94 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -2556,11 +2556,12 @@ export async function main(
),
async run({ args, error: incurError, format, options }) {
if (format === "md") {
- errorOutput.write(
- "codex-security: Markdown output is not supported for scan results.\n",
+ const error = new CodexSecurityError(
+ "Markdown output is not supported for scan results.",
);
+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);
exitCode = 2;
- return;
+ throw error;
}
let scanId: string | undefined;
try {
@@ -3382,6 +3383,7 @@ export async function main(
errorOutput.write(`codex-security: ${diagnosticValue(error)}\n`);
exitCode = 2;
}
+ if (cloudBatch === undefined) throw error;
return cloudBatch;
} finally {
removeSignalListeners();
@@ -3411,7 +3413,7 @@ export async function main(
return { ...result };
} catch (error) {
reportPublicationError(error, controller.signal.reason);
- return undefined;
+ throw error;
} finally {
removeSignals();
}
@@ -3493,7 +3495,7 @@ export async function main(
exitCode = 2;
errorOutput.write(`codex-security: ${diagnosticValue(error)}\n`);
}
- return undefined;
+ throw error;
} finally {
removeSignals();
}
@@ -3859,11 +3861,12 @@ export async function main(
if (options.host)
throw new CodexSecurityError("Use scan --host alone.");
if (format === "md") {
- errorOutput.write(
- "codex-security: Markdown output is not supported for scan results.\n",
+ const error = new CodexSecurityError(
+ "Markdown output is not supported for scan results.",
);
+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);
exitCode = 2;
- return;
+ throw error;
}
let outcome: ScanOutcome;
try {
@@ -4027,7 +4030,7 @@ export async function main(
} catch (error) {
errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);
exitCode = 2;
- return undefined;
+ throw error;
}
},
})
@@ -4095,7 +4098,7 @@ export async function main(
`codex-security: ${signal === "SIGINT" || signal === "SIGTERM" ? "Owner suggestions canceled." : diagnosticValue(error)}\n`,
);
exitCode = interruptedExitCode(controller.signal) ?? 2;
- return undefined;
+ throw error;
} finally {
removeSignals();
}
@@ -4203,7 +4206,7 @@ export async function main(
`codex-security: ${signal === "SIGINT" || signal === "SIGTERM" ? "Severity classification canceled." : diagnosticValue(error)}\n`,
);
exitCode = interruptedExitCode(controller.signal) ?? 2;
- return undefined;
+ throw error;
} finally {
removeSignals();
}
@@ -4631,6 +4634,7 @@ export async function main(
stopDashboard();
exitCode = interruptedExitCode(controller.signal) ?? 2;
errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);
+ throw error;
} finally {
stopDashboard();
signalHandlers(dependencies, "remove", onInterrupt, onTerminate);
@@ -4934,6 +4938,7 @@ export async function main(
? 130
: 2);
errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);
+ throw error;
} finally {
removeSignals();
}
@@ -5057,34 +5062,27 @@ export async function main(
sourceRoot,
pythonPath: options.python,
};
- try {
- const prepared = await resolveArtifactExportOutput(
- arguments_,
- dependencies.currentDirectory(),
- );
- const contents = await dependencies.exportFindings(
- prepared,
- output,
- );
- if (arguments_.output === "-") {
- if (contents !== undefined) {
- await writeCliOutput(output, Buffer.from(contents));
- }
- } else {
- errorOutput.write(
- `${arguments_.format.toUpperCase()}: ${arguments_.output}\n`,
- );
+ const prepared = await resolveArtifactExportOutput(
+ arguments_,
+ dependencies.currentDirectory(),
+ );
+ const contents = await dependencies.exportFindings(prepared, output);
+ if (arguments_.output === "-") {
+ if (contents !== undefined) {
+ await writeCliOutput(output, Buffer.from(contents));
}
- exitCode = 0;
- } catch (error) {
- errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);
- exitCode = 2;
+ } else {
+ errorOutput.write(
+ `${arguments_.format.toUpperCase()}: ${arguments_.output}\n`,
+ );
}
+ exitCode = 0;
} catch (error) {
if (exitCode !== 2) {
errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);
}
exitCode = 2;
+ throw error;
}
},
})
@@ -5157,9 +5155,11 @@ export async function main(
} catch (error) {
exitCode = 2;
errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);
+ throw error;
} finally {
await knowledgeBase?.cleanup();
}
+ if (exitCode !== 0) frameworkExit = exitCode;
},
})
.command("verify-fix", {
@@ -5317,6 +5317,7 @@ export async function main(
);
if (exitCode !== 0) {
if (exitCode !== 130 && exitCode !== 143) exitCode = 2;
+ frameworkExit = exitCode;
return undefined;
}
@@ -5371,7 +5372,7 @@ export async function main(
} catch (error) {
exitCode = 2;
errorOutput.write(`codex-security: ${diagnosticValue(error)}\n`);
- return undefined;
+ throw error;
}
},
})
@@ -5726,7 +5727,7 @@ export async function main(
if (exitCode === 0) exitCode = 2;
const message = errorMessage(error);
errorOutput.write(`codex-security: ${diagnosticValue(message)}\n`);
- if (!structuredOutput) return;
+ if (!structuredOutput) throw error;
structuredError = true;
const failure = {
code:
@@ -5908,6 +5909,7 @@ export async function main(
} catch (error) {
exitCode = 2;
errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);
+ throw error;
}
},
})
diff --git a/sdk/typescript/tests-ts/cli-diagnostics.test.ts b/sdk/typescript/tests-ts/cli-diagnostics.test.ts
index b96d765f1..01a4638b2 100644
--- a/sdk/typescript/tests-ts/cli-diagnostics.test.ts
+++ b/sdk/typescript/tests-ts/cli-diagnostics.test.ts
@@ -13,6 +13,72 @@ import { throwing } from "./support/errors.js";
import { createCliTest } from "./support/cli-run.js";
describe("CLI diagnostics", () => {
+ test.each<string[]>([
+ ["publish", "scan", "completed-scan", "--to", "cloud"],
+ ["publish", "check", "completed-scan", "--to", "linear"],
+ ["import", "github", "example/repository"],
+ ["install-hook"],
+ ["suggest-owners", "findings.json"],
+ ["classify-severity", "--scan", "latest"],
+ ["scan-components", "--component", "api=src", "--output-dir", "out"],
+ ["bulk-scan", "repositories.csv", "--output-dir", "out"],
+ ["export", "completed-scan", "--output", "findings.json"],
+ ["validate", "Synthetic finding"],
+ ["verify-fix", "Synthetic finding"],
+ ["init"],
+ ])("does not format a success result after %j fails", async (...command) => {
+ for (const flags of [["--full-output"], ["--token-count"]]) {
+ const { stdout, stderr, runCli } = createCliTest(main);
+ const deps = dependencies();
+ deps.currentDirectory = throwing("Synthetic command failure.");
+
+ expect(await runCli([...command, ...flags], deps)).toBe(2);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toBe(
+ "codex-security: Synthetic command failure.\n",
+ );
+ }
+ });
+
+ test("does not format a token count after patch fails", async () => {
+ const { stdout, stderr, runCli } = createCliTest(main);
+ const deps = dependencies();
+ deps.currentDirectory = throwing("Synthetic command failure.");
+
+ expect(
+ await runCli(["patch", "Synthetic finding", "--token-count"], deps),
+ ).toBe(2);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toBe("codex-security: Synthetic command failure.\n");
+ });
+
+ test.each<string[]>([["scan"], ["scans", "rerun", "scan-original"]])(
+ "rejects late Markdown selection for %j without a success result",
+ async (...command) => {
+ const { stdout, stderr, runCli } = createCliTest(main);
+ const fail = throwing("Must not load or execute a scan.");
+ const deps = dependencies({ onWorkbench: fail, onRun: fail });
+ deps.currentDirectory = fail;
+ expect(
+ await runCli(
+ [
+ ...command,
+ "--filter-output",
+ "--help",
+ "--format",
+ "md",
+ "--full-output",
+ ],
+ deps,
+ ),
+ ).toBe(2);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toBe(
+ "codex-security: Markdown output is not supported for scan results.\n",
+ );
+ },
+ );
+
test.each([
{
command: "policy",
@@ -50,6 +116,11 @@ describe("CLI diagnostics", () => {
args: ["patch", "Synthetic finding", "--json"],
structured: true,
},
+ {
+ command: "patch (full-output)",
+ args: ["patch", "Synthetic finding", "--json", "--full-output"],
+ structured: true,
+ },
{
command: "scan import",
args: ["--json", "scan", "import", "--csv", "findings.csv"],
@@ -82,6 +153,7 @@ describe("CLI diagnostics", () => {
expect(stderr.text()).not.toContain("\u001b");
if (structured) {
const result = JSON.parse(stdout.text());
+ expect(result.ok).toBe(false);
expect(result.error?.message ?? result.message).toBe(message);
}
},
diff --git a/sdk/typescript/tests-ts/cli-skills.test.ts b/sdk/typescript/tests-ts/cli-skills.test.ts
index d679bc63a..974eee741 100644
--- a/sdk/typescript/tests-ts/cli-skills.test.ts
+++ b/sdk/typescript/tests-ts/cli-skills.test.ts
@@ -1500,6 +1500,32 @@ process.stdout.write(JSON.stringify({
);
});
+ test.each([7, 130, 143])(
+ "retains validation output and returned status %i without a success envelope",
+ async (status) => {
+ for (const flags of [
+ ["--full-output"],
+ ["--format", "yaml", "--full-output"],
+ ["--token-count"],
+ ]) {
+ const { stdout, stderr, runCli } = createCliTest(main);
+ expect(
+ await runCli(
+ ["validate", "Synthetic finding", ...flags],
+ dependencies({
+ onCodex: (_args, output) => {
+ output?.stdout.write("Partial assessment.\n");
+ return status;
+ },
+ }),
+ ),
+ ).toBe(status);
+ expect(stdout.text()).toBe("Partial assessment.\n");
+ expect(stderr.text()).toBe("");
+ }
+ },
+ );
+
test("escapes native validation launch failures at the CLI boundary", async () => {
const directory = await temporaryDirectory("validation-launch-failure-");
try {
diff --git a/sdk/typescript/tests-ts/cli-verify-fix.test.ts b/sdk/typescript/tests-ts/cli-verify-fix.test.ts
index 1a917572e..0ca08121b 100644
--- a/sdk/typescript/tests-ts/cli-verify-fix.test.ts
+++ b/sdk/typescript/tests-ts/cli-verify-fix.test.ts
@@ -418,19 +418,70 @@ describe("read-only finding verification", () => {
] as const)(
"maps Codex exit %i to verification exit %i",
async (codexStatus, expectedStatus) => {
- const stdout = captureCli(main, "stdout");
+ for (const flags of [
+ ["--json"],
+ ["--json", "--full-output"],
+ ["--format", "yaml", "--full-output"],
+ ["--token-count"],
+ ]) {
+ const stdout = captureCli(main, "stdout");
+ expect(
+ await stdout.run(
+ ["verify-fix", "Synthetic finding", ...flags],
+ dependencies({ onCodex: () => codexStatus }),
+ ),
+ ).toBe(expectedStatus);
+ expect(stdout.text()).toBe("");
+ }
+ },
+ );
+ test.each([
+ ["fixed", 0],
+ ["still_vulnerable", 1],
+ ["inconclusive", 2],
+ ] as const)(
+ "retains completed %s results with full output",
+ async (status, code) => {
+ const { stdout, runCli } = createCliTest(main);
+ const results = [
+ {
+ id: "finding-1",
+ status,
+ evidence: "Synthetic verification evidence.",
+ },
+ ];
expect(
- await stdout.run(
- [
- "verify-fix",
- "A previously reported authorization bypass",
- "--json",
- ],
- dependencies({ onCodex: () => codexStatus }),
+ await runCli(
+ ["verify-fix", "Synthetic finding", "--json", "--full-output"],
+ dependencies({
+ onCodex: (_args, output) => {
+ output?.stdout.write(JSON.stringify({ results }));
+ return 0;
+ },
+ }),
),
- ).toBe(expectedStatus);
- expect(stdout.text()).toBe("");
+ ).toBe(code);
+ expect(JSON.parse(stdout.text()).data.results).toEqual(results);
+ },
+ );
+
+ test.each([130, 143])(
+ "preserves interrupted status %i without writing a success envelope",
+ async (status) => {
+ const { stderr } = createCliTest(main);
+ const write = mock(() => {
+ throw new Error("Unexpected stdout write.");
+ });
+ expect(
+ await main(
+ ["verify-fix", "Synthetic finding", "--json", "--full-output"],
+ { write },
+ stderr.stream,
+ dependencies({ onCodex: () => status }),
+ ),
+ ).toBe(status);
+ expect(write).not.toHaveBeenCalled();
},
);