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: recover cleanly when the scan dashboard cannot start (#391)

mldangelo-oaief4d7dff4f9c
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index e8483157d..be9bea5ae 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -2890,7 +2890,13 @@ async function runScan(
arguments_.dryRun ? "Validating scan inputs" : "Preparing scan",
);
} else {
- dashboard.start();
+ try {
+ dashboard.start();
+ } catch {
+ dashboard = null;
+ progress = new Progress(errorOutput, dependencies, false);
+ progress.startTimer("Preparing scan");
+ }
}
security = dependencies.createSecurity(config);
const options: ScanOptions = {
diff --git a/sdk/typescript/src/scan-dashboard.ts b/sdk/typescript/src/scan-dashboard.ts
index 06b1a3b27..61f5d6922 100644
--- a/sdk/typescript/src/scan-dashboard.ts
+++ b/sdk/typescript/src/scan-dashboard.ts
@@ -145,17 +145,32 @@ export class ScanDashboard {
public start(): void {
if (this.#timer !== null) return;
- this.#stream.write(`${ENTER_ALTERNATE_SCREEN}${HIDE_CURSOR}`);
const input = this.#options.input;
if (input?.isTTY === true) {
this.#inputWasRaw = input.isRaw === true;
- input.setRawMode?.(true);
- input.resume?.();
- input.on("data", this.#onInput);
- this.#stream.write(ENABLE_ALTERNATE_SCROLL);
}
- this.#render();
this.#timer = this.#options.clock.setInterval(() => this.#render(), 1_000);
+ try {
+ this.#stream.write(`${ENTER_ALTERNATE_SCREEN}${HIDE_CURSOR}`);
+ if (input?.isTTY === true) {
+ input.setRawMode?.(true);
+ input.resume?.();
+ input.on("data", this.#onInput);
+ this.#stream.write(ENABLE_ALTERNATE_SCROLL);
+ }
+ this.#render();
+ } catch (error) {
+ try {
+ this.stop();
+ } catch {
+ try {
+ this.#stream.write(
+ `${DISABLE_ALTERNATE_SCROLL}${SHOW_CURSOR}${EXIT_ALTERNATE_SCREEN}`,
+ );
+ } catch {}
+ }
+ throw error;
+ }
}
public stop(): void {
diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.ts
index 4aadd040c..09eb2157b 100644
--- a/sdk/typescript/tests-ts/cli.test.ts
+++ b/sdk/typescript/tests-ts/cli.test.ts
@@ -1625,6 +1625,35 @@ describe("CLI", () => {
expect(stderr.text()).not.toContain("\r");
});
+ test("falls back to plain progress when the dashboard cannot initialize", async () => {
+ const stdout = capture();
+ const stderr = capture(true);
+ let scans = 0;
+ let closed = 0;
+ let timers = 0;
+ const deps = dependencies({
+ onRun: () => {
+ scans += 1;
+ },
+ onClose: () => {
+ closed += 1;
+ },
+ });
+ deps.setInterval = () => {
+ timers += 1;
+ throw new Error("Dashboard timer unavailable.");
+ };
+
+ expect(await main(["scan", "."], stdout.stream, stderr.stream, deps)).toBe(
+ 0,
+ );
+ expect(scans).toBe(1);
+ expect(closed).toBe(1);
+ expect(timers).toBe(1);
+ expect(stderr.text()).toContain("Preparing scan");
+ expect(stderr.text()).toContain("Running scan");
+ });
+
test("keeps terminal scans in one live dashboard", async () => {
const stdout = capture();
const stderr = capture(true);
diff --git a/sdk/typescript/tests-ts/scan-dashboard.test.ts b/sdk/typescript/tests-ts/scan-dashboard.test.ts
index 048aa4a41..6cfe73c51 100644
--- a/sdk/typescript/tests-ts/scan-dashboard.test.ts
+++ b/sdk/typescript/tests-ts/scan-dashboard.test.ts
@@ -39,6 +39,87 @@ class DashboardTestInput extends EventEmitter {
}
describe("live scan dashboard", () => {
+ test("restores terminal state when dashboard initialization fails", () => {
+ const input = new DashboardTestInput();
+ const output: string[] = [];
+ let timerCleared = false;
+ const dashboard = new ScanDashboard(
+ {
+ write(chunk: string): boolean {
+ output.push(chunk);
+ if (chunk.includes("\u001B[H")) {
+ throw new Error("Dashboard rendering failed.");
+ }
+ return true;
+ },
+ },
+ {
+ repository: "/synthetic/repository",
+ input,
+ clock: {
+ ...fakeClock(),
+ clearInterval: () => {
+ timerCleared = true;
+ },
+ },
+ },
+ );
+
+ expect(() => dashboard.start()).toThrow("Dashboard rendering failed.");
+ expect(timerCleared).toBe(true);
+ expect(input.isRaw).toBe(false);
+ expect(input.listenerCount("data")).toBe(0);
+ expect(output.join("")).toContain("\u001B[?25h\u001B[?1049l");
+ dashboard.stop();
+ });
+
+ test("restores the screen when raw mode setup and cleanup both fail", () => {
+ const stderr = capture(true);
+ const input = new DashboardTestInput();
+ input.setRawMode = (enabled) => {
+ throw new Error(
+ enabled
+ ? "Raw mode initialization failed."
+ : "Raw mode cleanup failed.",
+ );
+ };
+ const dashboard = new ScanDashboard(stderr.stream, {
+ repository: "/synthetic/repository",
+ input,
+ clock: fakeClock(),
+ });
+
+ expect(() => dashboard.start()).toThrow("Raw mode initialization failed.");
+ expect(stderr.text()).toContain("\u001B[?25h\u001B[?1049l");
+ expect(input.listenerCount("data")).toBe(0);
+ });
+
+ test("disables alternate scrolling when dashboard rollback fails", () => {
+ const input = new DashboardTestInput();
+ const output: string[] = [];
+ input.setRawMode = (enabled) => {
+ if (!enabled) throw new Error("Raw mode cleanup failed.");
+ input.isRaw = enabled;
+ return input;
+ };
+ const dashboard = new ScanDashboard(
+ {
+ write(chunk: string): boolean {
+ output.push(chunk);
+ if (chunk.includes("\u001B[H")) {
+ throw new Error("Dashboard rendering failed.");
+ }
+ return true;
+ },
+ },
+ { repository: "/synthetic/repository", input, clock: fakeClock() },
+ );
+
+ expect(() => dashboard.start()).toThrow("Dashboard rendering failed.");
+ expect(output.join("")).toContain("\u001B[?1007h");
+ expect(output.join("")).toContain("\u001B[?1007l\u001B[?25h\u001B[?1049l");
+ });
+
test("redraws real scan activity and metrics in place", () => {
const stderr = capture(true);
const timers: NodeJS.Timeout[] = [];