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): reuse client construction and scan data (#1374)
mldangelo-oai0837899c0f3e
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.tsindex 8474ce7f4..52e96efc8 100644--- a/sdk/typescript/src/api.ts+++ b/sdk/typescript/src/api.ts@@ -519,7 +519,7 @@ export class CodexSecurity {/** @internal */public constructor(config: CodexSecurityConfig,- dependencies: ClientDependencies,+ dependencies: ClientDependencies | undefined,runtimeOptions: CodexSecurityRuntimeOptions,);public constructor(@@ -3629,14 +3629,7 @@ export async function listRepositoryFindings(export function createSecurity(config: CodexSecurityConfig = {},): CodexSecurity {- return createSecurityInternal(config, { surface: "sdk" });-}--export function createSecurityInternal(- config: CodexSecurityConfig = {},- runtimeOptions: CodexSecurityRuntimeOptions,-): CodexSecurity {- return new CodexSecurity(config, DEFAULT_DEPENDENCIES, runtimeOptions);+ return new CodexSecurity(config);}export async function initialCredentialsAvailable(@@ -3698,7 +3691,6 @@ interface ScanEventRunOptions extends Pick<ScanOptions, "onReconnect"> {modelProvider?: unknown;workbenchValidated?: boolean;model?: string;- expectedFilesTotal?: number;onFinalize?: (usage: unknown) => Promise<unknown>;onThreadStarted?: (threadId: string) => Promise<void> | void;onScanStarted?: () => void;@@ -3745,12 +3737,6 @@ export async function runScanEvents(notifyObserver(options, "onActivity")(activity);}for (const progress of scanProgressUpdatesFromEvent(event)) {- if (- options.expectedFilesTotal !== undefined &&- progress.filesTotal !== options.expectedFilesTotal- ) {- continue;- }notifyObserver(options, "onProgress")(progress);}const workerStatus = workerStatusFromEvent(event);@@ -4142,14 +4128,7 @@ function scanRecipe({}): JsonObject {return {repository,- target: {- kind: target.kind,- paths: [...target.paths],- ...(target.base === undefined ? {} : { base: target.base }),- ...(target.head === undefined ? {} : { head: target.head }),- ...(target.baseRef === undefined ? {} : { baseRef: target.baseRef }),- ...(target.headRef === undefined ? {} : { headRef: target.headRef }),- },+ target: { ...target, paths: [...target.paths] },mode,...(repositoryRevision === null ? {} : { repositoryRevision }),pluginVersion,diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex b55c9267b..c0d2b8013 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -63,7 +63,6 @@ import { parse as parseToml } from "smol-toml";import {classifyConnectionFailure,CodexSecurity,- createSecurityInternal,environmentValue,formatEnvironmentVariableRemovalGuidance,initialCredentialsAvailable,@@ -1226,7 +1225,7 @@ interface CliDependencies {const DEFAULT_DEPENDENCIES: CliDependencies = {createSecurity: (config) =>- createSecurityInternal(config, { surface: "cli" }),+ new CodexSecurity(config, undefined, { surface: "cli" }),environment: process.env,prepareAuthenticationHome: prepareCodexSecurityCredentialHome,checkForUpdate: (signal) =>@@ -3366,7 +3365,7 @@ export async function main(createSecurity:dependencies.createPolicySecurity ??((config) =>- createSecurityInternal(config, { surface: "cli" })),+ new CodexSecurity(config, undefined, { surface: "cli" })),chooseAuthentication: (config, auth, signal) =>chooseInteractiveAuthentication({diff --git a/sdk/typescript/src/cost.ts b/sdk/typescript/src/cost.tsindex 6d0895b0e..7ce4a73b3 100644--- a/sdk/typescript/src/cost.ts+++ b/sdk/typescript/src/cost.ts@@ -1,3 +1,4 @@+import { parseJson } from "./value.js";import { open, readdir } from "node:fs/promises";import { join } from "node:path";import { isRecord } from "./record.js";@@ -481,12 +482,7 @@ function readSessionEvent(repository?: string,): void {if (line.length === 0) return;- let event: unknown;- try {- event = JSON.parse(line) as unknown;- } catch {- return;- }+ const event = parseJson(() => line);if (!isRecord(event) || !isRecord(event["payload"])) return;const payload = event["payload"];if (event["type"] === "session_meta") {diff --git a/sdk/typescript/src/worker-progress.ts b/sdk/typescript/src/worker-progress.tsindex eb61c8e04..5f5e40120 100644--- a/sdk/typescript/src/worker-progress.ts+++ b/sdk/typescript/src/worker-progress.ts@@ -160,7 +160,6 @@ function preflightStatus(if (capacity.length > 1) return null;const capacityResult = capacity[0];const configuredSlots =- capacity.length === 1 &&capacityResult !== undefined &&isSafeNonNegativeInteger(capacityResult["actual"])? capacityResult["actual"]diff --git a/sdk/typescript/tests-ts/api-events.test.ts b/sdk/typescript/tests-ts/api-events.test.tsindex fc3367719..2aa7fb9d1 100644--- a/sdk/typescript/tests-ts/api-events.test.ts+++ b/sdk/typescript/tests-ts/api-events.test.ts@@ -1145,43 +1145,4 @@ describe("one-shot scan events", () => {{ phase: "validation", filesCompleted: 8, filesTotal: 8 },]);});-- test("forwards every file count printed by a completed review command", async () => {- const scanDir = await copyCompletedScan(await temporaryDirectory());- const updates: ScanProgress[] = [];-- async function* progressEvents(): AsyncGenerator<ThreadEvent> {- yield { type: "thread.started", thread_id: "thread-1" };- yield { type: "turn.started" };- yield {- type: "item.completed",- item: {- id: "file-review-1",- type: "command_execution",- command: "review the two files in the inventory",- aggregated_output: [- 'CODEX_SECURITY_SCAN_PROGRESS {"phase":"discovery","filesCompleted":3,"filesTotal":8}',- 'CODEX_SECURITY_SCAN_PROGRESS {"phase":"discovery","filesCompleted":0,"filesTotal":2}',- "--- commands.py ---",- "--- server.py ---",- 'CODEX_SECURITY_SCAN_PROGRESS {"phase":"discovery","filesCompleted":2,"filesTotal":2}',- ].join("\n"),- exit_code: 0,- status: "completed",- },- };- yield completedTurn();- }-- await expect(- runEvents(scanDir, progressEvents(), {- expectedFilesTotal: 2,- onProgress: (progress) => updates.push(progress),- }),- ).resolves.toBeDefined();- expect(updates).toEqual([- { phase: "discovery", filesCompleted: 0, filesTotal: 2 },- { phase: "discovery", filesCompleted: 2, filesTotal: 2 },- ]);- });});diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.tsindex ca34322e1..4b8fc0028 100644--- a/sdk/typescript/tests-ts/api.test.ts+++ b/sdk/typescript/tests-ts/api.test.ts@@ -3123,12 +3123,12 @@ describe("CodexSecurity orchestration", () => {},);- test("uses the actual scanner inventory instead of a stale workbench estimate", async () => {- const { root, repository, codexHome, scanDir } = await scanDirectories();- const updates: ScanProgress[] = [];- const client = new TestClient(- {},- {+ test.each(["agent_message", "command_execution"] as const)(+ "uses the actual scanner inventory instead of a stale workbench estimate (%s)",+ async (itemType) => {+ const { root, repository, codexHome, scanDir } = await scanDirectories();+ const updates: ScanProgress[] = [];+ const client = TestClient.withDependencies({...scanRuntimeDependencies(codexHome, scanDir),runWorkbench: async (_options: unknown,@@ -3138,59 +3138,74 @@ describe("CodexSecurity orchestration", () => {args[0] === "register-cli-scan"? { ...mockScanRegistration(args, input), scopeFileCount: 4_207 }: mockWorkbench(args, input),- createCodex: () => ({- startThread: () => ({- id: null,- async runStreamed(prompt: string) {- expect(prompt).toContain(- "The SDK's current in-scope file-count estimate is 4207",- );- await copyCompletedScan(root);- async function* scanEvents(): AsyncGenerator<ThreadEvent> {- for await (const event of completedEvents()) {- yield event;- if (event.type === "turn.started") {- for (const filesCompleted of [0, 250, 4_198]) {- const progress: ScanProgress = {- phase:- filesCompleted === 4_198 ? "validation" : "discovery",- filesCompleted,- filesTotal: 4_198,- };- yield {- type: "item.completed",- item: {- id: "inventory-" + filesCompleted,- type: "agent_message",- text:- "CODEX_SECURITY_SCAN_PROGRESS " +- JSON.stringify(progress),- },- };- }+ createCodex: codexFactory(async (prompt: string) => {+ expect(prompt).toContain(+ "The SDK's current in-scope file-count estimate is 4207",+ );+ await copyCompletedScan(root);+ async function* scanEvents(): AsyncGenerator<ThreadEvent> {+ for await (const event of completedEvents()) {+ yield event;+ if (event.type === "turn.started") {+ const texts = [0, 250, 4_198].map((filesCompleted) => {+ const progress: ScanProgress = {+ phase:+ filesCompleted === 4_198 ? "validation" : "discovery",+ filesCompleted,+ filesTotal: 4_198,+ };+ return (+ "CODEX_SECURITY_SCAN_PROGRESS " + JSON.stringify(progress)+ );+ });+ if (itemType === "command_execution") {+ yield {+ type: "item.completed",+ item: {+ id: "inventory-command",+ type: "command_execution",+ command: "review the files in the inventory",+ aggregated_output: [+ 'CODEX_SECURITY_SCAN_PROGRESS {"phase":"discovery","filesCompleted":3,"filesTotal":5000}',+ ...texts,+ ].join("\n"),+ exit_code: 0,+ status: "completed",+ },+ };+ } else {+ for (const [index, text] of texts.entries()) {+ yield {+ type: "item.completed",+ item: {+ id: "inventory-" + [0, 250, 4_198][index],+ type: "agent_message",+ text,+ },+ };}}}- return { events: scanEvents() };- },- }),+ }+ }+ return { events: scanEvents() };}),- },- );+ });- const result = await client.run(repository, {- onProgress: (progress) => updates.push(progress),- });+ const result = await client.run(repository, {+ onProgress: (progress) => updates.push(progress),+ });- expect(result.threadId).toBe("thread-1");- expect(updates).toEqual([- { phase: "preflight", filesCompleted: 0, filesTotal: 4_207 },- { phase: "discovery", filesCompleted: 0, filesTotal: 4_198 },- { phase: "discovery", filesCompleted: 250, filesTotal: 4_198 },- { phase: "validation", filesCompleted: 4_198, filesTotal: 4_198 },- ]);- await client.close();- });+ expect(result.threadId).toBe("thread-1");+ expect(updates).toEqual([+ { phase: "preflight", filesCompleted: 0, filesTotal: 4_207 },+ { phase: "discovery", filesCompleted: 0, filesTotal: 4_198 },+ { phase: "discovery", filesCompleted: 250, filesTotal: 4_198 },+ { phase: "validation", filesCompleted: 4_198, filesTotal: 4_198 },+ ]);+ await client.close();+ },+ );test("normalizes worker progress while streaming related session events", async () => {const { root, repository, codexHome, scanDir } = await scanDirectories();diff --git a/sdk/typescript/tests-ts/cli-project-config.test.ts b/sdk/typescript/tests-ts/cli-project-config.test.tsindex 727769e35..af9fe84ff 100644--- a/sdk/typescript/tests-ts/cli-project-config.test.ts+++ b/sdk/typescript/tests-ts/cli-project-config.test.ts@@ -22,7 +22,7 @@ const { temporaryDirectory, cleanup } = createApiTestFixtures(afterEach(cleanup);const cliClientFactory =- (deps: ConstructorParameters<typeof CodexSecurity>[1]) =>+ (deps: NonNullable<ConstructorParameters<typeof CodexSecurity>[1]>) =>(config: CodexSecurityConfig) =>new CodexSecurity(config, { ...deps }, { surface: "cli" });diff --git a/sdk/typescript/tests-ts/support/api-client.ts b/sdk/typescript/tests-ts/support/api-client.tsindex 94cfae4d8..5545eac82 100644--- a/sdk/typescript/tests-ts/support/api-client.ts+++ b/sdk/typescript/tests-ts/support/api-client.ts@@ -58,13 +58,15 @@ export function mockWorkbench(}export class TestClient extends CodexSecurity {- static withDependencies(dependencies: Partial<ClientArguments[1]>) {+ static withDependencies(+ dependencies: Partial<NonNullable<ClientArguments[1]>>,+ ) {return new TestClient({}, dependencies);}public constructor(config: ClientArguments[0],- dependencies: Partial<ClientArguments[1]>,+ dependencies: Partial<NonNullable<ClientArguments[1]>>,) {super(config,diff --git a/sdk/typescript/tests-ts/support/api-events.ts b/sdk/typescript/tests-ts/support/api-events.tsindex ceff9d12c..7303ebf1b 100644--- a/sdk/typescript/tests-ts/support/api-events.ts+++ b/sdk/typescript/tests-ts/support/api-events.ts@@ -6,7 +6,9 @@ import { PLUGIN_ROOT, copyCompletedScan } from "../plugin-root.js";type PreparedRuntime = Awaited<ReturnType<NonNullable<- ConstructorParameters<typeof CodexSecurity>[1]["prepareRuntime"]+ NonNullable<+ ConstructorParameters<typeof CodexSecurity>[1]+ >["prepareRuntime"]>>>;