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

85.0 KB
Raw Blame
import { existsSync } from "node:fs";
import { execFileSync, spawnSync } from "node:child_process";
import {
  appendFile,
  mkdir,
  mkdtemp,
  rm,
  symlink,
  writeFile,
} from "node:fs/promises";
import { delimiter, join, sep } from "node:path";
import { pathToFileURL } from "node:url";
import { afterEach, describe, expect, test } from "bun:test";
import type { ExternalFindings } from "../src/external-findings.js";
import { normalizeSnykIssues } from "../src/snyk-rest-findings.js";
import { snykRestFixture } from "./support/snyk-rest-fixtures.js";
import {
  listLocalExternalFindings,
  prepareNormalizedLocalImport,
} from "../src/external-findings-local.js";
import { localFindingsFixture } from "./external-findings-local-helpers.js";

const fixtures: Awaited<ReturnType<typeof localFindingsFixture>>[] = [];
async function fixture(
  options: Parameters<typeof localFindingsFixture>[0] = {},
) {
  const result = await localFindingsFixture(options);
  fixtures.push(result);
  return result;
}
afterEach(async () => {
  await Promise.all(fixtures.splice(0).map((item) => item.dispose()));
});

function input(): ExternalFindings {
  return {
    read: 3,
    findings: [
      {
        source_finding_id: "scanner-finding-1",
        evidence: {
          title: "Dependency vulnerability",
          severity: "unknown",
          source_data: {
            id: "scanner-finding-1",
            metadata: { nullable: null, list: [1, "two"] },
          },
        },
      },
      {
        source_finding_id: "scanner-finding-2",
        evidence: {
          title: "Reported source issue",
          severity: "high",
          branch: "release",
          code_revision: "scanner-revision",
          locations: [{ path: "src/main.ts" }],
          source_data: { finding: 2 },
        },
      },
    ],
    excluded: [
      {
        position: 2,
        source_finding_id: null,
        reason: "No supported finding evidence",
      },
    ],
  };
}

const source = { provider: "snyk" as const, sourceKey: "example-project/main" };

