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

test(imports): isolate Git commands in local import fixtures (#2206)

Michael D'Angelo417145afb3db
diff --git a/sdk/typescript/tests-ts/blackduck-repository-import.test.ts b/sdk/typescript/tests-ts/blackduck-repository-import.test.ts
index 0e5ac6cff..ba904e2b0 100644
--- a/sdk/typescript/tests-ts/blackduck-repository-import.test.ts
+++ b/sdk/typescript/tests-ts/blackduck-repository-import.test.ts
@@ -1,5 +1,4 @@
import { expect, test } from "bun:test";
-import { execFileSync } from "node:child_process";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { gzipSync } from "node:zlib";
@@ -108,9 +107,7 @@ test("Black Duck keeps package occurrences unassessed across replay, mitigation,
prepareNormalizedLocalImport(altered, options, dependencies),
).rejects.toThrow(/retained scanner repository/i);
const remote = sourceRepositoryUrl + "?repo=other";
- execFileSync("git", ["remote", "set-url", "origin", remote], {
- cwd: fixture.repository,
- });
+ fixture.git("remote", "set-url", "origin", remote);
await expect(
prepareNormalizedLocalImport(first.preview, options, dependencies),
).rejects.toThrow(/does not match a configured remote/);
@@ -138,13 +135,7 @@ test("Black Duck binds normalized summaries to native findings and comparable re
{ environment: fixture.environment, sourceRepositories },
);
- execFileSync(
- "git",
- ["remote", "add", "upstream", other + "?repo=different"],
- {
- cwd: fixture.repository,
- },
- );
+ fixture.git("remote", "add", "upstream", other + "?repo=different");
const typed = (id: string, url: string) => ({
source_finding_id: id,
evidence: {
@@ -210,9 +201,7 @@ test("Black Duck binds normalized summaries to native findings and comparable re
await prepare(native, alias);
await prepare(typed("sdk-source", source), alias);
- execFileSync("git", ["remote", "set-url", "upstream", other], {
- cwd: fixture.repository,
- });
+ fixture.git("remote", "set-url", "upstream", other);
const reference = { url: other, platform: "github" as const };
sourceRepositories.push(reference);
const pending = prepare(native);
@@ -312,7 +301,7 @@ test("a Wiz file can combine projected and native package repositories", async (
const source = "https://git.example.test/Team/Project";
const remote = source.toLowerCase();
- execFileSync("git", ["-C", f.repository, "remote", "add", "origin", remote]);
+ f.git("remote", "add", "origin", remote);
record.vulnerableAsset.cloudProviderURL = remote + "/tree/main";
const projected = {
...record,
diff --git a/sdk/typescript/tests-ts/external-findings-import.test.ts b/sdk/typescript/tests-ts/external-findings-import.test.ts
index 31ae5bba1..f588973cb 100644
--- a/sdk/typescript/tests-ts/external-findings-import.test.ts
+++ b/sdk/typescript/tests-ts/external-findings-import.test.ts
@@ -1,4 +1,4 @@
-import { execFileSync, spawnSync } from "node:child_process";
+import { spawnSync } from "node:child_process";
import { gzipSync } from "node:zlib";
import { access, writeFile } from "node:fs/promises";
import { join } from "node:path";
@@ -381,15 +381,11 @@ for (const kind of [
const parsed = await readVendorFindings(file, "local");
expect(parsed.findings).toHaveLength(1);
expect(parsed.excluded).toHaveLength(1);
- execFileSync(
- "git",
- [
- "remote",
- "set-url",
- "origin",
- "[email protected]:example/other.git",
- ],
- { cwd: fixture.repository },
+ fixture.git(
+ "remote",
+ "set-url",
+ "origin",
+ "[email protected]:example/other.git",
);
await expect(
prepareLocalExternalImport(file, options, dependencies),
@@ -397,15 +393,11 @@ for (const kind of [
await expect(
access(join(fixture.stateDirectory, "workbench.sqlite3")),
).rejects.toThrow();
- execFileSync(
- "git",
- [
- "remote",
- "set-url",
- "origin",
- "[email protected]:example/project.git",
- ],
- { cwd: fixture.repository },
+ fixture.git(
+ "remote",
+ "set-url",
+ "origin",
+ "[email protected]:example/project.git",
);
const prepared = await prepareLocalExternalImport(
file,
@@ -436,26 +428,17 @@ for (const kind of [
"[email protected]:example/other.git",
"[email protected]:example/project.git",
]) {
- execFileSync("git", [
- "-C",
- fixture.repository,
- "remote",
- "set-url",
- "origin",
- remote,
- ]);
+ fixture.git("remote", "set-url", "origin", remote);
await expect(
prepareLocalExternalImport(file, options, dependencies),
).rejects.toThrow("does not match");
}
- execFileSync("git", [
- "-C",
- fixture.repository,
+ fixture.git(
"remote",
"set-url",
"origin",
"[email protected]:example/project.git",
- ]);
+ );
expect(await listLocalExternalFindings(options, dependencies)).toEqual(
created.findings,
);
diff --git a/sdk/typescript/tests-ts/external-findings-local-helpers.ts b/sdk/typescript/tests-ts/external-findings-local-helpers.ts
index 34aaf2c94..b12667d15 100644
--- a/sdk/typescript/tests-ts/external-findings-local-helpers.ts
+++ b/sdk/typescript/tests-ts/external-findings-local-helpers.ts
@@ -18,20 +18,24 @@ export async function localFindingsFixture(options: { remote?: string } = {}) {
);
const repository = join(root, "repository");
const env = sonarGitTestEnvironment();
- await mkdir(repository);
- execFileSync("git", ["init", "--quiet", repository], { env });
- if (options.remote !== undefined)
- execFileSync(
- "git",
- ["-C", repository, "remote", "add", "origin", options.remote],
- { env },
- );
- const stateDirectory = join(root, "state");
const dispose = () => rm(root, { recursive: true, force: true });
+ const git = (...args: string[]) =>
+ execFileSync("git", ["-C", repository, ...args], { env, encoding: "utf8" });
+ try {
+ await mkdir(repository);
+ git("init", "--quiet");
+ if (options.remote !== undefined)
+ git("remote", "add", "origin", options.remote);
+ } catch (error) {
+ await dispose();
+ throw error;
+ }
+ const stateDirectory = join(root, "state");
return {
root,
repository,
stateDirectory,
+ git,
environment: {
...Object.fromEntries(
Object.entries(env).filter(
diff --git a/sdk/typescript/tests-ts/external-findings-local.test.ts b/sdk/typescript/tests-ts/external-findings-local.test.ts
index c3515b9b6..bbe75501d 100644
--- a/sdk/typescript/tests-ts/external-findings-local.test.ts
+++ b/sdk/typescript/tests-ts/external-findings-local.test.ts
@@ -475,6 +475,7 @@ describe("local normalized scanner findings", () => {
const common = join(root, "common.git");
const git = (args: string[], stdin?: string): string =>
execFileSync("git", args, {
+ env: environment,
encoding: "utf8",
input: stdin,
stdio: ["pipe", "pipe", "pipe"],
@@ -920,21 +921,16 @@ describe("local normalized scanner findings", () => {
test.each(["bare", "linked", "ordinary"] as const)(
"protects selected %s roots through nested paths and aliases",
async (kind) => {
- const { root, repository, environment } = await fixture();
+ const { root, repository, environment, git } = await fixture();
const owner = join(root, "owner");
const common = join(root, "common.git");
if (kind === "linked") {
- execFileSync("git", ["init", "--bare", "--quiet", common]);
+ git("init", "--bare", "--quiet", common);
await mkdir(owner);
await writeFile(join(owner, "HEAD"), "ref: refs/heads/main\n");
await writeFile(join(owner, "commondir"), "../common.git\n");
} else {
- execFileSync("git", [
- "init",
- "--quiet",
- ...(kind === "bare" ? ["--bare"] : []),
- owner,
- ]);
+ git("init", "--quiet", ...(kind === "bare" ? ["--bare"] : []), owner);
}
const nested = join(owner, "nested", "selected");
await mkdir(nested, { recursive: true });
@@ -950,7 +946,7 @@ describe("local normalized scanner findings", () => {
];
if (kind === "bare") {
const inner = join(owner, "inner-worktree");
- execFileSync("git", ["init", "--quiet", inner]);
+ git("init", "--quiet", inner);
selections.push(inner);
}
for (const protectedDirectory of kind === "linked"
@@ -1003,7 +999,7 @@ describe("local normalized scanner findings", () => {
["commondir", "newline-before-nul"],
]),
])("preserves Git %s pointer format %s", async (kind, format) => {
- const { root, stateDirectory, environment } = await fixture();
+ const { root, stateDirectory, environment, git } = await fixture();
const common = join(
root,
format === "spaces"
@@ -1014,15 +1010,15 @@ describe("local normalized scanner findings", () => {
? "metadata.git\n"
: "metadata.git",
);
- execFileSync("git", ["init", "--quiet", "--bare", common]);
- execFileSync("git", [
+ git("init", "--quiet", "--bare", common);
+ git(
"-C",
common,
"remote",
"add",
"origin",
"https://github.com/example/project",
- ]);
+ );
let repository = join(root, "pointer-source");
await mkdir(repository);
if (format.startsWith("self-")) {
@@ -1046,11 +1042,7 @@ describe("local normalized scanner findings", () => {
}
}
// Git accepts the exact bytes, including a path newline immediately before NUL.
- expect(
- execFileSync("git", ["-C", repository, "remote"], {
- encoding: "utf8",
- }).trim(),
- ).toBe("origin");
+ expect(git("-C", repository, "remote").trim()).toBe("origin");
const bin = join(common, "tools");
const marker = join(root, "pointer-git-executed");
await mkdir(bin);
@@ -1087,7 +1079,7 @@ describe("local normalized scanner findings", () => {
test.skipIf(process.platform === "win32")(
"Node protects associated metadata through POSIX path syntax",
async () => {
- const { root, environment } = await fixture();
+ const { root, environment, git } = await fixture();
const storage = join(root, "storage");
await mkdir(join(storage, "child"), { recursive: true });
await symlink(join(storage, "child"), join(root, "pointer-link"), "dir");
@@ -1096,7 +1088,7 @@ describe("local normalized scanner findings", () => {
const name =
syntax === "parent-traversal" ? "common.git" : "common\\data.git";
const common = join(storage, name);
- execFileSync("git", ["init", "--quiet", "--bare", common]);
+ git("init", "--quiet", "--bare", common);
const pointer =
syntax === "parent-traversal"
? "../pointer-link/../common.git"
@@ -1111,12 +1103,7 @@ describe("local normalized scanner findings", () => {
await writeFile(join(repository, "commondir"), `${pointer}\n`);
}
// Git must accept the pointer before testing the SDK's state boundary.
- execFileSync("git", [
- "-C",
- repository,
- "rev-parse",
- "--git-common-dir",
- ]);
+ git("-C", repository, "rev-parse", "--git-common-dir");
cases.push({ repository, state: join(common, "runtime-state") });
}
}
@@ -1315,19 +1302,19 @@ describe("local normalized scanner findings", () => {
);
test("preserves Git pointer formats and invalid-pointer diagnostics", async () => {
- const { root, environment, stateDirectory } = await fixture();
+ const { root, environment, stateDirectory, git } = await fixture();
const repository = join(root, "linked");
const common = join(root, "common.git");
await mkdir(repository);
- execFileSync("git", ["init", "--quiet", "--bare", common]);
- execFileSync("git", [
+ git("init", "--quiet", "--bare", common);
+ git(
"-C",
common,
"remote",
"add",
"origin",
"https://github.com/example/project",
- ]);
+ );
const marker = join(repository, ".git");
const dependencies = {
environment,
@@ -1454,7 +1441,7 @@ describe("local normalized scanner findings", () => {
});
test("matches source repository context against SSH remotes and reader metadata", async () => {
- const { repository, stateDirectory, environment } = await fixture({
+ const { repository, stateDirectory, environment, git } = await fixture({
remote: "[email protected]:Example/Project.git",
});
const data = input();
@@ -1486,14 +1473,7 @@ describe("local normalized scanner findings", () => {
{ environment },
),
).rejects.toThrow("does not match a configured remote");
- execFileSync("git", [
- "-C",
- repository,
- "remote",
- "add",
- "upstream",
- "https://github.com/example/other.git",
- ]);
+ git("remote", "add", "upstream", "https://github.com/example/other.git");
await prepareNormalizedLocalImport(
data,
{ repository, ...source },
@@ -1556,7 +1536,7 @@ describe("local normalized scanner findings", () => {
)(
"matches %s on %s over the HTTPS port without equating other SSH ports",
async (protocol, host) => {
- const { repository, stateDirectory, environment } = await fixture({
+ const { repository, stateDirectory, environment, git } = await fixture({
remote: `${protocol}://git@${host}:443/example/project.git`,
});
const dependencies = {
@@ -1578,14 +1558,12 @@ describe("local normalized scanner findings", () => {
},
),
).rejects.toThrow("does not match a configured remote");
- execFileSync("git", [
- "-C",
- repository,
+ git(
"remote",
"set-url",
"origin",
`${protocol}://[email protected]:2222/example/project.git`,
- ]);
+ );
await expect(
prepareNormalizedLocalImport(
input(),
@@ -1630,14 +1608,10 @@ describe("local normalized scanner findings", () => {
])(
"allows a manual Sonar mapping for %s alongside an HTTPS fork",
async (upstream, url) => {
- const { repository, environment } = await fixture({
+ const { repository, environment, git } = await fixture({
remote: "https://fork.example.invalid/team/project.git",
});
- execFileSync(
- "git",
- ["-C", repository, "remote", "add", "upstream", upstream],
- { env: environment },
- );
+ git("remote", "add", "upstream", upstream);
const prepare = (sourceUrl: string, sourceRepositoryUrl?: string) =>
prepareNormalizedLocalImport(
input(),
@@ -1815,7 +1789,7 @@ describe("local normalized scanner findings", () => {
"https://git.example.invalid/team/project",
"[email protected]:team/project.git",
])("a custom SSH remote must match the claimed project: %s", async (url) => {
- const { repository, environment } = await fixture({
+ const { repository, environment, git } = await fixture({
remote: "[email protected]:team/other.git",
});
await expect(
@@ -1828,17 +1802,11 @@ describe("local normalized scanner findings", () => {
},
),
).rejects.toThrow("does not match a configured remote");
- execFileSync(
- "git",
- [
- "-C",
- repository,
- "remote",
- "set-url",
- "origin",
- "[email protected]:team/project.git",
- ],
- { env: environment },
+ git(
+ "remote",
+ "set-url",
+ "origin",
+ "[email protected]:team/project.git",
);
expect(
(
@@ -2008,7 +1976,7 @@ describe("local normalized scanner findings", () => {
])(
"ignores local remote %s while enforcing any network remote",
async (remote) => {
- const { repository, stateDirectory, environment } = await fixture({
+ const { repository, stateDirectory, environment, git } = await fixture({
remote,
});
const dependencies = {
@@ -2020,14 +1988,7 @@ describe("local normalized scanner findings", () => {
{ repository, ...source },
dependencies,
);
- execFileSync("git", [
- "-C",
- repository,
- "remote",
- "add",
- "upstream",
- "https://github.com/example/project",
- ]);
+ git("remote", "add", "upstream", "https://github.com/example/project");
await prepareNormalizedLocalImport(
input(),
{ repository, ...source },
@@ -2093,7 +2054,13 @@ describe("local normalized scanner findings", () => {
});
test("requires Git for bare and linked metadata but permits plain directories without it", async () => {
- const { root, repository, stateDirectory, environment } = await fixture({
+ const {
+ root,
+ repository,
+ stateDirectory,
+ environment,
+ git: runGit,
+ } = await fixture({
remote: "https://github.com/example/project",
});
const plain = join(root, "plain");
@@ -2102,15 +2069,15 @@ describe("local normalized scanner findings", () => {
const linked = join(root, "linked");
const invalid = join(root, "invalid");
await Promise.all([mkdir(plain), mkdir(linked), mkdir(invalid)]);
- execFileSync("git", ["init", "--bare", "--quiet", bare]);
- execFileSync("git", [
+ runGit("init", "--bare", "--quiet", bare);
+ runGit(
"-C",
bare,
"remote",
"add",
"origin",
"https://github.com/example/project",
- ]);
+ );
await mkdir(nested);
await Promise.all([
writeFile(join(linked, "HEAD"), "ref: refs/heads/main\n"),
diff --git a/sdk/typescript/tests-ts/inspector-repository-import.test.ts b/sdk/typescript/tests-ts/inspector-repository-import.test.ts
index d866f1906..c2925d219 100644
--- a/sdk/typescript/tests-ts/inspector-repository-import.test.ts
+++ b/sdk/typescript/tests-ts/inspector-repository-import.test.ts
@@ -1,5 +1,4 @@
import { expect, test } from "bun:test";
-import { execFileSync } from "node:child_process";
import { mkdtemp, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { gzipSync } from "node:zlib";
@@ -20,9 +19,8 @@ async function checkout(
remote = inspectorRepositoryUrl,
source = inspectorRepositoryUrl,
) {
- const { root, repository, environment, dispose } = await localFindingsFixture(
- { remote },
- );
+ const { root, repository, environment, git, dispose } =
+ await localFindingsFixture({ remote });
return {
path: join(root, "inspector.json.gz"),
options: {
@@ -32,6 +30,7 @@ async function checkout(
sourceRepositoryUrl: source,
},
deps: { environment },
+ git,
[Symbol.asyncDispose]: dispose,
};
}
@@ -301,14 +300,7 @@ test("normalized imports bind mixed projects, native evidence and maps to their
).rejects.toThrow("more than one repository project");
const forkUrl = project.details.codeRepository.projectName;
- execFileSync("git", [
- "-C",
- f.options.repository,
- "remote",
- "add",
- "fork",
- forkUrl,
- ]);
+ f.git("remote", "add", "fork", forkUrl);
const plain = await mkdtemp(join(dirname(f.path), "plain-"));
for (const repository of [plain, f.options.repository]) {
await expect(
diff --git a/sdk/typescript/tests-ts/semgrep-platform-selection.test.ts b/sdk/typescript/tests-ts/semgrep-platform-selection.test.ts
index a001eb345..a04acc63a 100644
--- a/sdk/typescript/tests-ts/semgrep-platform-selection.test.ts
+++ b/sdk/typescript/tests-ts/semgrep-platform-selection.test.ts
@@ -1,5 +1,4 @@
import { expect, test } from "bun:test";
-import { execFileSync } from "node:child_process";
import {
listLocalExternalFindings,
prepareNormalizedLocalImport,
@@ -24,19 +23,8 @@ const page = (id: string, branch = ref, repo = url) =>
test("reimporting two selected IDs from a non-default branch leaves both SQLite records unchanged", async () => {
await using fixture = await localFindingsFixture({ remote: url });
- execFileSync(
- "git",
- ["-C", fixture.repository, "switch", "--quiet", "--orphan", "topic"],
- {
- env: fixture.environment,
- },
- );
- expect(
- execFileSync("git", ["-C", fixture.repository, "symbolic-ref", "HEAD"], {
- encoding: "utf8",
- env: fixture.environment,
- }).trim(),
- ).toBe(ref);
+ fixture.git("switch", "--quiet", "--orphan", "topic");
+ expect(fixture.git("symbolic-ref", "HEAD").trim()).toBe(ref);
const options = {
provider: "semgrep" as const,
sourceKey: "demo/topic",
diff --git a/sdk/typescript/tests-ts/sonar-findings.test.ts b/sdk/typescript/tests-ts/sonar-findings.test.ts
index d4fdc7f05..1f02380c1 100644
--- a/sdk/typescript/tests-ts/sonar-findings.test.ts
+++ b/sdk/typescript/tests-ts/sonar-findings.test.ts
@@ -1,6 +1,5 @@
import { expect, test } from "bun:test";
-import { execFileSync } from "node:child_process";
-import { mkdir, writeFile } from "node:fs/promises";
+import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { prepareNormalizedLocalImport } from "../src/external-findings-local.js";
import {
@@ -9,7 +8,7 @@ import {
sonarRepositoryUrls,
} from "../src/sonar-findings.js";
import { scopedTemporaryDirectory } from "./support/temporary-directories.js";
-import { sonarGitTestEnvironment } from "./support/sonar-import-fixtures.js";
+import { localFindingsFixture } from "./external-findings-local-helpers.js";
const project = "team:application";
const repository = "https://vcs.example.test/team/application";
@@ -230,25 +229,14 @@ test("validates the repository binding and native project or ref before attribut
});
test("normalized local imports verify both report and issue bindings against the checkout and projections", async () => {
- await using tmp = await scopedTemporaryDirectory("sonar-binding-");
- const checkout = join(tmp.path, "checkout");
- await mkdir(checkout);
- const environment = {
- ...sonarGitTestEnvironment(),
- CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "1",
- CODEX_SECURITY_STATE_DIR: join(tmp.path, "state"),
- };
const foreign = repository + "-other";
- const git = (...args: string[]) =>
- execFileSync("git", ["-C", checkout, ...args], { env: environment });
- git("init", "--quiet");
- git("remote", "add", "origin", foreign);
+ await using fixture = await localFindingsFixture({ remote: foreign });
const options = {
- repository: checkout,
+ repository: fixture.repository,
provider: "sonar",
sourceKey: project,
} as const;
- const deps = { environment };
+ const deps = { environment: fixture.environment };
const { report, ...rows } = normalizeSonarFindings(
capture([page([issue()])]),
repository,
@@ -263,7 +251,7 @@ test("normalized local imports verify both report and issue bindings against the
await expect(
prepareNormalizedLocalImport(reportOnly, options, deps),
).rejects.toThrow("does not match a configured remote");
- git("remote", "add", "upstream", repository);
+ fixture.git("remote", "add", "upstream", repository);
expect(
(await prepareNormalizedLocalImport(rows, options, deps))
.matchesRepositoryRemote,
diff --git a/sdk/typescript/tests-ts/veracode-repository-import.test.ts b/sdk/typescript/tests-ts/veracode-repository-import.test.ts
index 6f0471b88..31e32ae2e 100644
--- a/sdk/typescript/tests-ts/veracode-repository-import.test.ts
+++ b/sdk/typescript/tests-ts/veracode-repository-import.test.ts
@@ -1,5 +1,4 @@
import { expect, test } from "bun:test";
-import { execFileSync } from "node:child_process";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { gzipSync } from "node:zlib";
@@ -72,10 +71,8 @@ test("replaying a Veracode policy flaw tracks evidence without assessing another
"[email protected]:example/mirror.git";
const prepareMixed = () =>
prepareNormalizedLocalImport(mixed, options, dependencies);
- const remote = (...args: string[]) =>
- execFileSync("git", ["-C", fixture.repository, "remote", ...args]);
await expect(prepareMixed()).rejects.toThrow(/configured remote/);
- remote("add", "mirror", "https://github.com/example/mirror");
+ fixture.git("remote", "add", "mirror", "https://github.com/example/mirror");
expect((await prepareMixed()).preview.findings).toHaveLength(2);
const wrong = structuredClone(first.preview);
wrong.findings[0]!.evidence.details!.repository!.url =
@@ -103,7 +100,12 @@ test("replaying a Veracode policy flaw tracks evidence without assessing another
dependencies,
),
).rejects.toThrow(/same application/i);
- remote("set-url", "origin", "ssh://[email protected]/example/project?other");
+ fixture.git(
+ "remote",
+ "set-url",
+ "origin",
+ "ssh://[email protected]/example/project?other",
+ );
await expect(prepareMixed()).rejects.toThrow(/configured remote/);
} finally {
await fixture.dispose();