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(scan): verify assumed-unchanged files for committed diffs (#2554)

Michael D'Angelo4ede567343c9
diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.py
index 85fc06b28..5f62cf3a0 100644
--- a/plugins/codex-security/scripts/workbench_target.py
+++ b/plugins/codex-security/scripts/workbench_target.py
@@ -282,6 +282,7 @@ def git_command(
work_tree: Path | None = None,
stdout_file: BinaryIO | None = None,
environment_overrides: dict[str, str] | None = None,
+ inherit_git_environment: bool = True,
offline: bool = False,
) -> subprocess.CompletedProcess[str] | subprocess.CompletedProcess[bytes]:
"""Run Git against target and return the completed process with its exit status.
@@ -292,7 +293,13 @@ def git_command(
"""
if (git_dir is None) != (work_tree is None):
raise ValueError("git_dir and work_tree must be provided together")
- environment = os.environ.copy()
+ environment = (
+ os.environ.copy()
+ if inherit_git_environment
+ else {
+ name: value for name, value in os.environ.items() if not name.upper().startswith("GIT_")
+ }
+ )
for name in GIT_REPOSITORY_ENVIRONMENT:
environment.pop(name, None)
environment["GIT_LITERAL_PATHSPECS"] = "1"
@@ -660,14 +667,18 @@ def require_scan_target_unchanged(scan: sqlite3.Row) -> None:
if scan["diff_target_kind"] == "working_tree":
expected_digest = scan["diff_content_digest"]
elif scan["diff_target_kind"] in {"commit", "range"}:
- tracked = git_bytes(Path(scan["target_path"]), "ls-files", "-t", "-z")
+ target = Path(scan["target_path"])
+ tracked = git_bytes(target, "ls-files", "-v", "-z")
if tracked is None:
raise SystemExit("Could not inspect tracked files in the scan target.")
- if any(entry.startswith(b"S ") for entry in tracked.split(b"\0")):
+ entries = tracked.split(b"\0")
+ if any(entry.startswith((b"S ", b"s ")) for entry in entries):
raise SystemExit(
"Committed-diff scans require a full repository checkout. "
"Sparse checkouts are not supported; materialize skipped tracked files and retry."
)
+ if any(entry.startswith(b"h ") for entry in entries):
+ verify_assumed_unchanged_files(target)
expected_digest = clean_worktree_content_digest()
else:
expected_digest = scan["target_snapshot_digest"]
@@ -678,6 +689,39 @@ def require_scan_target_unchanged(scan: sqlite3.Row) -> None:
)
+def verify_assumed_unchanged_files(target: Path) -> None:
+ index = git_output(target, "rev-parse", "--path-format=absolute", "--git-path", "index")
+ if index is None:
+ raise SystemExit("Could not locate the Git index for the scan target.")
+ with tempfile.TemporaryDirectory(prefix="codex-security-git-index-") as directory:
+ # Refresh a private index without running checkout hooks or updating its shared index.
+ index_copy = Path(directory) / "index"
+ shutil.copyfile(index, index_copy)
+ result = git_command(
+ target,
+ "-c",
+ f"core.hooksPath={directory}",
+ "-c",
+ "core.splitIndex=false",
+ "update-index",
+ "--really-refresh",
+ text=False,
+ environment_overrides={"GIT_INDEX_FILE": str(index_copy)},
+ # A repository's clean filter can run while Git compares working-tree bytes.
+ inherit_git_environment=False,
+ offline=True,
+ )
+ if result.returncode == 1:
+ raise SystemExit(
+ "A tracked file marked assume-unchanged changed since the scan. "
+ "Run a new scan before validating its findings."
+ )
+ if result.returncode != 0:
+ raise SystemExit(
+ os.fsdecode(result.stderr) or "Could not refresh the scan target's Git index."
+ )
+
+
def worktree_content_digest_for_context(
repository: Path,
pathspec: str,
diff --git a/plugins/codex-security/tests/test_workbench_scan_validation.py b/plugins/codex-security/tests/test_workbench_scan_validation.py
index 877a9f0f8..0901c9e73 100644
--- a/plugins/codex-security/tests/test_workbench_scan_validation.py
+++ b/plugins/codex-security/tests/test_workbench_scan_validation.py
@@ -10,7 +10,7 @@ from pathlib import Path
import pytest
from test_workbench_scan_history import create_cli_scan
-from workbench_test_support import get_scan, initialize_git_repository
+from workbench_test_support import configure_git_command, get_scan, initialize_git_repository
def start_validation_scan(
@@ -119,6 +119,53 @@ def test_check_scan_target_rejects_changed_revision(
workbench_api["remediation_checkout_snapshot"](stored_scan)
+def test_check_committed_diff_target_rechecks_assume_unchanged_files(tmp_path: Path) -> None:
+ repository = tmp_path / "repository"
+ initialize_git_repository(repository)
+ state_dir, scan_id = start_validation_scan(
+ tmp_path, repository, target={"kind": "refs", "paths": [], "base": "HEAD", "head": "HEAD"}
+ )
+ source = repository / "README.md"
+ original = source.read_bytes()
+ subprocess.run(
+ ["git", "update-index", "--assume-unchanged", "--", source.name], cwd=repository, check=True
+ )
+ assert get_scan(state_dir, scan_id, "--check-target")["scan"]["scanId"] == scan_id
+
+ (repository / ".git" / "info" / "attributes").write_text("README.md filter=fixture\n")
+ clean_filter = tmp_path / "clean_filter.py"
+ clean_filter.write_text(
+ "import os, sys\n"
+ "from pathlib import Path\n"
+ "if os.environ.get('GIT_INDEX_FILE'):\n"
+ " inherited = os.environ.get('GIT_CONFIG_VALUE_0') == 'synthetic-fixture-value'\n"
+ " Path(__file__).with_suffix('.result').write_text('inherited' if inherited else 'isolated')\n"
+ "sys.stdout.buffer.write(sys.stdin.buffer.read())\n"
+ )
+ configure_git_command(repository, "filter.fixture.clean", clean_filter)
+ # Matching sizes force Git to compare the filtered contents.
+ source.write_bytes(b"x" * len(original))
+ os.utime(source, (1, 1))
+ changed = get_scan(
+ state_dir,
+ scan_id,
+ "--check-target",
+ check=False,
+ environment={
+ "GIT_CONFIG_COUNT": "1",
+ "GIT_CONFIG_KEY_0": "http.extraHeader",
+ "GIT_CONFIG_VALUE_0": "synthetic-fixture-value",
+ },
+ )
+ assert changed["returncode"] != 0
+ assert "changed" in changed["stderr"]
+ assert clean_filter.with_suffix(".result").read_text() == "isolated"
+ tracked = subprocess.check_output(["git", "ls-files", "-v", "--", source.name], cwd=repository)
+ assert tracked.split(maxsplit=1)[0] == b"h"
+ source.write_bytes(original)
+ assert get_scan(state_dir, scan_id, "--check-target")["scan"]["scanId"] == scan_id
+
+
def test_check_committed_diff_target_rejects_sparse_checkout_after_scan(tmp_path: Path) -> None:
repository = tmp_path / "repository"
initialize_git_repository(repository)
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index c233e6882..949ae8438 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -509,7 +509,8 @@ be clean and at the commit recorded by the parent.
Committed-diff scans require a clean, full checkout at the requested head
revision, including when using `--validate`. A different checkout revision or
local changes are rejected before scanning; check out the requested head and
-commit or stash local changes before retrying.
+commit or stash local changes before retrying. The check also verifies tracked
+files marked `assume-unchanged` and preserves their Git index flags.
`--owner-suggestions` writes optional `owner-suggestions.json` after a completed
committed Git scan, using the same checkout, model, and reasoning effort. Owner
diff --git a/sdk/typescript/src/targets.ts b/sdk/typescript/src/targets.ts
index 5009dd0ff..5e6a68fd4 100644
--- a/sdk/typescript/src/targets.ts
+++ b/sdk/typescript/src/targets.ts
@@ -2,8 +2,16 @@ import { isNonEmptyString } from "./value.js";
import { isUtf8 } from "node:buffer";
import { execFile as execFileCallback } from "node:child_process";
import { realpathSync, type Stats } from "node:fs";
-import { lstat, readFile, realpath, stat } from "node:fs/promises";
-import { homedir } from "node:os";
+import {
+ copyFile,
+ lstat,
+ mkdtemp,
+ readFile,
+ realpath,
+ rm,
+ stat,
+} from "node:fs/promises";
+import { homedir, tmpdir } from "node:os";
import {
basename,
dirname,
@@ -675,12 +683,60 @@ export async function validateCommittedDiffCheckout(
);
}
- const tracked = await gitOutput(repository, ["ls-files", "-t", "-z"], signal);
- if (tracked.split("\0").some((entry) => entry.startsWith("S "))) {
+ const tracked = (
+ await gitOutput(repository, ["ls-files", "-v", "-z"], signal)
+ ).split("\0");
+ if (tracked.some((entry) => /^[Ss] /u.test(entry))) {
throw new InvalidTargetError(
"Committed-diff scans require a full repository checkout. Sparse checkouts are not supported; materialize skipped tracked files and retry.",
);
}
+ if (tracked.some((entry) => entry.startsWith("h "))) {
+ await verifyAssumedUnchangedFiles(repository, signal);
+ }
+}
+
+async function verifyAssumedUnchangedFiles(
+ repository: string,
+ signal?: AbortSignal,
+): Promise<void> {
+ const index = await gitOutput(
+ repository,
+ ["rev-parse", "--path-format=absolute", "--git-path", "index"],
+ signal,
+ );
+ // Git changes to the repository before resolving GIT_INDEX_FILE, even when TMPDIR is relative.
+ const directory = await mkdtemp(
+ join(await realpath(tmpdir()), "codex-security-git-index-"),
+ );
+ const copy = join(directory, "index");
+ try {
+ // Git refreshes stat data and flags. Keep that write and any hooks out of the checkout.
+ await copyFile(index, copy);
+ await gitOutput(
+ repository,
+ [
+ "-c",
+ `core.hooksPath=${directory}`,
+ "-c",
+ "core.splitIndex=false",
+ "update-index",
+ "--really-refresh",
+ ],
+ signal,
+ { GIT_INDEX_FILE: copy },
+ );
+ } catch (error) {
+ if (error instanceof Error && "code" in error && error.code === 1) {
+ throw new InvalidTargetError(
+ "Committed-diff scans require a clean repository checkout. A tracked file marked assume-unchanged has changed on disk.",
+ { cause: error },
+ );
+ }
+ throw error;
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
}
export function validateMode(target: NormalizedTarget, mode: ScanMode): void {
diff --git a/sdk/typescript/tests-ts/targets.test.ts b/sdk/typescript/tests-ts/targets.test.ts
index 4a824872a..47e5a3dee 100644
--- a/sdk/typescript/tests-ts/targets.test.ts
+++ b/sdk/typescript/tests-ts/targets.test.ts
@@ -14,7 +14,7 @@ import {
writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
-import { delimiter, join } from "node:path";
+import { delimiter, join, relative } from "node:path";
import { fileURLToPath } from "node:url";
import { afterEach, describe, expect, test } from "bun:test";
import {
@@ -31,6 +31,7 @@ import {
gitMarkerRoot,
isGitMetadataDirectory,
gitProtectionRoots,
+ validateCommittedDiffCheckout,
} from "../src/targets.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
import { runCommand } from "./support/shell.js";
@@ -161,7 +162,10 @@ function git(repo: string, ...args: string[]): string {
return execFileSync("git", args, { cwd: repo, encoding: "utf8" }).trim();
}
-function validateCommittedDiffWithCredentialConfig(repo: string) {
+function validateCommittedDiffWithCredentialConfig(
+ repo: string,
+ environment: NodeJS.ProcessEnv = {},
+) {
return spawnSync(
process.execPath,
[
@@ -174,6 +178,7 @@ function validateCommittedDiffWithCredentialConfig(repo: string) {
encoding: "utf8",
env: {
...process.env,
+ ...environment,
GIT_CONFIG_COUNT: "1",
GIT_CONFIG_KEY_0: "http.extraHeader",
GIT_CONFIG_VALUE_0: "SYNTHETIC_GIT_CREDENTIAL",
@@ -569,6 +574,42 @@ describe("scan target normalization", () => {
}
});
+ test("checks assumed-unchanged files against a committed diff without changing index flags", async () => {
+ const repo = await repository();
+ const name = "src/app.ts";
+ const file = join(repo, name);
+ const original = await readFile(file);
+ const target = await normalizeTarget(
+ repo,
+ DiffTarget.refs({ base: "HEAD" }),
+ );
+ git(repo, "update-index", "--assume-unchanged", "--", name);
+ const hook = join(repo, ".git", "hooks", "post-index-change");
+ await writeFile(
+ hook,
+ '#!/bin/sh\nif test -n "$GIT_INDEX_FILE"; then echo ran >.git/refresh-hook-ran; fi\n',
+ { mode: 0o755 },
+ );
+ await validateCommittedDiffCheckout(repo, target);
+
+ await writeFile(file, "export const changed = true;\n");
+ expect(git(repo, "status", "--porcelain")).toBe("");
+ await expect(validateCommittedDiffCheckout(repo, target)).rejects.toThrow(
+ "clean repository checkout",
+ );
+ if (process.platform !== "win32") {
+ const child = validateCommittedDiffWithCredentialConfig(repo, {
+ TMPDIR: relative(process.cwd(), join(repo, "..")),
+ });
+ expect(child.status).toBe(1);
+ expect(child.stderr).toContain("clean repository checkout");
+ }
+ expect(git(repo, "ls-files", "-v", "--", name)).toBe(`h ${name}`);
+ await writeFile(file, original);
+ await validateCommittedDiffCheckout(repo, target);
+ expect(existsSync(join(repo, ".git", "refresh-hook-ran"))).toBe(false);
+ });
+
test("does not expose Git configuration to repository fsmonitor hooks", async () => {
const repo = await repository();
const root = join(repo, "..");