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): preserve results when update notices fail (#1800)

Michael D'Angelod08f215550f7
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index ffa6a2574..5adfa4386 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -6450,7 +6450,11 @@ export async function main(
renderedOutput = failureOutput.text();
}
let frameworkOutput = frameworkCapture.text();
- if (notice !== undefined) errorOutput.write(formatUpdateNotice(notice));
+ if (notice !== undefined) {
+ try {
+ errorOutput.write(formatUpdateNotice(notice));
+ } catch {}
+ }
if (frameworkExit !== undefined) {
if (policyFullOutput || structuredError) {
if (exitCode === 0) exitCode = 2;
diff --git a/sdk/typescript/tests-ts/update-notice.test.ts b/sdk/typescript/tests-ts/update-notice.test.ts
index c4c0e1b1c..e76385832 100644
--- a/sdk/typescript/tests-ts/update-notice.test.ts
+++ b/sdk/typescript/tests-ts/update-notice.test.ts
@@ -7,7 +7,7 @@ import {
formatUpdateNotice,
updateCommand,
} from "../src/version.js";
-import { capture, dependencies } from "./cli-fixtures.js";
+import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";
import { main } from "../src/cli.js";
import { rejecting } from "./support/errors.js";
import { responding } from "./support/responses.js";
@@ -311,4 +311,109 @@ describe("CLI update notice", () => {
});
expect(stderr.text()).toBe("");
});
+
+ const notice = {
+ currentVersion: "0.1.0",
+ latestVersion: "0.2.0",
+ command: "synthetic update command",
+ };
+
+ test("preserves JSON output when formatting the update banner fails", async () => {
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+ let attempts = 0;
+ expect(
+ await runCli(
+ ["info", "--json"],
+ dependencies({
+ onUpdateCheck: resolving({
+ ...notice,
+ get command(): string {
+ attempts += 1;
+ throw new Error("Synthetic update formatting failure");
+ },
+ }),
+ }),
+ ),
+ ).toBe(0);
+ expect(attempts).toBe(1);
+ expect(JSON.parse(stdout.text())).toMatchObject({
+ cliVersion: expect.any(String),
+ });
+ expect(stderr.text()).toBe("");
+ });
+
+ test.each([
+ ["success", 0, ""],
+ ["failure", 2, "Synthetic scan failure"],
+ ["SIGINT", 130, "Scan canceled by Ctrl-C."],
+ ["SIGTERM", 143, "Scan terminated by SIGTERM."],
+ ] as const)(
+ "preserves %s results when writing the update banner fails",
+ async (outcome, exitCode, message) => {
+ const stdout = capture();
+ const stderr = capture(true);
+ const signals = new FakeSignals();
+ let attempts = 0;
+ const write = stderr.stream.write;
+ stderr.stream.write = (chunk) => {
+ if (chunk.toString() === formatUpdateNotice(notice)) {
+ attempts += 1;
+ throw new Error("Synthetic update write failure");
+ }
+ return write(chunk);
+ };
+ const deps = dependencies({
+ signals,
+ onUpdateCheck: resolving(notice),
+ onRun: () => {
+ if (outcome === "SIGINT" || outcome === "SIGTERM")
+ signals.emit(outcome);
+ if (outcome === "failure") throw new Error(message);
+ },
+ });
+
+ expect(
+ await main(
+ outcome === "success" ? ["info", "--json"] : ["scan", ".", "--json"],
+ stdout.stream,
+ stderr.stream,
+ deps,
+ ),
+ ).toBe(exitCode);
+ expect(attempts).toBe(1);
+ expect(JSON.parse(stdout.text())).toMatchObject(
+ outcome === "success"
+ ? { cliVersion: expect.any(String) }
+ : { status: "failed", code: "SCAN_FAILED", message },
+ );
+ if (outcome !== "success") expect(stderr.text()).toContain(message);
+ expect(signals.listeners.get("SIGINT")?.size ?? 0).toBe(0);
+ expect(signals.listeners.get("SIGTERM")?.size ?? 0).toBe(0);
+ },
+ );
+
+ test("still reports required stdout failures after an update banner failure", async () => {
+ const stderr = capture(true);
+ const write = stderr.stream.write;
+ stderr.stream.write = (chunk) => {
+ if (chunk.toString() === formatUpdateNotice(notice))
+ throw new Error("Synthetic update write failure");
+ return write(chunk);
+ };
+
+ expect(
+ await main(
+ ["info", "--json"],
+ {
+ write() {
+ throw new Error("Synthetic stdout failure");
+ },
+ },
+ stderr.stream,
+ dependencies({ onUpdateCheck: resolving(notice) }),
+ ),
+ ).toBe(2);
+ expect(stderr.text()).toContain("Synthetic stdout failure");
+ expect(stderr.text()).not.toContain("Synthetic update write failure");
+ });
});