OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix: preserve helper values that start with a dash (#1809)
Michael D'Angelo2cdb120aaa49
diff --git a/plugins/codex-security/mcp-app/src/artifact-storage.ts b/plugins/codex-security/mcp-app/src/artifact-storage.tsindex 82d5ae2a1..012747d33 100644--- a/plugins/codex-security/mcp-app/src/artifact-storage.ts+++ b/plugins/codex-security/mcp-app/src/artifact-storage.ts@@ -15,6 +15,7 @@ import reservedArtifactPaths from "../../scripts/reserved_artifact_paths.json";import { requireArtifactRoot, type ArtifactContext } from "./artifact-io.js";import type { RunArtifactWorkbench } from "./artifact-context.js";import { handoffClaimTokenSchema } from "./server/handoff-tools.js";+import { workbenchArgs } from "./workbench-arguments.js";const locationShape = {scanId: z.string().uuid().optional(),@@ -46,11 +47,9 @@ export async function standaloneArtifactContext(scanRoot: string,storage: ArtifactLocation["storage"],): Promise<ArtifactContext> {- const target = await runWorkbench([- "inspect-target",- "--target-path",- targetPath,- ]);+ const target = await runWorkbench(+ workbenchArgs("inspect-target", { targetPath }),+ );if (typeof target.targetPath !== "string")throw new Error("Missing artifact target.");const repoRoot = await fs.realpath(target.targetPath);@@ -220,27 +219,19 @@ export async function saveCodexSecurityArtifact(const destination = join(selected.root, ...parts);if (input.storage === "persistent" && context.scanId) {await runWorkbench(- [- "save-scan-artifact",- "--scan-id",- context.scanId,- "--artifact-path",- parts.join("/"),- ...(context.handoffClaimToken- ? ["--claim-token", context.handoffClaimToken]- : []),- ],+ workbenchArgs("save-scan-artifact", {+ scanId: context.scanId,+ artifactPath: parts.join("/"),+ claimToken: context.handoffClaimToken || undefined,+ }),bytes,);} else {await runWorkbench(- [- "save-artifact",- "--artifact-root",- selected.root,- "--artifact-path",- parts.join("/"),- ],+ workbenchArgs("save-artifact", {+ artifactRoot: selected.root,+ artifactPath: parts.join("/"),+ }),bytes,);}@@ -276,13 +267,12 @@ async function readArtifactBytes(parts: string[],runWorkbench: RunArtifactWorkbench,): Promise<Buffer> {- const result = await runWorkbench([- "read-artifact",- "--artifact-root",- context.root,- "--artifact-path",- parts.join("/"),- ]);+ const result = await runWorkbench(+ workbenchArgs("read-artifact", {+ artifactRoot: context.root,+ artifactPath: parts.join("/"),+ }),+ );if (typeof result.content !== "string")throw new Error("Missing artifact content.");return Buffer.from(result.content, "base64");diff --git a/plugins/codex-security/mcp-app/src/deep-scan/engine.ts b/plugins/codex-security/mcp-app/src/deep-scan/engine.tsindex fed55951a..9f619af1c 100644--- a/plugins/codex-security/mcp-app/src/deep-scan/engine.ts+++ b/plugins/codex-security/mcp-app/src/deep-scan/engine.ts@@ -1,5 +1,6 @@import { createScanArtifactContext } from "../artifact-context.js";import { recordCodexSecurityScanDraftViaWorkbench } from "../artifact-scan-draft.js";+import { workbenchArgs } from "../workbench-arguments.js";import { CodexSdkWorkerExecutor } from "./executor.js";import type { DeepWorkerParentSandbox } from "./parent-sandbox.js";import type { DeepScanParentTurn } from "./permission-profile-preflight.js";@@ -75,22 +76,14 @@ export function startDeepScanEngine(options: {);},onStopped: async (stopped) => {- await runWorkbench([- "preserve-scan-results",- "--scan-id",- stopped.scanId,- "--thread-id",- threadId,- ...(handoffClaimToken === undefined- ? []- : ["--claim-token", handoffClaimToken]),- ...(stopped.coordinatorGeneration === undefined- ? []- : [- "--coordinator-generation",- String(stopped.coordinatorGeneration),- ]),- ]);+ await runWorkbench(+ workbenchArgs("preserve-scan-results", {+ scanId: stopped.scanId,+ threadId,+ claimToken: handoffClaimToken,+ coordinatorGeneration: stopped.coordinatorGeneration,+ }),+ );},},});diff --git a/plugins/codex-security/mcp-app/src/workbench-arguments.ts b/plugins/codex-security/mcp-app/src/workbench-arguments.tsindex d4e14276c..730f08391 100644--- a/plugins/codex-security/mcp-app/src/workbench-arguments.ts+++ b/plugins/codex-security/mcp-app/src/workbench-arguments.ts@@ -12,8 +12,12 @@ export function workbenchArgs(if (value === undefined || value === false) continue;const flag = `--${name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}`;(Array.isArray(value) ? value : [value]).forEach((item) => {- args.push(flag);- if (item !== true) args.push(String(item));+ if (item === true) args.push(flag);+ else {+ const scalar = String(item);+ if (scalar.startsWith("-")) args.push(`${flag}=${scalar}`);+ else args.push(flag, scalar);+ }});}return args;diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.ts b/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.tsindex 7e709f3e5..376425fac 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.ts@@ -162,6 +162,40 @@ try {await assert.rejects(fs.stat(scanRoot), { code: "ENOENT" });});+ await test("option-shaped temporary artifact names survive save, read and import", async () => {+ const context = await standaloneArtifactContext(+ repository,+ workbench,+ true,+ path.join(fixture, "literal-name-scans"),+ "persistent",+ );+ for (const name of ["--help", "-evidence=λ.txt"]) {+ const saved = await saveCodexSecurityArtifact(+ context,+ { storage: "temporary", path: name, content: "literal evidence\n" },+ workbench,+ );+ temporaryDirectories.push(saved.directory);+ const read = await readCodexSecurityArtifact(+ context,+ { storage: "temporary", path: name, encoding: "utf8" },+ workbench,+ );+ assert.equal(read.content, "literal evidence\n");+ const retained = await saveCodexSecurityArtifact(+ context,+ {+ storage: "persistent",+ path: `artifacts/${name}`,+ sourcePath: saved.path,+ },+ workbench,+ );+ assert.equal(await fs.readFile(retained.path, "utf8"), read.content);+ }+ });+await test("standalone persistent storage allows sibling-prefix directories", async () => {const scanRoot = repository + "-scans";const context = await standaloneArtifactContext(diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_finite_publication.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_finite_publication.tsindex 3ae3e338a..b296f26d0 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_finite_publication.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_finite_publication.ts@@ -1,4 +1,5 @@import assert from "node:assert/strict";+import { randomUUID } from "node:crypto";import { mkdir, readFile, writeFile } from "node:fs/promises";import { dirname, join } from "node:path";import { after, test } from "node:test";@@ -7,6 +8,7 @@ import type { CodexWorkerRequest } from "../src/deep-scan/types.js";import { importSource } from "./import-module.ts";import {DeepScanCoordinatorRegistry,+ FakeExecutor,FakeStore,fixtureRun,temporaryDirectories,@@ -27,6 +29,50 @@ const { recordCodexSecurityDeepReduction } = await importSource();after(() => temporaryDirectories.cleanup());+test("Deep engine preserves stopped results for an option-shaped thread ID", async () => {+ const fixture = await fixtureRun();+ const store = new FakeStore(fixture.run);+ store.run.coordinatorGeneration = 7;+ const handoffClaimToken = randomUUID();+ const registry = new DeepScanCoordinatorRegistry();+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });+ const start = registry.start.bind(registry);+ registry.start = (options: CoordinatorOptions) =>+ start({ ...options, executor });+ const calls: string[][] = [];+ const { coordinator } = await startDeepScanEngine({+ run: store.run,+ store,+ registry,+ pluginRoot: fixture.pluginRoot,+ pythonCommand: "fixture-python",+ parentSandbox: { filesystemDenies: [] },+ threadId: "--fixture-owner",+ handoffClaimToken,+ runWorkbench: async (args: string[]) => {+ calls.push(args);+ return {};+ },+ });+ await executor.discoveryStarted.promise;+ store.run.status = "canceled";+ coordinator.cancel("fixture canceled");+ const terminal = await coordinator.wait(undefined, 5_000);+ assert.equal(terminal?.status, "canceled");+ assert.deepEqual(calls, [+ [+ "preserve-scan-results",+ "--scan-id",+ store.run.scanId,+ "--thread-id=--fixture-owner",+ "--claim-token",+ handoffClaimToken,+ "--coordinator-generation",+ String(store.run.coordinatorGeneration),+ ],+ ]);+});+for (const route of ["mcp", "file"] as const) {test(`Deep engine publishes historical metadata inherited by a ${route} reducer`, async () => {const fixture = await fixtureRun({ maxDiscoveryRuns: 1 });diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts b/plugins/codex-security/mcp-app/tests/test_workbench_process.tsindex d5c309ff9..494c93bcd 100644--- a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts+++ b/plugins/codex-security/mcp-app/tests/test_workbench_process.ts@@ -7,6 +7,7 @@ import { execFile } from "node:child_process";import { promisify } from "node:util";import { temporaryDirectory } from "./support/temporary-directories.ts";import { loadWorkbenchProcess } from "./support/workbench-process.ts";+import { importSource } from "./import-module.ts";import { PassThrough } from "node:stream";import type { ExecFileOptionsWithStringEncoding } from "node:child_process";@@ -114,6 +115,9 @@ try {}const { executeWorkbench } = await loadWorkbenchProcess();+const { workbenchArgs } = await importSource(+ path.join(import.meta.dirname, "../src/workbench-arguments.ts"),+);async function userSitePython(): Promise<string> {// A caller's virtualenv may intentionally disable user-site imports.const { stdout } = await promisify(execFile)(@@ -135,6 +139,47 @@ async function fixtureWorkbench(root: string, source: string) {return executeWorkbench as WorkbenchModule["executeWorkbench"];}+await test("workbench scalar arguments reach the Python parser literally", async () => {+ const root = await temporaryDirectory("workbench-literal-arguments-");+ try {+ const scripts = path.resolve(import.meta.dirname, "../../scripts");+ const executeParser = await fixtureWorkbench(+ root,+ `import json, sys+sys.path.insert(0, ${JSON.stringify(scripts)})+from workbench_cli import parse_args+def main():+ print(json.dumps(vars(parse_args("argument transport"))))+`,+ );+ const workerIds = [+ "ordinary",+ "--help",+ "-",+ "",+ "λ 日本語😀\n\0",+ "-key=value",+ "x".repeat(200_000),+ "--help",+ ];+ const result = await executeParser(+ process.env.PYTHON?.trim() || "python3",+ workbenchArgs("claim-deep-scan-dedup", {+ scanId: randomUUID(),+ workerId: randomUUID(),+ promptPath: "--prompt.md",+ artifactDir: "",+ inputWorkerId: workerIds,+ }),+ );+ assert.equal(result.prompt_path, "--prompt.md");+ assert.equal(result.artifact_dir, "");+ assert.deepEqual(result.input_worker_id, workerIds);+ } finally {+ await rm(root, { recursive: true, force: true });+ }+});+for (const variable of ["PYTHONPATH", "PYTHONUSERBASE"]) {await test(`isolated workbench ignores ${variable} startup hooks and preserves its environment`, async () => {const root = await temporaryDirectory("workbench-python-startup-");@@ -244,17 +289,13 @@ try {for (const stdin of [true, false]) {const result = await executeWorkbench(process.env.PYTHON?.trim() || "python3",- [- "create-workspace",- "--workspace-id",- randomUUID(),- "--target-path",- target,- `--target-title=${title}`,- stdin- ? "--user-context-stdin"- : `--user-context=${input.toString("utf8")}`,- ],+ workbenchArgs("create-workspace", {+ workspaceId: randomUUID(),+ targetPath: target,+ targetTitle: title,+ userContextStdin: stdin,+ userContext: stdin ? undefined : input.toString("utf8"),+ }),stdin ? input : undefined,);assert.equal(result.targetTitle, Buffer.from(title).toString("utf8"));diff --git a/sdk/typescript/src/workbench-arguments.ts b/sdk/typescript/src/workbench-arguments.tsindex 914be7cb6..deac5ab02 100644--- a/sdk/typescript/src/workbench-arguments.ts+++ b/sdk/typescript/src/workbench-arguments.ts@@ -13,8 +13,12 @@ export function workbenchArgs(if (value === undefined || value === false) continue;const flag = `--${name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}`;(Array.isArray(value) ? value : [value]).forEach((item) => {- args.push(flag);- if (item !== true) args.push(String(item));+ if (item === true) args.push(flag);+ else {+ const scalar = String(item);+ if (scalar.startsWith("-")) args.push(`${flag}=${scalar}`);+ else args.push(flag, scalar);+ }});}return args;diff --git a/sdk/typescript/tests-ts/workbench-arguments.test.ts b/sdk/typescript/tests-ts/workbench-arguments.test.tsindex ea4f31022..77fc64fcc 100644--- a/sdk/typescript/tests-ts/workbench-arguments.test.ts+++ b/sdk/typescript/tests-ts/workbench-arguments.test.ts@@ -9,6 +9,7 @@ test("workbench arguments preserve field order, values, and repeated flags", ()archiveExisting: false,parentScanId: undefined,offset: 0,+ negative: -1e30,name: "",query: "--query\nUTF-8 λ\0",inputWorkerId: ["worker-2", "worker-1", "worker-2"],@@ -21,10 +22,10 @@ test("workbench arguments preserve field order, values, and repeated flags", ()"--registration-json-stdin","--offset","0",+ "--negative=-1e+30","--name","",- "--query",- "--query\nUTF-8 λ\0",+ "--query=--query\nUTF-8 λ\0","--input-worker-id","worker-2","--input-worker-id",