Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions sdk/typescript/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3294,6 +3294,22 @@ export function scanAuthentication(
: { method: "api_key", source: key.source, verified: false };
}

/** Shell-neutral guidance so PowerShell users are not told to run POSIX `unset`. */
export function formatEnvironmentVariableRemovalGuidance(
names: readonly string[],
): string {
if (names.length === 0) {
return "remove OPENAI_API_KEY and CODEX_API_KEY from the environment";
}
if (names.length === 1) {
return `remove ${names[0]} from the environment`;
}
if (names.length === 2) {
return `remove ${names[0]} and ${names[1]} from the environment`;
}
return `remove ${names.slice(0, -1).join(", ")}, and ${names[names.length - 1]} from the environment`;
}

async function runtimeScanAuthentication(
environment: ProcessEnvironment,
codexHome: string,
Expand Down
7 changes: 4 additions & 3 deletions sdk/typescript/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
CodexSecurity,
createSecurityInternal,
environmentValue,
formatEnvironmentVariableRemovalGuidance,
listRepositoryFindings,
SCAN_AUTH_MODES,
scanAuthentication,
Expand Down Expand Up @@ -4299,7 +4300,7 @@ export async function main(
`Effective scan authentication: API key from ${authentication.source}.\n`,
);
errorOutput.write(
"To use a ChatGPT sign-in, unset OPENAI_API_KEY and CODEX_API_KEY.\n",
`To use a ChatGPT sign-in, ${formatEnvironmentVariableRemovalGuidance(["OPENAI_API_KEY", "CODEX_API_KEY"])}.\n`,
);
}
} else if (exitCode === 0 && !options.withApiKey) {
Expand All @@ -4324,8 +4325,8 @@ export async function main(
: "your ChatGPT sign-in";
errorOutput.write(
loginWarning +
`To use ${storedCredentials}, pass '--auth chatgpt' or run ` +
`'unset ${configuredApiKeyVariables.join(" ")}'.\n`,
`To use ${storedCredentials}, pass '--auth chatgpt' or ` +
`${formatEnvironmentVariableRemovalGuidance(configuredApiKeyVariables)}.\n`,
);
}
}
Expand Down
34 changes: 33 additions & 1 deletion sdk/typescript/tests-ts/api-environment.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { describe, expect, test } from "bun:test";
import { environmentValue } from "../src/api.js";
import {
environmentValue,
formatEnvironmentVariableRemovalGuidance,
} from "../src/api.js";

describe("environmentValue", () => {
test("treats empty values as unset and finds case variants", () => {
Expand All @@ -18,3 +21,32 @@ describe("environmentValue", () => {
);
});
});

describe("formatEnvironmentVariableRemovalGuidance", () => {
test("lists one, two, or more names without a shell command", () => {
expect(formatEnvironmentVariableRemovalGuidance([])).toBe(
"remove OPENAI_API_KEY and CODEX_API_KEY from the environment",
);
expect(formatEnvironmentVariableRemovalGuidance(["OPENAI_API_KEY"])).toBe(
"remove OPENAI_API_KEY from the environment",
);
expect(
formatEnvironmentVariableRemovalGuidance([
"OPENAI_API_KEY",
"CODEX_API_KEY",
]),
).toBe("remove OPENAI_API_KEY and CODEX_API_KEY from the environment");
expect(
formatEnvironmentVariableRemovalGuidance([
"OPENAI_API_KEY",
"CODEX_API_KEY",
"Codex_Api_Key",
]),
).toBe(
"remove OPENAI_API_KEY, CODEX_API_KEY, and Codex_Api_Key from the environment",
);
expect(
formatEnvironmentVariableRemovalGuidance(["OPENAI_API_KEY"]),
).not.toContain("unset");
});
});
25 changes: 13 additions & 12 deletions sdk/typescript/tests-ts/cli-authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,25 @@ describe("CLI authentication", () => {
`Effective scan authentication: API key from ${expectedSource}.`,
);
expect(stderr.text()).toContain(
"To use a ChatGPT sign-in, unset OPENAI_API_KEY and CODEX_API_KEY.",
"To use a ChatGPT sign-in, remove OPENAI_API_KEY and CODEX_API_KEY from the environment.",
);
expect(stderr.text()).not.toContain("SYNTHETIC_SECRET");
}
});

test("explains interactive choice and how to unset every shadowing key after ChatGPT login", async () => {
for (const [argv, environment, source, unsetCommand] of [
for (const [argv, environment, source, removalGuidance] of [
[
["login"],
{ OPENAI_API_KEY: "sk-proj-SYNTHETIC_SECRET_123" },
"OPENAI_API_KEY",
"unset OPENAI_API_KEY",
"remove OPENAI_API_KEY from the environment",
],
[
["login", "--device-auth"],
{ Codex_Api_Key: "sk-proj-SYNTHETIC_SECRET_456" },
"CODEX_API_KEY",
"unset Codex_Api_Key",
"remove Codex_Api_Key from the environment",
],
[
["login"],
Expand All @@ -207,7 +207,7 @@ describe("CLI authentication", () => {
CODEX_API_KEY: "sk-proj-SYNTHETIC_SECRET_456",
},
"OPENAI_API_KEY",
"unset OPENAI_API_KEY CODEX_API_KEY",
"remove OPENAI_API_KEY and CODEX_API_KEY from the environment",
],
] as const) {
const stdout = capture();
Expand All @@ -229,30 +229,31 @@ describe("CLI authentication", () => {
`noninteractive scans will use ${source}.`,
);
expect(stderr.text()).toContain("--auth chatgpt");
expect(stderr.text()).toContain(`'${unsetCommand}'`);
expect(stderr.text()).toContain(removalGuidance);
expect(stderr.text()).not.toContain("unset ");
expect(stderr.text()).not.toContain("SYNTHETIC_SECRET");
}
});

test("warns when an environment API key overrides a successful access-token login", async () => {
for (const [environment, source, unsetCommand] of [
for (const [environment, source, removalGuidance] of [
[
{ OPENAI_API_KEY: "sk-proj-SYNTHETIC_SECRET_123" },
"OPENAI_API_KEY",
"unset OPENAI_API_KEY",
"remove OPENAI_API_KEY from the environment",
],
[
{ Codex_Api_Key: "sk-proj-SYNTHETIC_SECRET_456" },
"CODEX_API_KEY",
"unset Codex_Api_Key",
"remove Codex_Api_Key from the environment",
],
[
{
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",
"remove OPENAI_API_KEY and CODEX_API_KEY from the environment",
],
] as const) {
const stdout = capture();
Expand All @@ -271,9 +272,9 @@ describe("CLI authentication", () => {
`Access-token login succeeded, but noninteractive scans will use ${source}.`,
);
expect(stderr.text()).toContain(
"To use your stored credentials, pass '--auth chatgpt' or run ",
`To use your stored credentials, pass '--auth chatgpt' or ${removalGuidance}.`,
);
expect(stderr.text()).toContain(`'${unsetCommand}'`);
expect(stderr.text()).not.toContain("unset ");
expect(stderr.text()).not.toContain("ChatGPT login succeeded");
expect(stderr.text()).not.toContain("SYNTHETIC_SECRET");
}
Expand Down
Loading