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(sdk): simplify scan event callback dispatch (#1354)
mldangelo-oaif2c212417081
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.tsindex 639143df6..8474ce7f4 100644--- a/sdk/typescript/src/api.ts+++ b/sdk/typescript/src/api.ts@@ -917,12 +917,7 @@ export class CodexSecurity {let accumulatedCost: ScanCost | null = null;let completeCost = true;const warn = (message: string): void =>- notifyObserver(- "onWarning",- options.onWarning,- options.onObserverError,- message,- );+ notifyObserver(options, "onWarning")(message);try {const target = await resolveSecurityPolicyTarget(repository,@@ -995,12 +990,7 @@ export class CodexSecurity {outputDir,);}- notifyObserver(- "onOutputDirReady",- options.onOutputDirReady,- options.onObserverError,- outputDir,- );+ notifyObserver(options, "onOutputDirReady")(outputDir);const guidance = await resolveSecurityPolicyGuidance(target,runtime.plugin.pluginRoot,@@ -1044,13 +1034,7 @@ export class CodexSecurity {);const reportCost = (current: Readonly<ScanCost>): void => {const total = addScanCosts(accumulatedCost, current);- if (completeCost)- notifyObserver(- "onCost",- options.onCost,- options.onObserverError,- total,- );+ if (completeCost) notifyObserver(options, "onCost")(total);if (options.maxCostUsd !== undefined &&total.estimatedUsd > options.maxCostUsd@@ -1180,13 +1164,7 @@ export class CodexSecurity {protectedRoot: inputs.protectedRoot,signal,onWarning: warn,- onStage: (stage) =>- notifyObserver(- "onStage",- options.onStage,- options.onObserverError,- stage,- ),+ onStage: (stage) => notifyObserver(options, "onStage")(stage),answerQuestions: options.answerQuestions,run,cost: () => (completeCost ? accumulatedCost : null),@@ -1373,12 +1351,7 @@ export class CodexSecurity {);requireOutputOutsideRepository(protectedRoot, scanDir);requireModelSafeOutputDir(scanDir);- notifyObserver(- "onOutputDirReady",- options.onOutputDirReady,- options.onObserverError,- scanDir,- );+ notifyObserver(options, "onOutputDirReady")(scanDir);checkOpen();const workerProviderEnvironment = {@@ -1511,11 +1484,9 @@ export class CodexSecurity {}reviewedFileCount = progress.filesCompleted;notifyObserver(+ options,"onProgress",- options.onProgress,- options.onObserverError,- { ...progress, filesTotal: scopeFileCount },- );+ )({ ...progress, filesTotal: scopeFileCount });};const reportTrackingError = (error: unknown): void => {if (options.maxCostUsd !== undefined) {@@ -1523,11 +1494,9 @@ export class CodexSecurity {return;}notifyObserver(+ options,"onWarning",- options.onWarning,- options.onObserverError,- `Could not track scan activity: ${errorMessage(error)}`,- );+ )(`Could not track scan activity: ${errorMessage(error)}`);};const tracker = new ScanCostTracker({codexHome: runtime.codexHome,@@ -1538,33 +1507,15 @@ export class CodexSecurity {onActivity:options.onActivity === undefined? undefined- : (activity) =>- notifyObserver(- "onActivity",- options.onActivity,- options.onObserverError,- activity,- ),+ : (activity) => notifyObserver(options, "onActivity")(activity),onWorkerEvent:options.onWorkerEvent === undefined? undefined- : (event) =>- notifyObserver(- "onWorkerEvent",- options.onWorkerEvent,- options.onObserverError,- event,- ),+ : (event) => notifyObserver(options, "onWorkerEvent")(event),onSessionEvent:options.onSessionEvent === undefined? undefined- : (event) =>- notifyObserver(- "onSessionEvent",- options.onSessionEvent,- options.onObserverError,- event,- ),+ : (event) => notifyObserver(options, "onSessionEvent")(event),onProgress:options.onProgress === undefined ? undefined : reportProgress,onCost:@@ -1572,13 +1523,7 @@ export class CodexSecurity {? undefined: (cost) => {latestCost = cost;- notifyObserver(- "onCost",- options.onCost,- options.onObserverError,- cost,- maxCostUsd,- );+ notifyObserver(options, "onCost")(cost, maxCostUsd);if (maxCostUsd !== undefined &&cost.estimatedUsd > maxCostUsd@@ -1633,20 +1578,14 @@ export class CodexSecurity {);if (budgetSignal.aborted) return;maxCostUsd = next;- notifyObserver(- "onCost",- options.onCost,- options.onObserverError,- latestCost!,- maxCostUsd,- );+ notifyObserver(options, "onCost")(latestCost!, maxCostUsd);}).catch((error: unknown) => {if (!budgetSignal.aborted) {notifyObserver(+ options,"onWarning",- options.onWarning,- options.onObserverError,+ )(`Could not increase scan cost limit: ${errorMessage(error)}`,);}@@ -1811,15 +1750,13 @@ export class CodexSecurity {if (scopeFileCount !== null) {tracker.setExpectedFilesTotal(scopeFileCount);notifyObserver(+ options,"onProgress",- options.onProgress,- options.onObserverError,- {- phase: "preflight",- filesCompleted: 0,- filesTotal: scopeFileCount,- },- );+ )({+ phase: "preflight",+ filesCompleted: 0,+ filesTotal: scopeFileCount,+ });}activeScan = { id: scanId, options: workbenchOptions };if (mode === "deep" && options.onDeepProgress !== undefined) {@@ -1834,21 +1771,14 @@ export class CodexSecurity {["get-scan", "--scan-id", scanId],),onProgress: (progress) =>- notifyObserver(- "onDeepProgress",- options.onDeepProgress,- options.onObserverError,- progress,- ),+ notifyObserver(options, "onDeepProgress")(progress),onError: (error) => {if (progressWarningReported) return;progressWarningReported = true;notifyObserver(+ options,"onWarning",- options.onWarning,- options.onObserverError,- `Could not track Deep Scan progress: ${errorMessage(error)}`,- );+ )(`Could not track Deep Scan progress: ${errorMessage(error)}`);},});deepProgressTracker.start();@@ -2062,11 +1992,9 @@ export class CodexSecurity {]);} catch (error) {notifyObserver(+ options,"onWarning",- options.onWarning,- options.onObserverError,- `Could not save scan session: ${errorMessage(error)}`,- );+ )(`Could not save scan session: ${errorMessage(error)}`);}},onFinalize: async (usage) => {@@ -2105,10 +2033,7 @@ export class CodexSecurity {})).events,onReconnect: (message, attempts) =>- notifyObserver(- "onReconnect",- options.onReconnect,- options.onObserverError,+ notifyObserver(options, "onReconnect")(...attempts,reconnectDetails(message),),@@ -2137,9 +2062,9 @@ export class CodexSecurity {throwIfAborted(signal, scanDir);if (options.maxCostUsd !== undefined && snapshot.cost === null) {notifyObserver(+ options,"onWarning",- options.onWarning,- options.onObserverError,+ )("Scan completed, but its cost limit could not be verified because model pricing or token usage is unavailable.",);}@@ -2223,10 +2148,7 @@ export class CodexSecurity {]);for (const warning of completedScan["warnings"]) {if (typeof warning === "string") {- notifyObserver(- "onWarning",- options.onWarning,- options.onObserverError,+ notifyObserver(options, "onWarning")(warning,targetWarnings.has(warning)? { kind: "target_changed" }@@ -2321,11 +2243,9 @@ export class CodexSecurity {protectedRoot,);notifyObserver(+ options,"onWarning",- options.onWarning,- options.onObserverError,- `Could not run post-scan instructions: ${errorMessage(error)}`,- );+ )(`Could not run post-scan instructions: ${errorMessage(error)}`);}}try {@@ -2381,11 +2301,9 @@ export class CodexSecurity {}} catch (error) {notifyObserver(+ options,"onWarning",- options.onWarning,- options.onObserverError,- `Could not update repository findings: ${errorMessage(error)}`,- );+ )(`Could not update repository findings: ${errorMessage(error)}`);}return result;} catch (error) {@@ -2472,10 +2390,7 @@ export class CodexSecurity {for (const warning of warnings.length > 0? warnings: [failure.message]) {- notifyObserver(- "onWarning",- options.onWarning,- options.onObserverError,+ notifyObserver(options, "onWarning")(warning,targetWarnings.has(warning)? { kind: "target_changed" }@@ -2521,9 +2436,9 @@ export class CodexSecurity {}} catch (postScanError) {notifyObserver(+ options,"onWarning",- options.onWarning,- options.onObserverError,+ )(`Could not run post-scan instructions: ${errorMessage(postScanError)}`,);}@@ -3074,12 +2989,7 @@ export class CodexSecurity {"safetyIdentifier requires API-key authentication.",);}- notifyObserver(- "onAuthentication",- options.onAuthentication,- options.onObserverError,- authentication,- );+ notifyObserver(options, "onAuthentication")(authentication);const python = await (this.#dependencies.resolvePluginPython ?? resolvePluginPython)({@@ -3286,12 +3196,7 @@ export class CodexSecurity {archiveObserver(options, (path) => (archivedScanDir = path)),);requireModelSafeOutputDir(scanDir);- notifyObserver(- "onOutputDirReady",- options.onOutputDirReady,- options.onObserverError,- scanDir,- );+ notifyObserver(options, "onOutputDirReady")(scanDir);const revision = await repositoryRevision(local.repository, signal);const { model } = scanModelConfiguration({...DEFAULT_CODEX_CONFIG,@@ -3359,11 +3264,7 @@ export class CodexSecurity {);}activeScan = { id: scanId, options: workbenchOptions };- notifyObserver(- "onScanStarted",- options.onScanStarted,- options.onObserverError,- );+ notifyObserver(options, "onScanStarted")();await writeMockScanDraft(scanDir,scanId,@@ -3393,10 +3294,7 @@ export class CodexSecurity {if (isRecord(completedScan) && Array.isArray(completedScan["warnings"])) {for (const warning of completedScan["warnings"]) {if (typeof warning === "string")- notifyObserver(- "onWarning",- options.onWarning,- options.onObserverError,+ notifyObserver(options, "onWarning")(warning,Array.isArray(completion["targetWarnings"]) &&completion["targetWarnings"].includes(warning)@@ -3770,11 +3668,9 @@ function warnCleanupFailed(try {const message = String(reason instanceof Error ? reason.message : reason);notifyObserver(+ options,"onWarning",- options.onWarning,- options.onObserverError,- `Could not clean up after the Codex Security ${operation}: ${message}`,- );+ )(`Could not clean up after the Codex Security ${operation}: ${message}`);} catch {}}@@ -3833,19 +3729,12 @@ export async function runScanEvents(const tacStatus = trustedAccessStatusFromEvent(event);if (tacStatus !== null) {tacStatusReported = true;- notifyObserver(- "onTrustedAccessStatus",- options.onTrustedAccessStatus,- options.onObserverError,- tacStatus,- );+ notifyObserver(options, "onTrustedAccessStatus")(tacStatus);if (tacStatus !== "granted") {notifyObserver(+ options,"onWarning",- options.onWarning,- options.onObserverError,- trustedAccessWarning(tacStatus, options.authentication),- );+ )(trustedAccessWarning(tacStatus, options.authentication));}}}@@ -3853,12 +3742,7 @@ export async function runScanEvents(event,options.expectation.repository,)) {- notifyObserver(- "onActivity",- options.onActivity,- options.onObserverError,- activity,- );+ notifyObserver(options, "onActivity")(activity);}for (const progress of scanProgressUpdatesFromEvent(event)) {if (@@ -3867,21 +3751,11 @@ export async function runScanEvents() {continue;}- notifyObserver(- "onProgress",- options.onProgress,- options.onObserverError,- progress,- );+ notifyObserver(options, "onProgress")(progress);}const workerStatus = workerStatusFromEvent(event);if (workerStatus !== null) {- notifyObserver(- "onWorkerStatus",- options.onWorkerStatus,- options.onObserverError,- workerStatus,- );+ notifyObserver(options, "onWorkerStatus")(workerStatus);}if (event.type === "thread.started") {const startedThreadId = event["thread_id"];@@ -3890,19 +3764,12 @@ export async function runScanEvents(}if (!scanStarted) {scanStarted = true;- notifyObserver(- "onScanStarted",- options.onScanStarted,- options.onObserverError,- );+ notifyObserver(options, "onScanStarted")();}}},onReconnect: (message, reconnect) => {- notifyObserver(- "onReconnect",- options.onReconnect,- options.onObserverError,+ notifyObserver(options, "onReconnect")(...reconnect,reconnectDetails(message),);@@ -4546,25 +4413,23 @@ export function selectedScanEnvironment(const archiveObserver =(options: ScanOptions, save: (path: string) => void) => (path: string) => {save(path);- notifyObserver(- "onOutputArchived",- options.onOutputArchived,- options.onObserverError,- path,- );+ notifyObserver(options, "onOutputArchived")(path);};-function notifyObserver<Arguments extends unknown[]>(- observerName: ScanObserverName,- observer: ((...args: Arguments) => void) | undefined,- onObserverError:- ((observer: ScanObserverName, error: unknown) => void) | undefined,- ...args: Arguments-): void {- void Promise.resolve()- .then(() => observer?.(...args))- .catch((error: unknown) => onObserverError?.(observerName, error))- .catch(() => {});+function notifyObserver<Name extends ScanObserverName>(+ options: Pick<ScanOptions & SecurityPolicyOptions, Name | "onObserverError">,+ observerName: Name,+) {+ type Arguments = Parameters<NonNullable<(typeof options)[Name]>>;+ const observer = options[observerName] as+ ((...args: Arguments) => void) | undefined;+ const onObserverError = options.onObserverError;+ return (...args: Arguments): void => {+ void Promise.resolve()+ .then(() => observer?.(...args))+ .catch((error: unknown) => onObserverError?.(observerName, error))+ .catch(() => {});+ };}function environmentApiKey(diff --git a/sdk/typescript/tests-ts/api-events.test.ts b/sdk/typescript/tests-ts/api-events.test.tsindex d3bc1c220..e59eda880 100644--- a/sdk/typescript/tests-ts/api-events.test.ts+++ b/sdk/typescript/tests-ts/api-events.test.ts@@ -1,4 +1,4 @@-import { copyCompletedScan } from "./plugin-root.js";+import { copyCompletedScan, PLUGIN_ROOT } from "./plugin-root.js";import { once } from "node:events";import { mkdir, stat } from "node:fs/promises";import { existsSync } from "node:fs";@@ -28,6 +28,7 @@ import {} from "./support/api-events.js";import { createApiTestFixtures } from "./support/temporary-directories.js";import { throwing } from "./support/errors.js";+import { runScanEvents } from "../src/api.js";const { cleanup, temporaryDirectory } = createApiTestFixtures();@@ -455,21 +456,59 @@ describe("one-shot scan events", () => {expect(warnings).toEqual([]);});- test("isolates trusted cyber access warning observer failures", async () => {+ test("captures warning observers before payload evaluation and isolates failures", async () => {const scanDir = await copyCompletedScan(await temporaryDirectory());const observerErrors: Array<[ScanObserverName, string]> = [];-- const result = await runTacEvents(+ const replacement = mock();+ let warningPending = false;+ let onWarning: () => void = function (this: void) {+ expect(this).toBeUndefined();+ throw new Error("TAC warning observer failed");+ };+ let onObserverError = function (+ this: void,+ observer: ScanObserverName,+ error: unknown,+ ) {+ expect(this).toBeUndefined();+ observerErrors.push([observer, (error as Error).message]);+ };+ const result = await runScanEvents({+ thread: { id: null },+ events: tacEvents([tacToolCall("not_granted")]),+ signal: new AbortController().signal,scanDir,- [tacToolCall("not_granted")],- throwing("TAC warning observer failed"),- collectObserverErrors(observerErrors),- );-+ pluginRoot: PLUGIN_ROOT,+ expectation: {+ repository: "/repository",+ repositoryRevision: "deadbeef",+ target: { kind: "repository", paths: [] },+ mode: "standard",+ pluginVersion: "0.1.0",+ },+ get onTrustedAccessStatus() {+ warningPending = true;+ return undefined;+ },+ get authentication() {+ if (warningPending) {+ onWarning = replacement;+ onObserverError = replacement;+ }+ return undefined;+ },+ get onWarning() {+ return onWarning;+ },+ get onObserverError() {+ return onObserverError;+ },+ });expect(result.turnResult.status).toBe("completed");expect(observerErrors).toEqual([["onWarning", "TAC warning observer failed"],]);+ expect(replacement).not.toHaveBeenCalled();});test("isolates trusted cyber access status observer failures", async () => {