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(targets): report a failed ignored-but-tracked file listing (#864)

kevin734ae77cb73a
diff --git a/plugins/codex-security/mcp-app/src/helpers/generate-inventory.ts b/plugins/codex-security/mcp-app/src/helpers/generate-inventory.ts
index 3bff94588..4fdd1d00c 100644
--- a/plugins/codex-security/mcp-app/src/helpers/generate-inventory.ts
+++ b/plugins/codex-security/mcp-app/src/helpers/generate-inventory.ts
@@ -32,10 +32,9 @@ import {
import {
blobSamples,
changedPaths,
- decodeGitPath,
directoryPaths,
encodeGitPath,
- git,
+ ignoredTrackedPaths,
runRipgrep,
} from "./inventory-git";
import { PREVIEW_BYTES, previewForBytes, sampleFile } from "./source-preview";
@@ -296,31 +295,26 @@ export async function generateInventory(
`ripgrep ${result.signal ? `terminated by ${result.signal}` : `exited with status ${result.status}`}${result.stderr.trim() ? `: ${result.stderr.trim()}` : ""}`,
);
let bytes = result.stdout;
- if ([...ancestors(repo)].some((path) => exists(append(path, ".git")))) {
- const tracked = await git(repo, [
- "ls-files",
- "--cached",
- "--ignored",
- "--exclude-standard",
- "-z",
- "--",
- requested,
- ]).catch((error: unknown) => {
- if (error instanceof Error && "code" in error)
- return { status: 127, stdout: Buffer.alloc(0) };
- throw error;
- });
- if (tracked.status === 0) {
- const prefix =
- requested === "." || requested.startsWith("./") ? "./" : "";
- const paths = decodeGitPath(tracked.stdout)
- .split("\0")
- .filter((path) => path && regular(append(repo, path)));
- bytes = Buffer.concat([
- bytes,
- encodeGitPath(paths.map((path) => prefix + path + "\0").join("")),
- ]);
- }
+ const inWorktree = [...ancestors(repo)].some((path) => {
+ const marker = append(path, ".git");
+ // An empty .git directory can be a sandbox placeholder.
+ return (
+ exists(marker) &&
+ (!directory(marker) ||
+ exists(append(marker, "HEAD")) ||
+ regular(append(marker, "index")))
+ );
+ });
+ if (inWorktree) {
+ const paths = (await ignoredTrackedPaths(repo, requested)).filter(
+ (path) => regular(append(repo, path)),
+ );
+ const prefix =
+ requested === "." || requested.startsWith("./") ? "./" : "";
+ bytes = Buffer.concat([
+ bytes,
+ encodeGitPath(paths.map((path) => prefix + path + "\0").join("")),
+ ]);
}
for (const path of decodePosixBytes(bytes).split("\0").filter(Boolean)) {
if (path.split("/").some((part) => sourceMetadata.has(part))) continue;
diff --git a/plugins/codex-security/mcp-app/src/helpers/inventory-git.ts b/plugins/codex-security/mcp-app/src/helpers/inventory-git.ts
index 3e67743a5..d6df55961 100644
--- a/plugins/codex-security/mcp-app/src/helpers/inventory-git.ts
+++ b/plugins/codex-security/mcp-app/src/helpers/inventory-git.ts
@@ -427,6 +427,13 @@ async function collect({
};
}
+const unavailableGit: ToolResult = {
+ status: 127,
+ signal: null,
+ stdout: Buffer.alloc(0),
+ stderr: "",
+};
+
export async function git(repo: string, args: string[]) {
try {
const execution = gitProcess(repo, args);
@@ -437,7 +444,7 @@ export async function git(repo: string, args: string[]) {
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
}
- return { status: 127, signal: null, stdout: Buffer.alloc(0), stderr: "" };
+ return unavailableGit;
}
export function decodeGitPath(data: Buffer): string {
@@ -491,6 +498,26 @@ function requireSuccess(result: ToolResult): Buffer {
return result.stdout;
}
+export async function ignoredTrackedPaths(repo: string, scope: string) {
+ try {
+ const result = await git(repo, [
+ "ls-files",
+ "--cached",
+ "--ignored",
+ "--exclude-standard",
+ "-z",
+ "--",
+ scope,
+ ]);
+ return result === unavailableGit ? [] : paths(requireSuccess(result));
+ } catch (error) {
+ throw new Error(
+ `git ls-files: ${error instanceof Error ? error.message : String(error)}`,
+ { cause: error },
+ );
+ }
+}
+
export async function directoryPaths(
target: string,
): Promise<string[] | undefined> {
diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts
index f0a0f7053..2c3f085eb 100644
--- a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts
+++ b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts
@@ -50,7 +50,7 @@ try {
await testPrepareUsesTheExistingStandardGenerator();
if (!["win32", "darwin"].includes(process.platform))
await testNonUtf8InventoryPaths();
- await testPrepareListsIgnoredTrackedFilesOnce();
+ await testPrepareWithIgnoredTrackedFiles();
await testPrepareExcludesGitMetadata();
await testPrepareUsesOnlyAuthoritativeDiffChanges();
await testPrepareIncludesPerlSourceWithMixedEncodings();
@@ -95,6 +95,8 @@ async function testSchemasAreBoundAndExact() {
async function testPrepareUsesTheExistingStandardGenerator() {
const fixture = await createFixture("standard repository");
+ // Sandbox writable roots can have empty .git placeholders above an ordinary directory.
+ await mkdir(path.join(fixture.root, ".git"));
await fixture.writeRepositoryFile("src/a.ts", "export const a = 1;\n");
await fixture.writeRepositoryFile("-source.py", "source\n");
await fixture.writeRepositoryFile("src/résumé.ts", "export const b = 2;\n");
@@ -424,7 +426,7 @@ async function testNonUtf8InventoryPaths() {
});
}
-async function testPrepareListsIgnoredTrackedFilesOnce() {
+async function testPrepareWithIgnoredTrackedFiles() {
const fixture = await createFixture("ignored tracked files");
await runGit(fixture.repoRoot, "init", "-q");
await fixture.writeRepositoryFile(".gitignore", "-generated/\n");
@@ -454,12 +456,33 @@ async function testPrepareListsIgnoredTrackedFilesOnce() {
}),
{ items: [{ path: "-generated/b.ts" }] },
);
+
+ const previous = await readFile(fixture.scanInventory);
+ await fixture.writeRepositoryFile(".git/index", "invalid");
+ await assert.rejects(
+ inventory.prepareCodexSecurityReviewItems(fixture.scan),
+ /git ls-files.*fatal:.*index/isu,
+ );
+ assert.deepEqual(await readFile(fixture.scanInventory), previous);
+
+ const savedGit = process.env.CODEX_SECURITY_GIT;
+ try {
+ process.env.CODEX_SECURITY_GIT = "";
+ await inventory.prepareCodexSecurityReviewItems(fixture.scan);
+ assert.equal(
+ await readFile(fixture.scanInventory, "utf8"),
+ "./.gitignore\n",
+ );
+ } finally {
+ if (savedGit === undefined) delete process.env.CODEX_SECURITY_GIT;
+ else process.env.CODEX_SECURITY_GIT = savedGit;
+ }
}
async function testPrepareExcludesGitMetadata() {
const fixture = await createFixture("nested repositories");
+ await runGit(fixture.repoRoot, "init", "-q");
for (const [name, source] of [
- [".git/config", "[core]\n"],
["vendor/lib/.git/HEAD", "ref: refs/heads/main\n"],
["vendor/lib/.git/hooks/example.py", "pass\n"],
["vendor/lib/handler.py", "pass\n"],