OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
refactor(cli): share terminal error listener cleanup (#2481)
Michael D'Angelo7607a4d504fb
diff --git a/sdk/typescript/scripts/package-dist-files.mjs b/sdk/typescript/scripts/package-dist-files.mjsindex a4387076b..a09a38418 100644--- a/sdk/typescript/scripts/package-dist-files.mjs+++ b/sdk/typescript/scripts/package-dist-files.mjs@@ -179,6 +179,7 @@ const distModules = ["server/storage","targets","terminal-columns",+ "terminal-stream","thread-source","timestamps","trusted-executable",diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex 009933f3f..8c95afea1 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -350,6 +350,10 @@ import {} from "./scan-history-renderer.js";import { ScanDashboard } from "./scan-dashboard.js";import { fitCell, graphemes } from "./terminal-columns.js";+import {+ TerminalErrorListener,+ type TerminalStream,+} from "./terminal-stream.js";import {ACCOUNT_LIMITS_UNAVAILABLE,formatAccountLimits,@@ -444,13 +448,12 @@ const SHOW_CURSOR = "\u001B[?25h";const CHILD_TERMINATION_GRACE_MS = 1_000;const DUPLICATE_SIGNAL_WINDOW_MS = 500;-type Writable = Pick<NodeJS.WriteStream, "write"> & {- on?(event: "error", listener: (error: Error) => void): unknown;- off?(event: "error", listener: (error: Error) => void): unknown;+interface Writable extends TerminalStream {+ write: NodeJS.WriteStream["write"];readonly isTTY?: boolean;readonly fd?: number;readonly columns?: number;-};+}type SignalName = "SIGINT" | "SIGTERM";const DEFAULT_SCAN_MODEL_CONFIGURATION =@@ -10102,25 +10105,16 @@ async function withTerminalErrorsHandled<T>(errorOutput: Writable,operation: () => Promise<T>,): Promise<T> {- const observeTerminalErrors =+ const listener =typeof errorOutput.on === "function" &&- typeof errorOutput.off === "function";- const ignoreTerminalError = (): void => {};- if (observeTerminalErrors) {- errorOutput.on?.("error", ignoreTerminalError);- }+ typeof errorOutput.off === "function"+ ? new TerminalErrorListener(errorOutput)+ : undefined;+ listener?.start();try {return await operation();} finally {- if (observeTerminalErrors) {- try {- errorOutput.write("", () => {- queueMicrotask(() => errorOutput.off?.("error", ignoreTerminalError));- });- } catch {- errorOutput.off?.("error", ignoreTerminalError);- }- }+ listener?.stop();}}@@ -12187,9 +12181,7 @@ export class Progress {#timerMessage: string | null = null;#timerLineActive = false;#cursorHidden = false;- #observingStreamErrors = false;- #streamErrorGeneration = 0;- readonly #onStreamError = (): void => {};+ readonly #terminalErrors: TerminalErrorListener;public constructor(stream: Writable = process.stderr,@@ -12200,6 +12192,7 @@ export class Progress {interactive = true,) {this.#stream = stream;+ this.#terminalErrors = new TerminalErrorListener(stream);this.#dependencies = dependencies;this.#startedAt = dependencies.now();this.#interactive = interactive;@@ -12217,12 +12210,12 @@ export class Progress {}public stage(message: string): void {- this.#observeStreamErrors();+ this.#terminalErrors.start();this.#stream.write(`${this.#line(message)}\n`);}public startTimer(message: string): void {- this.#observeStreamErrors();+ this.#terminalErrors.start();if (!this.interactive) {this.stage(message);return;@@ -12254,25 +12247,7 @@ export class Progress {this.#cursorHidden = false;}} finally {- if (this.#observingStreamErrors) {- const generation = this.#streamErrorGeneration;- try {- this.#stream.write("", () => {- queueMicrotask(() => {- if (- generation === this.#streamErrorGeneration &&- this.#observingStreamErrors- ) {- this.#stream.off?.("error", this.#onStreamError);- this.#observingStreamErrors = false;- }- });- });- } catch {- this.#stream.off?.("error", this.#onStreamError);- this.#observingStreamErrors = false;- }- }+ this.#terminalErrors.stop();}}@@ -12295,14 +12270,6 @@ export class Progress {return `[${String(Math.floor(elapsedSeconds / 60)).padStart(2, "0")}:${String(elapsedSeconds % 60).padStart(2, "0")}] ${message}`;}- #observeStreamErrors(): void {- this.#streamErrorGeneration += 1;- if (!this.#observingStreamErrors && this.#stream.on !== undefined) {- this.#stream.on("error", this.#onStreamError);- this.#observingStreamErrors = true;- }- }-#renderTimer(message: string): void {const width = Math.max(0, (this.#stream.columns ?? 80) - 1);const line = fitProgressLine(this.#line(message), width);diff --git a/sdk/typescript/src/scan-dashboard.ts b/sdk/typescript/src/scan-dashboard.tsindex bc54a73fc..c14276deb 100644--- a/sdk/typescript/src/scan-dashboard.ts+++ b/sdk/typescript/src/scan-dashboard.ts@@ -4,6 +4,10 @@ import { PassThrough } from "node:stream";import { pathToFileURL } from "node:url";import stripAnsi from "strip-ansi";import stringWidth from "string-width";+import {+ TerminalErrorListener,+ type TerminalStream,+} from "./terminal-stream.js";import { fitCell, fitLine, graphemes } from "./terminal-columns.js";import { isRecord } from "./record.js";import type { ScanAuthMode, ScanBudget } from "./api.js";@@ -61,11 +65,8 @@ const COMPONENT_STATUS = {failed: "Failed",} as const;-interface DashboardStream {+interface DashboardStream extends TerminalStream {readonly writableNeedDrain?: boolean;- write(chunk: string, callback?: (error?: Error | null) => void): unknown;- on?(event: "error", listener: (error: Error) => void): unknown;- off?(event: "error", listener: (error: Error) => void): unknown;readonly columns?: number;readonly rows?: number;}@@ -193,8 +194,7 @@ export class ScanDashboard {#source: "all" | "main" | number = "all";#inputWasRaw = false;#noteCount = 0;- #observingStreamErrors = false;- readonly #onStreamError = (): void => {};+ readonly #terminalErrors: TerminalErrorListener;#keyInput: PassThrough | null = null;#ss3Parameters = false;#inputKeys: {@@ -400,6 +400,7 @@ export class ScanDashboard {public constructor(stream: DashboardStream, options: ScanDashboardOptions) {this.#stream = stream;+ this.#terminalErrors = new TerminalErrorListener(stream);this.#options = options;this.#startedAt = options.clock.now();}@@ -412,10 +413,7 @@ export class ScanDashboard {}this.#timer = this.#options.clock.setInterval(() => this.#refresh(), 1_000);try {- if (!this.#observingStreamErrors && this.#stream.on !== undefined) {- this.#stream.on("error", this.#onStreamError);- this.#observingStreamErrors = true;- }+ this.#terminalErrors.start();this.#stream.write(`${ENTER_ALTERNATE_SCREEN}${HIDE_CURSOR}`);if (input?.isTTY === true) {this.#setKeyInput(new PassThrough());@@ -457,21 +455,7 @@ export class ScanDashboard {`${input?.isTTY === true ? DISABLE_ALTERNATE_SCROLL : ""}${SHOW_CURSOR}${EXIT_ALTERNATE_SCREEN}`,);} finally {- if (this.#observingStreamErrors) {- try {- this.#stream.write("", () => {- queueMicrotask(() => {- if (this.#timer === null && this.#observingStreamErrors) {- this.#stream.off?.("error", this.#onStreamError);- this.#observingStreamErrors = false;- }- });- });- } catch {- this.#stream.off?.("error", this.#onStreamError);- this.#observingStreamErrors = false;- }- }+ this.#terminalErrors.stop();}}diff --git a/sdk/typescript/src/terminal-stream.ts b/sdk/typescript/src/terminal-stream.tsnew file mode 100644index 000000000..7d01c6c3c--- /dev/null+++ b/sdk/typescript/src/terminal-stream.ts@@ -0,0 +1,38 @@+export interface TerminalStream {+ write(chunk: string, callback?: (error?: Error | null) => void): unknown;+ on?(event: "error", listener: (error: Error) => void): unknown;+ off?(event: "error", listener: (error: Error) => void): unknown;+}++export class TerminalErrorListener {+ readonly #ignore = (): void => {};+ #generation = 0;+ #active = false;++ constructor(private readonly stream: TerminalStream) {}++ start(): void {+ this.#generation++;+ if (!this.#active && this.stream.on !== undefined) {+ this.stream.on("error", this.#ignore);+ this.#active = true;+ }+ }++ stop(): void {+ if (!this.#active) return;+ // A later start needs the listener even if these earlier writes have drained.+ const generation = this.#generation;+ const remove = (): void => {+ if (generation === this.#generation && this.#active) {+ this.stream.off?.("error", this.#ignore);+ this.#active = false;+ }+ };+ try {+ this.stream.write("", () => queueMicrotask(remove));+ } catch {+ remove();+ }+ }+}diff --git a/sdk/typescript/tests-ts/scan-dashboard.test.ts b/sdk/typescript/tests-ts/scan-dashboard.test.tsindex 8587dd858..c8473353e 100644--- a/sdk/typescript/tests-ts/scan-dashboard.test.ts+++ b/sdk/typescript/tests-ts/scan-dashboard.test.ts@@ -1783,14 +1783,14 @@ describe("live scan dashboard", () => {dashboard.stop();});- test("handles asynchronous dashboard stream failures while a scan is active", async () => {+ test("keeps dashboard stream errors protected through a restart and the final write", async () => {let redraw: (() => void) | undefined;let failRedraw = false;const stream = new Writable({autoDestroy: false,write(_chunk, _encoding, callback) {if (failRedraw) {- queueMicrotask(() => callback(new Error("Dashboard output failed.")));+ setImmediate(() => callback(new Error("Dashboard output failed.")));} else {callback();}@@ -1807,19 +1807,24 @@ describe("live scan dashboard", () => {},});+ dashboard.start();+ dashboard.stop();dashboard.start();expect(stream.listenerCount("error")).toBe(1);- const failure = new Promise<Error>((resolve) =>- stream.once("error", resolve),+ const failure = new Promise<[Error, number]>((resolve) =>+ stream.once("error", (error) =>+ resolve([error, stream.listenerCount("error")]),+ ),);failRedraw = true;redraw?.();dashboard.stop();expect(stream.listenerCount("error")).toBe(2);- await expect(failure).resolves.toMatchObject({- message: "Dashboard output failed.",- });+ await expect(failure).resolves.toEqual([+ new Error("Dashboard output failed."),+ 1,+ ]);await new Promise<void>((resolve) => queueMicrotask(resolve));expect(stream.listenerCount("error")).toBe(0);});