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(git): preserve whitespace in worktree paths (#199)

Dipesh Babu1e34724c750f
diff --git a/sdk/typescript/src/targets.ts b/sdk/typescript/src/targets.ts
index 5c8ab0888..3b4577d38 100644
--- a/sdk/typescript/src/targets.ts
+++ b/sdk/typescript/src/targets.ts
@@ -415,7 +415,7 @@ async function gitOutput(
env: command.environment,
},
);
- return stdout.trim();
+ return stdout.replace(process.platform === "win32" ? /\r?\n$/u : /\n$/u, "");
}
async function outermostGitMarkerRoot(
diff --git a/sdk/typescript/tests-ts/targets.test.ts b/sdk/typescript/tests-ts/targets.test.ts
index f24f2aa7b..f4309757c 100644
--- a/sdk/typescript/tests-ts/targets.test.ts
+++ b/sdk/typescript/tests-ts/targets.test.ts
@@ -23,6 +23,7 @@ import {
repositoryRevision,
type ScanTarget,
} from "../src/index.js";
+import { enclosingGitWorktreeRoot } from "../src/targets.js";
// @ts-expect-error DiffTarget is intentionally nominal; use its constructor helpers.
const structurallyInvalidTarget: ScanTarget = {
@@ -42,12 +43,12 @@ afterEach(async () => {
);
});
-async function repository(): Promise<string> {
+async function repository(name = "repo"): Promise<string> {
const root = await realpath(
await mkdtemp(join(tmpdir(), "codex-security-targets-")),
);
temporaryDirectories.push(root);
- const repo = join(root, "repo");
+ const repo = join(root, name);
await mkdir(join(repo, "src"), { recursive: true });
await writeFile(join(repo, "src", "app.ts"), "export const ok = true;\n");
git(repo, "init", "-b", "main");
@@ -125,6 +126,19 @@ describe("scan target normalization", () => {
});
});
+ test.skipIf(process.platform === "win32")(
+ "preserves trailing whitespace in Git worktree paths",
+ async () => {
+ for (const suffix of [" ", "\r"]) {
+ const repo = await repository(`repo${suffix}`);
+ expect(await enclosingGitWorktreeRoot(repo)).toBe(await realpath(repo));
+ await expect(
+ normalizeTarget(repo, DiffTarget.refs({ base: "HEAD" })),
+ ).resolves.toMatchObject({ kind: "refs" });
+ }
+ },
+ );
+
test("rejects empty and escaping paths", async () => {
const repo = await repository();
await expect(normalizeTarget(repo, [""])).rejects.toThrow("empty path");