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

chore(codex-security): sync public projection

mldangelo-oai49c218ce0e28
diff --git a/README.md b/README.md
index 441af3c65..0263f7980 100644
--- a/README.md
+++ b/README.md
@@ -36,7 +36,9 @@ file-based Codex sign-in. If Codex stores credentials in the system keyring,
run `npx codex-security login` once before scanning.
An environment API key takes precedence over a stored sign-in. Unset both
-`OPENAI_API_KEY` and `CODEX_API_KEY` to use your ChatGPT sign-in.
+`OPENAI_API_KEY` and `CODEX_API_KEY` to use your ChatGPT sign-in. The login
+status command reports the effective credential source without printing its
+value, including when no stored sign-in exists.
Scan a subset of a repository or write machine-readable results:
diff --git a/sdk/typescript/README.md b/sdk/typescript/README.md
index 0c0002efd..c5a668b57 100644
--- a/sdk/typescript/README.md
+++ b/sdk/typescript/README.md
@@ -48,7 +48,10 @@ sign-in. If Codex stores credentials in the system keyring, run
`npx codex-security login` once before scanning.
An environment API key takes precedence over a stored sign-in. Unset both
-`OPENAI_API_KEY` and `CODEX_API_KEY` to use your ChatGPT sign-in.
+`OPENAI_API_KEY` and `CODEX_API_KEY` to use your ChatGPT sign-in. When an
+environment key is configured, `codex-security login status` identifies the
+effective credential source without printing its value, including when no
+stored sign-in exists.
## CLI
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index d49103a6e..e08b047ac 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -126,6 +126,17 @@ export interface ScanOptions {
signal?: AbortSignal;
}
+export type ScanAuthentication =
+ | {
+ method: "api_key";
+ source: "OPENAI_API_KEY" | "CODEX_API_KEY";
+ verified: false;
+ }
+ | {
+ method: "stored_credentials";
+ verified: false;
+ };
+
type ScanObserverName =
| "onOutputArchived"
| "onOutputDirReady"
@@ -1178,6 +1189,15 @@ async function collectResult(
});
}
+export function scanAuthentication(
+ environment: ProcessEnvironment,
+): ScanAuthentication {
+ const key = environmentApiKeyEntry(environment);
+ return key === null
+ ? { method: "stored_credentials", verified: false }
+ : { method: "api_key", source: key.source, verified: false };
+}
+
function notifyObserver<Arguments extends unknown[]>(
observerName: ScanObserverName,
observer: ((...args: Arguments) => void) | undefined,
@@ -1193,12 +1213,19 @@ function notifyObserver<Arguments extends unknown[]>(
}
function environmentApiKey(environment: ProcessEnvironment): string | null {
- for (const requested of ["OPENAI_API_KEY", "CODEX_API_KEY"]) {
+ return environmentApiKeyEntry(environment)?.value ?? null;
+}
+
+function environmentApiKeyEntry(environment: ProcessEnvironment): {
+ source: "OPENAI_API_KEY" | "CODEX_API_KEY";
+ value: string;
+} | null {
+ for (const requested of ["OPENAI_API_KEY", "CODEX_API_KEY"] as const) {
const canonical = environment[requested]?.trim();
- if (canonical) return canonical;
+ if (canonical) return { source: requested, value: canonical };
for (const [name, value] of Object.entries(environment)) {
if (name.toUpperCase() === requested && value?.trim())
- return value.trim();
+ return { source: requested, value: value.trim() };
}
}
return null;
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 51d643f78..3c9914c46 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -26,7 +26,12 @@ import { pipeline } from "node:stream/promises";
import { pathToFileURL } from "node:url";
import { Cli, z } from "incur";
import { parse as parseToml } from "smol-toml";
-import { CodexSecurity, type ScanOptions, type ScanPreflight } from "./api.js";
+import {
+ CodexSecurity,
+ scanAuthentication,
+ type ScanOptions,
+ type ScanPreflight,
+} from "./api.js";
import {
createBulkScanDiscoveryDependencies,
runBulkScanWizard,
@@ -145,6 +150,7 @@ interface CliDependencies {
createSecurity(
config: CodexSecurityConfig,
): Pick<CodexSecurity, "run" | "preflight" | "close">;
+ environment: NodeJS.ProcessEnv;
currentDirectory(): string;
now(): number;
setInterval(callback: () => void, milliseconds: number): NodeJS.Timeout;
@@ -164,6 +170,7 @@ interface CliDependencies {
const DEFAULT_DEPENDENCIES: CliDependencies = {
createSecurity: (config) => new CodexSecurity(config),
+ environment: process.env,
currentDirectory: cwd,
now: Date.now,
setInterval: (callback, milliseconds) => setInterval(callback, milliseconds),
@@ -896,6 +903,21 @@ export async function main(
"-c",
'cli_auth_credentials_store="file"',
]);
+ if (args.action === "status") {
+ const authentication = scanAuthentication(dependencies.environment);
+ if (
+ authentication.method === "api_key" &&
+ (exitCode === 0 || exitCode === 1)
+ ) {
+ exitCode = 0;
+ errorOutput.write(
+ `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",
+ );
+ }
+ }
},
})
.command("logout", {
diff --git a/sdk/typescript/src/index.ts b/sdk/typescript/src/index.ts
index 14d75e259..e83a6a281 100644
--- a/sdk/typescript/src/index.ts
+++ b/sdk/typescript/src/index.ts
@@ -1,6 +1,7 @@
export { CodexSecurity } from "./api.js";
export type {
CodexSecurityMetadata,
+ ScanAuthentication,
ScanOptions,
ScanPreflight,
} from "./api.js";
diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.ts
index 4f9b13234..da9f5af2e 100644
--- a/sdk/typescript/tests-ts/cli.test.ts
+++ b/sdk/typescript/tests-ts/cli.test.ts
@@ -297,6 +297,7 @@ function dependencies(
onWorkbench?: (args: readonly string[]) => JsonObject | Promise<JsonObject>;
currentDirectory?: string;
preflight?: ScanPreflight;
+ environment?: NodeJS.ProcessEnv;
signals?: FakeSignals;
result?: ScanResult;
workerStatuses?: import("../src/index.js").ScanWorkerStatus[];
@@ -335,6 +336,7 @@ function dependencies(
options.onConfig?.(config);
return security;
},
+ environment: options.environment ?? {},
currentDirectory: () => options.currentDirectory ?? "/current/repository",
now: () => 0,
setInterval: () => ({}) as NodeJS.Timeout,
@@ -1124,6 +1126,97 @@ describe("CLI", () => {
}
});
+ test("explains when an environment API key overrides the stored login", async () => {
+ for (const [environment, expectedSource] 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) {
+ const stdout = capture();
+ const stderr = capture();
+ expect(
+ await main(
+ ["login", "status"],
+ stdout.stream,
+ stderr.stream,
+ dependencies({ environment }),
+ ),
+ ).toBe(0);
+ expect(stderr.text()).toContain(
+ `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.",
+ );
+ expect(stderr.text()).not.toContain("SYNTHETIC_SECRET");
+ }
+ });
+
+ test("keeps stored-login status unchanged when no environment key is set", async () => {
+ const stdout = capture();
+ const stderr = capture();
+ expect(
+ await main(
+ ["login", "status"],
+ stdout.stream,
+ stderr.stream,
+ dependencies({ environment: { OPENAI_API_KEY: " " } }),
+ ),
+ ).toBe(0);
+ expect(stderr.text()).toBe("");
+ });
+
+ test("reports effective environment credentials without a stored sign-in", async () => {
+ const stdout = capture();
+ const stderr = capture();
+ const environment: NodeJS.ProcessEnv = {
+ OPENAI_API_KEY: "synthetic-primary-key",
+ CODEX_API_KEY: "synthetic-secondary-key",
+ };
+ expect(
+ await main(
+ ["login", "status"],
+ stdout.stream,
+ stderr.stream,
+ dependencies({ environment, onCodex: () => 1 }),
+ ),
+ ).toBe(0);
+ expect(stderr.text()).toContain("API key from OPENAI_API_KEY");
+ expect(stderr.text()).not.toContain("synthetic");
+
+ delete environment["OPENAI_API_KEY"];
+ const rotated = capture();
+ expect(
+ await main(
+ ["login", "status"],
+ capture().stream,
+ rotated.stream,
+ dependencies({ environment, onCodex: () => 1 }),
+ ),
+ ).toBe(0);
+ expect(rotated.text()).toContain("API key from CODEX_API_KEY");
+
+ expect(
+ await main(
+ ["login", "status"],
+ capture().stream,
+ capture().stream,
+ dependencies({ environment: {}, onCodex: () => 1 }),
+ ),
+ ).toBe(1);
+
+ expect(
+ await main(
+ ["login", "status"],
+ capture().stream,
+ capture().stream,
+ dependencies({
+ environment: { OPENAI_API_KEY: "synthetic-key" },
+ onCodex: () => 17,
+ }),
+ ),
+ ).toBe(17);
+ });
+
test("keeps delegated credentials in the configured Codex home", async () => {
const root = await mkdtemp(join(tmpdir(), "codex-security-login-home-"));
const repository = join(root, "repository");