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(cli): keep hosted patches uncommitted and report revision changes (#2583)
mluck-openaid3b500c17638
diff --git a/sdk/typescript/docs/hosted-remediation.md b/sdk/typescript/docs/hosted-remediation.mdindex 45a4e9ded..df6a5f892 100644--- a/sdk/typescript/docs/hosted-remediation.md+++ b/sdk/typescript/docs/hosted-remediation.md@@ -7,10 +7,12 @@ separate from the Standard scan API and does not create scan/workbench state.The host prepares an isolated checkout, freezes its HEAD, supplies the originalfinding evidence and repository instructions, and enforces the requested-sandbox. For verification, the host must first apply the exact candidate patch-to its recorded base. The API does not apply a patch or infer a branch from the-finding. It checks HEAD before and after execution; the host owns checking the-candidate's bytes/digest and verifying that verification did not change them.+sandbox. For patching, the agent must edit the supplied checkout and leave its+changes uncommitted so the host can capture the diff. For verification, the+host must first apply the exact candidate patch to its recorded base. The API+does not apply a patch or infer a branch from the finding. It checks HEAD+before dispatch and before accepting a completed execution. The host checks+the candidate's bytes/digest and verifies that verification did not change them.In this TypeScript host integration, `executor` is your application's`RemediationExecutor`. It runs the request in the prepared checkout and returns@@ -131,6 +133,13 @@ the final execution receipt so usage and session identity survive cancellation.The transport emits one terminal result. Preparation failures are JSON-RPCerrors; a mismatched HEAD includes `error.data.reason: "revision_mismatch"`.+If a completed execution ends at a different commit, the terminal `result` has+`status: "failed"`, `error.code: "INVALID_RESULT"`, and+`error.reason: "revision_changed"`. It retains the execution receipt and omits+the patch or verification outcome. To retry, start a new attempt in a fresh+checkout at `result.revision`. If Git cannot resolve HEAD after execution, the+result also fails with `INVALID_RESULT` and omits `error.reason`.+To run the deterministic installed-package smoke check, use a source checkoutof this repository and run the following from `sdk/typescript`. The script isnot included in the npm package and needs no model credentials:diff --git a/sdk/typescript/src/hosted-remediation.ts b/sdk/typescript/src/hosted-remediation.tsindex 902abbd81..94f27e833 100644--- a/sdk/typescript/src/hosted-remediation.ts+++ b/sdk/typescript/src/hosted-remediation.ts@@ -89,7 +89,7 @@ export interface HostedRemediationResult {execution?: RemediationExecutionResult;patch?: FindingPatch;verification?: FindingVerification;- error?: { code: string; message: string };+ error?: { code: string; message: string; reason?: "revision_changed" };}export class HostedRemediationPreparationError extends Error {@@ -117,7 +117,7 @@ export async function runHostedRemediation(throw new HostedRemediationPreparationError();const pluginRoot = await bundledPluginRoot();const { version: pluginVersion } = await pluginMetadata(pluginRoot);- const prompt = await remediationPrompt({+ let prompt = await remediationPrompt({operation: input.operation,pluginRoot,contents: [@@ -129,6 +129,13 @@ export async function runHostedRemediation(validationInstructions: input.validationInstructions,verificationMode: input.verificationMode,});+ if (input.operation === "patch") {+ prompt = [+ "Work in the supplied checkout and leave all patch edits uncommitted for the host to capture, even if repository or validation instructions ask you to commit or use a separate worktree.",+ `Keep HEAD at the pinned revision ${input.revision}. Before returning, check git rev-parse HEAD and git status --short to confirm the revision and the files you report.`,+ prompt,+ ].join("\n");+ }const target = {version: 1 as const,operation: input.operation,@@ -170,10 +177,22 @@ export async function runHostedRemediation();if (execution.status !== "completed")return { ...base, status: execution.status, execution };- if ((await repositoryRevision(repository)) !== input.revision)- throw new Error(- "Checkout changed from the frozen revision during execution.",- );+ const revision = await repositoryRevision(repository);+ if (revision === null)+ throw new Error("Could not resolve HEAD after remediation execution.");+ if (revision !== input.revision) {+ return {+ ...base,+ status: "failed",+ execution,+ error: {+ code: "INVALID_RESULT",+ reason: "revision_changed",+ message:+ "Checkout changed from the frozen revision during execution.",+ },+ };+ }const response = execution.finalResponse ?? "";const outcome =input.operation === "patch"diff --git a/sdk/typescript/tests-ts/hosted-remediation.test.ts b/sdk/typescript/tests-ts/hosted-remediation.test.tsindex 9e3ba41e3..b52adc406 100644--- a/sdk/typescript/tests-ts/hosted-remediation.test.ts+++ b/sdk/typescript/tests-ts/hosted-remediation.test.ts@@ -233,6 +233,7 @@ test.each(["patch", "verify"] as const)(});expect(result.status).toBe("failed");expect(result.error?.code).toBe("INVALID_RESULT");+ expect(result.error?.reason).toBeUndefined();expect(result.patch).toBeUndefined();expect(result.verification).toBeUndefined();expect(result.execution).toMatchObject({@@ -242,7 +243,7 @@ test.each(["patch", "verify"] as const)(},);-test("a changed HEAD fails before execution, and cannot be accepted afterward", async () => {+test("a mismatched HEAD fails before execution", async () => {const input = await fixture();const never = mock(rejecting("Must not execute"));await expect(@@ -254,17 +255,50 @@ test("a changed HEAD fails before execution, and cannot be accepted afterward",),).rejects.toMatchObject({ reason: "revision_mismatch" });expect(never).not.toHaveBeenCalled();+});++test.each(["patch", "verify"] as const)(+ "a different commit invalidates a completed %s and preserves the receipt",+ async (operation) => {+ const input = await fixture(operation);+ const run = mock<RemediationExecutor["run"]>(async (request) => {+ writeSource(+ request.repository,+ "guard.ts",+ "export const allowed = true;\n",+ );+ git(request.repository, "commit", "-qam", "changed");+ return receipt(request);+ });+ const result = await runHostedRemediation(input, { executor: { run } });+ expect(run).toHaveBeenCalledTimes(1);+ expect(result.status).toBe("failed");+ expect(result.error).toMatchObject({+ code: "INVALID_RESULT",+ reason: "revision_changed",+ });+ expect(result.execution).toEqual(receipt(run.mock.calls[0]![0]));+ expect(result.patch).toBeUndefined();+ expect(result.verification).toBeUndefined();+ },+);++test("an unreadable HEAD fails without reporting a different commit", async () => {+ const input = await fixture();const result = await runHostedRemediation(input, {executor: {run: async (request) => {- git(input.repository, "commit", "--allow-empty", "-qm", "changed");+ git(request.repository, "symbolic-ref", "HEAD", "refs/heads/unborn");return receipt(request);},},});expect(result.status).toBe("failed");- expect(result.error?.message).toContain("frozen revision");+ expect(result.error?.code).toBe("INVALID_RESULT");+ expect(result.error?.reason).toBeUndefined();+ expect(result.error?.message).toContain("resolve HEAD");expect(result.execution?.sessionId).toBe("host-session");+ expect(result.patch).toBeUndefined();});test.each(["failed", "canceled", "acceptance_unknown"] as const)(@@ -289,12 +323,16 @@ test.each(["failed", "canceled", "acceptance_unknown"] as const)(test("a mismatched execution receipt is retained but never accepted", async () => {const result = await runHostedRemediation(await fixture(), {executor: {- run: async (request) => receipt(request, { requestId: "other" }),+ run: async (request) => {+ git(request.repository, "commit", "--allow-empty", "-qm", "changed");+ return receipt(request, { requestId: "other" });+ },},});expect(result.status).toBe("failed");expect(result.execution?.sessionId).toBe("host-session");expect(result.error?.message).toContain("does not match");+ expect(result.error?.reason).toBeUndefined();});test("malformed protocol input returns one error without dispatch", async () => {@@ -312,17 +350,36 @@ test("malformed protocol input returns one error without dispatch", async () =>input.destroy();});-test.each([- ["patch", false],- ["verify", false],- ["verify", true],-] as const)(- "CLI protocol pipes execute %s with cancellation=%p",- async (operation, cancel) => {+test("patch --host leaves the reported changes in the supplied checkout", async () => {+ const input = await fixture();+ await exerciseRemediationProtocol(+ process.execPath,+ join(import.meta.dirname, "../src/cli.ts"),+ input,+ {+ async onExecution(request) {+ expect(request.prompt).toContain("supplied checkout");+ expect(request.prompt).toContain("uncommitted");+ expect(request.prompt).toContain(input.revision);+ writeSource(+ request.repository,+ "guard.ts",+ "export const allowed = true;\n",+ );+ },+ },+ );+ expect(git(input.repository, "rev-parse", "HEAD")).toBe(input.revision);+ expect(git(input.repository, "diff", "--name-only", "HEAD")).toBe("guard.ts");+});++test.each([false, true])(+ "CLI protocol pipes execute verify with cancellation=%p",+ async (cancel) => {await exerciseRemediationProtocol(process.execPath,join(import.meta.dirname, "../src/cli.ts"),- await fixture(operation),+ await fixture("verify"),{ cancel },);},