Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

test: speed up skill authentication configuration coverage (#1904)

Michael D'Angeloc02da4455b6e
diff --git a/sdk/typescript/scripts/package-dist-files.mjs b/sdk/typescript/scripts/package-dist-files.mjs
index 44fa06602..f840cc29e 100644
--- a/sdk/typescript/scripts/package-dist-files.mjs
+++ b/sdk/typescript/scripts/package-dist-files.mjs
@@ -98,6 +98,7 @@ const distModules = [
"security-policy",
"security-policy-cli",
"security-policy-diff",
+ "skill-authentication",
"standalone-validation-prompt",
"validation-records",
"suggest-owners",
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 5d2dbae92..5a95ca55a 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -60,6 +60,7 @@ import { Cli, z } from "incur";
import Ajv2020, { type ValidateFunction } from "ajv/dist/2020.js";
import { formatCliHelp } from "./cli-help.js";
import { formatCoverageScope } from "./coverage-presentation.js";
+import { resolveSkillAuthentication } from "./skill-authentication.js";
import { scanLogsJson } from "./cli-scan-logs-json.js";
import {
ARTIFACT_EXPORT_FILENAMES,
@@ -72,7 +73,7 @@ import {
type ArtifactExportArguments,
} from "./artifact-export.js";
export { exportEnvironment } from "./artifact-export.js";
-import { parse as parseToml, stringify as stringifyToml } from "smol-toml";
+import { parse as parseToml } from "smol-toml";
import { parse as parseYaml } from "yaml";
import {
classifyConnectionFailure,
@@ -164,7 +165,6 @@ import {
} from "./bulk-scan-discovery.js";
import {
DEFAULT_CODEX_CONFIG,
- deepMerge,
EXTERNAL_CODEX_PROVIDERS,
isExternalModelProvider,
mergeCodexOverrides,
@@ -1470,117 +1470,25 @@ export async function runCodexSkillCommand(
output.appServer === undefined
? {}
: resolveCodexProfile(await readCodexHomeConfig(processEnvironment));
- const overrides = parseToml(
- stringifyToml(output.codexOverrides ?? {}),
- ) as JsonObject;
- const config = deepMerge(structuredClone(ambientConfig), overrides);
- const provider = output.modelProvider ?? scanModelProvider(config);
- // Native Codex ignores configured tables for these built-in providers.
- const providerConfiguration =
- typeof provider === "string" &&
- !["openai", "ollama", "lmstudio"].includes(provider)
- ? (
- config["model_providers"] as
- Record<string, JsonObject> | undefined
- )?.[provider]
- : undefined;
- const providerEnvKey =
- typeof providerConfiguration?.["env_key"] === "string"
- ? providerConfiguration["env_key"]
- : undefined;
- const commandAuth = providerConfiguration?.["auth"] != null;
- const explicitChatgpt =
- output.auth === "chatgpt" && !isExternalModelProvider(provider);
- const providerBearer =
- typeof providerConfiguration?.["experimental_bearer_token"] ===
- "string";
- const providerKeyConfigured =
- providerEnvKey !== undefined || providerBearer;
- const requiresOpenAiAuth =
- !commandAuth &&
- (explicitChatgpt || !providerKeyConfigured) &&
- (provider === undefined ||
- provider === "openai" ||
- providerConfiguration?.["requires_openai_auth"] === true ||
- (explicitChatgpt && providerKeyConfigured));
- modelProvider = output.modelProvider;
- let credentialConfig: JsonObject | undefined;
- 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 ${providerEnvKey ?? externalProvider.env_key} to run ${output.command} through ${externalProvider.name}.`,
- );
- }
- let selected = selectedScanEnvironment(
+ const resolved = resolveSkillAuthentication(
+ { ...output, auth: output.auth },
processEnvironment,
- authentication.method === "command" ? "chatgpt" : output.auth,
- provider,
+ ambientConfig,
);
- 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,
- ]),
- ];
- }
+ const {
+ provider,
+ providerConfiguration,
+ providerEnvKey,
+ requiresOpenAiAuth,
+ } = resolved;
+ authentication = resolved.authentication;
+ modelProvider = output.modelProvider;
+ let credentialConfig: JsonObject | undefined;
+ let selected = resolved.environment;
+ args = [
+ ...args,
+ ...resolved.configOverrides.flatMap((value) => ["--config", value]),
+ ];
if (
authentication.method === "stored_credentials" &&
requiresOpenAiAuth
diff --git a/sdk/typescript/src/skill-authentication.ts b/sdk/typescript/src/skill-authentication.ts
new file mode 100644
index 000000000..3c7517fea
--- /dev/null
+++ b/sdk/typescript/src/skill-authentication.ts
@@ -0,0 +1,135 @@
+import { parse as parseToml, stringify as stringifyToml } from "smol-toml";
+import {
+ scanAuthentication,
+ selectedScanEnvironment,
+ type ScanAuthMode,
+ type ScanAuthentication,
+} from "./api.js";
+import { environmentEntry } from "./auth.js";
+import {
+ deepMerge,
+ EXTERNAL_CODEX_PROVIDERS,
+ isExternalModelProvider,
+ modelProviderConfigOverride,
+ scanModelProvider,
+ type JsonObject,
+} from "./config.js";
+import { AuthenticationRequiredError } from "./errors.js";
+
+/** Resolve a skill's authentication before preparing homes or launching Codex. */
+export function resolveSkillAuthentication(
+ options: {
+ auth: ScanAuthMode;
+ modelProvider?: string;
+ codexOverrides?: JsonObject;
+ command: "validate" | "patch" | "verify-fix";
+ },
+ processEnvironment: NodeJS.ProcessEnv,
+ ambientConfig: JsonObject,
+) {
+ const overrides = parseToml(
+ stringifyToml(options.codexOverrides ?? {}),
+ ) as JsonObject;
+ const config = deepMerge(structuredClone(ambientConfig), overrides);
+ const provider = options.modelProvider ?? scanModelProvider(config);
+ // Native Codex ignores configured tables for these built-in providers.
+ const providerConfiguration =
+ typeof provider === "string" &&
+ !["openai", "ollama", "lmstudio"].includes(provider)
+ ? (config["model_providers"] as Record<string, JsonObject> | undefined)?.[
+ provider
+ ]
+ : undefined;
+ const providerEnvKey =
+ typeof providerConfiguration?.["env_key"] === "string"
+ ? providerConfiguration["env_key"]
+ : undefined;
+ const commandAuth = providerConfiguration?.["auth"] != null;
+ const explicitChatgpt =
+ options.auth === "chatgpt" && !isExternalModelProvider(provider);
+ const providerBearer =
+ typeof providerConfiguration?.["experimental_bearer_token"] === "string";
+ const providerKeyConfigured = providerEnvKey !== undefined || providerBearer;
+ const requiresOpenAiAuth =
+ !commandAuth &&
+ (explicitChatgpt || !providerKeyConfigured) &&
+ (provider === undefined ||
+ provider === "openai" ||
+ providerConfiguration?.["requires_openai_auth"] === true ||
+ (explicitChatgpt && providerKeyConfigured));
+ let authentication: ScanAuthentication;
+ let configOverrides: string[] = [];
+ if (providerEnvKey !== undefined && !commandAuth && !explicitChatgpt) {
+ const key = environmentEntry(processEnvironment, providerEnvKey)?.trim();
+ if (!key && options.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,
+ options.auth,
+ provider,
+ commandAuth,
+ );
+ }
+ if (
+ authentication.method === "stored_credentials" &&
+ isExternalModelProvider(provider)
+ ) {
+ const externalProvider = EXTERNAL_CODEX_PROVIDERS[provider];
+ throw new AuthenticationRequiredError(
+ `Set ${providerEnvKey ?? externalProvider.env_key} to run ${options.command} through ${externalProvider.name}.`,
+ );
+ }
+ let selected = selectedScanEnvironment(
+ processEnvironment,
+ authentication.method === "command" ? "chatgpt" : options.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;
+ }
+ configOverrides = modelProviderConfigOverride(overrides);
+ }
+ return {
+ authentication,
+ provider,
+ providerConfiguration,
+ providerEnvKey,
+ requiresOpenAiAuth,
+ environment: selected,
+ configOverrides,
+ };
+}
diff --git a/sdk/typescript/tests-ts/cli-skill-chatgpt-provider-auth.test.ts b/sdk/typescript/tests-ts/cli-skill-chatgpt-provider-auth.test.ts
index 8b9d847fd..e97265e8b 100644
--- a/sdk/typescript/tests-ts/cli-skill-chatgpt-provider-auth.test.ts
+++ b/sdk/typescript/tests-ts/cli-skill-chatgpt-provider-auth.test.ts
@@ -1,36 +1,80 @@
import { realpath, rm } from "node:fs/promises";
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { parse as parseToml } from "smol-toml";
+import { resolveCodexProfile, type JsonObject } from "../src/config.js";
+import { resolveSkillAuthentication } from "../src/skill-authentication.js";
import { runProviderSkill } from "./support/cli-provider-authentication.js";
import { temporaryDirectory } from "./support/temporary-directories.js";
-let stateDirectory: string;
+const commandSources = [
+ ["validate", "override"],
+ ["patch", "override"],
+ ["verify-fix", "override"],
+ ["patch", "ambient"],
+ ["verify-fix", "ambient"],
+ ["patch", "profile"],
+ ["verify-fix", "profile"],
+] as const;
-beforeEach(async () => {
- stateDirectory = await realpath(
- await temporaryDirectory("codex-security-cli-authentication-"),
- );
-});
-
-afterEach(async () => {
- await rm(stateDirectory, { recursive: true, force: true });
-});
+function providerSettings(
+ source: "override" | "ambient" | "profile",
+ requiresOpenAiAuth: boolean | undefined,
+ credential: "env_key" | "bearer",
+) {
+ 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 settings = [
+ ...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"']
+ : []),
+ ];
+ return {
+ providerConfig,
+ environment,
+ overrides:
+ source === "override"
+ ? [
+ 'model_provider="gateway"',
+ ...settings.map((setting) => `model_providers.gateway.${setting}`),
+ ]
+ : [],
+ ...(source === "override"
+ ? {}
+ : {
+ ambientConfig: [
+ ...(source === "profile"
+ ? ['profile="gateway-profile"', "[profiles.gateway-profile]"]
+ : []),
+ 'model_provider="gateway"',
+ "[model_providers.gateway]",
+ ...settings,
+ ].join("\n"),
+ }),
+ };
+}
-describe("skill authentication", () => {
+// Exercise every configuration combination without repeatedly preparing a
+// credential home and launching the same subprocess protocol.
+describe("skill authentication configuration", () => {
test.each(
- (
- [
- ["validate", "override"],
- ["patch", "override"],
- ["verify-fix", "override"],
- ["patch", "ambient"],
- ["verify-fix", "ambient"],
- ["patch", "profile"],
- ["verify-fix", "profile"],
- ] as const
- ).flatMap(([command, source]) =>
+ commandSources.flatMap(([command, source]) =>
[true, false, undefined].flatMap((requiresOpenAiAuth) =>
- ["env_key", "bearer"].map(
+ (["env_key", "bearer"] as const).map(
(credential) =>
[command, source, requiresOpenAiAuth, credential] as const,
),
@@ -38,60 +82,81 @@ describe("skill authentication", () => {
),
)(
"%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 }),
+ (command, source, requiresOpenAiAuth, credential) => {
+ const { providerConfig, environment, overrides, ambientConfig } =
+ providerSettings(source, requiresOpenAiAuth, credential);
+ const options = {
+ command,
+ auth: "chatgpt" as const,
+ codexOverrides: parseToml(overrides.join("\n")) as JsonObject,
};
- 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 ambient = resolveCodexProfile(
+ parseToml(ambientConfig ?? "") as JsonObject,
+ );
+ const originalOptions = structuredClone(options);
+ const originalAmbient = structuredClone(ambient);
+ const result = resolveSkillAuthentication(options, environment, ambient);
+
+ expect(result.authentication).toEqual({
+ method: "stored_credentials",
+ verified: false,
+ });
+ expect(result.requiresOpenAiAuth).toBe(true);
+ expect(result.provider).toBe("gateway");
+ expect(result.environment).toEqual(
+ credential === "env_key" ? {} : environment,
+ );
+ expect(result.providerConfiguration).toEqual({
+ ...providerConfig,
+ requires_openai_auth: true,
+ });
+ expect(parseToml(result.configOverrides.join("\n"))).toEqual(
+ source === "override"
+ ? {
+ model_providers: {
+ gateway: { ...providerConfig, requires_openai_auth: true },
+ },
+ }
+ : {},
+ );
+ expect(options).toEqual(originalOptions);
+ expect(ambient).toEqual(originalAmbient);
+ expect(environment).toEqual({ GATEWAY_API_KEY: "SYNTHETIC_GATEWAY_KEY" });
+ },
+ );
+});
+
+describe("skill authentication at the subprocess boundary", () => {
+ let stateDirectory: string;
+
+ beforeEach(async () => {
+ stateDirectory = await realpath(
+ await temporaryDirectory("codex-security-cli-authentication-"),
+ );
+ });
+
+ afterEach(async () => {
+ await rm(stateDirectory, { recursive: true, force: true });
+ });
+
+ // Each command/config source still launches Codex with stored credentials.
+ // Include both native credential forms together to verify neither leaks.
+ test.each(commandSources)(
+ "%s applies ChatGPT authentication to %s configuration in the child",
+ async (command, source) => {
+ const { providerConfig, ...settings } = providerSettings(
+ source,
+ false,
+ "env_key",
+ );
const result = await runProviderSkill(stateDirectory, {
+ ...settings,
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(result.launch.environment).toEqual({});
expect(parseToml(result.launch.config)["model_providers"]).toEqual({
gateway: { ...providerConfig, requires_openai_auth: true },
});
@@ -108,7 +173,9 @@ describe("skill authentication", () => {
expect(result.requests.map((request) => request.method)).not.toContain(
"account/login/start",
);
- expect(environment.GATEWAY_API_KEY).toBe("SYNTHETIC_GATEWAY_KEY");
+ expect(settings.environment.GATEWAY_API_KEY).toBe(
+ "SYNTHETIC_GATEWAY_KEY",
+ );
},
);
});