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(patches): publish verified saved-finding changes (#2429)
Michael D'Angelo9bdc765fea11
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.mdindex 980b9671f..00593bd00 100644--- a/sdk/typescript/docs/cli.md+++ b/sdk/typescript/docs/cli.md@@ -2572,8 +2572,13 @@ including code, HTML, and math, retain their original text.GITLAB_HOST=gitlab.example.com codex-security patch --scan SCAN_ID --create-pr```-Both providers return `pullRequest: { branch, url }`. Supplied-issue patching-requires a clean tree. If publication fails, run the printed+Both providers return `pullRequest: { branch, url }`. Saved-finding publication+includes files changed and verified in the same patch turn. If a selected file+already had local edits, the CLI keeps the patch in the working tree for review.+The risk assessment and commit use the same verified tree. If staging changed+during the run and Git cannot update the index, the CLI reports it and keeps+the existing staging.+Supplied-issue patching requires a clean tree. If publication fails, run the printed`patch --resume-pr BRANCH` command in the same repository. It reuses the savedcommit and refuses changed branches. Retain the GitLab host setting on resume.diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex 8c95afea1..03bbfd106 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -1334,6 +1334,7 @@ interface PatchRiskRequest {readonly environment?: NodeJS.ProcessEnv;repository: string;base: string;+ head?: string;files?: readonly string[];configuration: SkillConfiguration;}@@ -6884,12 +6885,18 @@ export async function main(selected.repository,dependencies.currentDirectory(),);- const patchRiskBase = options.assessPatchRisk- ? await snapshotPatchTree(selected.repository, dependencies)- : undefined;+ const publication =+ options.createPr && selected.findings.length > 0+ ? await prepareSavedPatchPublication(+ selected.repository,+ dependencies,+ )+ : undefined;const patchBase =- patchRiskBase ??- (await snapshotPatchState(selected.repository, dependencies));+ publication?.worktree ??+ (options.assessPatchRisk+ ? await snapshotPatchTree(selected.repository, dependencies)+ : await snapshotPatchState(selected.repository, dependencies));const patches = await runFindingPatches(selected,options,@@ -6900,6 +6907,7 @@ export async function main(externalSandbox: options.externalSandbox,validationPrompt,verificationMode: options.verificationMode,+ publication,},);exitCode = patchExitCode(patches);@@ -6907,6 +6915,7 @@ export async function main(selected.repository,patchBase,dependencies,+ publication !== undefined,);patchResult = {scanId: selected.scanId,@@ -6926,13 +6935,16 @@ export async function main(`Patch applied. Files changed: ${files.length}.\n`,);let patchRisk: PatchRiskAssessment | undefined;- if (patchRiskBase !== undefined) {- const files = verifiedPatchFiles(selected, patches);- if (files.length > 0) {+ if (options.assessPatchRisk && typeof patchBase === "string") {+ const files = publication+ ? undefined+ : verifiedPatchFiles(selected, patches);+ if (publication ? publication.hasChanges : files?.length) {patchRisk = await runPatchRiskAssessment({repository: selected.repository,- base: patchRiskBase,+ base: publication?.parent ?? patchBase,+ head: publication?.tree,files,configuration: options,auth: options.auth,@@ -6942,11 +6954,11 @@ export async function main();}}- const pullRequest = options.createPr+ const pullRequest = publication? await createPatchPullRequest(selected.repository,selected.scanId,- verifiedPatchFiles(selected, patches),+ publication,errorOutput,dependencies,patchRisk,@@ -8585,6 +8597,7 @@ function directPatchDigest(async function publishPatchBranch(repository: string,branch: string,+ commit: string,body: string,stderr: Writable,dependencies: CliDependencies,@@ -8604,7 +8617,20 @@ async function publishPatchBranch(body,publisher,pushBranch: async (branch) => {- await run("git", ["push", "--set-upstream", "origin", branch]);+ await run("git", ["push", "origin", `${commit}:refs/heads/${branch}`]);+ // A SHA refspec cannot set upstream implicitly.+ await run("git", [+ "config",+ "--local",+ `branch.${branch}.remote`,+ "origin",+ ]);+ await run("git", [+ "config",+ "--local",+ `branch.${branch}.merge`,+ `refs/heads/${branch}`,+ ]);},});stderr.write(`${publisher.label}: ${safePatchText(url)}\n`);@@ -8652,7 +8678,14 @@ async function resumePatchPullRequest(PATCH_PR_BODY,patchPullRequestBodyKey(branch),]);- return publishPatchBranch(repository, branch, body, stderr, dependencies);+ return publishPatchBranch(+ repository,+ branch,+ commit,+ body,+ stderr,+ dependencies,+ );}function verifiedPatchFiles(@@ -8679,16 +8712,173 @@ function verifiedPatchFiles(});}+type SavedPatchPublication = Awaited<+ ReturnType<typeof prepareSavedPatchPublication>+>;++async function prepareSavedPatchPublication(+ repository: string,+ dependencies: CliDependencies,+) {+ const git = (args: string[], options?: { trim?: boolean; input?: string }) =>+ dependencies.runRepositoryCommand("git", args, repository, options);+ const parent = await git(["rev-parse", "HEAD"]);+ const parentTree = await git(["rev-parse", parent + "^{tree}"]);+ let tree = parentTree;++ // Git quotes arbitrary path bytes the same way in a raw diff and --index-info.+ const diff = async (+ from: string,+ to?: string,+ paths: readonly string[] = [],+ ) => {+ const output = await git(+ [+ "-c",+ "core.quotePath=true",+ "--literal-pathspecs",+ "diff",+ "--no-relative",+ "--no-renames",+ "--ignore-submodules=none",+ "--raw",+ "--no-abbrev",+ ...(to === undefined+ ? ["--cached", "--ita-visible-in-index", from]+ : [from, to]),+ "--",+ ...paths,+ ],+ { trim: false },+ );+ return new Map(+ output+ .split("\n")+ .filter(Boolean)+ .map((line) => {+ const [metadata, path] = line.split("\t");+ const [, mode, , oid] = metadata!.split(" ");+ return [path!, `${mode} ${oid}`];+ }),+ );+ };+ const worktree = await snapshotPatchTree(repository, dependencies);+ const dirty = new Set([+ ...(await diff(parent, worktree)).keys(),+ ...(await diff(parent)).keys(),+ ]);+ const ignoredOutput = await git(+ [+ "-c",+ "core.quotePath=true",+ "ls-files",+ "--full-name",+ "--others",+ "--ignored",+ "--exclude-standard",+ "--",+ ".",+ ],+ { trim: false },+ );+ const ignored = new Set(ignoredOutput.split("\n"));++ return {+ parent,+ worktree,+ get tree() {+ return tree;+ },+ get hasChanges() {+ return tree !== parentTree;+ },+ async record(paths: string[], before: string, after: string) {+ const changes = paths.length+ ? await diff(before, after, paths)+ : new Map<string, string>();+ const changed = [...changes.keys()];+ if (!changed.length)+ return "The reported patch files contain no changes from this finding.";+ if (changed.some((path) => ignored.has(path)))+ return "A selected file was ignored and untracked before patching. Local edits were kept for review.";+ if (changed.some((path) => dirty.has(path)))+ return "The selected patch files contain pre-existing changes. Local edits were kept for review.";+ const prior = await diff(tree, before, paths);+ if (changed.some((path) => prior.has(path)))+ return "The selected files contain earlier unverified changes. Local edits were kept for review.";+ // Remove leaves before adding their replacements when a file changes into a directory.+ const records = [...changes]+ .map(([path, entry]) => `${entry}\t${path}\n`)+ .sort();+ const next = await withPatchIndex(+ repository,+ dependencies,+ async (run) => {+ await run(["read-tree", tree]);+ await run(["update-index", "--index-info"], records.join(""));+ return run(["write-tree"]);+ },+ );+ const applied = await diff(tree, next);+ if (+ applied.size !== changes.size ||+ changed.some((path) => applied.get(path) !== changes.get(path))+ )+ return "The selected changes also replace an unreported path. Local edits were kept for review.";+ tree = next;+ return undefined;+ },+ async commit(branch: string, stderr: Writable) {+ if ((await git(["rev-parse", "HEAD"])) !== parent)+ throw new CodexSecurityError(+ "The patch base changed during verification. Local edits were kept for review.",+ );+ await git(["switch", "-c", branch]);+ const verified = await diff(parent, tree);+ const later = await diff(+ tree,+ await snapshotPatchTree(repository, dependencies),+ );+ if ([...verified.keys()].some((path) => later.has(path)))+ throw new CodexSecurityError(+ "The working tree differs from the verified changes. Local edits were kept for review.",+ );+ await withPatchIndex(repository, dependencies, async (run) => {+ await run(["read-tree", tree]);+ await run(["commit", "-m", PATCH_PR_TITLE]);+ });+ const commit = await git(["rev-parse", "HEAD"]);+ if (+ (await git(["rev-parse", commit + "^@"])) !== parent ||+ (await git(["rev-parse", commit + "^{tree}"])) !== tree+ ) {+ throw new CodexSecurityError(+ "The patch commit differs from the verified changes. The commit was kept locally for review.",+ );+ }+ // Fast-forward just the verified entries in the real index, keeping unrelated staging.+ try {+ await git(["read-tree", "-i", "-m", parent, tree]);+ } catch (error) {+ stderr.write(+ `Could not update local staging to match the verified commit: ${diagnosticValue(error)}\n`,+ );+ }+ return commit;+ },+ };+}+async function createPatchPullRequest(repository: string,patchId: string,- files: readonly string[],+ selection: readonly string[] | SavedPatchPublication,stderr: Writable,dependencies: CliDependencies,patchRisk?: PatchRiskAssessment,introduction = PATCH_PR_BODY,): Promise<{ branch: string; url: string } | undefined> {- if (files.length === 0) {+ if ("commit" in selection ? !selection.hasChanges : selection.length === 0) {stderr.write("No verified patch changes to publish.\n");return;}@@ -8703,21 +8893,33 @@ async function createPatchPullRequest(stderr.write("Creating a draft pull request or merge request for verified patches...\n",);- await run(["switch", "-c", branch]);- await run(["--literal-pathspecs", "add", "--", ...files]);- await run([- "--literal-pathspecs",- "commit",- "--only",- "-m",- PATCH_PR_TITLE,- "--",- ...files,- ]);- const commit = await run(["rev-parse", "HEAD"]);+ let commit: string;+ if ("commit" in selection) {+ commit = await selection.commit(branch, stderr);+ } else {+ await run(["switch", "-c", branch]);+ await run(["--literal-pathspecs", "add", "--", ...selection]);+ await run([+ "--literal-pathspecs",+ "commit",+ "--only",+ "-m",+ PATCH_PR_TITLE,+ "--",+ ...selection,+ ]);+ commit = await run(["rev-parse", "HEAD"]);+ }await run(["config", "--local", patchCommitKey(branch), commit]);await run(["config", "--local", patchPullRequestBodyKey(branch), body]);- return publishPatchBranch(repository, branch, body, stderr, dependencies);+ return publishPatchBranch(+ repository,+ branch,+ commit,+ body,+ stderr,+ dependencies,+ );}async function requireCleanPatchPullRequestBase(@@ -8741,6 +8943,7 @@ async function changedPatchFiles(repository: string,base: string | Map<string, string>,dependencies: CliDependencies,+ includeSubmodules = false,): Promise<string[]> {if (base instanceof Map) {const head = await snapshotPatchDirectory(repository);@@ -8751,7 +8954,15 @@ async function changedPatchFiles(const head = await snapshotPatchTree(repository, dependencies);const output = await dependencies.runRepositoryCommand("git",- ["--literal-pathspecs", "diff", "--name-only", "-z", base, head],+ [+ "--literal-pathspecs",+ "diff",+ ...(includeSubmodules ? ["--ignore-submodules=none"] : []),+ "--name-only",+ "-z",+ base,+ head,+ ],repository,{ trim: false },);@@ -8889,12 +9100,15 @@ async function assessPatchRisk(const root = await mkdtemp(join(tmpdir(), "codex-security-patch-risk-"));const patchPath = join(root, "patch.diff");try {- const head = await snapshotPatchTree(request.repository, dependencies);+ const head =+ request.head ??+ (await snapshotPatchTree(request.repository, dependencies));await writeFile(patchPath, "", { encoding: "utf8", mode: 0o600 });+ const diff = ["--literal-pathspecs", "diff"];+ if (request.head !== undefined) diff.push("--ignore-submodules=none");const [, changedFilesOutput] = await Promise.all([run([- "--literal-pathspecs",- "diff",+ ...diff,"--binary","--full-index",`--output=${patchPath}`,@@ -8902,18 +9116,9 @@ async function assessPatchRisk(head,...pathspec,]),- run(- [- "--literal-pathspecs",- "diff",- "--name-only",- "-z",- request.base,- head,- ...pathspec,- ],- { trim: false },- ),+ run([...diff, "--name-only", "-z", request.base, head, ...pathspec], {+ trim: false,+ }),]);const changedFiles = changedFilesOutput.split("\0").filter(Boolean);if ((await lstat(patchPath)).size === 0 || changedFiles.length === 0) {@@ -8958,26 +9163,40 @@ async function assessPatchRisk(}}+async function withPatchIndex<T>(+ repository: string,+ dependencies: CliDependencies,+ operation: (+ git: (args: string[], input?: string) => Promise<string>,+ ) => Promise<T>,+): Promise<T> {+ const root = await mkdtemp(join(tmpdir(), "codex-security-patch-tree-"));+ try {+ return await operation((args, input) =>+ dependencies.runRepositoryCommand("git", args, repository, {+ environment: {+ GIT_INDEX_FILE: join(root, "index"),+ GIT_LITERAL_PATHSPECS: "0",+ },+ input,+ }),+ );+ } finally {+ await rm(root, { recursive: true, force: true });+ }+}+async function snapshotPatchTree(repository: string,dependencies: CliDependencies,): Promise<string> {- const root = await mkdtemp(join(tmpdir(), "codex-security-patch-tree-"));- const environment = {- GIT_INDEX_FILE: join(root, "index"),- GIT_LITERAL_PATHSPECS: "0",- };const sourcePathspecs = ["--",":/",":(top,glob,exclude)**/.svn",":(top,glob,exclude)**/.svn/**",];- const run = (args: string[]) =>- dependencies.runRepositoryCommand("git", args, repository, {- environment,- });- try {+ return withPatchIndex(repository, dependencies, async (run) => {let hasHead = true;try {await run(["rev-parse", "--verify", "--quiet", "HEAD"]);@@ -9007,18 +9226,14 @@ async function snapshotPatchTree({ trim: false, environment: { GIT_LITERAL_PATHSPECS: "0" } },);await run(["read-tree", "--empty"]);- await dependencies.runRepositoryCommand(- "git",+ await run(["-c", "core.ignorestat=false", "update-index", "--index-info"],- repository,- { environment, input: entries },+ entries,);}await run(["add", "--all", ...sourcePathspecs]);- return await run(["write-tree"]);- } finally {- await rm(root, { recursive: true, force: true });- }+ return run(["write-tree"]);+ });}async function resolvePatchValidationPrompt(@@ -9046,7 +9261,12 @@ async function runFindingPatches(configuration: SkillConfiguration,stderr: Writable,dependencies: CliDependencies,- options: Omit<SkillRunOptions, "directory" | "findings"> = {},+ {+ publication,+ ...options+ }: Omit<SkillRunOptions, "directory" | "findings"> & {+ publication?: SavedPatchPublication;+ } = {},interactive = true,): Promise<FindingPatch[]> {if (selected.findings.length === 0) {@@ -9076,9 +9296,11 @@ async function runFindingPatches(},});let status: number;- let changedFiles: string[];+ let changed: boolean;+ let observedTree: string | undefined;+ let base: string | Map<string, string>;try {- const base = await snapshotPatchState(selected.repository, dependencies);+ base = await snapshotPatchState(selected.repository, dependencies);status = await runSkill("fix-finding",[],@@ -9099,11 +9321,17 @@ async function runFindingPatches(if (status === 130 || status === 143) {throw new CodexSecurityError("Patch operation was interrupted.");}- changedFiles = await changedPatchFiles(- selected.repository,- base,- dependencies,- );+ if (publication && typeof base === "string") {+ observedTree = await snapshotPatchTree(+ selected.repository,+ dependencies,+ );+ changed = base !== observedTree;+ } else {+ changed =+ (await changedPatchFiles(selected.repository, base, dependencies))+ .length > 0;+ }} finally {progress.stop();}@@ -9143,10 +9371,7 @@ async function runFindingPatches("Patch verification was not reported.",parsed.data.files,);- } else if (- parsed.data.status === "verified" &&- changedFiles.length === 0- ) {+ } else if (parsed.data.status === "verified" && !changed) {patch = failed("No patch was applied; 0 files changed.");} else {patch = parsed.data;@@ -9157,6 +9382,19 @@ async function runFindingPatches(}}+ if (+ publication &&+ typeof base === "string" &&+ observedTree !== undefined &&+ patch.status === "verified"+ ) {+ const reason = await publication.record(+ verifiedPatchFiles(selected, [patch]),+ base,+ observedTree,+ );+ if (reason) patch = failed(reason);+ }const title = safePatchText(finding.title);stderr.write(` ${patch.status.toUpperCase()} ${title}${patch.reason === undefined ? "" : `: ${safePatchText(patch.reason)}`}\n`,@@ -11295,6 +11533,14 @@ async function executeScan(),};try {+ const publication =+ (arguments_.createPr || patchSelection?.createPullRequest) &&+ selected.findings.length > 0+ ? await prepareSavedPatchPublication(+ selected.repository,+ dependencies,+ )+ : undefined;patches = await runFindingPatches(selected,{@@ -11319,18 +11565,16 @@ async function executeScan(safetyIdentifier: arguments_.safetyIdentifier,auth,findingInstructions: patchSelection?.instructions,+ publication,},progress?.interactive === true,);scanData = { ...scanData, patchSeverity: patchThreshold, patches };- if (- (arguments_.createPr || patchSelection?.createPullRequest) &&- patchExitCode(patches) === 0- ) {+ if (publication && patchExitCode(patches) === 0) {const pullRequest = await createPatchPullRequest(selected.repository,selected.scanId,- verifiedPatchFiles(selected, patches),+ publication,errorOutput,dependencies,);diff --git a/sdk/typescript/tests-ts/cli-patch.test.ts b/sdk/typescript/tests-ts/cli-patch.test.tsindex d36148e42..e48cf48e6 100644--- a/sdk/typescript/tests-ts/cli-patch.test.ts+++ b/sdk/typescript/tests-ts/cli-patch.test.ts@@ -3,7 +3,7 @@ import { gitText } from "./support/shell.js";import { emptyPage } from "./support/linear-pagination.js";import { resolving } from "./support/promises.js";import { parse as parseToml } from "smol-toml";-import { describe, expect, test, mock } from "bun:test";+import { afterEach, describe, expect, test, mock } from "bun:test";import { execFileSync } from "node:child_process";import { hash } from "node:crypto";import { mkdir, readFile, rm, symlink, writeFile } from "node:fs/promises";@@ -20,7 +20,10 @@ import {fakeResult,type OnCodex,} from "./cli-fixtures.js";-import { temporaryDirectory } from "./support/temporary-directories.js";+import {+ createApiTestFixtures,+ temporaryDirectory,+} from "./support/temporary-directories.js";import { throwing } from "./support/errors.js";import { createCliTest } from "./support/cli-run.js";@@ -91,8 +94,7 @@ function patchRiskSummary() {].join("\n");}-function patchRiskAssessment() {- const summary = patchRiskSummary();+function patchRiskAssessment(summary = patchRiskSummary()) {return {report: ["<!-- codex-security:patch-risk-summary:start -->",@@ -106,9 +108,9 @@ function patchRiskAssessment() {};}-function patchRiskReport() {+function patchRiskReport(summary = patchRiskSummary()) {return [- patchRiskSummary(),+ summary,"","```json",'{"schemaVersion":1,"recommendation":"merge","workflowLabel":"human_review_required"}',@@ -116,6 +118,63 @@ function patchRiskReport() {].join("\n");}+const publicationDirectories = createApiTestFixtures("codex-security-publish-");+afterEach(publicationDirectories.cleanup);++const initialPatchFiles = {+ "src/finding-1.ts": "unsafe\n",+ "unrelated.ts": "original\n",+};++async function publicationFixtures(+ files: Record<string, string> = {},+ severities: SeverityLevel[] = ["high"],+ scanId = "scan-1",+) {+ const root = await publicationDirectories.temporaryDirectory();+ const repository = join(root, "repository");+ const remote = join(root, "remote.git");+ await mkdir(join(repository, "src"), { recursive: true });+ const git = repositoryGit(repository);+ git("init", "--initial-branch=main");+ git("config", "user.name", "Synthetic User");+ git("config", "user.email", "[email protected]");+ git("config", "commit.gpgsign", "false");+ for (const [path, contents] of Object.entries(files)) {+ const file = join(repository, path);+ await mkdir(dirname(file), { recursive: true });+ await writeFile(file, contents);+ }+ git("add", "--", ".");+ git("commit", "--allow-empty", "-m", "Initial synthetic checkout");+ git("init", "--bare", remote);+ git("remote", "add", "origin", remote);+ const result = resultWithFindings(severities);+ const onCodex: OnCodex = async (args, output) => {+ await writeFile(join(repository, "src", "finding-1.ts"), "fixed\n");+ completePatches(args, output);+ return 0;+ };+ return {+ git,+ remote,+ currentDirectory: repository,+ result,+ onWorkbench: () => savedScan(result, scanId, repository),+ onCodex,+ };+}++function resumedPublicationGit(args: readonly string[]) {+ if (+ args[0] === "rev-parse" ||+ args.at(-1)?.endsWith(".codexSecurityPatchCommit")+ )+ return "verified-commit";+ const defaultIndex = args.indexOf("--default");+ return defaultIndex < 0 ? "" : args[defaultIndex + 1]!;+}+async function runWorkflow(arguments_: string[],fixtures: Parameters<typeof dependencies>[0] = {},@@ -812,73 +871,6 @@ describe("scan and patch workflow", () => {}});- test("preserves patch-risk details in display and publication summaries", async () => {- const result = resultWithFindings(["high"]);- const detail = "Diagnostic detail: token=SYNTHETIC_RISK_VALUE";- const report = patchRiskAssessment().report.replace(- patchRiskSummary(),- `${patchRiskSummary()}\n\n${detail}`,- );- const repositoryCommands: Array<{- command: string;- args: readonly string[];- }> = [];- const outcome = await runWorkflow(- [- "patch",- "--scan",- "scan-1",- "--assess-patch-risk",- "--create-pr",- "--json",- ],- {- result,- onWorkbench: () => savedScan(result),- onRepositoryCommand: (command, args) => {- repositoryCommands.push({ command, args });- if (command === "git") {- if (args[0] === "remote") {- return "https://github.example.test/example/repository.git";- }- return args.includes("--name-only") ? "src/finding-1.ts\0" : "";- }- return args[1] === "create"- ? "https://github.example.test/example/repository/pull/15"- : "";- },- },- {- configure: (current) => {- Object.assign(current, {- assessPatchRisk: async () => ({ report }),- });- },- },- );-- expect(outcome.exitCode, outcome.stderr).toBe(0);- expect(outcome.stderr).toContain("Patch risk assessment:");- expect(outcome.stderr).toContain(patchRiskSummary());- expect(outcome.stderr).toContain(detail);- expect(JSON.parse(outcome.stdout).patchRisk.report).toContain(detail);- const published = repositoryCommands.find(- ({ command, args }) => command === "gh" && args[1] === "create",- )?.args;- const persisted = repositoryCommands.find(- ({ command, args }) =>- command === "git" &&- args[0] === "config" &&- args[2]?.endsWith(".codexSecurityPatchPullRequestBody"),- )?.args;- expect(published).toBeDefined();- expect(persisted).toBeDefined();- for (const body of [published?.at(-1), persisted?.at(-1)]) {- expect(body).toContain(patchRiskSummary());- expect(body).toContain(detail);- }- });-test("assesses only changes made during a literal patch run", async () => {const directory = await temporaryDirectory("codex-security-patch-risk-");const repository = join(directory, "repository");@@ -1494,36 +1486,40 @@ describe("scan and patch workflow", () => {}});- test("publishes only verified patch files and preserves unrelated staged changes", async () => {- const directory = await temporaryDirectory("codex-security-patch-pr-");- const repository = join(directory, "repository");- const remote = join(directory, "remote.git");- const url = "https://github.example.test/example/repository/pull/15";- const result = resultWithFindings(["high", "medium"]);- result.findings.findings[0]!.title = "Synthetic private finding";- const expectedPullRequestBody = [- "Applies verified security fixes from a completed scan.",- "",- "## Patch risk assessment",- "",- patchRiskSummary(),- ].join("\n");- let pullRequestArguments: readonly string[] = [];- const githubCommands: string[][] = [];- await mkdir(join(repository, "src"), { recursive: true });- const git = repositoryGit(repository);-- try {- git("init", "--initial-branch=main");- git("config", "user.name", "Synthetic User");- git("config", "user.email", "[email protected]");- git("config", "commit.gpgsign", "false");- await writeFile(join(repository, "src", "finding-1.ts"), "unsafe\n");- await writeFile(join(repository, "unrelated.ts"), "original\n");- git("add", "--", ".");- git("commit", "-m", "Initial synthetic checkout");- git("init", "--bare", remote);- git("remote", "add", "origin", remote);+ describe("saved-finding Git publication", () => {+ test("publishes only verified patch files and preserves unrelated staged changes", async () => {+ const fixtures = await publicationFixtures(+ initialPatchFiles,+ ["high", "medium"],+ "scan",+ );+ const { currentDirectory: repository, git, result } = fixtures;+ const url = "https://github.example.test/example/repository/pull/15";+ result.findings.findings[0]!.title = "Synthetic private finding";+ const detail = "Diagnostic detail: token=SYNTHETIC_RISK_VALUE";+ const summary = `${patchRiskSummary()}\n\n${detail}`;+ const expectedPullRequestBody = [+ "Applies verified security fixes from a completed scan.",+ "",+ "## Patch risk assessment",+ "",+ summary,+ ].join("\n");+ let pullRequestArguments: readonly string[] = [];+ const githubCommands: string[][] = [];+ const dependency = "src/dependency";+ git(+ "-c",+ "protocol.file.allow=always",+ "submodule",+ "add",+ "--",+ repository,+ dependency,+ );+ git("commit", "-m", "Add fixture dependency");+ git("config", "diff.ignoreSubmodules", "all");+ git("config", "submodule.src/dependency.ignore", "all");git("push", "--set-upstream", "origin", "main");await writeFile(join(repository, "unrelated.ts"), "staged separately\n");git("add", "--", "unrelated.ts");@@ -1540,9 +1536,7 @@ describe("scan and patch workflow", () => {"--json",],{- currentDirectory: repository,- result,- onWorkbench: () => savedScan(result, "scan", repository),+ ...fixtures,onCodex: async (args, output) => {if (output?.appServer?.prompt.includes(@@ -1578,17 +1572,42 @@ describe("scan and patch workflow", () => {};const patch = await readFile(artifact.path);expect(artifact.sourceType).toBe("patch_file");- expect(artifact.changedFiles).toEqual(["src/finding-1.ts"]);+ expect(artifact.changedFiles).toEqual([+ dependency,+ "src/finding-1.ts",+ ]);+ expect(patch.toString()).toContain(+ "+Subproject commit " ++ git("-C", dependency, "rev-parse", "HEAD"),+ );expect(patch.toString()).toEndWith("+fixed \n");expect(hash("sha256", patch)).toBe(artifact.sha256);- output.stdout.write(patchRiskAssessment().report);+ output.stdout.write(patchRiskAssessment(summary).report);return 0;}+ await writeFile(+ join(repository, "src/finding-1.ts"),+ "intermediate\n",+ );+ git("add", "--", "src/finding-1.ts");+ git("-C", dependency, "fetch", "origin", "main");+ git("-C", dependency, "checkout", "--detach", "FETCH_HEAD");await writeFile(join(repository, "src", "finding-1.ts"),"fixed \n",);- completePatches(args, output);+ output!.stdout.write(+ JSON.stringify({+ patches: [+ {+ occurrenceId: "occ_1",+ status: "verified",+ files: ["src", "unrelated.ts"],+ verification: "Focused checks pass.",+ },+ ],+ }),+ );return 0;},onRepositoryCommand: (@@ -1599,6 +1618,29 @@ describe("scan and patch workflow", () => {) => {expect(workingDirectory).toBe(repository);if (command === "git") {+ if (args[0] === "push") {+ const branch = git("symbolic-ref", "HEAD");+ const verified = git("rev-parse", branch);+ const later = git(+ "commit-tree",+ "HEAD^{tree}",+ "-p",+ verified,+ "-m",+ "Later commit",+ );+ git("update-ref", branch, later, verified);+ try {+ return runGitRepositoryCommand(+ command,+ args,+ workingDirectory,+ commandOptions,+ );+ } finally {+ git("update-ref", branch, verified, later);+ }+ }return runGitRepositoryCommand(command,args,@@ -1615,11 +1657,27 @@ describe("scan and patch workflow", () => {);expect(outcome.exitCode, outcome.stderr).toBe(0);+ expect(outcome.stderr).toContain("Patch risk assessment:");+ expect(outcome.stderr).toContain(summary);expect(git("branch", "--show-current")).toBe("codex-security/patch-scan");- expect(git("show", "--format=", "--name-only", "HEAD")).toBe(- "src/finding-1.ts",+ expect(git("rev-parse", "--abbrev-ref", "@{upstream}")).toBe(+ "origin/codex-security/patch-scan",);- expect(git("diff", "--cached", "--name-only")).toBe("unrelated.ts");+ expect(+ git(+ "show",+ "--ignore-submodules=none",+ "--format=",+ "--name-only",+ "HEAD",+ ),+ ).toBe(dependency + "\nsrc/finding-1.ts");+ expect(+ git("rev-parse", "origin/codex-security/patch-scan:" + dependency),+ ).toBe(git("-C", dependency, "rev-parse", "HEAD"));+ expect(git("show", ":src/finding-1.ts")).toBe("intermediate");+ expect(outcome.stderr).toContain("Could not update local staging");+ expect(git("show", ":unrelated.ts")).toBe("staged separately");expect(git("rev-parse", "HEAD")).toBe(git("rev-parse", "origin/codex-security/patch-scan"),);@@ -1651,65 +1709,100 @@ describe("scan and patch workflow", () => {expect(githubCommands.some((args) => args[1] === "comment")).toBe(false);expect(JSON.parse(outcome.stdout)).toMatchObject({pullRequest: { branch: "codex-security/patch-scan", url },- patchRisk: { report: patchRiskReport() },+ patchRisk: { report: patchRiskReport(summary) },});expect(outcome.stdout).not.toContain("codex-security:patch-risk-summary");- } finally {- await rm(directory, { recursive: true, force: true });- }- });+ });- test.each([- ["github", "push"],- ["github", "create"],- ["gitlab", "push"],- ["gitlab", "create"],- ["gitlab", "missing client"],- ])(- "resumes %s publication after %s fails without patching again",- async (provider, failure) => {- const directory = await temporaryDirectory("codex-security-pr-retry-");- const repository = join(directory, "repository");- const remote = join(directory, "remote.git");- const branch = "codex-security/patch-scan-1";- const gitlab = provider === "gitlab";- const origin = "https://gitlab.com/example/subgroup/repository.git";- const url = gitlab- ? "https://gitlab.com/example/subgroup/repository/-/merge_requests/16"- : "https://github.example.test/example/repository/pull/16";- const result = resultWithFindings(["high"]);- let modelCalls = 0;- let pushCalls = 0;- let created = 0;- let failOnce = true;- let publishedUrl = "";- await mkdir(join(repository, "src"), { recursive: true });- const git = repositoryGit(repository);+ test.each([+ ["entry/child", "entry"],+ ["entry", "entry/\uFFFD"],+ ])(+ "publishes two verified turns replacing %s with %s",+ async (from, to) => {+ const fixtures = await publicationFixtures({ [from]: "original\n" }, [+ "high",+ "high",+ ]);+ const { currentDirectory: repository, git, remote, result } = fixtures;+ let expectedTree = "";+ result.findings.findings[0]!.locations[0]!.path = from;+ result.findings.findings[1]!.locations[0]!.path = "entry";+ const outcome = await runWorkflow(+ ["patch", "--scan", "scan-1", "--create-pr", "--json"],+ {+ ...fixtures,+ onCodex: async (args, output) => {+ const [finding] = completePatches(args, output);+ if (finding!.occurrenceId === "occ_1") {+ await writeFile(join(repository, from), "first fix\n");+ } else {+ git("rm", "-f", "--", from);+ await mkdir(dirname(join(repository, to)), { recursive: true });+ await writeFile(join(repository, to), "second fix\n");+ if (from === "entry" && process.platform === "linux") {+ // These two sibling names are identical after UTF-8 decoding.+ const rawName = Buffer.concat([+ Buffer.from(join(repository, "entry") + "/"),+ Buffer.from([0xff]),+ ]);+ await writeFile(rawName, "raw filename fix\n");+ }+ git("add", "--", "entry");+ expectedTree = git("write-tree");+ }+ return 0;+ },+ onRepositoryCommand: (cmd, args, cwd, options) => {+ if (cmd === "gh")+ return args[1] === "create"+ ? "https://github.example.test/example/repository/pull/1"+ : "";+ return runGitRepositoryCommand(cmd, args, cwd, options);+ },+ },+ );- try {- git("init", "--initial-branch=main");- git("config", "user.name", "Synthetic User");- git("config", "user.email", "[email protected]");- git("config", "commit.gpgsign", "false");- await writeFile(join(repository, "src", "finding-1.ts"), "unsafe\n");- await writeFile(join(repository, "unrelated.ts"), "original\n");- git("add", ".");- git("commit", "-m", "Initial synthetic checkout");- git("init", "--bare", remote);- git("remote", "add", "origin", remote);+ expect(outcome.exitCode, outcome.stderr).toBe(0);+ const branch = "codex-security/patch-scan-1";+ const published = repositoryGit(remote);+ expect(published("rev-parse", `${branch}^{tree}`)).toBe(expectedTree);+ expect(published("show", `${branch}:${to}`)).toBe("second fix");+ expect(git("status", "--porcelain")).toBe("");+ },+ );++ test.each([+ ["github", "push"],+ ["github", "create"],+ ["gitlab", "push"],+ ["gitlab", "create"],+ ["gitlab", "missing client"],+ ])(+ "resumes %s publication after %s fails without patching again",+ async (provider, failure) => {+ const checkout = await publicationFixtures(initialPatchFiles);+ const { currentDirectory: repository, git } = checkout;+ const branch = "codex-security/patch-scan-1";+ const gitlab = provider === "gitlab";+ const origin = "https://gitlab.com/example/subgroup/repository.git";+ const url = gitlab+ ? "https://gitlab.com/example/subgroup/repository/-/merge_requests/16"+ : "https://github.example.test/example/repository/pull/16";+ let modelCalls = 0;+ let pushCalls = 0;+ let created = 0;+ let failOnce = true;+ let publishedUrl = "";git("push", "--set-upstream", "origin", "main");const fixtures: Parameters<typeof dependencies>[0] = {- currentDirectory: repository,- result,- onWorkbench: () => savedScan(result, "scan-1", repository),- onCodex: async (args, output) => {+ ...checkout,+ onCodex: (...args) => {modelCalls += 1;- await writeFile(join(repository, "src", "finding-1.ts"), "fixed\n");- completePatches(args, output);- return 0;+ return checkout.onCodex(...args);},- onRepositoryCommand: (command, args) => {+ onRepositoryCommand: (command, args, cwd, options) => {if (command === "git") {if (gitlab && args[0] === "remote") {expect(args).toEqual(["remote", "get-url", "--push", "origin"]);@@ -1722,7 +1815,7 @@ describe("scan and patch workflow", () => {throw new Error("Synthetic push failure");}}- return git(...args);+ return runGitRepositoryCommand(command, args, cwd, options);}expect(command).toBe(gitlab ? "glab" : "gh");if (failure === "missing client" && failOnce) {@@ -1783,11 +1876,9 @@ describe("scan and patch workflow", () => {expect(created).toBe(1);expect(pushCalls).toBe(pushes);expect(modelCalls).toBe(1);- } finally {- await rm(directory, { recursive: true, force: true });- }- },- );+ },+ );+ });test("refuses to resume a missing or changed patch commit", async () => {for (const saved of ["", "saved-commit"]) {@@ -1838,14 +1929,100 @@ describe("scan and patch workflow", () => {}});+ test.each([+ ["worktree", "pre-existing changes"],+ ["index", "pre-existing changes"],+ ["after verification", "differs from the verified changes"],+ ["ignored input", "ignored and untracked before patching"],+ ["intent-to-add", "pre-existing changes"],+ ["earlier turn", "earlier unverified changes"],+ ["parent commit", "base changed during verification"],+ ] as const)(+ "keeps local edits out of saved finding publication: %s",+ async (when, diagnostic) => {+ const path = "app.ts";+ const ignored = when === "ignored input" || when === "intent-to-add";+ const fixtures = await publicationFixtures(+ { [path]: "original\n", ".gitignore": ignored ? path + "\n" : "" },+ when === "earlier turn" ? ["high", "high"] : ["high"],+ );+ const { currentDirectory: repository, git } = fixtures;+ let turn = 0;+ let pushed = false;+ if (when === "intent-to-add") git("add", "-N", "-f", "--", path);+ if (when === "worktree" || when === "index") {+ await writeFile(join(repository, path), "in progress\n");+ if (when === "index") {+ git("add", "--", path);+ await writeFile(join(repository, path), "original\n");+ }+ }++ const outcome = await runWorkflow(+ ["patch", "--scan", "scan-1", "--create-pr", "--json"],+ {+ ...fixtures,+ onCodex: async (_args, output) => {+ turn++;+ const reported =+ when === "earlier turn" && turn === 1 ? "other.ts" : path;+ if (ignored) await writeFile(join(repository, ".gitignore"), "");+ if (when === "parent commit")+ git("commit", "--allow-empty", "-m", "Separate commit");+ if (reported !== path)+ await writeFile(join(repository, path), "first turn\n");+ await writeFile(join(repository, reported), `updated ${turn}\n`, {+ flag: when === "intent-to-add" ? "a" : "w",+ });+ if (when === "intent-to-add") git("add", "--", reported);+ output!.stdout.write(+ JSON.stringify({+ patches: [+ {+ occurrenceId: `occ_${turn}`,+ status: "verified",+ files: [reported],+ verification: "Focused checks pass.",+ },+ ],+ }),+ );+ return 0;+ },+ onRepositoryCommand: async (cmd, args, cwd, options) => {+ if (when === "after verification" && args[0] === "switch") {+ await writeFile(join(repository, path), "later edit\n");+ }+ if (args[0] === "push" || cmd !== "git") pushed = true;+ return runGitRepositoryCommand(cmd, args, cwd, options);+ },+ },+ );+ expect(outcome.exitCode).toBe(2);+ expect(outcome.stderr).toContain(diagnostic);+ expect(pushed).toBe(false);+ if (when !== "after verification")+ expect(git("branch", "--show-current")).toBe("main");+ if (when === "index") expect(git("show", ":" + path)).toBe("in progress");+ if (when === "intent-to-add")+ expect(git("show", ":" + path)).toBe("original\nupdated 1");+ },+ );+test("does not publish blocked, unchanged, or repository-external patches", async () => {for (const status of ["blocked", "no_change", "outside"] as const) {+ const fixtures = await publicationFixtures();let commandStarted = false;const outcome = await runWorkflow(["scan", "--patch", "--create-pr", "--json"],{- result: resultWithFindings(["high"]),- onCodex: (_args, output) => {+ ...fixtures,+ onCodex: async (_args, output) => {+ if (status === "outside")+ await writeFile(+ join(fixtures.currentDirectory, "src", "finding-1.ts"),+ "fixed\n",+ );output?.stdout.write(JSON.stringify({patches: [@@ -1862,13 +2039,11 @@ describe("scan and patch workflow", () => {);return 0;},- onRepositoryCommand: (command, args) => {+ onRepositoryCommand: (command, args, cwd, options) => {commandStarted ||=command !== "git" ||- ["checkout", "commit", "push"].includes(args[0]!);- return status === "outside" && args.includes("--name-only")- ? "src/finding-1.ts\0"- : "";+ ["switch", "commit", "push"].includes(args[0]!);+ return runGitRepositoryCommand(command, args, cwd, options);},},);@@ -1898,11 +2073,11 @@ describe("scan and patch workflow", () => {...(fullOutput ? ["--full-output"] : []),],{- result: resultWithFindings(["high"]),- onRepositoryCommand: (command, args) => {+ ...(await publicationFixtures()),+ onRepositoryCommand: (command, args, cwd, options) => {if (command === "gh")throw new Error("GitHub authentication failed.");- return args.includes("--name-only") ? "src/finding-1.ts\0" : "";+ return runGitRepositoryCommand(command, args, cwd, options);},},);@@ -2199,14 +2374,13 @@ describe("scan and patch workflow", () => {const outcome = await runWorkflow(["scan"],{- result: resultWithFindings(["high"]),- onRepositoryCommand: (command, args) => {- published ||= command === "gh" && args[1] === "create";- return command === "gh" && args[1] === "create"- ? url- : args.includes("--name-only")- ? "src/finding-1.ts\0"- : "";+ ...(await publicationFixtures()),+ onRepositoryCommand: (command, args, cwd, options) => {+ if (command === "git")+ return runGitRepositoryCommand(command, args, cwd, options);+ expect(command).toBe("gh");+ published ||= args[1] === "create";+ return published ? url : "";},},{@@ -2318,21 +2492,20 @@ describe("scan and patch workflow", () => {],["https://gitlab.example.test/example/repository.git", {}, "gh", undefined],] as const)(- "publishes saved-finding patches for origin %s with environment %j using %s",+ "publishes saved patch branches for origin %s with environment %j using %s",async (origin, environment, client, expectedSelector) => {const selector = expectedSelector ?? origin;- const result = resultWithFindings(["high"]);+ const branch = "codex-security/patch-scan-1";const url =client === "glab"? "https://gitlab.example.test/example/repository/-/merge_requests/14": "https://github.example.test/example/repository/pull/14";const publicationCommands: Array<readonly string[]> = [];const outcome = await runWorkflow(- ["patch", "--scan", "scan-1", "--create-pr", "--json"],+ ["patch", "--resume-pr", branch, "--json"],{environment,- result,- onWorkbench: () => savedScan(result),+ currentDirectory: SAVED_REPOSITORY,onRepositoryCommand: (command, args, target) => {expect(target).toBe(SAVED_REPOSITORY);if (command === "git") {@@ -2340,7 +2513,7 @@ describe("scan and patch workflow", () => {expect(args).toEqual(["remote", "get-url", "--push", "origin"]);return origin;}- return args.includes("--name-only") ? "src/finding-1.ts\0" : "";+ return resumedPublicationGit(args);}expect(command).toBe(client);publicationCommands.push(args);@@ -2391,8 +2564,7 @@ describe("scan and patch workflow", () => {`${client === "glab" ? "Merge" : "Pull"} request: ${url}`,);expect(JSON.parse(outcome.stdout)).toMatchObject({- scanId: "scan-1",- pullRequest: { branch: "codex-security/patch-scan-1", url },+ pullRequest: { branch, url },});},);@@ -2408,7 +2580,10 @@ describe("scan and patch workflow", () => {async (provider, mode) => {const gitlab = provider === "gitlab";const resumed = mode !== "fresh";- const result = resultWithFindings(["high"]);+ const fixtures = await publicationFixtures();+ const { git } = fixtures;+ const branch = "codex-security/patch-scan-1";+ const bodyKey = `branch.${branch}.codexSecurityPatchPullRequestBody`;const summary = ["### Regression examples","",@@ -2549,10 +2724,19 @@ describe("scan and patch workflow", () => {savedBody = savedBody.replaceAll("\n", "\r\n");if (mode === "resume embedded CR")savedBody = "Synthetic saved report\n\n>\r>>\n/label ~example";+ if (resumed) {+ git("branch", branch);+ git(+ "config",+ `branch.${branch}.codexSecurityPatchCommit`,+ git("rev-parse", "HEAD"),+ );+ git("config", bodyKey, savedBody);+ }let publishedBody = "";const outcome = await runWorkflow(resumed- ? ["patch", "--resume-pr", "codex-security/patch-scan-1", "--json"]+ ? ["patch", "--resume-pr", branch, "--json"]: ["patch","--scan",@@ -2562,28 +2746,12 @@ describe("scan and patch workflow", () => {"--json",],{- result,- onWorkbench: () => savedScan(result),- onRepositoryCommand: (command, args) => {+ ...fixtures,+ onRepositoryCommand: (command, args, cwd, options) => {if (command === "git") {- if (args[0] === "config") {- if (args.at(-1)?.endsWith(".codexSecurityPatchCommit"))- return "synthetic-commit";- if (- args.includes("--get") &&- args.at(-1)?.endsWith(".codexSecurityPatchPullRequestBody")- )- return savedBody;- if (args[2]?.endsWith(".codexSecurityPatchPullRequestBody"))- savedBody = args[3]!;- }- if (args[0] === "rev-parse" && args.includes("--verify"))- return "synthetic-commit";- return args[0] === "remote"- ? `https://${provider}.com/example/repository.git`- : args.includes("--name-only")- ? "src/finding-1.ts\0"- : "";+ if (args[0] === "remote")+ return `https://${provider}.com/example/repository.git`;+ return runGitRepositoryCommand(command, args, cwd, options);}expect(command).toBe(gitlab ? "glab" : "gh");if (args[1] === "list") return "";@@ -2603,6 +2771,7 @@ describe("scan and patch workflow", () => {},);expect(outcome.exitCode, outcome.stderr).toBe(0);+ if (!resumed) savedBody = git("config", "--get", bodyKey);const fence = mode === "resume embedded CR" ? ">>>>" : ">>>>>>";expect(publishedBody).toBe(gitlab ? `${fence}\n${savedBody}` : savedBody);if (!resumed)@@ -2613,30 +2782,26 @@ describe("scan and patch workflow", () => {test.each([false, true])("uses the GitLab project filter result before publishing (existing MR: %j)",async (hasExisting) => {- const result = resultWithFindings(["high"]);const origin = "ssh://[email protected]/example/subgroup/repository.git";const branch = "codex-security/patch-scan-1";const url ="https://gitlab.com/example/subgroup/repository/-/merge_requests/14";const commands: string[] = [];const outcome = await runWorkflow(- ["patch", "--scan", "scan-1", "--create-pr", "--json"],+ ["patch", "--resume-pr", branch, "--json"],{- result,- onWorkbench: () => savedScan(result),onRepositoryCommand: (command, args) => {if (command === "git") {if (args[0] === "remote") return origin;if (args[0] === "push") {expect(args).toEqual(["push",- "--set-upstream","origin",- branch,+ `verified-commit:refs/heads/${branch}`,]);commands.push("push");}- return args.includes("--name-only") ? "src/finding-1.ts\0" : "";+ return resumedPublicationGit(args);}expect(command).toBe("glab");commands.push(args[1]!);@@ -2852,7 +3017,8 @@ const runGitRepositoryCommand: NonNullable<const result = gitText(args, {cwd: workingDirectory,env: { ...process.env, ...options?.environment },- stdio: ["ignore", "pipe", "pipe"],+ input: options?.input,+ stdio: ["pipe", "pipe", "pipe"],});return options?.trim === false ? result : result.trim();};