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
fix(cli): preserve custom provider authentication in skill commands (#1187)
mldangelo-oaif32461ba2021
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.mjsindex 9aff0b304..79d37970d 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.mjs+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.mjs@@ -1111,6 +1111,7 @@ async function testWorkerRuntimeSettings() {"CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH","OPENAI_API_KEY","CODEX_API_KEY",+ "SYNTHETIC_GATEWAY_KEY",].map((name) => [name, process.env[name]]),);const originalSpawn = childProcess.spawn;@@ -1118,7 +1119,11 @@ async function testWorkerRuntimeSettings() {delete process.env.OPENAI_API_KEY;delete process.env.CODEX_API_KEY;for (const [configuration, expected] of cases) {- const fixture = await fakeCodexFixture(deniedWorkerPermissionProfile);+ const fixture = await fakeCodexFixture(+ deniedWorkerPermissionProfile,+ true,+ { account: null, requiresOpenaiAuth: false },+ );const python = path.join(fixture.root, "selected venv", "bin", "python");const helperPython = path.join(fixture.root,@@ -1138,6 +1143,17 @@ async function testWorkerRuntimeSettings() {const codexHome = path.join(fixture.root, "scan home");const promptPath = path.join(fixture.root, "prompt.md");await mkdir(codexHome);+ await writeFile(+ path.join(codexHome, "config.toml"),+ [+ 'model_provider = "synthetic"',+ "[model_providers.synthetic]",+ 'name = "Synthetic gateway"',+ 'base_url = "https://gateway.example.test/v1"',+ 'wire_api = "responses"',+ 'env_key = "SYNTHETIC_GATEWAY_KEY"',+ ].join("\n"),+ );await writeFile(configPath, configuration);await writeFile(promptPath, "synthetic worker configuration fixture");process.env.CODEX_CLI_PATH = process.execPath;@@ -1173,6 +1189,12 @@ async function testWorkerRuntimeSettings() {{ model: "gpt-5.6-sol", reasoningEffort: "xhigh" },{ model: "gpt-6-astra", reasoningEffort: "ultra" },{ model: "gpt-6.1-sol", reasoningEffort: "max" },+ { model: "gpt-6-sol", reasoningEffort: "high" },+ ];+ const providerKeys = [+ "synthetic-gateway-key-0",+ "synthetic-gateway-key-1",+ undefined,];const executors = settings.map((modelSettings) =>@@ -1193,8 +1215,14 @@ async function testWorkerRuntimeSettings() {for (const resumeThreadId of [undefined, "fixture-resumed-thread"]) {launches.length = 0;await Promise.all(- executors.map((executor) =>- executor.run({+ executors.map((executor, index) => {+ // Each concurrent launch snapshots its own scan environment.+ if (providerKeys[index] === undefined) {+ delete process.env.SYNTHETIC_GATEWAY_KEY;+ } else {+ process.env.SYNTHETIC_GATEWAY_KEY = providerKeys[index];+ }+ return executor.run({kind,promptPath,workingDirectory: fixture.root,@@ -1213,14 +1241,17 @@ async function testWorkerRuntimeSettings() {: {}),},signal: new AbortController().signal,- }),- ),+ });+ }),);const workerLaunches = launches.filter(({ args }) => args[0] === "exec",);assert.equal(workerLaunches.length, settings.length);- for (const { model, reasoningEffort } of settings) {+ for (const [+ index,+ { model, reasoningEffort },+ ] of settings.entries()) {const workerLaunch = workerLaunches.find(({ args }) => args[args.indexOf("--model") + 1] === model,);@@ -1254,6 +1285,12 @@ async function testWorkerRuntimeSettings() {const invocation = JSON.parse(await readFile(workerLaunch.markerPath, "utf8"),);+ assert.equal(invocation.providerKey, providerKeys[index]);+ assert.equal(workerLaunch.environment.CODEX_API_KEY, undefined);+ assert.equal(+ process.env.SYNTHETIC_GATEWAY_KEY,+ providerKeys.at(-1),+ );if (expected === undefined) {assert.equal(invocation.argv.some((arg) =>@@ -2443,7 +2480,7 @@ async function fakeCodexFixture("for await (const chunk of process.stdin) stdin += chunk;","const openaiAuthentication = stdin.includes('CAPTURE_SYNTHETIC_OPENAI_AUTH') ? { OPENAI_API_KEY: process.env.OPENAI_API_KEY, CODEX_API_KEY: process.env.CODEX_API_KEY } : undefined;","const bedrockAuthentication = stdin.includes('CAPTURE_SYNTHETIC_BEDROCK_AUTH') ? Object.fromEntries(JSON.parse(process.env.FAKE_CODEX_BEDROCK_ENV_KEYS).map((name) => [name, process.env[name]])) : undefined;",- "writeFileSync(process.env.FAKE_CODEX_MARKER, JSON.stringify({ argv: process.argv.slice(2), stdin, cwd: process.cwd(), codexHome: process.env.CODEX_HOME, gitEnvironment: Object.fromEntries(['PATH', 'CODEX_SECURITY_GIT', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL'].map(name => [name, process.env[name]])), configPath: process.env.CODEX_SECURITY_CONFIG_PATH, deepConfigPath: process.env.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH, python: process.env.PYTHON, originator: process.env.CODEX_INTERNAL_ORIGINATOR_OVERRIDE, ...(stdin.includes('COMPLETE_THEN_HANG') ? { pid: process.pid } : {}), ...(openaiAuthentication ? { openaiAuthentication } : {}), ...(bedrockAuthentication ? { bedrockAuthentication } : {}) }));",+ "writeFileSync(process.env.FAKE_CODEX_MARKER, JSON.stringify({ argv: process.argv.slice(2), stdin, cwd: process.cwd(), codexHome: process.env.CODEX_HOME, gitEnvironment: Object.fromEntries(['PATH', 'CODEX_SECURITY_GIT', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL'].map(name => [name, process.env[name]])), configPath: process.env.CODEX_SECURITY_CONFIG_PATH, deepConfigPath: process.env.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH, python: process.env.PYTHON, providerKey: process.env.SYNTHETIC_GATEWAY_KEY, originator: process.env.CODEX_INTERNAL_ORIGINATOR_OVERRIDE, ...(stdin.includes('COMPLETE_THEN_HANG') ? { pid: process.pid } : {}), ...(openaiAuthentication ? { openaiAuthentication } : {}), ...(bedrockAuthentication ? { bedrockAuthentication } : {}) }));","if (stdin.includes('COMPLETE_THEN_HANG')) process.on('SIGTERM', () => setTimeout(() => process.exit(0), 100));","if (stdin.includes('THREAD_START_CONFIG_ERROR')) { console.error('Error: thread/start: thread/start failed: agents.max_threads cannot be set when features.multi_agent_v2 is enabled (code -32600)'); process.exit(1); }","if (stdin.includes('CONFIG_ERROR')) { console.error('failed to load configuration: invalid value'); process.exit(2); }",diff --git a/sdk/typescript/README.md b/sdk/typescript/README.mdindex 1dd257574..7ab4701c3 100644--- a/sdk/typescript/README.md+++ b/sdk/typescript/README.md@@ -1001,7 +1001,16 @@ npx @openai/codex-security patch "Security issue" \```Set the selected provider's API-key environment variable before running the-command. Model, effort, and provider settings also apply to+command; `--auth api-key` uses that configured variable. For `patch` and+`verify-fix`, provider overrides preserve unspecified fields from the provider's+existing Codex configuration. With `auto` or `api-key` authentication, a configured+`env_key` takes precedence over OpenAI account authentication, matching native Codex.+Explicit `--auth chatgpt` omits the selected custom provider's API-key environment+variable, clears any configured `experimental_bearer_token`, and selects stored+account authentication in its runtime configuration, even if the provider normally+uses only an API key or bearer token. The original environment and+Codex configuration remain unchanged.+Model, effort, and provider settings also apply to`patch --assess-patch-risk`.Sandbox, approval, and plugin settings remain controlled by the command.diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.tsindex 14ae03461..0282c60f8 100644--- a/sdk/typescript/src/api.ts+++ b/sdk/typescript/src/api.ts@@ -347,11 +347,7 @@ export type ScanAuthentication =| { method: "command"; verified: false }| {method: "api_key";- source:- | "OPENAI_API_KEY"- | "CODEX_API_KEY"- | "OPENROUTER_API_KEY"- | "FIREWORKS_API_KEY";+ source: string;verified: false;}| {diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex 2b16e5716..7bd623d4f 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -71,6 +71,7 @@ import {CODEX_AUTH_CONFIG_KEYS,NO_CREDENTIALS_MESSAGE,configuredCodexHome,+ environmentEntry,readCodexHomeConfig,} from "./auth.js";import { loadContract } from "./contract.js";@@ -103,6 +104,7 @@ import {} from "./bulk-scan-discovery.js";import {DEFAULT_CODEX_CONFIG,+ deepMerge,EXTERNAL_CODEX_PROVIDERS,isExternalModelProvider,mergeCodexOverrides,@@ -110,6 +112,7 @@ import {inlineToml,modelProviderConfigOverride,resolveCommandAuthConfig,+ resolveCodexProfile,mergedCodexConfig,scanModel,scanModelConfiguration,@@ -1060,7 +1063,7 @@ interface SkillCommandOutput {readonly directory?: string;readonly auth?: ScanAuthMode;readonly modelProvider?: string;- readonly providerConfiguration?: JsonObject;+ readonly codexOverrides?: JsonObject;readonly command: "validate" | "patch" | "verify-fix";readonly stdout: Writable;readonly stderr: Writable;@@ -1401,48 +1404,71 @@ export async function runCodexSkillCommand(let modelProvider: string | undefined;// runSkill selects auth; other process callers supply their own environment.if (output?.auth !== undefined) {- const config =- output.modelProvider !== undefined- ? {- model_provider: output.modelProvider,- ...(output.providerConfiguration === undefined- ? {}- : {- model_providers: {- [output.modelProvider]: output.providerConfiguration,- },- }),- }- : output.appServer === undefined- ? {}- : await readCodexHomeConfig(processEnvironment);- const provider = scanModelProvider(config);+ const ambientConfig =+ output.appServer === undefined+ ? {}+ : resolveCodexProfile(await readCodexHomeConfig(processEnvironment));+ const overrides = structuredClone(output.codexOverrides ?? {});+ const config = deepMerge(ambientConfig, overrides);+ const provider = output.modelProvider ?? scanModelProvider(config);+ // Native Codex ignores configured tables for these built-in providers.const providerConfiguration =- typeof provider === "string"+ typeof provider === "string" &&+ !["openai", "ollama", "lmstudio"].includes(provider)? (config["model_providers"] asRecord<string, JsonObject> | undefined)?.[provider]: undefined;+ const providerEnvKey =+ typeof providerConfiguration?.["env_key"] === "string"+ ? providerConfiguration["env_key"]+ : undefined;+ const commandAuth = providerConfiguration?.["auth"] !== undefined;+ const explicitChatgpt =+ output.auth === "chatgpt" && !isExternalModelProvider(provider);+ const providerBearer =+ typeof providerConfiguration?.["experimental_bearer_token"] ===+ "string";+ const providerKeyConfigured =+ providerEnvKey !== undefined || providerBearer;const requiresOpenAiAuth =- provider === undefined ||- provider === "openai" ||- providerConfiguration?.["requires_openai_auth"] === true;+ !commandAuth &&+ (explicitChatgpt || !providerKeyConfigured) &&+ (provider === undefined ||+ provider === "openai" ||+ providerConfiguration?.["requires_openai_auth"] === true ||+ (explicitChatgpt && providerKeyConfigured));modelProvider = output.modelProvider;let credentialConfig: JsonObject | undefined;- authentication = scanAuthentication(- processEnvironment,- output.auth,- provider,- hasCommandAuth(config),- );+ if (providerEnvKey !== undefined && !commandAuth && !explicitChatgpt) {+ const key = environmentEntry(+ processEnvironment,+ providerEnvKey,+ )?.trim();+ if (!key && output.auth === "api-key") {+ throw new AuthenticationRequiredError(+ `API-key authentication requires ${providerEnvKey}. Set a valid API key for the selected provider.`,+ );+ }+ authentication = key+ ? { method: "api_key", source: providerEnvKey, verified: false }+ : { method: "stored_credentials", verified: false };+ } else {+ authentication = scanAuthentication(+ processEnvironment,+ output.auth,+ provider,+ commandAuth,+ );+ }if (authentication.method === "stored_credentials" &&isExternalModelProvider(provider)) {const externalProvider = EXTERNAL_CODEX_PROVIDERS[provider];throw new AuthenticationRequiredError(- `Set ${externalProvider.env_key} to run ${output.command} through ${externalProvider.name}.`,+ `Set ${providerEnvKey ?? externalProvider.env_key} to run ${output.command} through ${externalProvider.name}.`,);}let selected = selectedScanEnvironment(@@ -1450,6 +1476,47 @@ export async function runCodexSkillCommand(authentication.method === "command" ? "chatgpt" : output.auth,provider,);+ if (+ explicitChatgpt &&+ !commandAuth &&+ providerConfiguration !== undefined &&+ providerKeyConfigured+ ) {+ if (providerEnvKey !== undefined) {+ selected = Object.fromEntries(+ Object.entries(selected).filter(([name]) =>+ process.platform === "win32"+ ? name.toUpperCase() !== providerEnvKey.toUpperCase()+ : name !== providerEnvKey,+ ),+ );+ }+ // Native provider keys take precedence over stored authentication.+ // Apply explicit ChatGPT selection to the home and CLI configuration.+ const providerOverrides =+ typeof provider === "string"+ ? (+ overrides["model_providers"] as+ Record<string, JsonObject> | undefined+ )?.[provider]+ : undefined;+ for (const selectedConfig of [+ providerConfiguration,+ providerOverrides,+ ]) {+ if (selectedConfig === undefined) continue;+ delete selectedConfig["env_key"];+ delete selectedConfig["experimental_bearer_token"];+ selectedConfig["requires_openai_auth"] = true;+ }+ args = [+ ...args,+ ...modelProviderConfigOverride(overrides).flatMap((value) => [+ "--config",+ value,+ ]),+ ];+ }if (authentication.method === "stored_credentials" &&requiresOpenAiAuth@@ -1535,6 +1602,15 @@ export async function runCodexSkillCommand(];}}+ if (authentication.method === "api_key" && providerEnvKey !== undefined) {+ const providerKey = environmentEntry(+ processEnvironment,+ providerEnvKey,+ );+ if (providerKey !== undefined) {+ selected = { ...selected, [providerEnvKey]: providerKey };+ }+ }if (output.appServer === undefined) {const authConfig =credentialConfig ?? (await readCodexHomeConfig(selected));@@ -7409,6 +7485,20 @@ async function runSkill((isExternalModelProvider(provider)? EXTERNAL_CODEX_PROVIDERS[provider]: undefined)));+ const effectiveOverrides = resolveCommandAuthConfig(+ mergeCodexOverrides(+ overrides,+ provider === undefined+ ? {}+ : {+ model_provider: provider,+ ...(providerConfiguration === undefined+ ? {}+ : { model_providers: { [provider]: providerConfiguration } }),+ },+ ),+ configuredCodexHome(options.environment ?? dependencies.environment),+ );const directory = options.directory ?? dependencies.currentDirectory();const contents: Array<string | Finding> = [...(options.findings ?? [])];for (const input of inputs) {@@ -7574,21 +7664,10 @@ async function runSkill(...(provider === undefined? []: ["--config", `model_provider=${JSON.stringify(provider)}`]),- ...modelProviderConfigOverride(- resolveCommandAuthConfig(- mergeCodexOverrides(- overrides,- provider === undefined || providerConfiguration === undefined- ? {}- : {- model_providers: {- [provider]: providerConfiguration,- },- },- ),- configuredCodexHome(options.environment ?? dependencies.environment),- ),- ).flatMap((value) => ["--config", value]),+ ...modelProviderConfigOverride(effectiveOverrides).flatMap((value) => [+ "--config",+ value,+ ]),"--config",verify || assess? 'approval_policy="on-request"'@@ -7620,7 +7699,7 @@ async function runSkill(auth: options.auth ?? "auto",directory,modelProvider: provider,- providerConfiguration,+ codexOverrides: effectiveOverrides,stdout,stderr,...(appServerdiff --git a/sdk/typescript/tests-ts/cli-authentication.test.ts b/sdk/typescript/tests-ts/cli-authentication.test.tsindex 574118785..45dcb44d9 100644--- a/sdk/typescript/tests-ts/cli-authentication.test.ts+++ b/sdk/typescript/tests-ts/cli-authentication.test.ts@@ -1368,6 +1368,671 @@ describe("CLI authentication", () => {});describe("skill authentication", () => {+ async function runProviderSkill({+ command = "patch",+ auth = "api-key",+ overrides,+ environment,+ ambientConfig,+ storedCredentials = false,+ }: {+ command?: "validate" | "patch" | "verify-fix";+ auth?: "auto" | "chatgpt" | "api-key";+ overrides: readonly string[];+ environment?: NodeJS.ProcessEnv;+ ambientConfig?: string;+ storedCredentials?: boolean;+ }) {+ const repository = join(stateDirectory, "repository");+ const ambientHome = join(stateDirectory, "ambient");+ const log = join(stateDirectory, "provider.jsonl");+ await mkdir(repository);+ await mkdir(ambientHome);+ if (ambientConfig !== undefined) {+ await writeFile(join(ambientHome, "config.toml"), ambientConfig);+ }+ if (storedCredentials) {+ await writeFile(+ join(ambientHome, "auth.json"),+ JSON.stringify({+ auth_mode: "apikey",+ OPENAI_API_KEY: "SYNTHETIC_STORED_KEY",+ }),+ { mode: 0o600 },+ );+ }+ const stdout = capture();+ const stderr = capture();+ const status = await main(+ [+ command,+ "Synthetic issue",+ "--auth",+ auth,+ ...overrides.flatMap((value) => ["--codex", value]),+ ],+ stdout.stream,+ stderr.stream,+ dependencies({+ currentDirectory: repository,+ environment: {+ ...environment,+ CODEX_HOME: ambientHome,+ SYNTHETIC_PROVIDER_LOG: log,+ SYNTHETIC_SKILL_COMMAND: command,+ },+ onCodex: async (args, output, environment, input) => {+ const originalOverrides = structuredClone(output?.codexOverrides);+ const result = await runCodexSkillCommand(+ [+ fileURLToPath(+ new URL("./fixtures/skill-provider-auth.mjs", import.meta.url),+ ),+ ...args,+ ],+ output,+ { command: process.execPath },+ environment,+ input,+ );+ expect(output?.codexOverrides).toEqual(originalOverrides);+ return result;+ },+ }),+ );+ const records = existsSync(log)+ ? (await readFile(log, "utf8"))+ .trim()+ .split("\n")+ .map((line) => JSON.parse(line))+ : [];+ if (ambientConfig !== undefined) {+ expect(await readFile(join(ambientHome, "config.toml"), "utf8")).toBe(+ ambientConfig,+ );+ }+ return {+ status,+ stderr: stderr.text(),+ launch: records[0],+ requests: records.slice(1),+ };+ }++ test.each([+ ["validate", "auto", "gateway"],+ ["validate", "api-key", "gateway"],+ ["patch", "auto", "gateway"],+ ["patch", "api-key", "gateway"],+ ["verify-fix", "auto", "gateway"],+ ["verify-fix", "api-key", "gateway"],+ ["patch", "api-key", "openrouter"],+ ] as const)(+ "%s uses the custom provider env_key with %s auth (%s)",+ async (command, auth, provider) => {+ const result = await runProviderSkill({+ command,+ auth,+ overrides: [+ `model_provider=${JSON.stringify(provider)}`,+ `model_providers.${provider}.name="Synthetic gateway"`,+ `model_providers.${provider}.base_url="https://gateway.example.test/v1"`,+ `model_providers.${provider}.wire_api="responses"`,+ `model_providers.${provider}.env_key="GATEWAY_API_KEY"`,+ ],+ environment: { GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY" },+ });+ expect(result.status, result.stderr).toBe(0);+ expect(result.launch.environment).toEqual({+ GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY",+ });+ expect(result.requests.map((request) => request.method)).not.toContain(+ "account/login/start",+ );+ if (command !== "validate") {+ expect(+ result.requests.find((request) => request.method === "thread/start")+ .params.modelProvider,+ ).toBe(provider);+ }+ },+ );++ test("rejects a missing custom provider API key before launch", async () => {+ const result = await runProviderSkill({+ overrides: [+ 'model_provider="gateway"',+ 'model_providers.gateway.env_key="GATEWAY_API_KEY"',+ ],+ });+ expect(result.status).toBe(2);+ expect(result.stderr).toContain("GATEWAY_API_KEY");+ expect(result.launch).toBeUndefined();+ });++ test.each(["patch", "verify-fix"] as const)(+ "%s preserves valid names in unrelated ambient configuration",+ async (command) => {+ const result = await runProviderSkill({+ command,+ overrides: [],+ 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"',+ "[mcp_servers.prototype]",+ 'command="synthetic-mcp"',+ "enabled=false",+ ].join("\n"),+ environment: { GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY" },+ });+ expect(result.status, result.stderr).toBe(0);+ expect(result.launch.environment).toEqual({+ GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY",+ });+ },+ );++ test.each([true, false])(+ "patch uses a custom provider table override with ambient selection (new key present: %p)",+ async (hasSelectedKey) => {+ const result = await runProviderSkill({+ overrides: ['model_providers.gateway.env_key="GATEWAY_API_KEY"'],+ ambientConfig: [+ 'model_provider="gateway"',+ "[model_providers.gateway]",+ 'name="Synthetic gateway"',+ 'base_url="https://gateway.example.test/v1"',+ 'wire_api="responses"',+ 'env_key="OLD_GATEWAY_API_KEY"',+ ].join("\n"),+ environment: hasSelectedKey+ ? { GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY" }+ : { OLD_GATEWAY_API_KEY: "SYNTHETIC_OLD_KEY" },+ });+ if (hasSelectedKey) {+ expect(result.status, result.stderr).toBe(0);+ expect(result.launch.environment).toEqual({+ GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY",+ });+ } else {+ expect(result.status).toBe(2);+ expect(result.stderr).toMatch(/\bGATEWAY_API_KEY\b/u);+ expect(result.launch).toBeUndefined();+ }+ },+ );++ test.each([+ ["patch", false, false],+ ["patch", true, false],+ ["verify-fix", false, false],+ ["verify-fix", true, false],+ ["patch", true, true],+ ] as const)(+ "%s keeps ambient provider authentication with explicit selection (partial override: %p, profile: %p)",+ async (command, partialOverride, profile) => {+ const result = await runProviderSkill({+ command,+ overrides: [+ 'model_provider="gateway"',+ ...(partialOverride+ ? [+ 'model_providers.gateway.base_url="https://alternate.example.test/v1"',+ ]+ : []),+ ],+ ambientConfig: [+ ...(profile ? ['profile="ambient"'] : []),+ 'model_provider="gateway"',+ ...(profile ? ["[profiles.ambient]", 'model_provider="other"'] : []),+ "[model_providers.gateway]",+ 'name="Synthetic gateway"',+ 'base_url="https://gateway.example.test/v1"',+ 'wire_api="responses"',+ "requires_openai_auth=true",+ ...(profile+ ? [+ "[model_providers.other]",+ 'name="Other gateway"',+ 'base_url="https://other.example.test/v1"',+ 'wire_api="responses"',+ 'env_key="OTHER_API_KEY"',+ ]+ : []),+ ].join("\n"),+ environment: { OPENAI_API_KEY: "SYNTHETIC_OPENAI_KEY" },+ });+ expect(result.status, result.stderr).toBe(0);+ expect(result.launch.environment).toEqual({+ CODEX_API_KEY: "SYNTHETIC_OPENAI_KEY",+ });+ expect(result.launch.args).toContain(+ 'cli_auth_credentials_store="ephemeral"',+ );+ expect(+ result.requests+ .filter((request) => request.method === "account/login/start")+ .map((request) => request.params),+ ).toEqual([{ type: "apiKey", apiKey: "SYNTHETIC_OPENAI_KEY" }]);+ },+ );++ test.each([+ ["ollama", "auto"],+ ["ollama", "api-key"],+ ["lmstudio", "auto"],+ ["lmstudio", "api-key"],+ ] as const)(+ "patch ignores replacement authentication for native provider %s with %s auth",+ async (provider, auth) => {+ const result = await runProviderSkill({+ auth,+ overrides: [`model_provider=${JSON.stringify(provider)}`],+ ambientConfig: [+ `model_provider=${JSON.stringify(provider)}`,+ `[model_providers.${provider}]`,+ 'name="Synthetic override"',+ 'base_url="https://ignored.example.test/v1"',+ 'wire_api="responses"',+ 'env_key="IGNORED_KEY"',+ "requires_openai_auth=true",+ ].join("\n"),+ environment: { OPENAI_API_KEY: "SYNTHETIC_OPENAI_KEY" },+ });+ expect(result.status, result.stderr).toBe(0);+ expect(result.launch.environment).toEqual({+ OPENAI_API_KEY: "SYNTHETIC_OPENAI_KEY",+ });+ expect(result.requests.map((request) => request.method)).not.toContain(+ "account/login/start",+ );+ },+ );++ test.each(["patch", "verify-fix"] as const)(+ "%s keeps native OpenAI authentication despite a provider table override",+ async (command) => {+ const result = await runProviderSkill({+ command,+ overrides: ['model_provider="openai"'],+ ambientConfig: [+ 'model_provider="openai"',+ "[model_providers.openai]",+ 'name="Synthetic override"',+ 'base_url="https://ignored.example.test/v1"',+ 'wire_api="responses"',+ 'env_key="OPENAI_API_KEY"',+ ].join("\n"),+ environment: { OPENAI_API_KEY: "SYNTHETIC_OPENAI_KEY" },+ });+ expect(result.status, result.stderr).toBe(0);+ expect(result.launch.environment).toEqual({+ CODEX_API_KEY: "SYNTHETIC_OPENAI_KEY",+ });+ expect(result.launch.args).toContain(+ 'cli_auth_credentials_store="ephemeral"',+ );+ expect(+ result.requests+ .filter((request) => request.method === "account/login/start")+ .map((request) => request.params),+ ).toEqual([{ type: "apiKey", apiKey: "SYNTHETIC_OPENAI_KEY" }]);+ },+ );++ test.each([+ ["patch", "auto"],+ ["patch", "api-key"],+ ["verify-fix", "auto"],+ ["verify-fix", "api-key"],+ ] as const)(+ "%s uses the custom provider key before OpenAI login with %s auth",+ async (command, auth) => {+ const result = await runProviderSkill({+ 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"),+ environment: { GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY" },+ });+ expect(result.status, result.stderr).toBe(0);+ expect(result.launch.environment).toEqual({+ GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY",+ });+ expect(result.requests.map((request) => request.method)).not.toContain(+ "account/login/start",+ );+ },+ );++ test.each(+ (+ [+ ["validate", "override"],+ ["patch", "override"],+ ["verify-fix", "override"],+ ["patch", "ambient"],+ ["verify-fix", "ambient"],+ ["patch", "profile"],+ ["verify-fix", "profile"],+ ] as const+ ).flatMap(([command, source]) =>+ [true, false, undefined].flatMap((requiresOpenAiAuth) =>+ ["env_key", "bearer"].map(+ (credential) =>+ [command, source, requiresOpenAiAuth, credential] as const,+ ),+ ),+ ),+ )(+ "%s removes the custom provider key and %s config for explicit ChatGPT auth (requires OpenAI: %p, credential: %s)",+ async (command, source, requiresOpenAiAuth, credential) => {+ const configuredEnvKey =+ process.platform === "win32" ? "gateway_api_key" : "GATEWAY_API_KEY";+ const environment = { GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY" };+ const providerConfig = {+ name: "Synthetic gateway",+ base_url: "https://gateway.example.test/v1",+ wire_api: "responses",+ ...(requiresOpenAiAuth === undefined+ ? {}+ : { requires_openai_auth: requiresOpenAiAuth }),+ };+ const providerSettings = [+ ...Object.entries(providerConfig).map(+ ([key, value]) => `${key}=${JSON.stringify(value)}`,+ ),+ ...(credential === "env_key"+ ? [`env_key=${JSON.stringify(configuredEnvKey)}`]+ : []),+ ...(credential === "bearer" || requiresOpenAiAuth === false+ ? ['experimental_bearer_token="SYNTHETIC_FALLBACK_KEY"']+ : []),+ ];+ const result = await runProviderSkill({+ command,+ auth: "chatgpt",+ overrides:+ source === "override"+ ? [+ 'model_provider="gateway"',+ ...providerSettings.map(+ (setting) => `model_providers.gateway.${setting}`,+ ),+ ]+ : [],+ ...(source === "override"+ ? {}+ : {+ ambientConfig: [+ ...(source === "profile"+ ? ['profile="gateway-profile"', "[profiles.gateway-profile]"]+ : []),+ 'model_provider="gateway"',+ "[model_providers.gateway]",+ ...providerSettings,+ ].join("\n"),+ }),+ environment,+ storedCredentials: true,+ });+ expect(result.status, result.stderr).toBe(0);+ expect(result.launch.environment).toEqual(+ credential === "env_key" ? {} : environment,+ );+ expect(parseToml(result.launch.config)["model_providers"]).toEqual({+ gateway: { ...providerConfig, requires_openai_auth: true },+ });+ const providerOverride = result.launch.args.findLast((arg: string) =>+ arg.startsWith("model_providers="),+ );+ if (source === "override") {+ expect(parseToml(providerOverride)["model_providers"]).toEqual({+ gateway: { ...providerConfig, requires_openai_auth: true },+ });+ } else {+ expect(providerOverride).toBeUndefined();+ }+ expect(result.requests.map((request) => request.method)).not.toContain(+ "account/login/start",+ );+ expect(environment.GATEWAY_API_KEY).toBe("SYNTHETIC_GATEWAY_KEY");+ },+ );++ test.each(["patch", "verify-fix"] as const)(+ "%s keeps ambient provider credentials out of process arguments",+ async (command) => {+ const result = await runProviderSkill({+ command,+ auth: "chatgpt",+ overrides: [+ 'model_provider="gateway"',+ 'model_providers.gateway.env_key="GATEWAY_API_KEY"',+ "model_providers.gateway.requires_openai_auth=false",+ 'model_providers.gateway.http_headers.X-Explicit="SYNTHETIC_CLI_HEADER"',+ ],+ 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"',+ "[model_providers.gateway.http_headers]",+ 'Authorization="Bearer SYNTHETIC_SELECTED_CREDENTIAL"',+ "[model_providers.other]",+ 'name="Other gateway"',+ 'base_url="https://other.example.test/v1"',+ 'wire_api="responses"',+ 'experimental_bearer_token="SYNTHETIC_UNSELECTED_CREDENTIAL"',+ ].join("\n"),+ environment: { GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY" },+ storedCredentials: true,+ });+ expect(result.status, result.stderr).toBe(0);+ const argumentsText = JSON.stringify(result.launch.args);+ expect(argumentsText).not.toContain("SYNTHETIC_SELECTED_CREDENTIAL");+ expect(argumentsText).not.toContain("SYNTHETIC_UNSELECTED_CREDENTIAL");+ expect(argumentsText).toContain("SYNTHETIC_CLI_HEADER");+ expect(parseToml(result.launch.config)["model_providers"]).toEqual({+ gateway: {+ name: "Synthetic gateway",+ base_url: "https://gateway.example.test/v1",+ wire_api: "responses",+ requires_openai_auth: true,+ http_headers: {+ Authorization: "Bearer SYNTHETIC_SELECTED_CREDENTIAL",+ "X-Explicit": "SYNTHETIC_CLI_HEADER",+ },+ },+ });+ },+ );++ test.each(["patch", "verify-fix"] as const)(+ "%s rejects a missing custom provider key despite an available OpenAI key",+ async (command) => {+ const result = await runProviderSkill({+ 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"),+ environment: { OPENAI_API_KEY: "SYNTHETIC_UNRELATED_OPENAI_KEY" },+ });+ expect(result.status).toBe(2);+ expect(result.stderr).toMatch(/\bGATEWAY_API_KEY\b/u);+ expect(result.launch).toBeUndefined();+ },+ );++ test.each([+ ["patch", "OPENAI_API_KEY"],+ ["patch", "CODEX_API_KEY"],+ ["verify-fix", "OPENAI_API_KEY"],+ ["verify-fix", "CODEX_API_KEY"],+ ] as const)(+ "%s preserves provider key %s without OpenAI login",+ async (command, envKey) => {+ const configuredEnvKey =+ process.platform === "win32" ? envKey.toLowerCase() : envKey;+ const result = await runProviderSkill({+ 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=${JSON.stringify(configuredEnvKey)}`,+ "requires_openai_auth=true",+ ].join("\n"),+ environment: {+ OPENAI_API_KEY: "SYNTHETIC_OPENAI_KEY",+ CODEX_API_KEY: "SYNTHETIC_CODEX_KEY",+ },+ });+ expect(result.status, result.stderr).toBe(0);+ expect(result.requests.map((request) => request.method)).not.toContain(+ "account/login/start",+ );+ expect(result.launch.environment[envKey]).toBe(+ envKey === "OPENAI_API_KEY"+ ? "SYNTHETIC_OPENAI_KEY"+ : "SYNTHETIC_CODEX_KEY",+ );+ },+ );++ test("resolves custom provider key casing according to the platform", async () => {+ const result = await runProviderSkill({+ overrides: [+ 'model_provider="gateway"',+ 'model_providers.gateway.env_key="GATEWAY_API_KEY"',+ ],+ environment: { gateway_api_key: "SYNTHETIC_GATEWAY_KEY" },+ });+ if (process.platform === "win32") {+ expect(result.status, result.stderr).toBe(0);+ expect(result.launch.environment.GATEWAY_API_KEY).toBe(+ "SYNTHETIC_GATEWAY_KEY",+ );+ } else {+ expect(result.status).toBe(2);+ expect(result.stderr).toContain("GATEWAY_API_KEY");+ expect(result.launch).toBeUndefined();+ }+ });++ test.each([+ ["openrouter", "api-key"],+ ["fireworks", "api-key"],+ ["openrouter", "chatgpt"],+ ["fireworks", "chatgpt"],+ ] as const)(+ "preserves OPENAI_API_KEY when configured as the %s provider key with %s auth",+ async (provider, auth) => {+ const result = await runProviderSkill({+ auth,+ overrides: [+ `model_provider=${JSON.stringify(provider)}`,+ `model_providers.${provider}.name="Synthetic gateway"`,+ `model_providers.${provider}.base_url="https://gateway.example.test/v1"`,+ `model_providers.${provider}.wire_api="responses"`,+ `model_providers.${provider}.env_key="OPENAI_API_KEY"`,+ `model_providers.${provider}.requires_openai_auth=${auth === "chatgpt"}`,+ ],+ environment: {+ OPENAI_API_KEY: "SYNTHETIC_OPENAI_KEY",+ CODEX_API_KEY: "SYNTHETIC_CODEX_KEY",+ },+ });+ expect(result.status, result.stderr).toBe(0);+ expect(result.launch.environment).toEqual({+ OPENAI_API_KEY: "SYNTHETIC_OPENAI_KEY",+ });+ expect(result.requests.map((request) => request.method)).not.toContain(+ "account/login/start",+ );+ },+ );++ test.each([+ ["validate", "override"],+ ["patch", "override"],+ ["verify-fix", "override"],+ ["patch", "ambient"],+ ["verify-fix", "ambient"],+ ] as const)(+ "%s uses native provider bearer authentication from %s without an OpenAI login",+ async (command, source) => {+ const providerConfig = {+ name: "Synthetic gateway",+ base_url: "https://gateway.example.test/v1",+ wire_api: "responses",+ experimental_bearer_token: "SYNTHETIC_BEARER_TOKEN",+ requires_openai_auth: true,+ };+ const settings = Object.entries(providerConfig).map(+ ([key, value]) => `${key}=${JSON.stringify(value)}`,+ );+ const result = await runProviderSkill({+ command,+ auth: "auto",+ overrides: [+ 'model_provider="gateway"',+ ...(source === "override"+ ? settings.map((setting) => `model_providers.gateway.${setting}`)+ : []),+ ],+ ...(source === "ambient"+ ? {+ ambientConfig: ["[model_providers.gateway]", ...settings].join(+ "\n",+ ),+ }+ : {}),+ });+ expect(result.status, result.stderr).toBe(0);+ expect(result.launch.environment).toEqual({});+ if (source === "override") {+ const override = result.launch.args.findLast((arg: string) =>+ arg.startsWith("model_providers="),+ );+ expect(parseToml(override)["model_providers"]).toEqual({+ gateway: providerConfig,+ });+ } else {+ expect(parseToml(result.launch.config)["model_providers"]).toEqual({+ gateway: providerConfig,+ });+ }+ expect(result.requests.map((request) => request.method)).not.toContain(+ "account/login/start",+ );+ },+ );+test.each(["validate", "patch", "verify-fix"])("%s advertises scan auth modes",async (command) => {diff --git a/sdk/typescript/tests-ts/cli-patch.test.ts b/sdk/typescript/tests-ts/cli-patch.test.tsindex 0beda9ef3..c44517c3f 100644--- a/sdk/typescript/tests-ts/cli-patch.test.ts+++ b/sdk/typescript/tests-ts/cli-patch.test.ts@@ -842,9 +842,11 @@ describe("scan and patch workflow", () => {expect(args).toContain("analytics.enabled=false");expect(args).toContain('model_provider="synthetic.gateway"');expect(output?.modelProvider).toBe("synthetic.gateway");- expect(output?.providerConfiguration?.["env_key"]).toBe(- "SYNTHETIC_KEY",- );+ expect(output?.codexOverrides).toMatchObject({+ model_providers: {+ "synthetic.gateway": { env_key: "SYNTHETIC_KEY" },+ },+ });expect(parseToml(args.find((arg) => arg.startsWith("model_providers="))!,@@ -1229,9 +1231,15 @@ describe("scan and patch workflow", () => {arg.startsWith("model_providers="),);expect(output?.modelProvider).toBe(provider);- expect(output?.providerConfiguration?.["auth"]).toEqual({- command: "./synthetic-auth",- args: ["--json"],+ expect(output?.codexOverrides).toMatchObject({+ model_providers: {+ [provider]: {+ auth: {+ command: "./synthetic-auth",+ args: ["--json"],+ },+ },+ },});completePatches(args, output);return 0;diff --git a/sdk/typescript/tests-ts/cli-skills.test.ts b/sdk/typescript/tests-ts/cli-skills.test.tsindex f1069c5b5..a97f85a1e 100644--- a/sdk/typescript/tests-ts/cli-skills.test.ts+++ b/sdk/typescript/tests-ts/cli-skills.test.ts@@ -886,9 +886,9 @@ describe("CLI skill commands", () => {onCodex: (args, output, environment) => {invocation = args;expect(output?.modelProvider).toBe("synthetic");- expect(output?.providerConfiguration).toEqual(- providerConfiguration,- );+ expect(output?.codexOverrides).toMatchObject({+ model_providers: { synthetic: providerConfiguration },+ });expect(environment?.["SYNTHETIC_GATEWAY_KEY"]).toBe("SYNTHETIC_VALUE",);diff --git a/sdk/typescript/tests-ts/fixtures/skill-provider-auth.mjs b/sdk/typescript/tests-ts/fixtures/skill-provider-auth.mjsnew file mode 100644index 000000000..a46879dc5--- /dev/null+++ b/sdk/typescript/tests-ts/fixtures/skill-provider-auth.mjs@@ -0,0 +1,85 @@+import { appendFileSync, existsSync, readFileSync } from "node:fs";+import { join } from "node:path";+import { createInterface } from "node:readline";++const record = (value) =>+ appendFileSync(+ process.env.SYNTHETIC_PROVIDER_LOG,+ `${JSON.stringify(value)}\n`,+ );+const send = (value) => process.stdout.write(`${JSON.stringify(value)}\n`);+const configPath = join(process.env.CODEX_HOME, "config.toml");+record({+ args: process.argv.slice(2),+ config: existsSync(configPath) ? readFileSync(configPath, "utf8") : "",+ environment: {+ GATEWAY_API_KEY: process.env.GATEWAY_API_KEY,+ OPENAI_API_KEY: process.env.OPENAI_API_KEY,+ CODEX_API_KEY: process.env.CODEX_API_KEY,+ },+});++if (process.env.SYNTHETIC_SKILL_COMMAND === "validate") {+ send({+ type: "item.completed",+ item: { type: "agent_message", text: "Synthetic validation complete" },+ });+} else {+ for await (const line of createInterface({ input: process.stdin })) {+ const request = JSON.parse(line);+ record(request);+ if (request.method === "initialize") {+ send({ id: request.id, result: {} });+ } else if (request.method === "account/login/start") {+ send({ id: request.id, result: { type: "apiKey" } });+ } else if (request.method === "config/read") {+ send({ id: request.id, result: { layers: [] } });+ } else if (request.method === "thread/start") {+ send({+ id: request.id,+ result: {+ thread: { id: "synthetic-thread" },+ sandbox: {+ type:+ request.params.sandbox === "read-only"+ ? "readOnly"+ : "workspaceWrite",+ },+ },+ });+ } else if (request.method === "command/exec") {+ send({ id: request.id, result: { exitCode: 0 } });+ } else if (request.method === "turn/start") {+ send({ id: request.id, result: { turn: { id: "synthetic-turn" } } });+ send({+ method: "item/completed",+ params: {+ threadId: "synthetic-thread",+ turnId: "synthetic-turn",+ item: {+ type: "agentMessage",+ text:+ process.env.SYNTHETIC_SKILL_COMMAND === "verify-fix"+ ? JSON.stringify({+ results: [+ {+ id: "finding-1",+ status: "fixed",+ evidence: "Synthetic verification",+ },+ ],+ })+ : "Synthetic patch complete",+ },+ },+ });+ send({+ method: "turn/completed",+ params: {+ threadId: "synthetic-thread",+ turn: { id: "synthetic-turn", status: "completed" },+ },+ });+ }+ }+}