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): preserve literal paths when replaying saved scopes (#1812)

Michael D'Angelo1e5a5f322c22
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index f37c9bbaa..ae6279dd2 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -1511,6 +1511,9 @@ scan and custom-validation prompts when the original used them:
codex-security scans rerun SCAN_ID --scan-prompt-file instructions.md
```
+Saved path scopes remain relative to the repository. Names such as `~` and
+`~/source.ts` are replayed literally; they do not select the home directory.
+
A rerun creates a new scan. When the original has saved input identities, the
rerun reports changes to the scan instructions or knowledge base. `scans show`
includes the saved scope, runtime and Deep Scan settings, instruction digest,
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 727dc0bd0..896dff43d 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -6733,9 +6733,14 @@ async function prepareScanArgumentsFromRecipe(
repository,
auth: auth.data ?? DEFAULT_SCAN_AUTH,
cyberAccessProgram: cyberAccessProgram.data,
+ // Saved scopes contain literal repository names, not home-relative inputs.
target:
paths.length > 0
- ? paths
+ ? paths.map((path) =>
+ path === "~" || path.startsWith("~/") || path.startsWith("~\\")
+ ? `./${path}`
+ : path,
+ )
: kind === "refs"
? DiffTarget.refs({ base: reference!, head: head ?? "HEAD" })
: kind === "working_tree"
diff --git a/sdk/typescript/tests-ts/cli-workbench.test.ts b/sdk/typescript/tests-ts/cli-workbench.test.ts
index 9267eb73d..e14000a6f 100644
--- a/sdk/typescript/tests-ts/cli-workbench.test.ts
+++ b/sdk/typescript/tests-ts/cli-workbench.test.ts
@@ -6,6 +6,7 @@ import { describe, expect, test, mock, spyOn } from "bun:test";
import type { CodexSecurityConfig, JsonObject } from "../src/index.js";
import { DiffTarget, type ScanOptions } from "../src/index.js";
import { main } from "../src/cli.js";
+import { normalizeTarget } from "../src/targets.js";
import {
matchScanFindings,
type ScanComparisonInput,
@@ -1638,6 +1639,116 @@ describe("CLI workbench", () => {
},
);
+ test.each([
+ ["standard", "rerun"],
+ ["deep", "rerun"],
+ ["deep", "resume"],
+ ] as const)(
+ "%s %s preserves literal paths from a saved scope",
+ async (mode, command) => {
+ const root = await temporaryDirectory("saved-literal-paths-");
+ try {
+ const repository = join(root, "repository");
+ const outside = join(root, "outside");
+ await mkdir(join(repository, "~"), { recursive: true });
+ await mkdir(join(repository, "nested"));
+ await mkdir(outside);
+ const files = ["~/source.ts", "~name", "nested/~", "with space"];
+ for (const file of files)
+ await writeFile(join(repository, file), "synthetic source\n");
+ const paths = ["~", ...files, "."];
+ const target = await normalizeTarget(
+ repository,
+ paths.map((path) => `./${path}`),
+ );
+ expect(target).toEqual({ kind: "paths", paths });
+ const recipe = {
+ repository,
+ target: { ...target, paths: [...target.paths] },
+ mode,
+ config: {},
+ };
+ const originalRecipe = JSON.stringify(recipe);
+ const onTurn =
+ mock<(repository: string, options: ScanOptions) => void>();
+ const { runCli, stderr } = createCliTest(main);
+ expect(
+ await runCli(
+ ["scans", command, "saved-literal", "--json"],
+ dependencies({
+ currentDirectory: outside,
+ onTurn,
+ onWorkbench: () => ({
+ scanId: "saved-literal",
+ scanDir: join(root, "scan"),
+ recipe,
+ }),
+ }),
+ ),
+ stderr.text(),
+ ).toBe(0);
+ expect(onTurn).toHaveBeenCalledTimes(1);
+ expect(onTurn.mock.lastCall![0]).toBe(repository);
+ const replayTarget = onTurn.mock.lastCall![1].target!;
+ // Check the arguments before normalization can consult the real home.
+ expect(replayTarget).toEqual([
+ "./~",
+ "./~/source.ts",
+ "~name",
+ "nested/~",
+ "with space",
+ ".",
+ ]);
+ expect(await normalizeTarget(repository, replayTarget)).toEqual(target);
+ expect(JSON.stringify(recipe)).toBe(originalRecipe);
+
+ await rm(join(repository, "~", "source.ts"));
+ await expect(normalizeTarget(repository, replayTarget)).rejects.toThrow(
+ "Path target does not exist: ./~/source.ts",
+ );
+ await rm(join(repository, "~"), { recursive: true });
+ await symlink(
+ outside,
+ join(repository, "~"),
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ await expect(normalizeTarget(repository, replayTarget)).rejects.toThrow(
+ "Path target is outside the repository: ./~",
+ );
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+ },
+ );
+
+ test("replays saved backslash names without home expansion", async () => {
+ const onTurn = mock<(repository: string, options: ScanOptions) => void>();
+ expect(
+ await runCapturedCli(
+ main,
+ ["scans", "rerun", "scan-original"],
+ dependencies({
+ onTurn,
+ onWorkbench: () =>
+ savedRecipe({}, { kind: "paths", paths: ["~\\source.ts"] }),
+ }),
+ ),
+ ).toBe(0);
+ expect(onTurn.mock.lastCall![1].target).toEqual(["./~\\source.ts"]);
+ });
+
+ test("preserves home syntax for fresh scan path arguments", async () => {
+ const onTurn = mock<(repository: string, options: ScanOptions) => void>();
+ expect(
+ await runCapturedCli(
+ main,
+ ["scan", "/current/repository", "--path", "~", "--path", "~/source.ts"],
+ dependencies({ onTurn }),
+ ),
+ ).toBe(0);
+ expect(onTurn.mock.lastCall![1].target).toEqual(["~", "~/source.ts"]);
+ });
+
test("reruns canonical recipes with exact config, policy, plugin, and lineage", async () => {
const onConfig = mock<(config: CodexSecurityConfig) => void>();
const onTurn = mock<(repository: string, options: ScanOptions) => void>();