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: honor the requested output format when rerunning a scan (#203)

Mario Hercules2a98fd1bcb98
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 915df1487..741f65f18 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -2333,7 +2333,14 @@ export async function main(
.describe("Print scan diagnostics to stderr."),
}),
output: z.record(z.string(), z.unknown()).optional(),
- async run({ args, error: incurError, options }) {
+ async run({ args, error: incurError, format, options }) {
+ if (format === "md") {
+ errorOutput.write(
+ "codex-security: Markdown output is not supported for scan results.\n",
+ );
+ exitCode = 2;
+ return;
+ }
const scanId = args.scanId ?? (await latestScans())?.[0]?.scanId;
if (scanId === undefined) return;
let scanArguments: ScanArguments;
@@ -2421,7 +2428,12 @@ export async function main(
exitCode,
});
}
- const outcome = await runScan(scanArguments, errorOutput, dependencies);
+ const outcome = await runScan(
+ scanArguments,
+ errorOutput,
+ dependencies,
+ format !== "json" && format !== "jsonl",
+ );
exitCode = outcome.exitCode;
if (outcome.error !== undefined) {
return incurError({
diff --git a/sdk/typescript/tests-ts/cli-authentication.test.ts b/sdk/typescript/tests-ts/cli-authentication.test.ts
index ade062b24..dc74503c5 100644
--- a/sdk/typescript/tests-ts/cli-authentication.test.ts
+++ b/sdk/typescript/tests-ts/cli-authentication.test.ts
@@ -17,7 +17,11 @@ import { fileURLToPath, pathToFileURL } from "node:url";
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { parse as parseToml } from "smol-toml";
import { main, runCodexSkillCommand } from "../src/cli.js";
-import { CodexSecurityError, type ScanOptions } from "../src/index.js";
+import {
+ CodexSecurityError,
+ type JsonObject,
+ type ScanOptions,
+} from "../src/index.js";
import {
codexSecurityCredentialAllowsAmbientImport,
prepareCodexSecurityCredentialHome,
@@ -465,11 +469,8 @@ describe("CLI authentication", () => {
for (const [argv, selection] of [
[["scan"], "chatgpt"],
[["scan"], "api-key"],
- [["scans", "rerun", "scan-original", "--verbose", "--json"], "chatgpt"],
- [
- ["scans", "rerun", "scan-original", "--verbose", "--format", "jsonl"],
- "chatgpt",
- ],
+ [["scans", "rerun", "scan-original", "--verbose"], "chatgpt"],
+ [["scans", "rerun", "scan-original", "--verbose"], "api-key"],
] as const) {
const stderr = capture(true);
let selected: ScanOptions["auth"];
@@ -585,6 +586,32 @@ describe("CLI authentication", () => {
stored: true,
key: true,
},
+ {
+ argv: ["scans", "rerun", "scan-original", "--verbose", "--json"],
+ terminal: true,
+ stored: true,
+ key: true,
+ },
+ {
+ argv: ["scans", "rerun", "--format", "jsonl"],
+ terminal: true,
+ stored: true,
+ key: true,
+ },
+ {
+ argv: ["scans", "rerun", "scan-original", "--json"],
+ terminal: true,
+ stored: true,
+ key: true,
+ recipeAuth: "chatgpt" as const,
+ },
+ {
+ argv: ["scans", "rerun", "scan-original", "--json"],
+ terminal: true,
+ stored: true,
+ key: true,
+ recipeAuth: "api-key" as const,
+ },
{
argv: ["scan", "--dry-run"],
terminal: true,
@@ -596,12 +623,14 @@ describe("CLI authentication", () => {
terminal: true,
stored: true,
key: true,
+ expectedAuth: "chatgpt" as const,
},
{
argv: ["scan", "--auth", "api-key"],
terminal: true,
stored: true,
key: true,
+ expectedAuth: "api-key" as const,
},
{ argv: ["scan"], terminal: false, stored: true, key: true },
{ argv: ["scan"], terminal: true, stored: false, key: true },
@@ -614,9 +643,11 @@ describe("CLI authentication", () => {
inputInteractive: false,
},
]) {
+ const stdout = capture();
const stderr = capture(scenario.terminal);
let selected: ScanOptions["auth"];
let prompts = 0;
+ let discoveries = 0;
const deps = dependencies({
environment: scenario.key
? { OPENAI_API_KEY: "synthetic-private-key" }
@@ -624,8 +655,25 @@ describe("CLI authentication", () => {
onTurn: (_repository, options) => {
selected = (options as ScanOptions).auth;
},
+ onWorkbench: (args): JsonObject =>
+ args[0] === "list-scans"
+ ? { scans: [{ scanId: "scan-original" }] }
+ : {
+ recipe: {
+ repository: "/original/repository",
+ target: { kind: "repository", paths: [] },
+ mode: "standard",
+ ...(scenario.recipeAuth === undefined
+ ? {}
+ : { auth: scenario.recipeAuth }),
+ config: {},
+ },
+ },
});
- deps.hasStoredChatGPTSignIn = async () => scenario.stored;
+ deps.hasStoredChatGPTSignIn = async () => {
+ discoveries += 1;
+ return scenario.stored;
+ };
deps.scanAuthenticationPrompt = {
isInteractive: () => scenario.inputInteractive !== false,
select: async <Value extends string>(
@@ -638,16 +686,17 @@ describe("CLI authentication", () => {
};
expect(
- await main(scenario.argv, capture().stream, stderr.stream, deps),
+ await main(scenario.argv, stdout.stream, stderr.stream, deps),
).toBe(0);
expect(prompts).toBe(0);
+ if (scenario.argv.includes("--json") || scenario.argv.includes("jsonl")) {
+ expect(discoveries).toBe(0);
+ expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
+ expect(stderr.text()).not.toMatch(/\x1b\[/u);
+ }
if (!scenario.argv.includes("--dry-run")) {
expect(selected).toBe(
- scenario.argv.includes("chatgpt")
- ? "chatgpt"
- : scenario.argv.includes("api-key")
- ? "api-key"
- : "auto",
+ scenario.recipeAuth ?? scenario.expectedAuth ?? "auto",
);
}
expect(stderr.text()).not.toContain("synthetic-private-key");
diff --git a/sdk/typescript/tests-ts/cli-workbench.test.ts b/sdk/typescript/tests-ts/cli-workbench.test.ts
index 5bb35fada..b2c25f6ca 100644
--- a/sdk/typescript/tests-ts/cli-workbench.test.ts
+++ b/sdk/typescript/tests-ts/cli-workbench.test.ts
@@ -1228,6 +1228,40 @@ describe("CLI workbench", () => {
}
});
+ test.each(["scan-original", undefined])(
+ "rejects Markdown rerun output before loading scan %p",
+ async (scanId) => {
+ const stdout = capture();
+ const stderr = capture();
+ let workbenchCalls = 0;
+
+ expect(
+ await main(
+ [
+ "scans",
+ "rerun",
+ ...(scanId === undefined ? [] : [scanId]),
+ "--format",
+ "md",
+ ],
+ stdout.stream,
+ stderr.stream,
+ dependencies({
+ onWorkbench: () => {
+ workbenchCalls += 1;
+ return {};
+ },
+ }),
+ ),
+ ).toBe(2);
+ expect(workbenchCalls).toBe(0);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toContain(
+ "Markdown output is not supported for scan results.",
+ );
+ },
+ );
+
test("reruns the latest completed scan by default", async () => {
let parentScanId: unknown;