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

fix: make ChatGPT/API-key auth conflicts actionable (#19)

mldangelo-oaia1f3821a9f06
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index aa77aa3ac..f829130a6 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -1433,11 +1433,7 @@ export async function main(
"To use a ChatGPT sign-in, unset OPENAI_API_KEY and CODEX_API_KEY.\n",
);
}
- } else if (
- exitCode === 0 &&
- !options.withApiKey &&
- !options.withAccessToken
- ) {
+ } else if (exitCode === 0 && !options.withApiKey) {
const authentication = scanAuthentication(dependencies.environment);
if (authentication.method === "api_key") {
const configuredApiKeyVariables = Object.entries(
@@ -1450,10 +1446,16 @@ export async function main(
name.toUpperCase() === "CODEX_API_KEY"),
)
.map(([name]) => name);
+ const loginKind = options.withAccessToken
+ ? "Access-token"
+ : "ChatGPT";
+ const credential = options.withAccessToken
+ ? "access token"
+ : "ChatGPT sign-in";
errorOutput.write(
- "ChatGPT login succeeded. Interactive scans will ask which account to use; " +
+ `${loginKind} login succeeded. Interactive scans will ask which account to use; ` +
`noninteractive scans will use ${authentication.source}.\n` +
- "To use your ChatGPT sign-in, pass '--auth chatgpt' or run " +
+ `To use your ${credential}, pass '--auth chatgpt' or run ` +
`'unset ${configuredApiKeyVariables.join(" ")}'.\n`,
);
}
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index a84e7f914..378bc262d 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -3410,6 +3410,7 @@ appendFileSync(${JSON.stringify(keyLog)}, apiKey.trim() + "\\n");
const repository = join(root, "repository");
const codexHome = join(root, "codex-home");
const fakeCodex = join(root, "codex.mjs");
+ const keyLog = join(root, "api-keys");
const scanDir = join(root, "scan");
await mkdir(repository);
await mkdir(codexHome);
@@ -3417,12 +3418,16 @@ appendFileSync(${JSON.stringify(keyLog)}, apiKey.trim() + "\\n");
await writeFile(
fakeCodex,
`
+import { appendFileSync } from "node:fs";
+
const args = process.argv.slice(2).join(" ");
if (args === "login --with-api-key") {
let apiKey = "";
for await (const chunk of process.stdin) apiKey += chunk;
if (!["secret-key", "ambient-key"].includes(apiKey.trim())) {
process.exitCode = 3;
+ } else {
+ appendFileSync(${JSON.stringify(keyLog)}, apiKey);
}
} else if (args === "login") {
console.error("Open https://auth.example.test/login");
@@ -3485,6 +3490,7 @@ if (args === "login --with-api-key") {
OPENAI_API_KEY: "ambient-key",
CODEX_API_KEY: "secondary-ambient-key",
});
+ expect(await readFile(keyLog, "utf8")).toBe("secret-key\nambient-key\n");
} finally {
await client.close();
}
diff --git a/sdk/typescript/tests-ts/cli-authentication.test.ts b/sdk/typescript/tests-ts/cli-authentication.test.ts
index 7f1975db0..3b5654c2b 100644
--- a/sdk/typescript/tests-ts/cli-authentication.test.ts
+++ b/sdk/typescript/tests-ts/cli-authentication.test.ts
@@ -106,6 +106,7 @@ describe("CLI authentication", () => {
dependencies({ environment }),
),
).toBe(0);
+ expect(stdout.text()).toBe("");
expect(stderr.text()).toContain(
"ChatGPT login succeeded. Interactive scans will ask which account to use;",
);
@@ -118,13 +119,58 @@ describe("CLI authentication", () => {
}
});
+ test("explains when an environment API key overrides an access-token login", async () => {
+ for (const [environment, source, unsetCommand] of [
+ [
+ { OPENAI_API_KEY: "sk-proj-SYNTHETIC_SECRET_123" },
+ "OPENAI_API_KEY",
+ "unset OPENAI_API_KEY",
+ ],
+ [
+ { Codex_Api_Key: "sk-proj-SYNTHETIC_SECRET_456" },
+ "CODEX_API_KEY",
+ "unset Codex_Api_Key",
+ ],
+ [
+ {
+ OPENAI_API_KEY: "sk-proj-SYNTHETIC_SECRET_123",
+ CODEX_API_KEY: "sk-proj-SYNTHETIC_SECRET_456",
+ },
+ "OPENAI_API_KEY",
+ "unset OPENAI_API_KEY CODEX_API_KEY",
+ ],
+ ] as const) {
+ const stdout = capture();
+ const stderr = capture();
+
+ expect(
+ await main(
+ ["login", "--with-access-token"],
+ stdout.stream,
+ stderr.stream,
+ dependencies({ environment }),
+ ),
+ ).toBe(0);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toContain(
+ "Access-token login succeeded. Interactive scans will ask which account to use;",
+ );
+ expect(stderr.text()).toContain(
+ `noninteractive scans will use ${source}.`,
+ );
+ expect(stderr.text()).toContain("--auth chatgpt");
+ expect(stderr.text()).toContain(`'${unsetCommand}'`);
+ expect(stderr.text()).not.toContain("SYNTHETIC_SECRET");
+ }
+ });
+
test("does not report a ChatGPT login warning for failed or API-key logins", async () => {
const environment = { OPENAI_API_KEY: "synthetic-private-key" };
for (const [argv, exitCode] of [
[["login"], 2],
[["login", "--with-api-key"], 0],
- [["login", "--with-access-token"], 0],
+ [["login", "--with-access-token"], 2],
] as const) {
const stderr = capture();
@@ -137,6 +183,7 @@ describe("CLI authentication", () => {
),
).toBe(exitCode);
expect(stderr.text()).not.toContain("ChatGPT login succeeded");
+ expect(stderr.text()).not.toContain("Access-token login succeeded");
expect(stderr.text()).not.toContain("synthetic-private-key");
}
});
@@ -210,6 +257,25 @@ describe("CLI authentication", () => {
}
});
+ test("does not hide or relabel a failed ChatGPT login", async () => {
+ const stdout = capture();
+ const stderr = capture();
+
+ expect(
+ await main(
+ ["login"],
+ stdout.stream,
+ stderr.stream,
+ dependencies({
+ environment: { OPENAI_API_KEY: "sk-proj-SYNTHETIC_SECRET_123" },
+ onCodex: () => 17,
+ }),
+ ),
+ ).toBe(17);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toBe("");
+ });
+
test("never prompts during automation, explicit selection, or unavailable credentials", async () => {
for (const scenario of [
{ argv: ["scan", "--json"], terminal: true, stored: true, key: true },
@@ -474,6 +540,47 @@ describe("CLI authentication", () => {
);
});
+ test("identifies overriding API keys in noninteractive scan auth failures", async () => {
+ for (const [environment, source] of [
+ [{ OPENAI_API_KEY: "sk-proj-SYNTHETIC_SECRET_123" }, "OPENAI_API_KEY"],
+ [{ CODEX_API_KEY: "sk-proj-SYNTHETIC_SECRET_456" }, "CODEX_API_KEY"],
+ ] as const) {
+ for (const [detail, expected] of [
+ ["401 invalid API key for org-private", "Authentication failed"],
+ [
+ "403 model access denied for org-private",
+ "cannot access the configured model",
+ ],
+ ] as const) {
+ const stdout = capture();
+ const stderr = capture(false);
+ const deps = dependencies({ environment });
+ deps.createSecurity = () => ({
+ run: async (_repository, options) => {
+ options?.onAuthentication?.({
+ method: "api_key",
+ source,
+ verified: false,
+ });
+ throw new CodexSecurityError(detail);
+ },
+ preflight: async () => fakePreflight(),
+ close: async () => {},
+ });
+
+ expect(await main(["scan"], stdout.stream, stderr.stream, deps)).toBe(
+ 2,
+ );
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toContain(expected);
+ expect(stderr.text()).toContain(source);
+ expect(stderr.text()).toContain("--auth chatgpt");
+ expect(stderr.text()).not.toContain("SYNTHETIC_SECRET");
+ expect(stderr.text()).not.toContain("org-private");
+ }
+ }
+ });
+
test("prints the ChatGPT recovery hint on noninteractive scan output", async () => {
const stdout = capture();
const stderr = capture(false);