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-oai47e018acf664
diff --git a/sdk/typescript/README.md b/sdk/typescript/README.md
index f8c3f9834..3cef4a33c 100644
--- a/sdk/typescript/README.md
+++ b/sdk/typescript/README.md
@@ -269,6 +269,11 @@ Python interpreter. The preflight result includes the selected authentication
method and, for an environment API key, its variable name. Authentication and
model access remain unverified until a real scan starts.
+Scan progress identifies the selected credential source before Codex starts.
+Interactive terminals also show how to retry with ChatGPT when an environment
+API key overrides the stored sign-in. Progress remains on stderr so JSON output
+stays machine readable.
+
## Documentation and security
- [CLI quickstart](https://developers.openai.com/codex/security/cli)
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index 49e8cabab..862045ca0 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -120,6 +120,7 @@ export interface ScanOptions {
failureSeverity?: SeverityLevel;
onOutputArchived?: (archiveDir: string) => void;
onOutputDirReady?: (scanDir: string) => void;
+ onAuthentication?: (authentication: ScanAuthentication) => void;
onScanStarted?: () => void;
onReconnect?: (attempt: number, maxAttempts: number) => void;
onWorkerStatus?: (status: ScanWorkerStatus) => void;
@@ -139,6 +140,7 @@ export type ScanAuthentication =
};
type ScanObserverName =
+ | "onAuthentication"
| "onOutputArchived"
| "onOutputDirReady"
| "onScanStarted"
@@ -353,6 +355,12 @@ export class CodexSecurity {
"OPENAI_API_KEY or CODEX_API_KEY for CI.",
);
}
+ notifyObserver(
+ "onAuthentication",
+ options.onAuthentication,
+ options.onObserverError,
+ scanAuthentication(this.#dependencies.environment),
+ );
const python = await (
this.#dependencies.resolvePluginPython ?? resolvePluginPython
)({
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index f51e32366..f54e9b115 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -1976,6 +1976,24 @@ async function runScan(
onOutputDirReady: (path) => {
scanDir = path;
},
+ onAuthentication: (authentication) => {
+ progress?.stopTimer();
+ if (authentication.method === "api_key") {
+ progress?.stage(
+ `Authentication: API key from ${authentication.source}.`,
+ );
+ if (errorOutput.isTTY === true) {
+ progress?.stage(
+ process.platform === "win32"
+ ? "To use a ChatGPT sign-in, unset OPENAI_API_KEY and CODEX_API_KEY, then retry the scan."
+ : "Retry with ChatGPT: env -u OPENAI_API_KEY -u CODEX_API_KEY codex-security scan ...",
+ );
+ }
+ } else {
+ progress?.stage("Authentication: stored Codex credentials.");
+ }
+ progress?.startTimer("Preparing scan");
+ },
onScanStarted: () => {
progress?.stopTimer();
progress?.startTimer(runningMessage());
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index 82a8396fe..a62c9fd3f 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -1024,6 +1024,48 @@ describe("CodexSecurity orchestration", () => {
}
});
+ test("isolates authentication observer failures from scan startup", async () => {
+ const root = await temporaryDirectory();
+ const repository = join(root, "repository");
+ const codexHome = join(root, "codex-home");
+ await mkdir(repository);
+ await mkdir(codexHome);
+ const observerErrors: Array<[ScanObserverName, string]> = [];
+ const client = new TestClient(
+ {},
+ {
+ environment: {},
+ prepareRuntime: async () => preparedRuntime(codexHome),
+ resolvePluginPython: async () => "/managed/python",
+ repositoryRevision: async () => null,
+ createCodex: () => ({
+ startThread: () => ({
+ id: null,
+ async runStreamed() {
+ throw new Error("scan did not start");
+ },
+ }),
+ }),
+ },
+ );
+
+ await expect(
+ client.run(repository, {
+ outputDir: join(root, "scan"),
+ onAuthentication: () => {
+ throw new Error("authentication observer exploded");
+ },
+ onObserverError: (observer, error) => {
+ observerErrors.push([observer, (error as Error).message]);
+ },
+ }),
+ ).rejects.toThrow("scan did not start");
+ expect(observerErrors).toEqual([
+ ["onAuthentication", "authentication observer exploded"],
+ ]);
+ await client.close();
+ });
+
test("previews an existing output archive without changing files", async () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
@@ -2437,6 +2479,7 @@ if (process.argv.slice(2).join(" ") !== "login --with-api-key") {
`,
);
let codexOptions: CodexOptions | null = null;
+ let selectedAuthentication: unknown;
let pythonEnvironment: Record<string, string | undefined> | undefined;
let pythonProtectedRoot: string | undefined;
const client = new TestClient(
@@ -2493,7 +2536,16 @@ if (process.argv.slice(2).join(" ") !== "login --with-api-key") {
},
);
- await client.run(repository);
+ await client.run(repository, {
+ onAuthentication: (authentication) => {
+ selectedAuthentication = authentication;
+ },
+ });
+ expect(selectedAuthentication).toEqual({
+ method: "api_key",
+ source: "OPENAI_API_KEY",
+ verified: false,
+ });
expect((codexOptions as CodexOptions | null)?.apiKey).toBeUndefined();
expect(
(codexOptions as CodexOptions | null)?.codexPathOverride,
diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.ts
index d9467977a..a2be0fa84 100644
--- a/sdk/typescript/tests-ts/cli.test.ts
+++ b/sdk/typescript/tests-ts/cli.test.ts
@@ -3121,6 +3121,70 @@ describe("CLI", () => {
expect(stderr.text()).toContain("Running scan");
});
+ test("reports selected scan credentials without contaminating JSON output", async () => {
+ const stdout = capture();
+ const stderr = capture(true);
+ const deps = dependencies();
+ deps.createSecurity = () => ({
+ run: async (_repository, options) => {
+ options?.onAuthentication?.({
+ method: "api_key",
+ source: "OPENAI_API_KEY",
+ verified: false,
+ });
+ options?.onScanStarted?.();
+ return fakeResult();
+ },
+ preflight: async () => fakePreflight(),
+ close: async () => {},
+ });
+
+ expect(
+ await main(["scan", "--json"], stdout.stream, stderr.stream, deps),
+ ).toBe(0);
+ expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
+ expect(stderr.text()).toContain(
+ "Authentication: API key from OPENAI_API_KEY.",
+ );
+ expect(stderr.text()).toContain(
+ process.platform === "win32"
+ ? "unset OPENAI_API_KEY and CODEX_API_KEY, then retry the scan"
+ : "env -u OPENAI_API_KEY -u CODEX_API_KEY codex-security scan ...",
+ );
+ });
+
+ test("reports stored and secondary-key scan authentication on stderr", async () => {
+ for (const [authentication, expected] of [
+ [
+ { method: "stored_credentials", verified: false },
+ "Authentication: stored Codex credentials.",
+ ],
+ [
+ { method: "api_key", source: "CODEX_API_KEY", verified: false },
+ "Authentication: API key from CODEX_API_KEY.",
+ ],
+ ] as const) {
+ const stdout = capture();
+ const stderr = capture();
+ const deps = dependencies();
+ deps.createSecurity = () => ({
+ run: async (_repository, options) => {
+ options?.onAuthentication?.(authentication);
+ return fakeResult();
+ },
+ preflight: async () => fakePreflight(),
+ close: async () => {},
+ });
+
+ expect(
+ await main(["scan", "--json"], stdout.stream, stderr.stream, deps),
+ ).toBe(0);
+ expect(stderr.text()).toContain(expected);
+ expect(stderr.text()).not.toContain("env -u");
+ expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
+ }
+ });
+
test("renders scan output with the Incur default format", async () => {
const stdout = capture();
const stderr = capture();