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(cli): report patch failures and changed files (#874)

ianw-oai4f5a304ea669
diff --git a/sdk/typescript/README.md b/sdk/typescript/README.md
index 6cd71dd83..bd86a0fc3 100644
--- a/sdk/typescript/README.md
+++ b/sdk/typescript/README.md
@@ -1627,11 +1627,31 @@ npx @openai/codex-security patch --scan SCAN_ID --assess-patch-risk --create-pr
npx @openai/codex-security patch --linear-issue SEC-123 --assess-patch-risk --create-pr
```
-`--scan latest` selects the current repository's latest scan. Saved-finding
-patch commands support `--json`; literal-text and file inputs don't. Change
+`--scan latest` selects the current repository's latest scan. Patch commands
+support `--json`, including literal-text and file inputs. Change
the model with `--codex 'model="gpt-5.6-sol"'` or effort with `--effort high`.
Each finding gets its own saved Codex desktop task.
+Before patching, the CLI runs a command with the task's sandbox policy. If the
+sandbox cannot start, the command exits with a nonzero status and reports
+`SANDBOX_UNAVAILABLE`. JSON errors include `ok: false`. A completed model response
+with no repository changes fails with `NO_PATCH_APPLIED`. Results report `applied`,
+`filesChanged`, and `files`; existing local changes do not count as patch changes.
+These fields report file changes, not proof that the security issue is fixed.
+Saved findings still require a verified result from the patch task.
+
+For a controlled container that provides its own isolation, explicitly opt in:
+
+```bash
+npx @openai/codex-security patch "Security issue" --external-sandbox --json
+```
+
+`--external-sandbox` defaults to false. It uses Codex's external-sandbox policy
+and prints a warning: Codex does not enforce filesystem or network isolation for
+the patch task. The container must enforce those boundaries. The CLI never
+falls back to this mode automatically. Optional patch-risk assessment still uses
+its read-only Codex sandbox.
+
`scan --patch` patches after a complete scan. `--patch-severity` defaults to
`low`; `high` selects high and critical findings. Use the interactive browser
to select findings and add patch instructions. Results include a `patches`
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index f9de5fd0a..9c00f32af 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -21,6 +21,8 @@ import {
mkdir,
mkdtemp,
readFile,
+ readdir,
+ readlink,
realpath,
rm,
writeFile,
@@ -1012,10 +1014,23 @@ interface SkillCommandOutput {
readonly prompt: string;
readonly threadSource: SkillThreadSource;
readonly sandbox?: "read-only" | "workspace-write";
+ readonly externalSandbox?: boolean;
readonly onEvent?: (event: Readonly<Record<string, unknown>>) => void;
};
}
+class PatchCommandError extends CodexSecurityError {
+ constructor(
+ readonly code: string,
+ message: string,
+ ) {
+ super(message);
+ }
+}
+
+const SANDBOX_UNAVAILABLE_MESSAGE =
+ "The patch sandbox could not start. Check the runner's sandbox permissions and retry. No patch was applied; 0 files changed.";
+
const findingPatchSchema = z.object({
occurrenceId: z.string(),
status: z.enum(["verified", "no_change", "blocked", "failed"]),
@@ -1035,6 +1050,7 @@ const findingVerificationSchema = z.object({
type FindingVerification = z.infer<typeof findingVerificationSchema>;
interface SkillRunOptions {
+ externalSandbox?: boolean;
readonly auth?: ScanAuthMode;
safetyIdentifier?: string;
directory?: string;
@@ -1567,6 +1583,7 @@ export async function runCodexSkillCommand(
threadSource: output.appServer.threadSource,
input: invocation.stdin!,
sandbox: output.appServer.sandbox,
+ externalSandbox: output.appServer.externalSandbox,
onEvent: output.appServer.onEvent,
},
),
@@ -1601,6 +1618,12 @@ export async function runCodexSkillCommand(
invocation.once(output === undefined ? "exit" : "close", complete);
});
let [status, events] = await Promise.all([invocationStatus, captured]);
+ if (events?.sandboxUnavailable && requestedSignal === null) {
+ throw new PatchCommandError(
+ "SANDBOX_UNAVAILABLE",
+ SANDBOX_UNAVAILABLE_MESSAGE,
+ );
+ }
if (status === 0 && output?.appServer !== undefined && events?.error) {
status = 1;
}
@@ -1753,6 +1776,8 @@ export async function main(
let renderedHistory: string | undefined;
let renderedPublication: string | undefined;
let renderedPolicy: string | undefined;
+ let renderedPatch: string | undefined;
+ let patchStructuredError = false;
const runImport = (options: ImportScanOptions) =>
runScanImport(options, errorOutput, dependencies);
const history = async (
@@ -4856,6 +4881,12 @@ export async function main(
.default("auto")
.describe("Credential source: auto, chatgpt, or api-key."),
effort: effortOption(),
+ externalSandbox: z
+ .boolean()
+ .default(false)
+ .describe(
+ "Use the container's isolation instead of the Codex sandbox (default: false).",
+ ),
scan: optionValue("--scan")
.optional()
.describe("Patch open findings from a saved scan."),
@@ -4889,7 +4920,15 @@ export async function main(
),
}),
output: z.record(z.string(), z.unknown()).optional(),
- async run({ format, options }) {
+ async run({ format, options, error: commandError }) {
+ const jsonOutput = format === "json" || format === "jsonl";
+ const fullOutput = argv.includes("--full-output");
+ const structuredOutput = jsonOutput || fullOutput;
+ let patchResult: Record<string, unknown> = {
+ applied: false,
+ filesChanged: 0,
+ files: [],
+ };
try {
const linear =
options.linearIssue.length > 0 || !!options.linearProject;
@@ -4900,6 +4939,7 @@ export async function main(
options.severity !== undefined ||
options.createPr ||
options.assessPatchRisk ||
+ options.externalSandbox ||
linear ||
options.linearFilter !== undefined ||
options.linearApiKey !== undefined ||
@@ -4945,6 +4985,11 @@ export async function main(
"Saved findings cannot be combined with Linear issues or projects.",
);
}
+ if (options.externalSandbox) {
+ errorOutput.write(
+ "WARNING: --external-sandbox disables Codex sandbox enforcement for patching. The container must provide isolation.\n",
+ );
+ }
if (savedFindings) {
const selected = await selectSavedFindings(
positionals,
@@ -4955,23 +5000,48 @@ export async function main(
const patchRiskBase = options.assessPatchRisk
? await snapshotPatchTree(selected.repository, dependencies)
: undefined;
+ const patchBase =
+ patchRiskBase ??
+ (await snapshotPatchState(selected.repository, dependencies));
const patches = await runFindingPatches(
selected,
options.codex,
options.effort,
errorOutput,
dependencies,
- { auth: options.auth },
+ { auth: options.auth, externalSandbox: options.externalSandbox },
);
exitCode = patchExitCode(patches);
+ const files = await changedPatchFiles(
+ selected.repository,
+ patchBase,
+ dependencies,
+ );
+ patchResult = {
+ scanId: selected.scanId,
+ repository: selected.repository,
+ patches,
+ applied: files.length > 0,
+ filesChanged: files.length,
+ files,
+ };
+ if (exitCode !== 0 || files.length === 0) {
+ throw new PatchCommandError(
+ exitCode === 0 ? "NO_PATCH_APPLIED" : "PATCH_FAILED",
+ `Patch did not complete successfully; ${files.length} files changed. Review the patch results before retrying.`,
+ );
+ }
+ errorOutput.write(
+ `Patch applied. Files changed: ${files.length}.\n`,
+ );
let patchRisk: PatchRiskAssessment | undefined;
- if (options.assessPatchRisk && exitCode === 0) {
+ if (patchRiskBase !== undefined) {
const files = verifiedPatchFiles(selected, patches);
if (files.length > 0) {
patchRisk = await runPatchRiskAssessment(
{
repository: selected.repository,
- base: patchRiskBase!,
+ base: patchRiskBase,
files,
codexOverrides: options.codex,
effort: options.effort,
@@ -4982,22 +5052,19 @@ export async function main(
);
}
}
- const pullRequest =
- options.createPr && exitCode === 0
- ? await createPatchPullRequest(
- selected.repository,
- selected.scanId,
- verifiedPatchFiles(selected, patches),
- errorOutput,
- dependencies,
- patchRisk?.summary,
- )
- : undefined;
- if (format === "json" || format === "jsonl") {
+ const pullRequest = options.createPr
+ ? await createPatchPullRequest(
+ selected.repository,
+ selected.scanId,
+ verifiedPatchFiles(selected, patches),
+ errorOutput,
+ dependencies,
+ patchRisk?.summary,
+ )
+ : undefined;
+ if (structuredOutput) {
return {
- scanId: selected.scanId,
- repository: selected.repository,
- patches,
+ ...patchResult,
...(patchRisk === undefined
? {}
: { patchRisk: { report: patchRisk.report } }),
@@ -5016,12 +5083,6 @@ export async function main(
"--severity requires a saved finding identifier or --scan.",
);
}
- if (format === "json" || format === "jsonl") {
- throw new CodexSecurityError(
- "JSON patch output requires a saved finding identifier or --scan.",
- );
- }
-
const imports = linear
? await importLinearIssues({
issues: options.linearIssue,
@@ -5044,66 +5105,119 @@ export async function main(
),
);
const repository = dependencies.currentDirectory();
- const patchBase =
+ const patchGitBase =
options.assessPatchRisk || options.createPr
? await snapshotPatchTree(repository, dependencies)
: undefined;
+ const patchBase =
+ patchGitBase ??
+ (await snapshotPatchState(repository, dependencies));
if (options.createPr) {
await requireCleanPatchPullRequestBase(
repository,
- patchBase!,
+ patchGitBase!,
dependencies,
);
}
+ let report = "";
exitCode = await runSkill(
"fix-finding",
[...positionals, ...imports],
options.codex,
options.effort,
- output,
+ {
+ write: (value) => {
+ report += value.toString();
+ return true;
+ },
+ },
errorOutput,
dependencies,
- { environment, auth: options.auth },
+ {
+ environment,
+ auth: options.auth,
+ externalSandbox: options.externalSandbox,
+ },
+ );
+ if (!jsonOutput) output.write(report);
+ const files = await changedPatchFiles(
+ repository,
+ patchBase,
+ dependencies,
);
- if (patchBase !== undefined && exitCode === 0) {
- const files = await changedPatchFiles(
- repository,
- patchBase,
- dependencies,
+ patchResult = {
+ repository,
+ applied: files.length > 0,
+ filesChanged: files.length,
+ files,
+ };
+ if (exitCode !== 0) {
+ throw new PatchCommandError(
+ "PATCH_FAILED",
+ `Patch command exited with status ${exitCode}.`,
);
- const patchRisk = options.assessPatchRisk
- ? await runPatchRiskAssessment(
- {
- repository,
- environment,
- base: patchBase,
- files,
- codexOverrides: options.codex,
- effort: options.effort,
- auth: options.auth,
- },
- errorOutput,
- dependencies,
- )
- : undefined;
- if (options.createPr) {
- const identifier = directPatchIdentifier(positionals, imports);
- await createPatchPullRequest(
- repository,
- identifier ?? directPatchDigest(positionals, imports),
- files,
+ }
+ if (files.length === 0) {
+ throw new PatchCommandError(
+ "NO_PATCH_APPLIED",
+ "No patch was applied; 0 files changed.",
+ );
+ }
+ errorOutput.write(`Patch applied. Files changed: ${files.length}.\n`);
+ const patchRisk = options.assessPatchRisk
+ ? await runPatchRiskAssessment(
+ {
+ repository,
+ environment,
+ base: patchGitBase!,
+ files,
+ codexOverrides: options.codex,
+ effort: options.effort,
+ auth: options.auth,
+ },
errorOutput,
dependencies,
- patchRisk?.summary,
- identifier === undefined
- ? "Applies a security fix generated from supplied issue data."
- : `Applies a security fix generated for ${identifier}.`,
- );
- }
+ )
+ : undefined;
+ if (options.createPr) {
+ const identifier = directPatchIdentifier(positionals, imports);
+ await createPatchPullRequest(
+ repository,
+ identifier ?? directPatchDigest(positionals, imports),
+ files,
+ errorOutput,
+ dependencies,
+ patchRisk?.summary,
+ identifier === undefined
+ ? "Applies a security fix generated from supplied issue data."
+ : `Applies a security fix generated for ${identifier}.`,
+ );
}
+ if (structuredOutput)
+ return {
+ ...patchResult,
+ ...(jsonOutput ? { report } : {}),
+ };
} catch (error) {
- exitCode = 2;
- errorOutput.write(`codex-security: ${safeErrorMessage(error)}\n`);
+ if (exitCode === 0) exitCode = 2;
+ const message = safeErrorMessage(error);
+ errorOutput.write(`codex-security: ${message}\n`);
+ if (!structuredOutput) return;
+ patchStructuredError = true;
+ const failure = {
+ code:
+ error instanceof PatchCommandError ? error.code : "PATCH_FAILED",
+ message,
+ };
+ if (!fullOutput) {
+ renderedPatch =
+ JSON.stringify({
+ ok: false,
+ ...patchResult,
+ error: failure,
+ }) + "\n";
+ }
+ return commandError({ ...failure, exitCode });
}
},
})
@@ -5558,7 +5672,7 @@ export async function main(
}
if (notice !== undefined) errorOutput.write(formatUpdateNotice(notice));
if (frameworkExit !== undefined) {
- if (policyFullOutput) {
+ if (policyFullOutput || patchStructuredError) {
if (exitCode === 0) exitCode = 2;
} else {
if (exitCode !== 0) return exitCode;
@@ -5573,6 +5687,7 @@ export async function main(
await writeCliOutput(
output,
renderedPolicy ??
+ renderedPatch ??
renderedPublication ??
renderedHistory ??
frameworkOutput,
@@ -6599,9 +6714,15 @@ async function requireCleanPatchPullRequestBase(
async function changedPatchFiles(
repository: string,
- base: string,
+ base: string | Map<string, string>,
dependencies: CliDependencies,
): Promise<string[]> {
+ if (base instanceof Map) {
+ const head = await snapshotPatchDirectory(repository);
+ return [...new Set([...base.keys(), ...head.keys()])]
+ .filter((path) => base.get(path) !== head.get(path))
+ .sort();
+ }
const head = await snapshotPatchTree(repository, dependencies);
const output = await dependencies.runRepositoryCommand(
"git",
@@ -6612,6 +6733,60 @@ async function changedPatchFiles(
return output.split("\0").filter(Boolean);
}
+async function snapshotPatchState(
+ repository: string,
+ dependencies: CliDependencies,
+): Promise<string | Map<string, string>> {
+ try {
+ await dependencies.runRepositoryCommand(
+ "git",
+ ["rev-parse", "--show-toplevel"],
+ repository,
+ { environment: { LC_ALL: "C" } },
+ );
+ } catch (error) {
+ const message = errorMessage(error);
+ if (
+ !message.includes("not a git repository") &&
+ message !== "git is not available on a trusted PATH."
+ )
+ throw error;
+ return snapshotPatchDirectory(repository);
+ }
+ return snapshotPatchTree(repository, dependencies);
+}
+
+// Literal patch inputs also work in directories without Git metadata.
+async function snapshotPatchDirectory(
+ repository: string,
+): Promise<Map<string, string>> {
+ const files = new Map<string, string>();
+ const visit = async (directory: string): Promise<void> => {
+ for (const entry of await readdir(join(repository, directory), {
+ withFileTypes: true,
+ })) {
+ if (entry.name === ".git") continue;
+ const path = directory ? `${directory}/${entry.name}` : entry.name;
+ const absolute = join(repository, path);
+ if (entry.isDirectory()) {
+ await visit(path);
+ } else if (entry.isSymbolicLink()) {
+ files.set(path, `link:${await readlink(absolute)}`);
+ } else if (entry.isFile()) {
+ const hash = createHash("sha256");
+ for await (const chunk of createReadStream(absolute))
+ hash.update(chunk);
+ files.set(
+ path,
+ `${(await lstat(absolute)).mode & 0o111}:${hash.digest("hex")}`,
+ );
+ }
+ }
+ };
+ await visit("");
+ return files;
+}
+
function safePatchText(value: string): string {
return stripVTControlCharacters(safeErrorMessage(value)).replaceAll(
/[\u0000-\u001F\u007F-\u009F\u2028\u2029]/gu,
@@ -6802,6 +6977,7 @@ async function runFindingPatches(
);
const patches: FindingPatch[] = [];
for (const finding of selected.findings) {
+ const base = await snapshotPatchState(selected.repository, dependencies);
let response = "";
const stdout: Writable = {
write(value: string | Uint8Array): boolean {
@@ -6831,6 +7007,12 @@ async function runFindingPatches(
throw new CodexSecurityError("Patch operation was interrupted.");
}
+ const changedFiles = await changedPatchFiles(
+ selected.repository,
+ base,
+ dependencies,
+ );
+
const failed = (reason: string, files: string[] = []): FindingPatch => ({
occurrenceId: finding.occurrenceId,
status: "failed",
@@ -6866,6 +7048,11 @@ async function runFindingPatches(
"Patch verification was not reported.",
parsed.data.files,
);
+ } else if (
+ parsed.data.status === "verified" &&
+ changedFiles.length === 0
+ ) {
+ patch = failed("No patch was applied; 0 files changed.");
} else {
patch = parsed.data;
}
@@ -7127,6 +7314,7 @@ async function runSkill(
directory,
prompt,
threadSource,
+ ...(options.externalSandbox ? { externalSandbox: true } : {}),
...(verify || assess ? { sandbox: "read-only" as const } : {}),
...(options.onEvent === undefined
? {}
@@ -7151,6 +7339,7 @@ export async function readSkillCommandOutput(
readonly threadSource: SkillThreadSource;
readonly input: NodeJS.WritableStream;
readonly sandbox?: "read-only" | "workspace-write";
+ readonly externalSandbox?: boolean;
readonly onEvent?: (event: Readonly<Record<string, unknown>>) => void;
},
): Promise<{
@@ -7158,6 +7347,7 @@ export async function readSkillCommandOutput(
error?: string;
malformed: boolean;
completed?: boolean;
+ sandboxUnavailable?: boolean;
}> {
let message: string | undefined;
let error: string | undefined;
@@ -7165,9 +7355,24 @@ export async function readSkillCommandOutput(
let threadId: string | undefined;
let turnId: string | undefined;
let completed = false;
+ let sandboxUnavailable = false;
+ const externalSandbox = appServer?.externalSandbox
+ ? { type: "externalSandbox", networkAccess: "enabled" }
+ : undefined;
const send = (request: JsonObject): void => {
appServer?.input.write(`${JSON.stringify(request)}\n`);
};
+ const startTurn = (): void => {
+ send({
+ id: 3,
+ method: "turn/start",
+ params: {
+ threadId: threadId!,
+ ...(externalSandbox ? { sandboxPolicy: externalSandbox } : {}),
+ input: [{ type: "text", text: appServer!.prompt, text_elements: [] }],
+ },
+ });
+ };
const startThread = (config?: JsonObject): void => {
if (appServer === undefined) return;
send({
@@ -7231,6 +7436,7 @@ export async function readSkillCommandOutput(
});
} else if (value["error"] !== undefined) {
error = (value["error"] as { message: string }).message;
+ sandboxUnavailable = value["id"] === 5;
appServer.input.end();
} else if (value["id"] === 1 || value["id"] === "login") {
if (value["id"] === 1) {
@@ -7323,17 +7529,35 @@ export async function readSkillCommandOutput(
);
} else if (value["id"] === 2) {
await appServer.onThreadStarted?.();
- threadId = (value["result"] as { thread: { id: string } }).thread.id;
- send({
- id: 3,
- method: "turn/start",
- params: {
- threadId,
- input: [
- { type: "text", text: appServer.prompt, text_elements: [] },
- ],
- },
- });
+ const result = value["result"] as {
+ thread: { id: string };
+ sandbox: JsonObject;
+ };
+ threadId = result.thread.id;
+ if (
+ appServer.threadSource === CODEX_SECURITY_THREAD_SOURCES.remediation
+ ) {
+ send({
+ id: 5,
+ method: "command/exec",
+ params: {
+ command: [process.execPath, "-e", ""],
+ cwd: appServer.directory!,
+ sandboxPolicy: externalSandbox ?? result.sandbox,
+ },
+ });
+ continue;
+ }
+ startTurn();
+ } else if (value["id"] === 5) {
+ const result = value["result"] as { exitCode: number };
+ if (result.exitCode !== 0) {
+ sandboxUnavailable = true;
+ error = SANDBOX_UNAVAILABLE_MESSAGE;
+ appServer.input.end();
+ continue;
+ }
+ startTurn();
} else if (value["id"] === 3) {
turnId = (value["result"] as { turn: { id: string } }).turn.id;
}
@@ -7408,6 +7632,7 @@ export async function readSkillCommandOutput(
...(error === undefined ? {} : { error }),
malformed,
...(appServer === undefined ? {} : { completed }),
+ ...(sandboxUnavailable ? { sandboxUnavailable } : {}),
};
}
diff --git a/sdk/typescript/tests-ts/cli-authentication.test.ts b/sdk/typescript/tests-ts/cli-authentication.test.ts
index 881e031c4..c27b96d00 100644
--- a/sdk/typescript/tests-ts/cli-authentication.test.ts
+++ b/sdk/typescript/tests-ts/cli-authentication.test.ts
@@ -1380,7 +1380,7 @@ describe("skill authentication", () => {
? "Authentication failed using a stored API key"
: "Authentication failed using OPENAI_API_KEY",
);
- else expect(stderr.text()).toBe("");
+ else expect(stderr.text()).toBe("Patch applied. Files changed: 1.\n");
const requests = (await readFile(requestLog, "utf8"))
.trim()
.split("\n")
@@ -1397,7 +1397,7 @@ describe("skill authentication", () => {
"notifications/initialized",
...(usesSessionKey ? ["account/login/start"] : []),
...(loginFailure && usesSessionKey ? [] : ["thread/start"]),
- ...(loginFailure ? [] : ["turn/start"]),
+ ...(loginFailure ? [] : ["command/exec", "turn/start"]),
]);
expect(await readFile(join(credentialHome, "auth.json"), "utf8")).toBe(
stored,
diff --git a/sdk/typescript/tests-ts/cli-fixtures.ts b/sdk/typescript/tests-ts/cli-fixtures.ts
index 20ace6d03..185bd26f9 100644
--- a/sdk/typescript/tests-ts/cli-fixtures.ts
+++ b/sdk/typescript/tests-ts/cli-fixtures.ts
@@ -271,7 +271,7 @@ export function dependencies(
args,
repository,
commandOptions,
- )) ?? "",
+ )) ?? (args.includes("--name-only") ? "src/finding-1.ts\0" : ""),
...(options.bulkScan === undefined ? {} : { bulkScan: options.bulkScan }),
...(options.linearClient === undefined
? {}
diff --git a/sdk/typescript/tests-ts/cli-patch-results.test.ts b/sdk/typescript/tests-ts/cli-patch-results.test.ts
new file mode 100644
index 000000000..febd49243
--- /dev/null
+++ b/sdk/typescript/tests-ts/cli-patch-results.test.ts
@@ -0,0 +1,289 @@
+import { execFileSync } from "node:child_process";
+import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, describe, expect, test } from "bun:test";
+import { main, runCodexSkillCommand } from "../src/cli.js";
+import { capture, dependencies } from "./cli-fixtures.js";
+
+type FixtureOptions = NonNullable<Parameters<typeof dependencies>[0]>;
+const directories: string[] = [];
+afterEach(async () => {
+ for (const directory of directories.splice(0))
+ await rm(directory, { recursive: true, force: true });
+});
+
+async function repositoryFixture() {
+ const directory = await mkdtemp(join(tmpdir(), "patch-results-"));
+ directories.push(directory);
+ const runRepositoryCommand: NonNullable<
+ FixtureOptions["onRepositoryCommand"]
+ > = (command, args, cwd, options) => {
+ const result = execFileSync(command, [...args], {
+ cwd,
+ encoding: "utf8",
+ env: { ...process.env, ...options?.environment },
+ stdio: ["ignore", "pipe", "pipe"],
+ });
+ return options?.trim === false ? result : result.trim();
+ };
+ const git = (...args: string[]) =>
+ runRepositoryCommand("git", args, directory);
+ git("init", "--initial-branch=main");
+ git("config", "user.name", "Synthetic User");
+ git("config", "user.email", "[email protected]");
+ await writeFile(join(directory, "app.ts"), "original\n");
+ git("add", ".");
+ git("commit", "-m", "Synthetic fixture");
+ return {
+ directory,
+ git,
+ async patch(args: string[], options: FixtureOptions = {}) {
+ const stdout = capture();
+ const stderr = capture();
+ const status = await main(
+ ["patch", ...args, "--json"],
+ stdout.stream,
+ stderr.stream,
+ dependencies({
+ currentDirectory: directory,
+ environment: {
+ CODEX_HOME: join(directory, ".codex-home"),
+ CODEX_API_KEY: "sk-proj-SYNTHETIC_KEY",
+ },
+ onRepositoryCommand: runRepositoryCommand,
+ ...options,
+ }),
+ );
+ return {
+ status,
+ result: JSON.parse(stdout.text()),
+ stdout: stdout.text(),
+ stderr: stderr.text(),
+ };
+ },
+ };
+}
+
+describe("patch outcomes", () => {
+ test("uses the external sandbox only after explicit opt-in and prints a warning", async () => {
+ const fixture = await repositoryFixture();
+ const source = `
+const assert = require("node:assert/strict");
+const lines = require("node:readline").createInterface({ input: process.stdin });
+const send = (value) => process.stdout.write(JSON.stringify(value) + "\\n");
+lines.on("line", (line) => {
+ const request = JSON.parse(line);
+ if (request.method === "initialize" || request.method === "account/login/start") send({ id: request.id, result: {} });
+ if (request.method === "thread/start") {
+ assert.equal(request.params.sandbox, "workspace-write");
+ send({ id: request.id, result: { thread: { id: "thread" }, sandbox: { type: "workspaceWrite" } } });
+ }
+ if (request.method === "command/exec") {
+ assert.deepEqual(request.params.sandboxPolicy, { type: "externalSandbox", networkAccess: "enabled" });
+ send({ id: request.id, result: { exitCode: 0 } });
+ }
+ if (request.method === "turn/start") {
+ assert.deepEqual(request.params.sandboxPolicy, { type: "externalSandbox", networkAccess: "enabled" });
+ require("node:fs").writeFileSync("app.ts", "fixed\\n");
+ send({ id: request.id, result: { turn: { id: "turn" } } });
+ send({ method: "item/completed", params: { threadId: "thread", turnId: "turn", item: { type: "agentMessage", text: "Applied the fix." } } });
+ send({ method: "turn/completed", params: { threadId: "thread", turn: { id: "turn", status: "completed" } } });
+ }
+});`;
+ const outcome = await fixture.patch(
+ ["Synthetic issue", "--external-sandbox"],
+ {
+ onCodex: (_args, output, environment) =>
+ runCodexSkillCommand(
+ ["-e", source],
+ output,
+ {
+ command: process.execPath,
+ },
+ environment,
+ ),
+ },
+ );
+ expect(outcome.status).toBe(0);
+ expect(outcome.result).toMatchObject({
+ applied: true,
+ filesChanged: 1,
+ files: ["app.ts"],
+ });
+ expect(outcome.stderr).toContain(
+ "WARNING: --external-sandbox disables Codex sandbox enforcement",
+ );
+ });
+
+ test.each([false, true])(
+ "fails a no-op with full output %s and preserves local changes",
+ async (fullOutput) => {
+ const fixture = await repositoryFixture();
+ await writeFile(join(fixture.directory, "app.ts"), "staged change\n");
+ fixture.git("add", "app.ts");
+ await writeFile(join(fixture.directory, "app.ts"), "unstaged change\n");
+ await writeFile(join(fixture.directory, "local.txt"), "untracked\n");
+ const index = fixture.git("write-tree");
+ const outcome = await fixture.patch(
+ ["Synthetic issue", ...(fullOutput ? ["--full-output"] : [])],
+ {
+ onCodex: (_args, output) => {
+ output?.stdout.write("I could not run the patch commands.");
+ return 0;
+ },
+ },
+ );
+ expect(outcome.status).toBe(2);
+ expect(outcome.result).toMatchObject({
+ ok: false,
+ error: { code: "NO_PATCH_APPLIED" },
+ });
+ expect(fixture.git("write-tree")).toBe(index);
+ expect(await readFile(join(fixture.directory, "app.ts"), "utf8")).toBe(
+ "unstaged change\n",
+ );
+ expect(await readFile(join(fixture.directory, "local.txt"), "utf8")).toBe(
+ "untracked\n",
+ );
+ },
+ );
+
+ test("reports actual changed files and preserves the index", async () => {
+ const fixture = await repositoryFixture();
+ await writeFile(join(fixture.directory, "local.txt"), "unrelated\n");
+ fixture.git("add", "local.txt");
+ const index = fixture.git("write-tree");
+ const outcome = await fixture.patch(["Synthetic issue", "--full-output"], {
+ onCodex: async (_args, output) => {
+ await writeFile(join(fixture.directory, "app.ts"), "fixed\n");
+ await writeFile(
+ join(fixture.directory, "regression.test.ts"),
+ "test\n",
+ );
+ output?.stdout.write("Applied the patch.");
+ return 0;
+ },
+ });
+ expect(outcome.status).toBe(0);
+ expect(outcome.result).toMatchObject({
+ ok: true,
+ data: {
+ applied: true,
+ filesChanged: 2,
+ files: ["app.ts", "regression.test.ts"],
+ },
+ });
+ expect(fixture.git("write-tree")).toBe(index);
+ });
+
+ test.each([false, true])(
+ "checks patch changes outside a Git repository: %s",
+ async (apply) => {
+ const fixture = await repositoryFixture();
+ await rm(join(fixture.directory, ".git"), {
+ recursive: true,
+ force: true,
+ });
+ const outcome = await fixture.patch(["Synthetic issue"], {
+ onCodex: async () => {
+ if (apply)
+ await writeFile(join(fixture.directory, "app.ts"), "fixed\n");
+ return 0;
+ },
+ });
+ expect(outcome.status).toBe(apply ? 0 : 2);
+ expect(outcome.result).toMatchObject({
+ applied: apply,
+ filesChanged: apply ? 1 : 0,
+ });
+ },
+ );
+
+ test("rejects a verified saved-finding result when no files change", async () => {
+ const fixture = await repositoryFixture();
+ const outcome = await fixture.patch(["--scan", "scan-1"], {
+ onWorkbench: () => ({
+ scan: {
+ scanId: "scan-1",
+ targetPath: fixture.directory,
+ findings: [
+ {
+ occurrenceId: "occ_1",
+ title: "Synthetic finding",
+ severity: { level: "high" },
+ locations: [{ path: "app.ts", startLine: 1 }],
+ },
+ ],
+ },
+ }),
+ onCodex: (_args, output) => {
+ output?.stdout.write(
+ JSON.stringify({
+ patches: [
+ {
+ occurrenceId: "occ_1",
+ status: "verified",
+ files: ["app.ts"],
+ verification: "The checks pass.",
+ },
+ ],
+ }),
+ );
+ return 0;
+ },
+ });
+ expect(outcome.status).toBe(2);
+ expect(outcome.result).toMatchObject({
+ ok: false,
+ error: { code: "PATCH_FAILED" },
+ });
+ expect(outcome.stderr).toContain("No patch was applied; 0 files changed.");
+ });
+
+ test.each(["exit", "rpc"])(
+ "fails before starting a model turn when sandbox preflight returns %s failure",
+ async (failure) => {
+ const fixture = await repositoryFixture();
+ const source = `
+const assert = require("node:assert/strict");
+const lines = require("node:readline").createInterface({ input: process.stdin });
+const send = (value) => process.stdout.write(JSON.stringify(value) + "\\n");
+lines.on("line", (line) => {
+ const request = JSON.parse(line);
+ if (request.method === "initialize" || request.method === "account/login/start") send({ id: request.id, result: {} });
+ if (request.method === "thread/start") send({ id: request.id, result: { thread: { id: "thread" }, sandbox: { type: "workspaceWrite", writableRoots: [], networkAccess: false, excludeTmpdirEnvVar: false, excludeSlashTmp: false } } });
+ if (request.method === "command/exec") {
+ assert.equal(request.params.sandboxPolicy.type, "workspaceWrite");
+ assert.deepEqual(request.params.command, [process.execPath, "-e", ""]);
+ send({ id: request.id, ${failure === "rpc" ? 'error: { code: -32603, message: "sandbox: unshare: Permission denied /private/repository sk-proj-SYNTHETIC_SECRET" }' : 'result: { exitCode: 1, stdout: "", stderr: "sandbox: unshare: Permission denied /private/repository sk-proj-SYNTHETIC_SECRET" }'} });
+ }
+ if (request.method === "turn/start") throw new Error("Model turn must not start");
+});`;
+ const outcome = await fixture.patch(
+ ["Synthetic issue", "--full-output"],
+ {
+ onCodex: (_args, output, environment) =>
+ runCodexSkillCommand(
+ ["-e", source],
+ output,
+ {
+ command: process.execPath,
+ },
+ environment,
+ ),
+ },
+ );
+ expect(outcome.status).toBe(2);
+ expect(outcome.result).toMatchObject({
+ ok: false,
+ error: { code: "SANDBOX_UNAVAILABLE" },
+ });
+ expect(outcome.stdout + outcome.stderr).not.toContain("SYNTHETIC_SECRET");
+ expect(outcome.stdout + outcome.stderr).not.toContain(
+ "/private/repository",
+ );
+ expect(fixture.git("status", "--porcelain")).toBe("");
+ },
+ );
+});
diff --git a/sdk/typescript/tests-ts/cli-patch-trust.test.ts b/sdk/typescript/tests-ts/cli-patch-trust.test.ts
index 5991fa6a5..1dcb24aba 100644
--- a/sdk/typescript/tests-ts/cli-patch-trust.test.ts
+++ b/sdk/typescript/tests-ts/cli-patch-trust.test.ts
@@ -122,8 +122,11 @@ test.each([
write(chunk, _encoding, callback) {
const request = JSON.parse(chunk.toString());
// Inspect the native task without making a model request.
- if (request.method === "turn/start") {
- inspectedThreadId = request.params.threadId;
+ if (
+ request.method === "turn/start" ||
+ request.method === "command/exec"
+ ) {
+ inspectedThreadId = request.params.threadId ?? inspectedThreadId;
child.stdin.write(
`${JSON.stringify({
id: 6,
@@ -140,17 +143,18 @@ test.each([
async function* events(): AsyncGenerator<string> {
for await (const line of createInterface({ input: child.stdout })) {
const event = JSON.parse(line);
+ if (event.id === 2) inspectedThreadId = event.result?.thread.id;
if (event.id === 6) {
analyticsEnabled = event.result?.config?.analytics?.enabled;
child.stdin.write(
`${JSON.stringify({
- id: 5,
+ id: 7,
method: "mcpServerStatus/list",
params: { threadId: inspectedThreadId },
})}\n`,
);
}
- if (event.id === 5) {
+ if (event.id === 7) {
servers = event.result?.data.map(
(server: { name: string }) => server.name,
);
@@ -266,7 +270,7 @@ test.each(["openai", undefined])(
const request = JSON.parse(chunk.toString());
// Inspect authentication before the first model request.
child.stdin.write(
- request.method === "turn/start"
+ request.method === "turn/start" || request.method === "command/exec"
? JSON.stringify({
id: "inspect",
method: "account/read",
diff --git a/sdk/typescript/tests-ts/cli-patch.test.ts b/sdk/typescript/tests-ts/cli-patch.test.ts
index 1fe49c5bd..e8acfd3c4 100644
--- a/sdk/typescript/tests-ts/cli-patch.test.ts
+++ b/sdk/typescript/tests-ts/cli-patch.test.ts
@@ -1068,6 +1068,7 @@ describe("scan and patch workflow", () => {
["--linear-issue", "SEC-123"],
["--create-pr"],
["--assess-patch-risk"],
+ ["--external-sandbox"],
["occ_1"],
]) {
let commandStarted = false;
@@ -1116,9 +1117,13 @@ describe("scan and patch workflow", () => {
);
return 0;
},
- onRepositoryCommand: () => {
- commandStarted = true;
- return "";
+ onRepositoryCommand: (command, args) => {
+ commandStarted ||=
+ command !== "git" ||
+ ["checkout", "commit", "push"].includes(args[0]!);
+ return status === "outside" && args.includes("--name-only")
+ ? "src/finding-1.ts\0"
+ : "";
},
},
);
@@ -1141,8 +1146,10 @@ describe("scan and patch workflow", () => {
["scan", "--patch", "--create-pr", "--json"],
{
result: resultWithFindings(["high"]),
- onRepositoryCommand: () => {
- throw new Error("GitHub authentication failed.");
+ onRepositoryCommand: (command, args) => {
+ if (command === "gh")
+ throw new Error("GitHub authentication failed.");
+ return args.includes("--name-only") ? "src/finding-1.ts\0" : "";
},
},
);
@@ -1427,7 +1434,11 @@ describe("scan and patch workflow", () => {
result: resultWithFindings(["high"]),
onRepositoryCommand: (command, args) => {
published ||= command === "gh" && args[1] === "create";
- return command === "gh" && args[1] === "create" ? url : "";
+ return command === "gh" && args[1] === "create"
+ ? url
+ : args.includes("--name-only")
+ ? "src/finding-1.ts\0"
+ : "";
},
},
{
@@ -1533,7 +1544,7 @@ describe("scan and patch workflow", () => {
expect(args).toEqual(["remote", "get-url", "--push", "origin"]);
return origin;
}
- return "";
+ return args.includes("--name-only") ? "src/finding-1.ts\0" : "";
}
expect(command).toBe(client);
publicationCommands.push(args);
diff --git a/sdk/typescript/tests-ts/cli-skills.test.ts b/sdk/typescript/tests-ts/cli-skills.test.ts
index 36279e205..45e3e59b8 100644
--- a/sdk/typescript/tests-ts/cli-skills.test.ts
+++ b/sdk/typescript/tests-ts/cli-skills.test.ts
@@ -120,7 +120,11 @@ describe("CLI skill commands", () => {
"\\\\server\\share\\issue.txt",
]);
expect(stdout.text()).toBe("");
- expect(stderr.text()).toBe("");
+ expect(stderr.text()).toBe(
+ command === "patch"
+ ? "codex-security: Patch command exited with status 7.\n"
+ : "",
+ );
const help = capture();
expect(
@@ -787,7 +791,9 @@ describe("CLI skill commands", () => {
).toBe(0);
expect(invocation).toContain('model="gpt-5.6-custom"');
expect(invocation).toContain('model_reasoning_effort="high"');
- expect(stderr.text()).toBe("");
+ expect(stderr.text()).toBe(
+ command === "patch" ? "Patch applied. Files changed: 1.\n" : "",
+ );
}
const longLiteral =
@@ -948,7 +954,9 @@ describe("CLI skill commands", () => {
).toBe(0);
expect(invocation).toContain('model="gpt-5.6-terra"');
expect(invocation).toContain('model_reasoning_effort="max"');
- expect(stderr.text()).toBe("");
+ expect(stderr.text()).toBe(
+ command === "patch" ? "Patch applied. Files changed: 1.\n" : "",
+ );
for (const [options, message] of [
[
@@ -1343,7 +1351,11 @@ lines.on("line", (line) => {
} else if (request.method === "thread/start") {
assert.equal(process.cwd(), ${JSON.stringify(process.cwd())});
assert.deepEqual(request.params, { threadSource: "security_remediation", approvalPolicy: "never", sandbox: "workspace-write" });
- send({ id: 2, result: { thread: { id: "parent", source: "vscode", ephemeral: false } } });
+ send({ id: 2, result: { thread: { id: "parent", source: "vscode", ephemeral: false }, sandbox: { type: "workspaceWrite", writableRoots: [], networkAccess: false, excludeTmpdirEnvVar: false, excludeSlashTmp: false } } });
+ } else if (request.method === "command/exec") {
+ assert.equal(request.params.sandboxPolicy.type, "workspaceWrite");
+ assert.deepEqual(request.params.command, [process.execPath, "-e", ""]);
+ send({ id: request.id, result: { exitCode: 0, stdout: "", stderr: "" } });
} else if (request.method === "turn/start") {
assert.equal(request.params.threadId, "parent");
assert.equal(request.params.input[0].text, "Fix the synthetic finding");
@@ -1507,6 +1519,7 @@ lines.on("line", (line) => {
const request = JSON.parse(line);
if (request.method === "initialize") send({ id: 1, result: {} });
if (request.method === "thread/start") send({ id: 2, result: { thread: { id: "parent" } } });
+ if (request.method === "command/exec") send({ id: request.id, result: { exitCode: 0 } });
if (request.method === "turn/start") process.stdout.write(${JSON.stringify(events.map((event) => JSON.stringify(event)).join("\n") + "\n")}, () => process.exit(0));
});
`;
diff --git a/sdk/typescript/tests-ts/fixtures/skill-auth.mjs b/sdk/typescript/tests-ts/fixtures/skill-auth.mjs
index f0a790360..7f94c5619 100644
--- a/sdk/typescript/tests-ts/fixtures/skill-auth.mjs
+++ b/sdk/typescript/tests-ts/fixtures/skill-auth.mjs
@@ -78,8 +78,24 @@ for await (const line of createInterface({ input: process.stdin })) {
error: { code: -32000, message: "Unauthorized" },
});
} else {
- send({ id: request.id, result: { thread: { id: "synthetic-thread" } } });
+ send({
+ id: request.id,
+ result: {
+ thread: { id: "synthetic-thread" },
+ sandbox: { type: "workspaceWrite" },
+ },
+ });
+ }
+ } else if (request.method === "command/exec") {
+ if (process.env.SYNTHETIC_CHECK_STARTUP_LOCK) {
+ assert.equal(
+ existsSync(join(process.env.CODEX_HOME, ".codex-security-scan.lock")),
+ false,
+ );
}
+ assert.equal(request.params.sandboxPolicy.type, "workspaceWrite");
+ assert.deepEqual(request.params.command, [process.execPath, "-e", ""]);
+ send({ id: request.id, result: { exitCode: 0 } });
} else if (request.method === "turn/start") {
if (process.env.SYNTHETIC_CHECK_STARTUP_LOCK) {
assert.equal(
diff --git a/sdk/typescript/tests-ts/fixtures/skill-native-config.mjs b/sdk/typescript/tests-ts/fixtures/skill-native-config.mjs
index 7a201bcca..c927d1789 100644
--- a/sdk/typescript/tests-ts/fixtures/skill-native-config.mjs
+++ b/sdk/typescript/tests-ts/fixtures/skill-native-config.mjs
@@ -14,7 +14,9 @@ await Promise.all([
(async () => {
for await (const line of createInterface({ input: process.stdin })) {
const request = JSON.parse(line);
- if (request.method === "turn/start") {
+ if (request.method === "command/exec") {
+ send({ id: request.id, result: { exitCode: 0 } });
+ } else if (request.method === "turn/start") {
turnRequest = request;
child.stdin.write(
JSON.stringify({