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
test(sdk): reuse CLI authentication and publication fixtures (#1398)
mldangelo-oaib7db2f96d59f
diff --git a/sdk/typescript/tests-ts/cli-authentication.test.ts b/sdk/typescript/tests-ts/cli-authentication.test.tsindex 892238d9c..4b4885341 100644--- a/sdk/typescript/tests-ts/cli-authentication.test.ts+++ b/sdk/typescript/tests-ts/cli-authentication.test.ts@@ -43,11 +43,22 @@ import { temporaryDirectory } from "./support/temporary-directories.js";import { throwing } from "./support/errors.js";import {+ selectionPrompt,createCliTest,captureCli,runCapturedCli,} from "./support/cli-run.js";+const GATEWAY_CONFIGURATION = [+ 'model_provider="gateway"',+ "[model_providers.gateway]",+ 'name="Synthetic gateway"',+ 'base_url="https://gateway.example.test/v1"',+ 'wire_api="responses"',+ 'env_key="GATEWAY_API_KEY"',+ "requires_openai_auth=true",+].join("\n");+let stateDirectory: string;beforeEach(async () => {@@ -477,8 +488,8 @@ describe("CLI authentication", () => {const deps = dependencies({environment: { AWS_PROFILE: "synthetic-profile" },});- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onAuthentication?.({method: "aws_credentials",source: "AWS_PROFILE",@@ -487,10 +498,7 @@ describe("CLI authentication", () => {throw new CodexSecurityError("403 ExpiredTokenException: security token has expired",);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await stderr.run(["scan", "--codex", 'model_provider="amazon-bedrock"'],@@ -538,14 +546,11 @@ describe("CLI authentication", () => {const { stdout, stderr, runCli } = createCliTest(main, { stderr: false });const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onAuthentication?.({ method: "command", verified: false });throw new CodexSecurityError(detail);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await runCli([@@ -597,17 +602,13 @@ describe("CLI authentication", () => {onWorkbench: () => savedRecipe(),});deps.hasStoredChatGPTSignIn = async () => true;- deps.scanAuthenticationPrompt = {- isInteractive: () => true,- select: async <Value extends string>(- message: string,- options: readonly { label: string; value: Value }[],- ): Promise<Value> => {+ deps.scanAuthenticationPrompt = selectionPrompt(+ async (message, options) => {question = message;choices = options;return options.find((option) => option.value === selection)!.value;},- };+ );expect(await stderr.run(argv, deps)).toBe(0);expect(onTurn.mock.results.at(-1)?.value).toBe(selection);@@ -641,15 +642,9 @@ describe("CLI authentication", () => {deps.createSecurity = createSecurity;deps.hasStoredChatGPTSignIn = (signal) =>stage === "status" ? interrupt(signal) : Promise.resolve(true);- deps.scanAuthenticationPrompt = {- isInteractive: () => true,- select: <Value extends string>(- _message: string,- _options: readonly { label: string; value: Value }[],- _presentation?: { header?: string },- signal?: AbortSignal,- ) => interrupt(signal),- };+ deps.scanAuthenticationPrompt = selectionPrompt(+ (_message, _options, _presentation, signal) => interrupt(signal),+ );expect(await main(["scan"], capture().stream, capture(true).stream, deps),@@ -771,16 +766,13 @@ describe("CLI authentication", () => {},});deps.hasStoredChatGPTSignIn = hasStoredChatGPTSignIn;- deps.scanAuthenticationPrompt = {- isInteractive: () => scenario.inputInteractive !== false,- select: async <Value extends string>(- _message: string,- options: readonly { label: string; value: Value }[],- ): Promise<Value> => {+ deps.scanAuthenticationPrompt = selectionPrompt(+ async (_message, options) => {prompts += 1;return options[0]!.value;},- };+ () => scenario.inputInteractive !== false,+ );expect(await runCli(scenario.argv, deps)).toBe(0);expect(prompts).toBe(0);@@ -1497,15 +1489,7 @@ describe("skill authentication", () => {command,auth,overrides: ['model_provider="gateway"'],- ambientConfig: [- 'model_provider="gateway"',- "[model_providers.gateway]",- 'name="Synthetic gateway"',- 'base_url="https://gateway.example.test/v1"',- 'wire_api="responses"',- 'env_key="GATEWAY_API_KEY"',- "requires_openai_auth=true",- ].join("\n"),+ ambientConfig: GATEWAY_CONFIGURATION,environment: { GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY" },});expect(result.status, result.stderr).toBe(0);@@ -1574,15 +1558,7 @@ describe("skill authentication", () => {const result = await runProviderSkill(stateDirectory, {command,overrides: ['model_provider="gateway"'],- ambientConfig: [- 'model_provider="gateway"',- "[model_providers.gateway]",- 'name="Synthetic gateway"',- 'base_url="https://gateway.example.test/v1"',- 'wire_api="responses"',- 'env_key="GATEWAY_API_KEY"',- "requires_openai_auth=true",- ].join("\n"),+ ambientConfig: GATEWAY_CONFIGURATION,environment: { OPENAI_API_KEY: "SYNTHETIC_UNRELATED_OPENAI_KEY" },});expect(result.status).toBe(2);diff --git a/sdk/typescript/tests-ts/cli-fixtures.ts b/sdk/typescript/tests-ts/cli-fixtures.tsindex db38dcdb5..d9822ec3e 100644--- a/sdk/typescript/tests-ts/cli-fixtures.ts+++ b/sdk/typescript/tests-ts/cli-fixtures.ts@@ -214,7 +214,6 @@ export function dependencies(onRepositoryCommand?: (...arguments_: Parameters<MainDependencies["runRepositoryCommand"]>) => string | Promise<string>;- bulkScan?: MainDependencies["bulkScan"];onWorkbench?: (args: readonly string[],input?: string,@@ -288,7 +287,6 @@ export function dependencies(repository,commandOptions,)) ?? (args.includes("--name-only") ? "src/finding-1.ts\0" : ""),- ...(options.bulkScan === undefined ? {} : { bulkScan: options.bulkScan }),...(options.linearClient === undefined? {}: { linearClient: options.linearClient }),diff --git a/sdk/typescript/tests-ts/cli-publish.test.ts b/sdk/typescript/tests-ts/cli-publish.test.tsindex 57594d169..af13c288a 100644--- a/sdk/typescript/tests-ts/cli-publish.test.ts+++ b/sdk/typescript/tests-ts/cli-publish.test.ts@@ -9,6 +9,7 @@ import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";import { createApiTestFixtures } from "./support/temporary-directories.js";import { fail } from "./support/errors.js";import {+ selectionPrompt,createCliTest,captureCli,runCapturedCli,@@ -1252,19 +1253,14 @@ describe("publish scan", () => {},});deps.now = () => Date.parse("2026-08-15T02:17:00Z");- deps.publishPrompt = {- isInteractive: () => true,- select: async <Value extends string>(- message: string,- options: readonly { label: string; short?: string; value: Value }[],- presentation?: { header?: string },- ): Promise<Value> => {+ deps.publishPrompt = selectionPrompt(+ async (message, options, presentation) => {question = message;choices = options;header = presentation?.header;return options[1]!.value;},- };+ );deps.publishScan = publishScan;expect(@@ -1421,18 +1417,13 @@ describe("publish scan", () => {}),});deps.now = () => currentTime;- deps.publishPrompt = {- isInteractive: () => true,- select: async <Value extends string>(- _message: string,- options: readonly { label: string; short?: string; value: Value }[],- presentation?: { header?: string },- ): Promise<Value> => {+ deps.publishPrompt = selectionPrompt(+ async (_message, options, presentation) => {header = presentation?.header;choices = options;return options[0]!.value;},- };+ );deps.publishScan = successfulPublication<[]>;expect(@@ -1566,16 +1557,10 @@ describe("publish scan", () => {}),});deps.now = () => Date.parse("2026-08-15T01:01:00Z");- deps.publishPrompt = {- isInteractive: () => true,- select: async <Value extends string>(- _message: string,- options: readonly { label: string; value: Value }[],- ): Promise<Value> => {- choice = options[0]!.label;- return options[0]!.value;- },- };+ deps.publishPrompt = selectionPrompt(async (_message, options) => {+ choice = options[0]!.label;+ return options[0]!.value;+ });deps.publishScan = successfulPublication<[]>;expect(@@ -2006,16 +1991,10 @@ describe("publish scan", () => {})),}),});- deps.publishPrompt = {- isInteractive: () => true,- select: async <Value extends string>(- _message: string,- choices: readonly { label: string; value: Value }[],- ): Promise<Value> => {- offered = choices;- return choices[1]!.value;- },- };+ deps.publishPrompt = selectionPrompt(async (_message, choices) => {+ offered = choices;+ return choices[1]!.value;+ });deps.publishScan = publishScan;expect(@@ -2047,13 +2026,10 @@ describe("publish scan", () => {const deps = dependencies({onWorkbench,});- deps.publishPrompt = {- isInteractive: () => false,- select: async <Value extends string>(- _message: string,- options: readonly { label: string; value: Value }[],- ): Promise<Value> => options[0]!.value,- };+ deps.publishPrompt = selectionPrompt(+ async (_message, options) => options[0]!.value,+ () => false,+ );deps.publishScan = publishScan;expect(@@ -2074,16 +2050,10 @@ describe("publish scan", () => {let prompted = false;const publishScan = mock(successfulPublication<[]>);const deps = dependencies({ onWorkbench: () => ({ scans: [] }) });- deps.publishPrompt = {- isInteractive: () => true,- select: async <Value extends string>(- _message: string,- options: readonly { label: string; value: Value }[],- ): Promise<Value> => {- prompted = true;- return options[0]!.value;- },- };+ deps.publishPrompt = selectionPrompt(async (_message, options) => {+ prompted = true;+ return options[0]!.value;+ });deps.publishScan = publishScan;expect(@@ -2125,16 +2095,10 @@ describe("publish scan", () => {})),}),});- deps.publishPrompt = {- isInteractive: () => true,- select: async <Value extends string>(- _message: string,- choices: readonly { value: Value }[],- ): Promise<Value> => {- prompted = true;- return choices[0]!.value;- },- };+ deps.publishPrompt = selectionPrompt(async (_message, choices) => {+ prompted = true;+ return choices[0]!.value;+ });deps.publishScan = publishScan;expect(diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.tsindex 372a7beac..6ea3e743b 100644--- a/sdk/typescript/tests-ts/cli.test.ts+++ b/sdk/typescript/tests-ts/cli.test.ts@@ -68,6 +68,38 @@ import {runCapturedCli,} from "./support/cli-run.js";+const profileScenarios = [+ {+ overrides: [+ 'profile="review"',+ 'model="gpt-5.6-sol"',+ 'model_reasoning_effort="low"',+ 'profiles.review.model="gpt-5.6-terra"',+ 'profiles.review.model_reasoning_effort="high"',+ ],+ model: "gpt-5.6-terra",+ reasoningEffort: "high",+ },+ {+ overrides: [+ 'profile="review"',+ 'model_reasoning_effort="low"',+ 'profiles.review.model="gpt-5.6-terra"',+ ],+ model: "gpt-5.6-terra",+ reasoningEffort: "low",+ },+ {+ overrides: [+ 'profile="review"',+ 'model="gpt-5.6-terra"',+ 'profiles.review.model_reasoning_effort="medium"',+ ],+ model: "gpt-5.6-terra",+ reasoningEffort: "medium",+ },+] as const;+const DEFAULT_SCAN_MODEL_CONFIGURATION =scanModelConfiguration(DEFAULT_CODEX_CONFIG);@@ -2939,39 +2971,7 @@ describe("CLI", () => {});test("reports effective model and reasoning settings from the selected Codex profile", async () => {- const scenarios = [- {- overrides: [- 'profile="review"',- 'model="gpt-5.6-sol"',- 'model_reasoning_effort="low"',- 'profiles.review.model="gpt-5.6-terra"',- 'profiles.review.model_reasoning_effort="high"',- ],- model: "gpt-5.6-terra",- reasoningEffort: "high",- },- {- overrides: [- 'profile="review"',- 'model_reasoning_effort="low"',- 'profiles.review.model="gpt-5.6-terra"',- ],- model: "gpt-5.6-terra",- reasoningEffort: "low",- },- {- overrides: [- 'profile="review"',- 'model="gpt-5.6-terra"',- 'profiles.review.model_reasoning_effort="medium"',- ],- model: "gpt-5.6-terra",- reasoningEffort: "medium",- },- ] as const;-- for (const scenario of scenarios) {+ for (const scenario of profileScenarios) {const { stdout, stderr, runCli } = createCliTest(main);expect(@@ -3117,91 +3117,69 @@ describe("CLI", () => {},);- test("enables verbose diagnostics through CODEX_SECURITY_LOG_LEVEL", async () => {- const { stdout, stderr, runCli } = createCliTest(main);-- expect(- await runCli(- ["scan", ".", "--json"],- dependencies({- environment: { CODEX_SECURITY_LOG_LEVEL: " DeBuG " },- }),- ),- ).toBe(0);- expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());- expect(stderr.text()).toContain(- "codex-security: debug: scan.configuration",- );- expect(stderr.text()).toContain("codex-security: debug: scan.started");- expect(stderr.text()).toContain("codex-security: debug: scan.completed");- });-- test("accepts Promptfoo-compatible LOG_LEVEL as a verbose fallback", async () => {- const { stdout, stderr, runCli } = createCliTest(main);-- expect(- await runCli(- ["scan", ".", "--json"],- dependencies({- environment: {- CODEX_SECURITY_LOG_LEVEL: " ",- LOG_LEVEL: " DEBUG ",- },- }),- ),- ).toBe(0);- expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());- expect(stderr.text()).toContain(- "codex-security: debug: scan.configuration",- );- expect(stderr.text()).toContain("codex-security: debug: scan.completed");- });-- test("prefers CODEX_SECURITY_LOG_LEVEL over a shared LOG_LEVEL", async () => {- const { stdout, stderr, runCli } = createCliTest(main);-- expect(- await runCli(- ["scan", ".", "--json"],- dependencies({- environment: {- CODEX_SECURITY_LOG_LEVEL: "info",- LOG_LEVEL: "debug",- },- }),- ),- ).toBe(0);- expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());- expect(stderr.text()).not.toContain("codex-security: debug:");- });-- test("lets --verbose override non-debug environment log levels", async () => {- const { stdout, stderr, runCli } = createCliTest(main);-- expect(- await runCli(- ["scan", ".", "--verbose", "--json"],- dependencies({- environment: {- CODEX_SECURITY_LOG_LEVEL: "error",- LOG_LEVEL: "warn",- },- }),- ),- ).toBe(0);- expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());- expect(stderr.text()).toContain(- "codex-security: debug: scan.configuration",- );- expect(stderr.text()).toContain("codex-security: debug: scan.completed");- });-- test("does not emit verbose diagnostics unless explicitly requested", async () => {+ test.each([+ [+ "enables verbose diagnostics through CODEX_SECURITY_LOG_LEVEL",+ ["scan", ".", "--json"],+ { CODEX_SECURITY_LOG_LEVEL: " DeBuG " },+ [+ "codex-security: debug: scan.configuration",+ "codex-security: debug: scan.started",+ "codex-security: debug: scan.completed",+ ],+ [],+ ],+ [+ "accepts Promptfoo-compatible LOG_LEVEL as a verbose fallback",+ ["scan", ".", "--json"],+ {+ CODEX_SECURITY_LOG_LEVEL: " ",+ LOG_LEVEL: " DEBUG ",+ },+ [+ "codex-security: debug: scan.configuration",+ "codex-security: debug: scan.completed",+ ],+ [],+ ],+ [+ "prefers CODEX_SECURITY_LOG_LEVEL over a shared LOG_LEVEL",+ ["scan", ".", "--json"],+ {+ CODEX_SECURITY_LOG_LEVEL: "info",+ LOG_LEVEL: "debug",+ },+ [],+ ["codex-security: debug:"],+ ],+ [+ "lets --verbose override non-debug environment log levels",+ ["scan", ".", "--verbose", "--json"],+ {+ CODEX_SECURITY_LOG_LEVEL: "error",+ LOG_LEVEL: "warn",+ },+ [+ "codex-security: debug: scan.configuration",+ "codex-security: debug: scan.completed",+ ],+ [],+ ],+ [+ "does not emit verbose diagnostics unless explicitly requested",+ ["scan", ".", "--json"],+ {},+ [],+ ["codex-security: debug:"],+ ],+ ] as const)("%s", async (_name, args, environment, present, absent) => {const { stdout, stderr, runCli } = createCliTest(main);-- expect(await runCli(["scan", ".", "--json"], dependencies())).toBe(0);+ expect(await runCli(args, dependencies({ environment }))).toBe(0);expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());- expect(stderr.text()).not.toContain("codex-security: debug:");+ for (const diagnostic of present)+ expect(stderr.text()).toContain(diagnostic);+ for (const diagnostic of absent)+ expect(stderr.text()).not.toContain(diagnostic);});test("keeps verbose dry-run authentication unverified without starting a scan", async () => {@@ -3239,39 +3217,7 @@ describe("CLI", () => {});test("reports selected profile configuration consistently in verbose dry runs", async () => {- const scenarios = [- {- overrides: [- 'profile="review"',- 'model="gpt-5.6-sol"',- 'model_reasoning_effort="low"',- 'profiles.review.model="gpt-5.6-terra"',- 'profiles.review.model_reasoning_effort="high"',- ],- model: "gpt-5.6-terra",- reasoningEffort: "high",- },- {- overrides: [- 'profile="review"',- 'model_reasoning_effort="low"',- 'profiles.review.model="gpt-5.6-terra"',- ],- model: "gpt-5.6-terra",- reasoningEffort: "low",- },- {- overrides: [- 'profile="review"',- 'model="gpt-5.6-terra"',- 'profiles.review.model_reasoning_effort="medium"',- ],- model: "gpt-5.6-terra",- reasoningEffort: "medium",- },- ] as const;-- for (const scenario of scenarios) {+ for (const scenario of profileScenarios) {const { stdout, stderr, runCli } = createCliTest(main);expect(diff --git a/sdk/typescript/tests-ts/publication-integration.test.ts b/sdk/typescript/tests-ts/publication-integration.test.tsindex 61e496389..241c682bc 100644--- a/sdk/typescript/tests-ts/publication-integration.test.ts+++ b/sdk/typescript/tests-ts/publication-integration.test.ts@@ -187,6 +187,20 @@ async function fixture(count: number): Promise<PublicationFixture> {};}+function publishCommand(scanDirectory: string, ...flags: string[]): string[] {+ return [+ "publish",+ "scan",+ scanDirectory,+ "--to",+ "linear",+ "--linear-team",+ OPTIONS.teamId,+ ...flags,+ "--json",+ ];+}+async function publicationPayload(value: string,): Promise<@@ -395,18 +409,11 @@ describe("database-backed Linear publication integration", () => {},}) as unknown as LinearClient,});- const command = [- "publish",- "scan",+ const command = publishCommand(completed.scanDirectory,- "--to",- "linear",- "--linear-team",- OPTIONS.teamId,"--project",OPTIONS.projectId,- "--json",- ];+ );const run = async (flags: string[] = []) => {const { stdout, runCli } = createCliTest(main);@@ -547,21 +554,7 @@ describe("database-backed Linear publication integration", () => {},});- expect(- await runCli(- [- "publish",- "scan",- completed.scanDirectory,- "--to",- "linear",- "--linear-team",- OPTIONS.teamId,- "--json",- ],- cli,- ),- ).toBe(0);+ expect(await runCli(publishCommand(completed.scanDirectory), cli)).toBe(0);const result = JSON.parse(stdout.text()) as PublishScanResult;expect(result.destination).toEqual({@@ -682,18 +675,7 @@ describe("database-backed Linear publication integration", () => {expect(await runCli(- [- "publish",- "scan",- completed.scanDirectory,- "--to",- "linear",- "--linear-team",- OPTIONS.teamId,- "--project",- OPTIONS.projectId,- "--json",- ],+ publishCommand(completed.scanDirectory, "--project", OPTIONS.projectId),cli,),).toBe(0);@@ -784,21 +766,7 @@ describe("database-backed Linear publication integration", () => {},});- expect(- await runCli(- [- "publish",- "scan",- completed.scanDirectory,- "--to",- "linear",- "--linear-team",- OPTIONS.teamId,- "--json",- ],- cli,- ),- ).toBe(2);+ expect(await runCli(publishCommand(completed.scanDirectory), cli)).toBe(2);const result = JSON.parse(stdout.text()) as PublishScanResult;expect(result.destination).toEqual({@@ -866,18 +834,7 @@ describe("database-backed Linear publication integration", () => {expect(await runCli(- [- "publish",- "scan",- completed.scanDirectory,- "--to",- "linear",- "--linear-team",- OPTIONS.teamId,- "--project",- OPTIONS.projectId,- "--json",- ],+ publishCommand(completed.scanDirectory, "--project", OPTIONS.projectId),cli,),).toBe(0);@@ -975,19 +932,12 @@ describe("database-backed Linear publication integration", () => {expect(await runCli(- [- "publish",- "scan",+ publishCommand(completed.scanDirectory,- "--to",- "linear",- "--linear-team",- OPTIONS.teamId,"--project",OPTIONS.projectId,...(skipExisting ? ["--skip-existing"] : []),- "--json",- ],+ ),cli,),).toBe(2);@@ -1279,18 +1229,7 @@ for (;;) Atomics.wait(waiter, 0, 0, 1000);`,expect(await runCli(- [- "publish",- "scan",- completed.scanDirectory,- "--to",- "linear",- "--linear-team",- OPTIONS.teamId,- "--project",- OPTIONS.projectId,- "--json",- ],+ publishCommand(completed.scanDirectory, "--project", OPTIONS.projectId),cli,),).toBe(130);diff --git a/sdk/typescript/tests-ts/support/cli-run.ts b/sdk/typescript/tests-ts/support/cli-run.tsindex 9dc61bf49..bd1b74fdf 100644--- a/sdk/typescript/tests-ts/support/cli-run.ts+++ b/sdk/typescript/tests-ts/support/cli-run.ts@@ -1,3 +1,4 @@+import type { BulkScanPrompt } from "../../src/bulk-scan-discovery.js";import type { main as cliMain } from "../../src/cli.js";import { capture } from "../cli-fixtures.js";@@ -41,3 +42,10 @@ export function runCapturedCli() {return main(args, capture().stream, capture().stream, dependencies);}++export function selectionPrompt(+ select: BulkScanPrompt["select"],+ isInteractive = () => true,+) {+ return { isInteractive, select };+}