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): report full-output scan failures as errors (#971)

abhijeet sharmae268029bc2bd
diff --git a/sdk/typescript/README.md b/sdk/typescript/README.md
index b846992df..6d3406509 100644
--- a/sdk/typescript/README.md
+++ b/sdk/typescript/README.md
@@ -598,6 +598,9 @@ execution failure writes one structured object to stdout:
}
```
+With `--full-output`, the same code and message are reported under `error` in
+an `ok: false` envelope instead.
+
The command still exits with `2` for runtime, export, invalid-input, or
incomplete-scan failures, and human-readable diagnostics remain on stderr.
Use `scan --schema --format json` to discover this failure variant alongside
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 496ecd068..9423a4eb9 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -1815,6 +1815,7 @@ export async function main(
let renderedPolicy: string | undefined;
let renderedPatch: string | undefined;
let patchStructuredError = false;
+ let scanStructuredError = false;
const runImport = (options: ImportScanOptions) =>
runScanImport(options, errorOutput, dependencies);
const history = async (
@@ -3677,11 +3678,12 @@ export async function main(
exitCode = outcome.exitCode;
if (outcome.error !== undefined) {
if (format === "json" || format === "jsonl") {
- return {
- status: "failed",
- code: "SCAN_FAILED",
- message: safeErrorMessage(outcome.error),
- };
+ const message = safeErrorMessage(outcome.error);
+ if (!argv.includes("--full-output"))
+ return { status: "failed", code: "SCAN_FAILED", message };
+ // Incur would wrap returned data in an ok: true envelope.
+ scanStructuredError = true;
+ return incurError({ code: "SCAN_FAILED", message, exitCode });
}
return incurError({
code: "SCAN_FAILED",
@@ -5773,7 +5775,7 @@ export async function main(
}
if (notice !== undefined) errorOutput.write(formatUpdateNotice(notice));
if (frameworkExit !== undefined) {
- if (policyFullOutput || patchStructuredError) {
+ if (policyFullOutput || patchStructuredError || scanStructuredError) {
if (exitCode === 0) exitCode = 2;
} else {
if (exitCode !== 0) return exitCode;
diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.ts
index 56ad5004c..d1ee568d7 100644
--- a/sdk/typescript/tests-ts/cli.test.ts
+++ b/sdk/typescript/tests-ts/cli.test.ts
@@ -4130,6 +4130,70 @@ describe("CLI", () => {
}
});
+ test("reports full-output scan failures in an error envelope", async () => {
+ const message = "This content was flagged for possible cybersecurity risk.";
+ for (const formatArgs of [
+ ["--json"],
+ ["--format", "json"],
+ ["--format", "jsonl"],
+ ] as const) {
+ const stdout = capture();
+ const stderr = capture();
+ const deps = dependencies();
+ deps.createSecurity = () => ({
+ run: async () => {
+ throw new CodexSecurityError(message);
+ },
+ preflight: async () => fakePreflight(),
+ close: async () => {},
+ });
+
+ expect(
+ await main(
+ ["scan", ".", ...formatArgs, "--full-output"],
+ stdout.stream,
+ stderr.stream,
+ deps,
+ ),
+ ).toBe(2);
+ const envelope = JSON.parse(stdout.text().trim());
+ expect(envelope.ok).toBe(false);
+ expect(envelope.error).toEqual({ code: "SCAN_FAILED", message });
+ expect(envelope).not.toHaveProperty("data");
+ expect(stderr.text()).toContain(`${message}\n`);
+ }
+ });
+
+ test("redacts credentials from full-output scan failures", async () => {
+ const stdout = capture();
+ const stderr = capture();
+ const deps = dependencies();
+ deps.createSecurity = () => ({
+ run: async () => {
+ throw new CodexSecurityError(
+ `network failure ECONNRESET ${SYNTHETIC_CREDENTIALS}`,
+ );
+ },
+ preflight: async () => fakePreflight(),
+ close: async () => {},
+ });
+
+ expect(
+ await main(
+ ["scan", ".", "--json", "--full-output"],
+ stdout.stream,
+ stderr.stream,
+ deps,
+ ),
+ ).toBe(2);
+ expect(JSON.parse(stdout.text()).error).toEqual({
+ code: "SCAN_FAILED",
+ message: "[redacted]",
+ });
+ expect(stdout.text()).not.toContain("SYNTHETIC_KEY_123");
+ expect(stderr.text()).toContain("SYNTHETIC_KEY_123");
+ });
+
test("surfaces underlying scanner errors instead of inventing a model outage", async () => {
for (const message of [
"Could not save the Codex Security scan: UNIQUE constraint failed: scans.scan_dir",