describe("local normalized scanner findings", () => {
  test("reports empty and excluded selections without storage and preserves failed scanner diagnostics", async () => {
    const { repository, stateDirectory, environment } = await fixture();
    const options = { repository, ...source };
    const empty = { read: 0, findings: [], excluded: [] };
    const failed = {
      ...empty,
      report: { errors: [{ message: "Scanner timed out" }] },
    };
    const excluded = {
      ...empty,
      read: 1,
      excluded: [
        {
          position: 1,
          source_finding_id: null,
          reason: "Unsupported severity",
        },
      ],
    };
    for (const [selection, shouldFail] of [
      [empty, false],
      [excluded, false],
      [failed, true],
      [{ ...excluded, report: failed.report }, true],
      [{ ...empty, read: 1 }, true],
    ] as const) {
      const prepared = await prepareNormalizedLocalImport(selection, options, {
        environment,
      });
      expect(prepared.preview).toMatchObject(selection);
      if (shouldFail)
        await expect(prepared.import()).rejects.toMatchObject({ selection });
      else
        expect(await prepared.import()).toMatchObject({
          ...selection,
          counts: { created: 0, updated: 0, unchanged: 0 },
        });
      expect(existsSync(stateDirectory)).toBe(false);
    }
  });

  test("rejects unserializable report metadata before storing findings", async () => {
    const { repository, stateDirectory, environment } = await fixture();
    await expect(
      prepareNormalizedLocalImport(
        { ...input(), report: { elapsedNs: 1n } },
        { repository, ...source },
        { environment },
      ),
    ).rejects.toThrow(/bigint/i);
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test("persists sparse evidence through the native helper and reads it in a separate process", async () => {
    const { repository, stateDirectory, environment } = await fixture();
    const normalized = input();
    normalized.report = { errors: normalized.excluded };
    const prepared = await prepareNormalizedLocalImport(
      normalized,
      { repository, ...source },
      { environment },
    );
    expect(prepared.preview).toEqual({
      ...normalized,
      repository,
      source: { provider: source.provider, source_key: source.sourceKey },
    });
    expect(existsSync(stateDirectory)).toBe(false);
    normalized.findings[0]!.evidence.title = "Later caller edit";
    prepared.preview.findings[0]!.evidence.title = "Later preview edit";
    const result = await prepared.import();
    expect(result).toMatchObject({
      repository,
      read: 3,
      ready: 2,
      excluded: input().excluded,
      counts: { created: 2, updated: 0, unchanged: 0 },
    });
    (result.report!["errors"] as typeof normalized.excluded).pop();
    Object.assign(result.excluded[0]!, { onSelect: () => undefined });
    expect(await prepared.import()).toMatchObject({
      excluded: normalized.excluded,
      report: normalized.report,
      counts: { created: 0, updated: 0, unchanged: 2 },
    });
    expect(existsSync(join(stateDirectory, "workbench.sqlite3"))).toBe(true);
    expect(result.findings.map((row) => row.evidence)).toEqual(
      input().findings.map((row) => row.evidence),
    );
    for (const row of result.findings) {
      expect(row).toMatchObject({
        repository,
        ...source,
        version: 1,
        assessment: { state: "not_assessed" },
      });
      expect(row.createdAt).toBe(row.updatedAt);
    }
    const reloaded = await listLocalExternalFindings(
      { repository },
      { environment },
    );
    expect(new Map(reloaded.map((row) => [row.id, row]))).toEqual(
      new Map(result.findings.map((row) => [row.id, row])),
    );
    expect(
      await listLocalExternalFindings(
        { repository, id: result.findings[0]!.id },
        { environment },
      ),
    ).toEqual([result.findings[0]!]);
  });

  test("upserts reordered and changed exports without deleting omitted records or mixing namespaces", async () => {
    const { root, repository, environment } = await fixture();
    const save = async (
      data: ExternalFindings,
      options = { repository, ...source },
    ) =>
      (
        await prepareNormalizedLocalImport(data, options, { environment })
      ).import();
    const initial = await save(input());
    const reordered = input();
    reordered.findings.reverse();
    reordered.findings[1]!.evidence.source_data = {
      metadata: { list: [1, "two"], nullable: null },
      id: "scanner-finding-1",
    };
    const repeated = await save(reordered);
    expect(repeated.counts).toEqual({ created: 0, updated: 0, unchanged: 2 });
    expect(repeated.findings).toEqual([...initial.findings].reverse());
    const changed = input();
    changed.findings = [changed.findings[0]!];
    changed.findings[0]!.evidence.severity = "critical";
    const updated = await save(changed);
    expect(updated.counts).toEqual({ created: 0, updated: 1, unchanged: 0 });
    expect(updated.findings[0]).toMatchObject({
      id: initial.findings[0]!.id,
      version: 2,
      createdAt: initial.findings[0]!.createdAt,
    });
    expect(
      await listLocalExternalFindings(
        { repository, id: initial.findings[1]!.id },
        { environment },
      ),
    ).toEqual([initial.findings[1]!]);
    const otherSource = await save(changed, {
      repository,
      ...source,
      sourceKey: "other-project/main",
    });
    const otherProvider = await (
      await prepareNormalizedLocalImport(
        changed,
        { repository, ...source, provider: "endor" },
        { environment },
      )
    ).import();
    const otherRepository = join(root, "other-repository");
    await mkdir(otherRepository);
    const otherCheckout = await save(changed, {
      repository: otherRepository,
      ...source,
    });
    expect(
      new Set(
        [updated, otherSource, otherProvider, otherCheckout].map(
          (result) => result.findings[0]!.id,
        ),
      ).size,
    ).toBe(4);
    expect(
      await listLocalExternalFindings(
        { repository: otherRepository },
        { environment },
      ),
    ).toEqual(otherCheckout.findings);
    expect(
      (await listLocalExternalFindings({ repository }, { environment })).length,
    ).toBe(4);
  });

  test("reads retained evidence after the checkout is removed and still requires a directory for new imports", async () => {
    const { repository, environment } = await fixture();
    const result = await (
      await prepareNormalizedLocalImport(
        input(),
        { repository, ...source },
        { environment },
      )
    ).import();
    await rm(repository, { recursive: true });
    const reloaded = await listLocalExternalFindings(
      { repository: result.repository },
      { environment },
    );
    expect(new Map(reloaded.map((row) => [row.id, row]))).toEqual(
      new Map(result.findings.map((row) => [row.id, row])),
    );
    expect(
      await listLocalExternalFindings(
        { repository: result.repository, id: result.findings[0]!.id },
        { environment },
      ),
    ).toEqual([result.findings[0]!]);
    await expect(
      prepareNormalizedLocalImport(
        input(),
        { repository, ...source },
        { environment },
      ),
    ).rejects.toThrow("Repository is not a directory");
  });

  test("preparation and cancellation leave the database untouched", async () => {
    const { repository, stateDirectory, environment } = await fixture();
    const controller = new AbortController();
    const prepared = await prepareNormalizedLocalImport(
      input(),
      { repository, ...source },
      { environment, signal: controller.signal },
    );
    expect(existsSync(stateDirectory)).toBe(false);
    controller.abort();
    await expect(prepared.import()).rejects.toThrow();
    await expect(
      prepareNormalizedLocalImport(
        input(),
        { repository, ...source },
        { environment, signal: controller.signal },
      ),
    ).rejects.toThrow();
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test("refreshes caller protections when importing a previously prepared payload", async () => {
    const { root, repository, stateDirectory, environment } = await fixture();
    const originalCaller = await fixture();
    const nextCaller = await fixture();
    const originalBin = join(originalCaller.repository, "tools");
    const nextBin = join(nextCaller.repository, "tools");
    const nextDirectory = join(nextCaller.repository, "src");
    const unsafeState = join(nextCaller.repository, "state");
    const marker = join(root, "caller-node-executed");
    await Promise.all([
      mkdir(originalBin),
      mkdir(nextBin),
      mkdir(nextDirectory),
    ]);
    await Promise.all(
      [originalBin, nextBin].map((bin) =>
        writeFile(
          join(bin, process.platform === "win32" ? "node.exe" : "node"),
          process.platform === "win32"
            ? "untrusted executable fixture\n"
            : '#!/bin/sh\nprintf executed > "$TEST_LOCAL_IMPORT_NODE_MARKER"\nexit 1\n',
          { mode: 0o700 },
        ),
      ),
    );
    const inheritedPath = Object.entries(environment).find(
      ([name]) => name.toUpperCase() === "PATH",
    )?.[1];
    const module = new URL("../src/external-findings-local.ts", import.meta.url)
      .href;
    const script = `
      import { deepEqual, equal, rejects } from "node:assert/strict";
      const { prepareNormalizedLocalImport } = await import(${JSON.stringify(module)});
      const input = ${JSON.stringify(input())};
      const options = ${JSON.stringify({ repository, ...source })};
      const prepared = await prepareNormalizedLocalImport(input, options);
      const unsafeEnvironment = {
        ...process.env,
        CODEX_SECURITY_STATE_DIR: ${JSON.stringify(unsafeState)},
      };
      const unsafe = await prepareNormalizedLocalImport(input, options, {
        environment: unsafeEnvironment,
      });
      unsafeEnvironment.CODEX_SECURITY_STATE_DIR = ${JSON.stringify(stateDirectory)};
      process.chdir(${JSON.stringify(nextDirectory)});
      await rejects(unsafe.import(), /outside/);
      const result = await prepared.import();
      equal(result.repository, options.repository);
      deepEqual(result.counts, { created: 2, updated: 0, unchanged: 0 });
      console.log("IMPORTED");
    `;
    const result = spawnSync(process.execPath, ["-e", script], {
      cwd: originalCaller.repository,
      encoding: "utf8",
      env: {
        ...Object.fromEntries(
          Object.entries(environment).filter(
            ([name]) => name.toUpperCase() !== "PATH",
          ),
        ),
        PATH: [nextBin, originalBin, inheritedPath ?? ""].join(delimiter),
        TEST_LOCAL_IMPORT_NODE_MARKER: marker,
      },
    });
    expect(result.status, result.stderr).toBe(0);
    expect(result.stdout.trim()).toBe("IMPORTED");
    expect(existsSync(marker)).toBe(false);
    expect(existsSync(unsafeState)).toBe(false);
    expect(existsSync(join(stateDirectory, "workbench.sqlite3"))).toBe(true);
  });

  test.each(["bare", "linked-metadata", "linked-worktree"] as const)(
    "protects %s caller metadata during preparation, readback, and deferred import",
    async (kind) => {
      const { root, repository, stateDirectory, environment } = await fixture({
        remote: "https://github.com/example/project",
      });
      const originalCaller = await fixture();
      const caller = join(root, "caller");
      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"],
        }).trim();
      let metadataRoots: string[];
      let pointerPaths: string[] = [];
      if (kind === "bare") {
        git(["init", "--quiet", "--bare", caller]);
        metadataRoots = [caller];
      } else {
        git(["init", "--quiet", "--bare", common]);
        if (kind === "linked-metadata") {
          await mkdir(caller);
          await writeFile(join(caller, "HEAD"), "ref: refs/heads/main\n");
          await writeFile(join(caller, "commondir"), `${common}\n`);
          metadataRoots = [caller, common];
          pointerPaths = [join(caller, "commondir")];
        } else {
          const tree = git(["--git-dir", common, "mktree"], "");
          const commit = git(
            [
              "--git-dir",
              common,
              "-c",
              "user.name=Fixture",
              "-c",
              "[email protected]",
              "-c",
              "commit.gpgsign=false",
              "commit-tree",
              tree,
            ],
            "Synthetic worktree fixture\n",
          );
          git([
            "--git-dir",
            common,
            "worktree",
            "add",
            "--detach",
            caller,
            commit,
          ]);
          const gitDirectory = git([
            "-C",
            caller,
            "rev-parse",
            "--absolute-git-dir",
          ]);
          metadataRoots = [gitDirectory, common];
          pointerPaths = [
            join(caller, ".git"),
            join(gitDirectory, "commondir"),
          ];
        }
        // A linked repository owns HEAD; its shared object store need not.
        await rm(join(common, "HEAD"));
      }
      const callerDirectory = join(caller, "src");
      await mkdir(callerDirectory);
      const gitMarker = join(root, "metadata-git-executed");
      const nodeMarker = join(root, "metadata-node-executed");
      const bins = metadataRoots.map((directory) => join(directory, "tools"));
      for (const bin of bins) {
        await mkdir(bin);
        for (const executable of ["git", "node"]) {
          await writeFile(
            join(
              bin,
              process.platform === "win32" ? `${executable}.exe` : executable,
            ),
            process.platform === "win32"
              ? "untrusted executable fixture\n"
              : `#!/bin/sh\nprintf executed > "$TEST_METADATA_${executable.toUpperCase()}_MARKER"\nexit 1\n`,
            { mode: 0o700 },
          );
        }
      }
      const inheritedPath = Object.entries(environment).find(
        ([name]) => name.toUpperCase() === "PATH",
      )?.[1];
      const module = new URL(
        "../src/external-findings-local.ts",
        import.meta.url,
      ).href;
      const script = `
        import { deepEqual, equal, rejects } from "node:assert/strict";
        import { existsSync, readFileSync } from "node:fs";
        import { join } from "node:path";
        const { prepareNormalizedLocalImport, listLocalExternalFindings } =
          await import(${JSON.stringify(module)});
        const input = ${JSON.stringify(input())};
        const options = ${JSON.stringify({ repository, ...source })};
        const pointerPaths = ${JSON.stringify(pointerPaths)};
        const pointerContents = pointerPaths.map((path) => readFileSync(path, "utf8"));
        for (const metadataRoot of ${JSON.stringify(metadataRoots)}) {
          const state = join(metadataRoot, "security-state");
          const environment = { ...process.env, CODEX_SECURITY_STATE_DIR: state };
          process.chdir(${JSON.stringify(originalCaller.repository)});
          const prepared = await prepareNormalizedLocalImport(input, options, {
            environment,
          });
          equal(existsSync(state), false);
          process.chdir(${JSON.stringify(callerDirectory)});
          await rejects(
            prepareNormalizedLocalImport(input, options, { environment }),
            /outside/,
          );
          await rejects(
            listLocalExternalFindings({ repository: options.repository }, { environment }),
            /outside/,
          );
          await rejects(prepared.import(), /outside/);
          equal(existsSync(state), false);
        }
        // The metadata shims must be excluded for both Git discovery and Node launch.
        const prepared = await prepareNormalizedLocalImport(input, options, {
          sourceRepositories: [{ url: "https://github.com/example/project" }],
        });
        const result = await prepared.import();
        deepEqual(result.counts, { created: 2, updated: 0, unchanged: 0 });
        equal(existsSync(${JSON.stringify(gitMarker)}), false);
        equal(existsSync(${JSON.stringify(nodeMarker)}), false);
        deepEqual(
          pointerPaths.map((path) => readFileSync(path, "utf8")),
          pointerContents,
        );
        console.log("PROTECTED");
      `;
      const result = spawnSync(process.execPath, ["-e", script], {
        cwd: originalCaller.repository,
        encoding: "utf8",
        env: {
          ...Object.fromEntries(
            Object.entries(environment).filter(
              ([name]) => name.toUpperCase() !== "PATH",
            ),
          ),
          PATH: [...bins, inheritedPath ?? ""].join(delimiter),
          TEST_METADATA_GIT_MARKER: gitMarker,
          TEST_METADATA_NODE_MARKER: nodeMarker,
        },
      });
      expect(result.status, result.stderr).toBe(0);
      expect(result.stdout.trim()).toBe("PROTECTED");
      expect(existsSync(gitMarker)).toBe(false);
      expect(existsSync(nodeMarker)).toBe(false);
      for (const metadataRoot of metadataRoots)
        expect(existsSync(join(metadataRoot, "security-state"))).toBe(false);
      expect(existsSync(join(stateDirectory, "workbench.sqlite3"))).toBe(true);
    },
  );

  const pendingPaths = [
    "repository",
    "prepare-state",
    "read-state",
    "prepare-metadata",
    "prepare-snyk-remotes",
  ];
  test.each(pendingPaths)(
    "cancels %s resolution while its filesystem lookup is pending",
    async (operation) => {
      const { repository, stateDirectory, environment } = await fixture({
        isolatedGit: operation === "prepare-snyk-remotes",
      });
      if (operation === "prepare-metadata")
        await rm(join(repository, ".git"), { recursive: true });
      const module = new URL(
        "../src/external-findings-local.ts",
        import.meta.url,
      ).href;
      const mocks = new URL("./support/module-mocks.ts", import.meta.url).href;
      const script = `
      import { strictEqual } from "node:assert/strict";
      import * as fs from "node:fs/promises";
      import { setImmediate } from "node:timers/promises";
      import { mockFs } from ${JSON.stringify(mocks)};
      const repository = ${JSON.stringify(repository)};
      const operation = ${JSON.stringify(operation)};
      const heldPath = operation === "repository" ? repository
        : operation === "prepare-metadata" ? ${JSON.stringify(join(repository, "HEAD"))}
        : operation === "prepare-snyk-remotes" ? ${JSON.stringify(join(repository, ".git", "remotes"))}
        : ${JSON.stringify(stateDirectory)};
      const holdLstat = operation === "prepare-metadata" || operation === "prepare-snyk-remotes";
      const originalRealpath = fs.realpath;
      const originalLstat = fs.lstat;
      let enteredLookup, releaseLookup;
      const entered = new Promise((resolve) => { enteredLookup = resolve; });
      const lookup = new Promise((_, reject) => { releaseLookup = reject; });
      mockFs(() => ({
        realpath: (path, ...args) => {
          if (holdLstat || path !== heldPath)
            return originalRealpath(path, ...args);
          enteredLookup();
          return lookup;
        },
        lstat: (path, ...args) => {
          if (!holdLstat || path !== heldPath)
            return originalLstat(path, ...args);
          enteredLookup();
          return lookup;
        },
      }));
      const { listLocalExternalFindings, prepareNormalizedLocalImport } =
        await import(${JSON.stringify(module)});
      const controller = new AbortController();
      const reason = new Error("Synthetic path resolution cancellation");
      let outcome;
      const pending = operation.startsWith("prepare-")
        ? prepareNormalizedLocalImport(
            ${JSON.stringify(
              operation === "prepare-snyk-remotes"
                ? normalizeSnykIssues(
                    snykRestFixture("open-source", {
                      sourceRepositoryUrl: "https://github.com/example/project",
                    }),
                  )
                : { read: 0, findings: [], excluded: [] },
            )},
            { repository, provider: "snyk", sourceKey: "synthetic-project" },
            { signal: controller.signal,
              sourceRepositories: [{ url: "https://github.com/example/project" }] },
          )
        : listLocalExternalFindings(
            { repository }, { signal: controller.signal },
          );
      const reading = pending.then(
        () => { outcome = "fulfilled"; },
        (error) => { outcome = error; },
      );
      await entered;
      controller.abort(reason);
      await setImmediate();
      try {
        strictEqual(outcome, reason);
      } finally {
        releaseLookup(new Error("Synthetic filesystem failure"));
        await reading;
      }
      strictEqual(outcome, reason);
      console.log("CANCELLED");
    `;
      const result = spawnSync(process.execPath, ["-e", script], {
        encoding: "utf8",
        env: environment,
      });
      expect(result.status, result.stderr).toBe(0);
      expect(result.stdout.trim()).toBe("CANCELLED");
      expect(existsSync(stateDirectory)).toBe(false);
    },
  );

  test.each([
    ["selected-roots", "prepare"],
    ["selected-roots", "read"],
    ["caller-roots", "prepare"],
    ["caller-roots", "read"],
    ["caller-roots", "import"],
    ["plugin-manifest", "prepare"],
    ["plugin-path", "read"],
    ["git-executable", "prepare"],
    ["workbench-path", "read"],
    ["workbench-path", "import"],
  ])("cancels pending %s lookup during %s", async (site, operation) => {
    const { repository, stateDirectory, environment } = await fixture({
      remote: "https://github.com/example/project",
    });
    const caller = await fixture();
    const module = new URL("../src/external-findings-local.ts", import.meta.url)
      .href;
    const mocks = new URL("./support/module-mocks.ts", import.meta.url).href;
    const trusted = new URL("../src/trusted-executable.ts", import.meta.url)
      .href;
    const plugin = new URL("../_bundled_plugin", import.meta.url).href;
    const script = `
      import { strictEqual } from "node:assert/strict";
      import * as fs from "node:fs/promises";
      import { dirname, join } from "node:path";
      import { fileURLToPath } from "node:url";
      import { setImmediate } from "node:timers/promises";
      import { mockFs } from ${JSON.stringify(mocks)};
      const site = ${JSON.stringify(site)};
      const operation = ${JSON.stringify(operation)};
      const repository = ${JSON.stringify(repository)};
      const original = { lstat: fs.lstat, realpath: fs.realpath, stat: fs.stat };
      const held = new Map();
      function lookup(method, path, args) {
        // Repository normalization checks a byte path before the protected-root lookups.
        const gate = typeof path === "string" && held.get(method + ":" + path);
        if (gate) { gate.entered(); return gate.wait; }
        return original[method](path, ...args);
      }
      mockFs(() => ({
        lstat: (path, ...args) => lookup("lstat", path, args),
        realpath: (path, ...args) => lookup("realpath", path, args),
        stat: (path, ...args) => lookup("stat", path, args),
      }));
      const { prepareNormalizedLocalImport, listLocalExternalFindings } =
        await import(${JSON.stringify(module)});
      const { resolveTrustedExecutable } = await import(${JSON.stringify(trusted)});
      const nodeDirectory = site === "workbench-path"
        ? dirname((await resolveTrustedExecutable("node", process.env, [repository])).executable)
        : undefined;
      const controller = new AbortController();
      const reason = new Error("Synthetic setup cancellation");
      let helperCalls = 0;
      const dependencies = {
        signal: controller.signal,
        sourceRepositories: site === "git-executable"
          ? [{ url: "https://github.com/example/project" }] : [],
        ...(site === "workbench-path" ? {
          environment: {
            ...Object.fromEntries(Object.entries(process.env).filter(([name]) => name.toUpperCase() !== "PATH")),
            PATH: nodeDirectory,
          },
        } : {
          runWorkbench: async () => {
            helperCalls++;
            return { findings: [], counts: { created: 0, updated: 0, unchanged: 0 } };
          },
        }),
      };
      const input = ${JSON.stringify(input())};
      const options = { repository, provider: "snyk", sourceKey: "synthetic-project" };
      const prepared = operation === "import"
        ? await prepareNormalizedLocalImport(input, options, dependencies)
        : undefined;
      let paths;
      if (site.endsWith("-roots")) {
        const directory = site === "selected-roots" ? repository
          : await original.realpath(process.cwd());
        paths = [["lstat", directory], ["lstat", join(directory, ".git")]];
      } else if (site.startsWith("plugin-")) {
        const plugin = fileURLToPath(${JSON.stringify(plugin)});
        paths = site === "plugin-manifest"
          ? [["lstat", join(plugin, ".codex-plugin", "plugin.json")]]
          : [["realpath", plugin]];
      } else if (site === "workbench-path") {
        paths = [["realpath", nodeDirectory]];
      } else {
        const git = await resolveTrustedExecutable("git", process.env, [repository]);
        paths = [["stat", await original.realpath(git.executable)]];
      }
      const gates = [];
      for (const [method, path] of paths) {
        const value = await original[method](path);
        let entered, release;
        const reached = new Promise((resolve) => { entered = resolve; });
        const wait = new Promise((resolve, reject) => {
          release = site === "workbench-path"
            ? () => reject(Object.assign(new Error("Synthetic PATH lookup failure"), { code: "EIO" }))
            : resolve;
        });
        const gate = { key: method + ":" + path, entered, release, reached, wait, value };
        held.set(gate.key, gate);
        gates.push(gate);
      }
      let outcome;
      const pending = (operation === "import" ? prepared.import()
        : operation === "prepare" ? prepareNormalizedLocalImport(input, options, dependencies)
        : listLocalExternalFindings({ repository }, dependencies)).then(
          () => { outcome = "fulfilled"; }, (error) => { outcome = error; },
        );
      await Promise.all(gates.map((gate) => gate.reached));
      controller.abort(reason);
      await setImmediate();
      try {
        strictEqual(outcome, reason);
      } finally {
        for (const gate of gates) {
          held.delete(gate.key);
          gate.release(gate.value);
        }
        await pending;
      }
      await setImmediate();
      strictEqual(outcome, reason);
      strictEqual(helperCalls, 0);
      console.log("CANCELLED");
    `;
    const result = spawnSync(process.execPath, ["-e", script], {
      cwd: caller.repository,
      encoding: "utf8",
      env: environment,
    });
    expect(result.status, result.stderr).toBe(0);
    expect(result.stdout.trim()).toBe("CANCELLED");
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test("rejects duplicate occurrence identities before creating local state", async () => {
    const { repository, stateDirectory, environment } = await fixture();
    const duplicate = input();
    duplicate.findings.push(duplicate.findings[0]!);
    await expect(
      prepareNormalizedLocalImport(
        duplicate,
        { repository, ...source },
        { environment },
      ),
    ).rejects.toThrow("Duplicate source finding id");
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test("rejects non-finite JSON before it can be stored as null", async () => {
    const { repository, stateDirectory, environment } = await fixture();
    const data = input();
    data.findings[0]!.evidence.source_data = JSON.parse('{"score":1e999}');
    await expect(
      prepareNormalizedLocalImport(
        data,
        { repository, ...source },
        { environment },
      ),
    ).rejects.toThrow("non-finite numbers");
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test("local previews and storage capture the same numeric source data", async () => {
    const { repository, environment } = await fixture();
    const data = input();
    let reads = 0;
    const sourceData = {
      scores: Object.assign([3], {
        note: Object.assign(new Number(4), {
          toJSON() {
            throw new Error("JSON ignores named array properties");
          },
        }),
      }),
      get section() {
        const n = ++reads;
        return [new Number(n), new Number(n)];
      },
      value: Object.assign(new Number(9), {
        toJSON: (key: string) => (key === "value" ? 0 : 9),
      }),
    };
    data.findings[0]!.evidence.source_data = sourceData;
    data.report = { value: sourceData.value, alias: sourceData.value };
    const prepared = await prepareNormalizedLocalImport(
      data,
      { repository, ...source },
      { environment },
    );
    const expected = { scores: [3], section: [1, 1], value: 0 };
    expect(reads).toBe(1);
    const preview = prepared.preview.findings[0]!.evidence.source_data!;
    expect(JSON.parse(JSON.stringify(preview))).toEqual(expected);
    expect(prepared.preview.report).toEqual({ value: 0, alias: 9 });
    expect((await prepared.import()).report).toEqual(prepared.preview.report);
    const rows = await listLocalExternalFindings(
      { repository },
      { environment },
    );
    expect(
      rows.find((item) => item.sourceFindingId === "scanner-finding-1")
        ?.evidence.source_data,
    ).toEqual(expected);
  });

  test("captures numeric fields before later getters mutate the source", async () => {
    const { repository, environment } = await fixture();
    const data = input();
    class Scores {
      score = new Number(3);
    }
    const scores = new Scores();
    let reads = 0;
    Object.defineProperty(scores, "getterScore", {
      enumerable: true,
      get() {
        reads++;
        scores.score = new Number(7);
        return Object.assign(new Number(1), { toJSON: () => 5 });
      },
    });
    data.findings[0]!.evidence.source_data = { scores };
    Object.assign(data.findings[0]!, {
      vendorCursor: 2 ** 53,
      previousCursor: new Number(2 ** 53),
    });
    const prepared = await prepareNormalizedLocalImport(
      data,
      { repository, ...source },
      { environment },
    );
    expect(reads).toBe(1);
    const expected = { scores: { score: 3, getterScore: 5 } };
    expect(
      JSON.parse(
        JSON.stringify(prepared.preview.findings[0]!.evidence.source_data),
      ),
    ).toEqual(expected);
    await prepared.import();
    const rows = await listLocalExternalFindings(
      { repository },
      { environment },
    );
    expect(
      rows.find((item) => item.sourceFindingId === "scanner-finding-1")
        ?.evidence.source_data,
    ).toEqual(expected);
  });

  test.each([2 ** 53, new Number(2 ** 53)])(
    "rejects unsafe normalized evidence before writing any local state",
    async (score) => {
      const { repository, stateDirectory, environment } = await fixture();
      const data = input();
      data.findings[0]!.evidence.source_data = { score };
      await expect(
        prepareNormalizedLocalImport(
          data,
          { repository, ...source },
          { environment },
        ),
      ).rejects.toThrow("safe integer");
      expect(existsSync(stateDirectory)).toBe(false);
    },
  );

  test("uses the canonical selected checkout and rejects state inside it", async () => {
    const { root, repository, environment } = await fixture();
    const link = join(root, "repository-link");
    await symlink(
      repository,
      link,
      process.platform === "win32" ? "junction" : "dir",
    );
    const prepared = await prepareNormalizedLocalImport(
      input(),
      { repository: link, ...source },
      { environment },
    );
    expect(prepared.preview.repository).toBe(repository);
    await expect(
      prepareNormalizedLocalImport(
        input(),
        { repository, ...source },
        {
          environment: {
            ...environment,
            CODEX_SECURITY_STATE_DIR: join(repository, "state"),
          },
        },
      ),
    ).rejects.toThrow();
    const child = join(repository, "src");
    await mkdir(child);
    await expect(
      prepareNormalizedLocalImport(
        input(),
        { repository: child, ...source },
        {
          environment: {
            ...environment,
            CODEX_SECURITY_STATE_DIR: join(repository, "state"),
          },
        },
      ),
    ).rejects.toThrow();
    expect(existsSync(join(repository, "state"))).toBe(false);
  });

  test("protects imported findings readback through parent traversal", async () => {
    const { root, repository, environment } = await fixture();
    const owner = join(root, "owner");
    await mkdir(owner);
    await mkdir(join(repository, "child"));
    const linkType = process.platform === "win32" ? "junction" : "dir";
    const link = join(owner, "link");
    await symlink(join(repository, "child"), link, linkType);
    const direct = join(owner, "direct");
    await symlink(repository, direct, linkType);
    const state = join(owner, "state");
    const dependencies = {
      environment: { ...environment, CODEX_SECURITY_STATE_DIR: state },
    };
    await expect(
      listLocalExternalFindings(
        { repository: `${link}${sep}..` },
        dependencies,
      ),
    ).rejects.toThrow("must be outside the protected scan root");
    expect(existsSync(state)).toBe(false);
    expect(
      await listLocalExternalFindings({ repository: direct }, dependencies),
    ).toEqual([]);
  });

  test.each(["bare", "linked", "ordinary"] as const)(
    "protects selected %s roots through nested paths and aliases",
    async (kind) => {
      const { root, repository, environment, git } = await fixture();
      const owner = join(root, "owner");
      const common = join(root, "common.git");
      if (kind === "linked") {
        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 {
        git("init", "--quiet", ...(kind === "bare" ? ["--bare"] : []), owner);
      }
      const nested = join(owner, "nested", "selected");
      await mkdir(nested, { recursive: true });
      const linkType = process.platform === "win32" ? "junction" : "dir";
      await symlink(repository, join(owner, "to-selected"), linkType);
      const alias = join(root, "owner-alias");
      await symlink(owner, alias, linkType);
      const nestedAlias = join(root, "nested-alias");
      await symlink(join(owner, "nested"), nestedAlias, linkType);
      await mkdir(join(repository, "child"));
      await mkdir(join(repository, "selected"));
      await symlink(
        join(repository, "child"),
        join(owner, "nested", "link"),
        linkType,
      );
      const selections = [
        owner,
        nested,
        join(owner, "to-selected"),
        join(alias, "to-selected"),
        `${join(nestedAlias, "link")}${sep}..${sep}selected`,
      ];
      if (kind === "bare") {
        const inner = join(owner, "inner-worktree");
        git("init", "--quiet", inner);
        selections.push(inner);
      }
      for (const protectedDirectory of kind === "linked"
        ? [owner, common]
        : [owner]) {
        const state = join(protectedDirectory, "runtime-state");
        let helperCalls = 0;
        const dependencies = {
          environment: { ...environment, CODEX_SECURITY_STATE_DIR: state },
          runWorkbench: async () => {
            helperCalls++;
            return {
              findings: [],
              counts: { created: 0, updated: 0, unchanged: 0 },
            };
          },
        };
        for (const selected of selections) {
          await expect(
            (async () => {
              const prepared = await prepareNormalizedLocalImport(
                input(),
                { repository: selected, ...source },
                dependencies,
              );
              await prepared.import();
            })(),
          ).rejects.toThrow("must be outside the protected scan root");
          await expect(
            listLocalExternalFindings({ repository: selected }, dependencies),
          ).rejects.toThrow("must be outside the protected scan root");
          expect(helperCalls).toBe(0);
          expect(existsSync(state)).toBe(false);
        }
      }
    },
  );

  test.each([
    ["gitdir", "spaces"],
    ["commondir", "spaces"],
    ["commondir", "nul-suffix"],
    ["gitdir", "self-nul"],
    ["commondir", "self-nul"],
    ["commondir", "self-newline"],
    ...(process.platform === "win32"
      ? []
      : [
          ["gitdir", "newline-before-nul"],
          ["commondir", "newline-before-nul"],
        ]),
  ])("preserves Git %s pointer format %s", async (kind, format) => {
    const { root, stateDirectory, environment, git } = await fixture();
    const common = join(
      root,
      format === "spaces"
        ? process.platform === "win32"
          ? "metadata with spaces"
          : " metadata with spaces "
        : format === "newline-before-nul"
          ? "metadata.git\n"
          : "metadata.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-")) {
      if (kind === "gitdir") {
        repository = common;
        await writeFile(join(common, ".git"), "gitdir: \0ignored\r\n");
      } else {
        await writeFile(join(repository, ".git"), `gitdir: ${common}\n`);
        await writeFile(
          join(common, "commondir"),
          format === "self-nul" ? "\0ignored\r\n" : "\r\n",
        );
      }
    } else {
      const pointer = `${common}${format === "spaces" ? "" : "\0ignored"}\r\n`;
      if (kind === "gitdir")
        await writeFile(join(repository, ".git"), `gitdir: ${pointer}`);
      else {
        await writeFile(join(repository, "HEAD"), "ref: refs/heads/main\n");
        await writeFile(join(repository, "commondir"), pointer);
      }
    }
    // Git accepts the exact bytes, including a path newline immediately before NUL.
    expect(git("-C", repository, "remote").trim()).toBe("origin");
    const bin = join(common, "tools");
    const marker = join(root, "pointer-git-executed");
    await mkdir(bin);
    await writeFile(
      join(bin, process.platform === "win32" ? "git.exe" : "git"),
      process.platform === "win32"
        ? "untrusted executable fixture\n"
        : '#!/bin/sh\nprintf executed > "$TEST_POINTER_GIT_MARKER"\nexit 61\n',
      { mode: 0o700 },
    );
    const inheritedPath = Object.entries(environment).find(
      ([name]) => name.toUpperCase() === "PATH",
    )?.[1];
    await prepareNormalizedLocalImport(
      input(),
      { repository, ...source },
      {
        environment: {
          ...Object.fromEntries(
            Object.entries(environment).filter(
              ([name]) => name.toUpperCase() !== "PATH",
            ),
          ),
          PATH: [bin, inheritedPath ?? ""].join(delimiter),
          TEST_POINTER_GIT_MARKER: marker,
        },
        sourceRepositories: [{ url: "https://github.com/example/project" }],
      },
    );
    expect(existsSync(marker)).toBe(false);
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test.skipIf(process.platform === "win32")(
    "Node protects associated metadata through POSIX path syntax",
    async () => {
      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");
      const cases: { repository: string; state: string }[] = [];
      for (const syntax of ["parent-traversal", "literal-backslash"]) {
        const name =
          syntax === "parent-traversal" ? "common.git" : "common\\data.git";
        const common = join(storage, name);
        git("init", "--quiet", "--bare", common);
        const pointer =
          syntax === "parent-traversal"
            ? "../pointer-link/../common.git"
            : `../storage/${name}`;
        for (const kind of ["gitdir", "commondir"]) {
          const repository = join(root, `${syntax}-${kind}`);
          await mkdir(repository);
          if (kind === "gitdir") {
            await writeFile(join(repository, ".git"), `gitdir: ${pointer}\n`);
          } else {
            await writeFile(join(repository, "HEAD"), "ref: refs/heads/main\n");
            await writeFile(join(repository, "commondir"), `${pointer}\n`);
          }
          // Git must accept the pointer before testing the SDK's state boundary.
          git("-C", repository, "rev-parse", "--git-common-dir");
          cases.push({ repository, state: join(common, "runtime-state") });
        }
      }
      // Exercise supported Node filesystem semantics, independent of Bun's realpath.
      // Keep the bundle beneath the SDK so Node resolves external packages.
      const bundle = await mkdtemp(
        join(import.meta.dir, "..", ".import-metadata-"),
      );
      try {
        const built = await Bun.build({
          entrypoints: [
            join(import.meta.dir, "..", "src", "external-findings-local.ts"),
          ],
          outdir: bundle,
          target: "node",
          format: "esm",
          packages: "external",
        });
        expect(built.success).toBe(true);
        const invocation = join(bundle, "invoke.mjs");
        await writeFile(
          invocation,
          `
          import assert from "node:assert/strict";
          import { existsSync } from "node:fs";
          import { listLocalExternalFindings, prepareNormalizedLocalImport } from ${JSON.stringify(pathToFileURL(built.outputs[0]!.path).href)};
          for (const { repository, state } of ${JSON.stringify(cases)}) {
            let helperCalls = 0;
            const dependencies = {
              environment: { ...process.env, CODEX_SECURITY_STATE_DIR: state },
              runWorkbench: async () => {
                helperCalls++;
                return { findings: [], counts: { created: 0, updated: 0, unchanged: 0 } };
              },
            };
            await assert.rejects(async () => {
              const prepared = await prepareNormalizedLocalImport(
                ${JSON.stringify(input())},
                { repository, ...${JSON.stringify(source)} },
                dependencies,
              );
              await prepared.import();
            }, /must be outside the protected scan root/);
            await assert.rejects(
              listLocalExternalFindings({ repository }, dependencies),
              /must be outside the protected scan root/,
            );
            assert.equal(helperCalls, 0, repository);
            assert.equal(existsSync(state), false, state);
          }
          console.log("metadata-pointer-cases:4");
        `,
        );
        const child = Bun.spawn([Bun.which("node")!, invocation], {
          env: environment,
          stdout: "pipe",
          stderr: "pipe",
        });
        const [code, stdout, stderr] = await Promise.all([
          child.exited,
          new Response(child.stdout).text(),
          new Response(child.stderr).text(),
        ]);
        expect({ code, stderr }).toEqual({ code: 0, stderr: "" });
        expect(stdout.trim()).toBe("metadata-pointer-cases:4");
      } finally {
        await rm(bundle, { recursive: true, force: true });
      }
    },
  );

  test.skipIf(process.platform !== "win32")(
    "Node protects Windows metadata resolved from selected and discovered directories",
    async () => {
      const { root, environment } = await fixture();
      const caller = join(root, "caller");
      await mkdir(caller);
      const bundle = await mkdtemp(
        join(import.meta.dir, "..", ".import-metadata-"),
      );
      try {
        const built = await Bun.build({
          entrypoints: [
            join(import.meta.dir, "..", "src", "external-findings-local.ts"),
          ],
          outdir: bundle,
          target: "node",
          format: "esm",
          packages: "external",
        });
        expect(built.success).toBe(true);
        const invocation = join(bundle, "invoke.mjs");
        await writeFile(
          invocation,
          String.raw`
          import assert from "node:assert/strict";
          import { execFileSync } from "node:child_process";
          import { existsSync } from "node:fs";
          import { mkdir, realpath, writeFile } from "node:fs/promises";
          import { join, parse } from "node:path";
          import { listLocalExternalFindings, prepareNormalizedLocalImport } from ${JSON.stringify(pathToFileURL(built.outputs[0]!.path).href)};
          const root = ${JSON.stringify(root)};
          const drive = parse(root).root.slice(0, 2);
          const git = (args) => execFileSync("git", args, {
            encoding: "utf8", stdio: ["ignore", "pipe", "pipe"],
          }).trim();
          const createStore = (path) => {
            git(["init", "--quiet", "--bare", path]);
            // These stores back worktrees, so Git changes to the discovered checkout.
            git(["config", "--file", join(path, "config"), "core.bare", "false"]);
          };
          const cases = [];
          {
            const checkout = join(root, "drive-relative-checkout");
            const selected = join(checkout, "src");
            const common = join(root, "drive-relative-common.git");
            await mkdir(selected, { recursive: true });
            createStore(common);
            await writeFile(join(checkout, ".git"),
              "gitdir: " + drive + "..\\..\\drive-relative-common.git\n");
            cases.push({ selected, common, protectedRoots: [common] });
          }
          {
            const checkout = join(root, "commondir-checkout");
            const selected = join(checkout, "src");
            const metadata = join(root, "linked-metadata");
            const discoveryCommon = join(checkout, "common.git");
            const configuredCommon = join(root, "common.git");
            await mkdir(selected, { recursive: true });
            await mkdir(metadata);
            createStore(discoveryCommon);
            createStore(configuredCommon);
            await writeFile(join(checkout, ".git"), "gitdir: " + metadata + "\n");
            await writeFile(join(metadata, "HEAD"), "ref: refs/heads/main\n");
            await writeFile(join(metadata, "commondir"), drive + "..\\common.git\n");
            cases.push({
              selected,
              common: configuredCommon,
              protectedRoots: [discoveryCommon, configuredCommon],
            });
          }
          let protectedRootCount = 0;
          for (const { selected, common, protectedRoots } of cases) {
            assert.notEqual(await realpath(process.cwd()), await realpath(selected));
            // Check Git's final metadata resolution before relying on the fixture.
            const actual = git(["-C", selected, "rev-parse", "--path-format=absolute", "--git-common-dir"]);
            assert.equal(await realpath(actual), await realpath(common));
            for (const protectedRoot of protectedRoots) {
              const state = join(protectedRoot, "runtime-state");
              let helperCalls = 0;
              const dependencies = {
                environment: { ...process.env, CODEX_SECURITY_STATE_DIR: state },
                runWorkbench: async () => {
                  helperCalls++;
                  return { findings: [], counts: { created: 0, updated: 0, unchanged: 0 } };
                },
              };
              await assert.rejects(async () => {
                const prepared = await prepareNormalizedLocalImport(
                  ${JSON.stringify(input())},
                  { repository: selected, ...${JSON.stringify(source)} },
                  dependencies,
                );
                await prepared.import();
              }, /must be outside the protected scan root/);
              await assert.rejects(
                listLocalExternalFindings({ repository: selected }, dependencies),
                /must be outside the protected scan root/,
              );
              assert.equal(helperCalls, 0, protectedRoot);
              assert.equal(existsSync(state), false, state);
              protectedRootCount++;
            }
          }
          assert.equal(protectedRootCount, 3);
          console.log("windows-metadata-roots:3");
        `,
        );
        const child = Bun.spawn([Bun.which("node")!, invocation], {
          cwd: caller,
          env: environment,
          stdout: "pipe",
          stderr: "pipe",
        });
        const [code, stdout, stderr] = await Promise.all([
          child.exited,
          new Response(child.stdout).text(),
          new Response(child.stderr).text(),
        ]);
        expect({ code, stderr }).toEqual({ code: 0, stderr: "" });
        expect(stdout.trim()).toBe("windows-metadata-roots:3");
      } finally {
        await rm(bundle, { recursive: true, force: true });
      }
    },
  );

  test("preserves Git pointer formats and invalid-pointer diagnostics", async () => {
    const { root, environment, stateDirectory, git } = await fixture();
    const repository = join(root, "linked");
    const common = join(root, "common.git");
    await mkdir(repository);
    git("init", "--quiet", "--bare", common);
    git(
      "-C",
      common,
      "remote",
      "add",
      "origin",
      "https://github.com/example/project",
    );
    const marker = join(repository, ".git");
    const dependencies = {
      environment,
      sourceRepositories: [{ url: "https://github.com/example/project" }],
    };
    await writeFile(marker, `gitdir: ${common}\0trailing\n`);
    await prepareNormalizedLocalImport(
      input(),
      { repository, ...source },
      dependencies,
    );
    await writeFile(marker, "gitdir: \0invalid\n");
    const diagnostic = spawnSync(
      "git",
      ["-c", "core.fsmonitor=false", "-C", repository, "remote"],
      {
        encoding: "utf8",
        env: environment,
      },
    );
    expect(diagnostic.status).toBe(128);
    await expect(
      prepareNormalizedLocalImport(
        input(),
        { repository, ...source },
        dependencies,
      ),
    ).rejects.toMatchObject({
      code: diagnostic.status,
      stderr: diagnostic.stderr,
    });
    const unrelated = join(root, "unrelated");
    for (const name of ["HEAD", "objects", "refs"])
      await mkdir(join(unrelated, name), { recursive: true });
    await writeFile(marker, `gitdir: ${unrelated}\n`);
    let helperCalls = 0;
    await listLocalExternalFindings(
      { repository },
      {
        environment: {
          ...environment,
          CODEX_SECURITY_STATE_DIR: join(unrelated, "state"),
        },
        runWorkbench: async () => {
          helperCalls++;
          return { findings: [] };
        },
      },
    );
    expect(helperCalls).toBe(1);
    expect(existsSync(stateDirectory)).toBe(false);
    expect(existsSync(join(unrelated, "state"))).toBe(false);
  });

  test("protects the checkout containing a directory link when selecting another repository", async () => {
    const { root, repository, stateDirectory, environment } = await fixture({
      remote: "https://github.com/example/project",
    });
    const outer = await fixture();
    const link = join(outer.repository, "linked-repository");
    await symlink(
      repository,
      link,
      process.platform === "win32" ? "junction" : "dir",
    );
    const unsafeState = join(outer.repository, "state");
    await expect(
      prepareNormalizedLocalImport(
        input(),
        { repository: link, ...source },
        {
          environment: {
            ...environment,
            CODEX_SECURITY_STATE_DIR: unsafeState,
          },
        },
      ),
    ).rejects.toThrow("outside");

    const bin = join(outer.repository, "tools");
    const marker = join(root, "linked-checkout-git-executed");
    await mkdir(bin);
    await writeFile(
      join(bin, process.platform === "win32" ? "git.exe" : "git"),
      process.platform === "win32"
        ? "untrusted executable fixture\n"
        : '#!/bin/sh\nprintf executed > "$TEST_LOCAL_IMPORT_GIT_MARKER"\nexit 1\n',
      { mode: 0o700 },
    );
    const inheritedPath = Object.entries(environment).find(
      ([name]) => name.toUpperCase() === "PATH",
    )?.[1];
    const selectedEnvironment = {
      ...Object.fromEntries(
        Object.entries(environment).filter(
          ([name]) => name.toUpperCase() !== "PATH",
        ),
      ),
      PATH: [bin, inheritedPath ?? ""].join(delimiter),
      TEST_LOCAL_IMPORT_GIT_MARKER: marker,
    };
    const prepared = await prepareNormalizedLocalImport(
      input(),
      { repository: link, ...source },
      {
        environment: selectedEnvironment,
        sourceRepositories: [{ url: "https://github.com/example/project" }],
      },
    );
    expect(prepared.preview.repository).toBe(repository);
    await expect(
      prepareNormalizedLocalImport(
        input(),
        { repository: link, ...source },
        {
          environment: selectedEnvironment,
          sourceRepositories: [{ url: "https://github.com/example/other" }],
        },
      ),
    ).rejects.toThrow("does not match a configured remote");
    expect(existsSync(marker)).toBe(false);
    expect(existsSync(unsafeState)).toBe(false);
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test("matches source repository context against SSH remotes and reader metadata", async () => {
    const { repository, stateDirectory, environment, git } = await fixture({
      remote: "[email protected]:Example/Project.git",
    });
    const data = input();
    data.findings[0]!.evidence.details = {
      kind: "sast",
      repository: { url: "https://github.com/example/project" },
    };
    await prepareNormalizedLocalImport(
      data,
      { repository, ...source },
      { environment },
    );
    await expect(
      prepareNormalizedLocalImport(
        data,
        { repository, ...source },
        {
          environment,
          sourceRepositories: [{ url: "https://github.com/example/other" }],
        },
      ),
    ).rejects.toThrow("does not match a configured remote");
    data.findings[0]!.evidence.details.repository!.url =
      "https://github.com/example/other";
    await expect(
      prepareNormalizedLocalImport(
        data,
        { repository, ...source },
        { environment },
      ),
    ).rejects.toThrow("does not match a configured remote");
    git("remote", "add", "upstream", "https://github.com/example/other.git");
    await prepareNormalizedLocalImport(
      data,
      { repository, ...source },
      { environment },
    );
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test.each(
    [
      ["git", "9418"],
      ["ftp", "21"],
      ["ftps", "990"],
      ["git+ssh", "22"],
      ["ssh+git", "22"],
    ].flatMap(([protocol, defaultPort]) =>
      ["", `:${defaultPort}`, ":19418"].map(
        (port) => [protocol, port] as const,
      ),
    ),
  )("matches %s transport remotes with port %s", async (protocol, port) => {
    const { repository, stateDirectory, environment } = await fixture({
      remote: `${protocol}://git.example.test${port}/Example/Project.git`,
    });
    const expectedPort = port === ":19418" ? port : "";
    await prepareNormalizedLocalImport(
      input(),
      { repository, ...source },
      {
        environment,
        sourceRepositories: [
          {
            url: `https://git.example.test${expectedPort}/Example/Project`,
          },
        ],
      },
    );
    for (const url of [
      `https://git.example.test${expectedPort}/Example/Other`,
      `https://git.example.test${expectedPort}/example/project`,
      `https://git.example.test${expectedPort ? "" : ":19418"}/Example/Project`,
    ]) {
      await expect(
        prepareNormalizedLocalImport(
          input(),
          { repository, ...source },
          { environment, sourceRepositories: [{ url }] },
        ),
      ).rejects.toThrow("does not match a configured remote");
    }
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test.each(
    ["ssh", "git+ssh", "ssh+git"].flatMap((protocol) =>
      ["ssh.github.com", "SSH.GITHUB.COM"].map(
        (host) => [protocol, host] as const,
      ),
    ),
  )(
    "matches %s on %s over the HTTPS port without equating other SSH ports",
    async (protocol, host) => {
      const { repository, stateDirectory, environment, git } = await fixture({
        remote: `${protocol}://git@${host}:443/example/project.git`,
      });
      const dependencies = {
        environment,
        sourceRepositories: [{ url: "https://github.com/example/project" }],
      };
      await prepareNormalizedLocalImport(
        input(),
        { repository, ...source },
        dependencies,
      );
      await expect(
        prepareNormalizedLocalImport(
          input(),
          { repository, ...source },
          {
            environment,
            sourceRepositories: [{ url: "https://github.com/example/other" }],
          },
        ),
      ).rejects.toThrow("does not match a configured remote");
      git(
        "remote",
        "set-url",
        "origin",
        `${protocol}://[email protected]:2222/example/project.git`,
      );
      await expect(
        prepareNormalizedLocalImport(
          input(),
          { repository, ...source },
          dependencies,
        ),
      ).rejects.toThrow("does not match a configured remote");
      expect(existsSync(stateDirectory)).toBe(false);
    },
  );

  test.each(["\nhttps://github.com/other/repo", "\r"])(
    "a line break in the primary Git URL cannot establish a repository match: %j",
    async (suffix) => {
      const url = "https://github.com/example/project";
      const { repository, environment, git } = await fixture();
      // A quoted value preserves a trailing carriage return on older Git.
      const quotedSuffix = suffix.replaceAll("\n", "\\n");
      await appendFile(
        join(repository, ".git", "config"),
        `\n[remote "origin"]\n\turl = "${url}${quotedSuffix}"\n`,
      );
      expect(git("config", "--null", "--get", "remote.origin.url")).toBe(
        `${url}${suffix}\0`,
      );
      await expect(
        prepareNormalizedLocalImport(
          input(),
          { repository, ...source, provider: "sonar" },
          {
            environment,
            sourceRepositories: [{ url }],
          },
        ),
      ).rejects.toThrow("remote URL contains a line break");
    },
  );

  test.each([
    ["git@github-work:team/project.git", "https://github.com/team/project"],
    [
      "[email protected]:team/project.git",
      "https://github.com/team/project",
    ],
    [
      "ssh://git@github-work/team/project.git",
      "https://github.com/team/project",
    ],
  ])(
    "allows a manual Sonar mapping for %s alongside an HTTPS fork",
    async (upstream, url) => {
      const { repository, environment, git } = await fixture({
        remote: "https://fork.example.invalid/team/project.git",
      });
      git("remote", "add", "upstream", upstream);
      const prepare = (sourceUrl: string, sourceRepositoryUrl?: string) =>
        prepareNormalizedLocalImport(
          input(),
          { repository, ...source, provider: "sonar", sourceRepositoryUrl },
          { environment, sourceRepositories: [{ url: sourceUrl }] },
        );
      await expect(prepare(url)).rejects.toThrow(
        "does not match a configured remote",
      );
      await expect(prepare(url, url + "-other")).rejects.toThrow(
        "does not match the declared source repository",
      );
      expect((await prepare(url, url)).matchesRepositoryRemote).toBe(false);
      await expect(prepare(url + "-other", url + "-other")).rejects.toThrow(
        "does not match a configured remote",
      );
    },
  );

  test.each([
    ["[email protected].:team/project.git", "https://gitlab.com/team/project"],
    ["[email protected].:team/project.git", "https://gitlab.com./team/project"],
    ["git@work:team/project.git", "https://gitlab.com./team/project"],
    [
      "ssh://[email protected].:443/team/project.git",
      "https://github.com/team/project",
    ],
    [
      "[email protected].:v3/team/project/repo",
      "https://dev.azure.com/team/project/_git/repo",
    ],
  ])(
    "an explicit Sonar mapping accepts the dotted provider host in %s",
    async (remote, url) => {
      const { repository, environment } = await fixture({ remote });
      const prepared = await prepareNormalizedLocalImport(
        input(),
        { repository, ...source, provider: "sonar", sourceRepositoryUrl: url },
        { environment, sourceRepositories: [{ url }] },
      );
      expect(prepared.matchesRepositoryRemote).toBe(false);
    },
  );

  test.each([
    "[email protected]:team/project.git",
    "ssh://gitlab.com/team/project.git",
    "[email protected].:team/project.git",
    "[email protected].:team/project.git",
  ])(
    "a recognized Sonar SSH host cannot be remapped to another provider: %s",
    async (remote) => {
      const { repository, environment } = await fixture({
        remote,
      });
      await expect(
        prepareNormalizedLocalImport(
          input(),
          {
            repository,
            ...source,
            provider: "sonar",
            sourceRepositoryUrl: "https://github.com/team/project",
          },
          {
            environment,
            sourceRepositories: [{ url: "https://github.com/team/project" }],
          },
        ),
      ).rejects.toThrow("does not match a configured remote");
    },
  );

  test.each([
    [
      "[email protected].:team/project.git",
      "https://github.com/team/project",
      false,
    ],
    [
      "[email protected]:team/project.git",
      "https://github.com./team/project",
      true,
    ],
    [
      "ssh://[email protected].:443/team/project.git",
      "https://github.com./team/project",
      false,
    ],
  ] as const)(
    "a GitHub reader hint preserves a dotted Sonar mapping for %s and %s",
    async (remote, url, verified) => {
      const { repository, environment } = await fixture({ remote });
      const prepared = await prepareNormalizedLocalImport(
        input(),
        { repository, ...source, provider: "sonar", sourceRepositoryUrl: url },
        { environment, sourceRepositories: [{ url, platform: "github" }] },
      );
      expect(prepared.matchesRepositoryRemote).toBe(verified);
    },
  );

  test.each([
    "ssh://gitlab.com/team/project.git",
    "[email protected].:team/project.git",
  ])(
    "a GitHub reader hint cannot remap a recognized GitLab SSH host: %s",
    async (remote) => {
      const { repository, environment } = await fixture({ remote });
      const url = "https://github.com/team/project";
      await expect(
        prepareNormalizedLocalImport(
          input(),
          {
            repository,
            ...source,
            provider: "sonar",
            sourceRepositoryUrl: url,
          },
          { environment, sourceRepositories: [{ url, platform: "github" }] },
        ),
      ).rejects.toThrow("does not match a configured remote");
    },
  );

  test("the normalized Sonar repository selection must agree with retained and reader sources, even outside Git", async () => {
    const url = "https://github.com/example/project";
    const { repository, root, environment } = await fixture({ remote: url });
    const directory = join(root, "plain");
    await mkdir(directory);
    const retained = {
      ...input(),
      report: { format: "sonar-issues/v1", repositoryUrl: url },
    };
    for (const checkout of [repository, directory]) {
      const options = {
        repository: checkout,
        ...source,
        provider: "sonar" as const,
        sourceRepositoryUrl: url + "-other",
      };
      await expect(
        prepareNormalizedLocalImport(retained, options, { environment }),
      ).rejects.toThrow("does not match the declared source repository");
      await expect(
        prepareNormalizedLocalImport(input(), options, {
          environment,
          sourceRepositories: [{ url }],
        }),
      ).rejects.toThrow("does not match the declared source repository");
    }
  });

  test("an explicit HTTPS mapping accepts an equivalent SCP reader claim for a checkout SSH alias", async () => {
    const { repository, environment } = await fixture({
      remote: "git@work:team/project.git",
    });
    const prepared = await prepareNormalizedLocalImport(
      input(),
      {
        repository,
        ...source,
        provider: "sonar",
        sourceRepositoryUrl: "https://github.com/team/project",
      },
      {
        environment,
        sourceRepositories: [{ url: "[email protected]:team/project.git" }],
      },
    );
    expect(prepared.matchesRepositoryRemote).toBe(false);
  });

  test.each([
    "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, git } = await fixture({
      remote: "[email protected]:team/other.git",
    });
    await expect(
      prepareNormalizedLocalImport(
        input(),
        { repository, ...source, provider: "sonar" },
        {
          environment,
          sourceRepositories: [{ url }],
        },
      ),
    ).rejects.toThrow("does not match a configured remote");
    git(
      "remote",
      "set-url",
      "origin",
      "[email protected]:team/project.git",
    );
    expect(
      (
        await prepareNormalizedLocalImport(
          input(),
          { repository, ...source, provider: "sonar" },
          { environment, sourceRepositories: [{ url }] },
        )
      ).matchesRepositoryRemote,
    ).toBe(false);
  });

  test.each([
    [
      "ftp://example.invalid/team/project",
      "https://example.invalid/team/project",
      false,
    ],
    [
      "https://example.invalid/team/pro\tject",
      "https://example.invalid/team/project",
      false,
    ],
    [
      "[email protected]:v3/example/My%20Project/repo",
      "https://dev.azure.com/example/My%20Project/_git/repo",
      true,
    ],
  ] as const)(
    "checks the same Sonar binding for replay of %s as for discovery",
    async (remote, url, matches) => {
      const { repository, environment } = await fixture({ remote });
      const prepared = await prepareNormalizedLocalImport(
        input(),
        { repository, ...source, provider: "sonar" },
        { environment, sourceRepositories: [{ url }] },
      );
      expect(prepared.matchesRepositoryRemote).toBe(matches);
    },
  );

  test.each(["local", "global"])(
    "matches all effective fetch URLs with %s Git URL rewrites",
    async (scope) => {
      const { root, repository, stateDirectory, environment } = await fixture({
        remote: "gh:example/project.git",
      });
      const gitEnvironment = {
        ...environment,
        GIT_CONFIG_GLOBAL: join(root, "global-git-config"),
        GIT_CONFIG_NOSYSTEM: "1",
      };
      for (const args of [
        ["config", `--${scope}`, "url.https://github.com/.insteadOf", "gh:"],
        ["config", "--add", "remote.origin.url", "gh:example/secondary.git"],
        ["remote", "add", "upstream", "gh:example/library.git"],
      ]) {
        execFileSync("git", ["-C", repository, ...args], {
          env: gitEnvironment,
        });
      }
      const module = new URL(
        "../src/external-findings-local.ts",
        import.meta.url,
      ).href;
      const script = `
        import { rejects } from "node:assert/strict";
        const { prepareNormalizedLocalImport } = await import(${JSON.stringify(module)});
        const input = { read: 0, findings: [], excluded: [] };
        const options = ${JSON.stringify({ repository, ...source })};
        for (const name of ["project", "secondary", "library"]) {
          await prepareNormalizedLocalImport(input, options, {
            sourceRepositories: [{ url: "https://github.com/example/" + name }],
          });
        }
        await rejects(
          prepareNormalizedLocalImport(input, options, {
            sourceRepositories: [{ url: "https://github.com/example/other" }],
          }),
          /does not match a configured remote/,
        );
      `;
      const result = spawnSync(process.execPath, ["-e", script], {
        encoding: "utf8",
        env: gitEnvironment,
      });
      expect(result.status, result.stderr).toBe(0);
      expect(existsSync(stateDirectory)).toBe(false);
    },
  );

  test.each(["HOME", "GIT_CONFIG_GLOBAL"])(
    "isolates concurrent injected %s configurations without redirecting the selected repository",
    async (setting) => {
      const { root, repository, stateDirectory, environment } = await fixture({
        remote: "gh:project.git",
      });
      const other = await fixture({
        remote: "https://github.com/redirected/project",
      });
      const configurations = await Promise.all(
        ["first", "second"].map(async (name) => {
          const home = join(root, name);
          await mkdir(home);
          const config = join(home, ".gitconfig");
          await writeFile(
            config,
            `[url "https://github.com/${name}/"]\n\tinsteadOf = gh:\n`,
          );
          return {
            url: `https://github.com/${name}/project`,
            environment: {
              ...environment,
              ...(setting === "HOME" ? { HOME: home, USERPROFILE: home } : {}),
              GIT_CONFIG_GLOBAL:
                setting === "GIT_CONFIG_GLOBAL" ? config : undefined,
              XDG_CONFIG_HOME: join(home, "config"),
              GIT_CONFIG_NOSYSTEM: "1",
              GIT_DIR: join(other.repository, ".git"),
              GIT_WORK_TREE: other.repository,
            },
          };
        }),
      );
      const originalEnvironments = configurations.map((configuration) => ({
        ...configuration.environment,
      }));
      await Promise.all(
        configurations.map((configuration) =>
          prepareNormalizedLocalImport(
            input(),
            { repository, ...source },
            {
              environment: configuration.environment,
              sourceRepositories: [{ url: configuration.url }],
            },
          ),
        ),
      );
      await Promise.all(
        configurations.map((configuration, index) =>
          expect(
            prepareNormalizedLocalImport(
              input(),
              { repository, ...source },
              {
                environment: configuration.environment,
                sourceRepositories: [{ url: configurations[1 - index]!.url }],
              },
            ),
          ).rejects.toThrow("does not match a configured remote"),
        ),
      );
      expect(
        configurations.map((configuration) => configuration.environment),
      ).toEqual(originalEnvironments);
      expect(existsSync(stateDirectory)).toBe(false);
      expect(existsSync(other.stateDirectory)).toBe(false);
    },
  );

  test.each([
    "C:\\repos\\source",
    "C:/repos/source",
    "/synthetic/repos/source",
    "file:///C:/repos/source",
  ])(
    "ignores local remote %s while enforcing any network remote",
    async (remote) => {
      const { repository, stateDirectory, environment, git } = await fixture({
        remote,
      });
      const dependencies = {
        environment,
        sourceRepositories: [{ url: "https://github.com/example/project" }],
      };
      await prepareNormalizedLocalImport(
        input(),
        { repository, ...source },
        dependencies,
      );
      git("remote", "add", "upstream", "https://github.com/example/project");
      await prepareNormalizedLocalImport(
        input(),
        { repository, ...source },
        dependencies,
      );
      await expect(
        prepareNormalizedLocalImport(
          input(),
          { repository, ...source },
          {
            environment,
            sourceRepositories: [{ url: "https://github.com/example/other" }],
          },
        ),
      ).rejects.toThrow("does not match a configured remote");
      expect(existsSync(stateDirectory)).toBe(false);
    },
  );

  test("preserves Git configuration errors during repository matching", async () => {
    const { repository, stateDirectory, environment } = await fixture({
      remote: "https://github.com/example/project",
    });
    await appendFile(join(repository, ".git", "config"), "[invalid\n");
    const git = spawnSync("git", ["-C", repository, "remote"], {
      encoding: "utf8",
      env: { ...environment, LC_ALL: "C" },
    });
    expect(git.status).toBe(128);
    expect(git.stderr).toContain("fatal: bad config line");
    await expect(
      prepareNormalizedLocalImport(
        input(),
        { repository, ...source },
        {
          environment,
          sourceRepositories: [{ url: "https://github.com/example/other" }],
        },
      ),
    ).rejects.toMatchObject({ code: git.status, stderr: git.stderr });
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test("accepts repository context for checkouts without remotes and plain source directories", async () => {
    const { repository, stateDirectory, environment } = await fixture();
    const dependencies = {
      environment,
      sourceRepositories: [{ url: "https://github.com/example/project" }],
    };
    await prepareNormalizedLocalImport(
      input(),
      { repository, ...source },
      dependencies,
    );
    await rm(join(repository, ".git"), { recursive: true });
    const prepared = await prepareNormalizedLocalImport(
      input(),
      { repository, ...source },
      dependencies,
    );
    expect(prepared.preview.repository).toBe(repository);
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test("requires Git for bare and linked metadata but permits plain directories without it", async () => {
    const {
      root,
      repository,
      stateDirectory,
      environment,
      git: runGit,
    } = await fixture({
      remote: "https://github.com/example/project",
    });
    const plain = join(root, "plain");
    const bare = join(root, "bare");
    const nested = join(bare, "objects", "nested");
    const linked = join(root, "linked");
    const invalid = join(root, "invalid");
    await Promise.all([mkdir(plain), mkdir(linked), mkdir(invalid)]);
    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"),
      writeFile(join(linked, "commondir"), `${bare}\n`),
      writeFile(join(invalid, ".git"), "not a Git file\n"),
    ]);
    const sourceRepositories = [{ url: "https://github.com/example/other" }];
    const withoutGit = {
      ...Object.fromEntries(
        Object.entries(environment).filter(
          ([name]) => name.toUpperCase() !== "PATH",
        ),
      ),
      PATH: "",
    };
    const prepared = await prepareNormalizedLocalImport(
      input(),
      { repository: plain, ...source },
      { environment: withoutGit, sourceRepositories },
    );
    expect(prepared.preview.repository).toBe(plain);
    for (const selected of [repository, bare, nested, linked]) {
      await expect(
        prepareNormalizedLocalImport(
          input(),
          { repository: selected, ...source },
          { environment, sourceRepositories },
        ),
      ).rejects.toThrow("does not match a configured remote");
      await expect(
        prepareNormalizedLocalImport(
          input(),
          { repository: selected, ...source },
          { environment: withoutGit, sourceRepositories },
        ),
      ).rejects.toThrow("Git is not available on a trusted PATH");
    }
    const git = spawnSync("git", ["-C", invalid, "remote"], {
      encoding: "utf8",
      env: { ...environment, LC_ALL: "C" },
    });
    expect(git.status).toBe(128);
    expect(git.stderr).toContain("fatal: invalid gitfile format");
    await expect(
      prepareNormalizedLocalImport(
        input(),
        { repository: invalid, ...source },
        { environment, sourceRepositories },
      ),
    ).rejects.toMatchObject({ code: git.status, stderr: git.stderr });
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test.each(["checkout", "plain", "plain-with-head-directory"])(
    "repository matching ignores Git executables in a separate %s caller directory",
    async (kind) => {
      const { root, repository, stateDirectory, environment } = await fixture({
        remote: "https://github.com/example/project",
      });
      const caller = join(root, "caller");
      const callerDirectory = join(caller, "src");
      const callerBin =
        kind === "checkout"
          ? join(caller, "tools")
          : join(callerDirectory, "node_modules", ".bin");
      const marker = join(root, "caller-git-executed");
      await Promise.all([
        mkdir(callerDirectory, { recursive: true }),
        mkdir(callerBin, { recursive: true }),
      ]);
      if (kind === "checkout") await mkdir(join(caller, ".git"));
      if (kind === "plain-with-head-directory")
        for (const name of ["HEAD", "objects", "refs"])
          await mkdir(join(callerDirectory, name));
      await writeFile(
        join(callerBin, process.platform === "win32" ? "git.exe" : "git"),
        process.platform === "win32"
          ? "untrusted executable fixture\n"
          : '#!/bin/sh\nprintf executed > "$TEST_LOCAL_IMPORT_GIT_MARKER"\nexit 1\n',
        { mode: 0o700 },
      );
      const inheritedPath = Object.entries(environment).find(
        ([name]) => name.toUpperCase() === "PATH",
      )?.[1];
      const module = new URL(
        "../src/external-findings-local.ts",
        import.meta.url,
      ).href;
      const script = `
      import { rejects } from "node:assert/strict";
      const { prepareNormalizedLocalImport } = await import(${JSON.stringify(module)});
      const input = { read: 0, findings: [], excluded: [] };
      const options = ${JSON.stringify({ repository, ...source })};
      await prepareNormalizedLocalImport(input, options, {
        sourceRepositories: [{ url: "https://github.com/example/project" }],
      });
      await rejects(
        prepareNormalizedLocalImport(input, options, {
          sourceRepositories: [{ url: "https://github.com/example/other" }],
        }),
        /does not match a configured remote/,
      );
    `;
      const result = spawnSync(process.execPath, ["-e", script], {
        cwd: callerDirectory,
        encoding: "utf8",
        env: {
          ...Object.fromEntries(
            Object.entries(environment).filter(
              ([name]) =>
                name.toUpperCase() !== "PATH" &&
                (kind === "checkout" ||
                  name.toUpperCase() !== "CODEX_SECURITY_STATE_DIR"),
            ),
          ),
          ...(kind !== "checkout"
            ? {
                HOME: callerDirectory,
                USERPROFILE: callerDirectory,
                CODEX_HOME: join(callerDirectory, ".codex"),
              }
            : {}),
          PATH: [callerBin, inheritedPath ?? ""].join(delimiter),
          TEST_LOCAL_IMPORT_GIT_MARKER: marker,
        },
      });
      expect(result.status, result.stderr).toBe(0);
      expect(existsSync(marker)).toBe(false);
      expect(existsSync(stateDirectory)).toBe(false);
      expect(existsSync(join(callerDirectory, ".codex", "state"))).toBe(false);
    },
  );

  test("matches custom GitHub Enterprise names without changing other VCS identities", async () => {
    const { repository, stateDirectory, environment } = await fixture({
      remote: "[email protected]:example/project.git",
    });
    const data = input();
    data.findings[0]!.evidence.details = {
      kind: "sast",
      repository: { url: "https://git.example.test/Example/Project" },
    };
    const options = { repository, ...source, provider: "github" as const };
    const dependencies = {
      environment,
      sourceRepositories: [{ url: "https://git.example.test/EXAMPLE/PROJECT" }],
    };
    const prepared = await prepareNormalizedLocalImport(
      data,
      options,
      dependencies,
    );
    expect(prepared.preview.findings).toEqual(data.findings);
    await expect(
      prepareNormalizedLocalImport(
        data,
        { ...options, provider: "semgrep" },
        dependencies,
      ),
    ).rejects.toThrow("does not match a configured remote");
    for (const url of [
      "https://git.example.test/example/other",
      "https://other.example.test/example/project",
      "http://git.example.test/example/project",
      "https://git.example.test:8443/example/project",
    ]) {
      data.findings[0]!.evidence.details.repository!.url = url;
      await expect(
        prepareNormalizedLocalImport(data, options, dependencies),
      ).rejects.toThrow("does not match a configured remote");
    }
    expect(existsSync(stateDirectory)).toBe(false);
  });

  test.each([
    "https://git.example.test/Example/Project",
    "https://git.example.test/example/project",
    "https://git.example.test/example/project.git/",
    "HTTPS://GIT.EXAMPLE.TEST/example/project",
    "https://%67it.example.test/example/project",
    "https://git.example.test/other/../example/project",
    "https://git.example.test:443/example/project",
    "[email protected]:example/project.git",
    "ssh://[email protected]:22/example/project.git",
    "git://git.example.test:9418/example/project.git",
  ])("retains reader-provided GitHub identity from %s", async (metadataUrl) => {
    const { repository, stateDirectory, environment } = await fixture({
      remote: "[email protected]:example/project.git",
    });
    const data = input();
    const url = "https://git.example.test/Example/Project";
    data.findings[0]!.evidence.details = { kind: "sast", repository: { url } };
    const options = { repository, ...source, provider: "wiz" as const };
    await expect(
      prepareNormalizedLocalImport(data, options, { environment }),
    ).rejects.toThrow("does not match a configured remote");
    const dependencies = {
      environment,
      sourceRepositories: [
        {
          url: metadataUrl,
          platform: "github" as const,
          extra: { retained: true },
        },
        { url },
      ],
    };
    const originalSources = structuredClone(dependencies.sourceRepositories);
    const prepared = await prepareNormalizedLocalImport(
      data,
      options,
      dependencies,
    );
    expect(prepared.preview.findings).toEqual(data.findings);
    expect(dependencies.sourceRepositories).toEqual(originalSources);
    for (const different of [
      "https://git.example.test/example/other",
      "https://other.example.test/example/project",
      "http://git.example.test/example/project",
      "https://git.example.test:8443/example/project",
    ]) {
      await expect(
        prepareNormalizedLocalImport(data, options, {
          ...dependencies,
          sourceRepositories: [
            ...dependencies.sourceRepositories,
            { url: different, platform: "github" },
          ],
        }),
      ).rejects.toThrow("does not match a configured remote");
    }
    expect(existsSync(stateDirectory)).toBe(false);
  });
});