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

feat(imports): store external findings locally (#1847)

Michael D'Angeloe6ce47f67cc3
diff --git a/plugins/codex-security/mcp-app/helpers-main.ts b/plugins/codex-security/mcp-app/helpers-main.ts
index ec7f32402..a05ed29b2 100644
--- a/plugins/codex-security/mcp-app/helpers-main.ts
+++ b/plugins/codex-security/mcp-app/helpers-main.ts
@@ -65,6 +65,10 @@ function runHelper(): void {
"Usage: store-findings\nReads a JSON object from stdin with an absolute stateDirectory and payload.entries containing finding and embedding records; payload.repositoryId is optional.",
"list-stored-findings":
"Usage: list-stored-findings\nReads a JSON object from stdin with an absolute stateDirectory, positive payload.limit and non-negative payload.offset.",
+ "store-external-findings":
+ "Usage: store-external-findings\nReads a JSON object from stdin with an absolute stateDirectory and payload containing repository, provider, sourceKey and findings with source_finding_id and evidence.",
+ "list-external-findings":
+ "Usage: list-external-findings\nReads a JSON object from stdin with an absolute stateDirectory and payload.repository; payload.id optionally selects one external finding.",
"find-potential-duplicates":
"Usage: find-potential-duplicates\nReads a JSON object from stdin with an absolute stateDirectory, payload.findingId and payload.scope containing either repositoryId or allRepositories: true.",
"store-dedupe-groups":
diff --git a/plugins/codex-security/mcp-app/src/workbench/commands.ts b/plugins/codex-security/mcp-app/src/workbench/commands.ts
index 67d5bf25b..8d610db3b 100644
--- a/plugins/codex-security/mcp-app/src/workbench/commands.ts
+++ b/plugins/codex-security/mcp-app/src/workbench/commands.ts
@@ -10,6 +10,11 @@ import {
listDedupeGroups,
storeDedupeGroups,
} from "./duplicates";
+import {
+ listExternalFindings,
+ storeExternalFindings,
+ type ExternalFindingsImport,
+} from "./external-findings";
import {
readSeverityClassification,
@@ -76,11 +81,23 @@ export async function workbenchCommand(
{
deferred:
command !== "store-findings" &&
+ command !== "store-external-findings" &&
command !== "store-dedupe-groups" &&
command !== "severity-classification",
},
);
try {
+ if (command === "store-external-findings")
+ return storeExternalFindings(
+ database,
+ payload as ExternalFindingsImport,
+ new Date().toISOString(),
+ );
+ if (command === "list-external-findings")
+ return listExternalFindings(
+ database,
+ payload as { repository: string; id?: string },
+ );
if (command === "severity-classification")
return severityCheckpoint(
database,
diff --git a/plugins/codex-security/mcp-app/src/workbench/external-findings.ts b/plugins/codex-security/mcp-app/src/workbench/external-findings.ts
new file mode 100644
index 000000000..7d14f2112
--- /dev/null
+++ b/plugins/codex-security/mcp-app/src/workbench/external-findings.ts
@@ -0,0 +1,174 @@
+import { createHash } from "node:crypto";
+import { isAbsolute } from "node:path";
+import type { DatabaseSync } from "node:sqlite";
+import { isDeepStrictEqual } from "node:util";
+import {
+ object,
+ parseJson,
+ requireFiniteNumbers,
+ stringifyJson,
+} from "../helpers/json";
+import { requireSqliteText } from "./database";
+import { transaction } from "./transaction";
+
+export interface ExternalFindingInput {
+ source_finding_id: string;
+ evidence: Record<string, unknown>;
+}
+
+export interface ExternalFindingsImport {
+ repository: string;
+ provider: string;
+ sourceKey: string;
+ findings: ExternalFindingInput[];
+}
+
+export interface StoredExternalFinding {
+ id: string;
+ repository: string;
+ provider: string;
+ sourceKey: string;
+ sourceFindingId: string;
+ evidence: Record<string, unknown>;
+ assessment: { state: "not_assessed" };
+ version: number;
+ createdAt: string;
+ updatedAt: string;
+}
+
+function requireText(value: unknown, name: string): asserts value is string {
+ if (typeof value !== "string" || value.length === 0)
+ throw new TypeError(`${name} must be a non-empty string.`);
+ requireSqliteText([value]);
+}
+
+function requireRepository(repository: unknown): asserts repository is string {
+ requireText(repository, "repository");
+ if (!isAbsolute(repository))
+ throw new TypeError("repository must be an absolute local path.");
+}
+
+function fromRow(row: Record<string, unknown>): StoredExternalFinding {
+ return {
+ id: String(row.id),
+ repository: String(row.repository),
+ provider: String(row.provider),
+ sourceKey: String(row.source_key),
+ sourceFindingId: String(row.source_finding_id),
+ evidence: parseJson(String(row.evidence_json)) as Record<string, unknown>,
+ assessment: { state: "not_assessed" },
+ version: Number(row.version),
+ createdAt: String(row.created_at),
+ updatedAt: String(row.updated_at),
+ };
+}
+
+export function storeExternalFindings(
+ database: DatabaseSync,
+ input: ExternalFindingsImport,
+ timestamp: string,
+): {
+ findings: StoredExternalFinding[];
+ counts: { created: number; updated: number; unchanged: number };
+} {
+ requireRepository(input?.repository);
+ requireText(input.provider, "provider");
+ requireText(input.sourceKey, "sourceKey");
+ requireSqliteText([timestamp]);
+ if (!Array.isArray(input.findings))
+ throw new TypeError("findings must be an array.");
+
+ // Validate and serialize the complete batch before taking the write lock.
+ const sourceIds = new Set<string>();
+ const entries = input.findings.map((finding) => {
+ requireText(finding?.source_finding_id, "source_finding_id");
+ if (sourceIds.has(finding.source_finding_id))
+ throw new TypeError(
+ "findings must not contain duplicate source_finding_id values.",
+ );
+ sourceIds.add(finding.source_finding_id);
+ if (!object(finding.evidence))
+ throw new TypeError("evidence must be an object.");
+ requireFiniteNumbers(finding.evidence);
+ const evidenceJson = stringifyJson(finding.evidence, 0);
+ const identity = [
+ input.repository,
+ input.provider,
+ input.sourceKey,
+ finding.source_finding_id,
+ ];
+ return {
+ id: `ext_${createHash("sha256").update(JSON.stringify(identity)).digest("hex")}`,
+ sourceFindingId: finding.source_finding_id,
+ evidenceJson,
+ };
+ });
+
+ return transaction(database, "BEGIN IMMEDIATE", () => {
+ const stored = database.prepare(
+ "SELECT * FROM external_source_reports WHERE id = ?",
+ );
+ const insert = database.prepare(
+ `INSERT INTO external_source_reports
+ (id, repository, provider, source_key, source_finding_id,
+ evidence_json, version, created_at, updated_at)
+ VALUES (?, ?, ?, ?, ?, ?, 1, ?, ?)`,
+ );
+ const update = database.prepare(
+ `UPDATE external_source_reports
+ SET evidence_json = ?, version = version + 1, updated_at = ?
+ WHERE id = ?`,
+ );
+ const counts = { created: 0, updated: 0, unchanged: 0 };
+ const findings = entries.map((entry) => {
+ const previous = stored.get(entry.id);
+ if (!previous) {
+ insert.run(
+ entry.id,
+ input.repository,
+ input.provider,
+ input.sourceKey,
+ entry.sourceFindingId,
+ entry.evidenceJson,
+ timestamp,
+ timestamp,
+ );
+ counts.created++;
+ } else if (
+ isDeepStrictEqual(
+ parseJson(String(previous.evidence_json)),
+ parseJson(entry.evidenceJson),
+ )
+ ) {
+ counts.unchanged++;
+ } else {
+ update.run(entry.evidenceJson, timestamp, entry.id);
+ counts.updated++;
+ }
+ return fromRow(stored.get(entry.id)!);
+ });
+ return { findings, counts };
+ });
+}
+
+export function listExternalFindings(
+ database: DatabaseSync,
+ selection: { repository: string; id?: string },
+): { findings: StoredExternalFinding[] } {
+ requireRepository(selection?.repository);
+ if (selection.id !== undefined) requireText(selection.id, "id");
+ const rows =
+ selection.id === undefined
+ ? database
+ .prepare(
+ `SELECT * FROM external_source_reports
+ WHERE repository = ? ORDER BY created_at, id`,
+ )
+ .all(selection.repository)
+ : database
+ .prepare(
+ "SELECT * FROM external_source_reports WHERE repository = ? AND id = ?",
+ )
+ .all(selection.repository, selection.id);
+ return { findings: rows.map(fromRow) };
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_external_findings.ts b/plugins/codex-security/mcp-app/tests/test_external_findings.ts
new file mode 100644
index 000000000..aff718b6f
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/test_external_findings.ts
@@ -0,0 +1,262 @@
+import assert from "node:assert/strict";
+import { execFileSync } from "node:child_process";
+import { join } from "node:path";
+import { DatabaseSync } from "node:sqlite";
+import { after, test, type TestContext } from "node:test";
+import { fileURLToPath } from "node:url";
+import { importSource } from "./import-module.ts";
+import { createTemporaryDirectories } from "./support/temporary-directories.ts";
+import type * as External from "../src/workbench/external-findings.ts";
+import type * as Migrations from "../src/workbench/migrations.ts";
+
+const { storeExternalFindings, listExternalFindings } = (await importSource(
+ "src/workbench/external-findings.ts",
+)) as typeof External;
+const { applyMigrations, migrations } = (await importSource(
+ "src/workbench/migrations.ts",
+)) as typeof Migrations;
+const temporary = createTemporaryDirectories(true);
+after(() => temporary.cleanup());
+const repository = join(process.cwd(), "synthetic-repository");
+const created = "2026-01-01T00:00:00.000Z";
+const updated = "2026-01-02T00:00:00.000Z";
+
+function open(t: TestContext) {
+ const database = new DatabaseSync(":memory:");
+ t.after(() => database.close());
+ applyMigrations(database);
+ return database;
+}
+
+function batch(): External.ExternalFindingsImport {
+ return {
+ repository,
+ provider: "synthetic-scanner",
+ sourceKey: "project-one",
+ findings: [
+ {
+ source_finding_id: "finding-one",
+ evidence: {
+ title: "Dependency finding",
+ source_data: { retained: { fields: [null, "value"] } },
+ commit_sha: "a".repeat(40),
+ },
+ },
+ { source_finding_id: "finding-two", evidence: {} },
+ ],
+ };
+}
+
+test("external imports preserve incomplete evidence without creating assessed findings or scans", (t) => {
+ const database = open(t);
+ const input = batch();
+ const result = storeExternalFindings(database, input, created);
+ assert.deepEqual(result.counts, { created: 2, updated: 0, unchanged: 0 });
+ for (const [index, finding] of result.findings.entries()) {
+ assert.match(finding.id, /^ext_[0-9a-f]{64}$/u);
+ assert.deepEqual(finding, {
+ id: finding.id,
+ repository,
+ provider: input.provider,
+ sourceKey: input.sourceKey,
+ sourceFindingId: input.findings[index].source_finding_id,
+ evidence: input.findings[index].evidence,
+ assessment: { state: "not_assessed" },
+ version: 1,
+ createdAt: created,
+ updatedAt: created,
+ });
+ }
+ for (const table of ["scans", "findings", "finding_occurrences"])
+ assert.equal(
+ database.prepare(`SELECT COUNT(*) AS count FROM ${table}`).get()!.count,
+ 0,
+ );
+});
+
+test("repeat imports keep identities and unchanged timestamps; changed evidence increments once", (t) => {
+ const database = open(t);
+ const input = batch();
+ const first = storeExternalFindings(database, input, created);
+ const originalJson = database
+ .prepare("SELECT evidence_json FROM external_source_reports WHERE id = ?")
+ .get(first.findings[0].id)!.evidence_json;
+ const reordered = {
+ ...input,
+ findings: [
+ {
+ source_finding_id: "finding-one",
+ evidence: {
+ commit_sha: "a".repeat(40),
+ source_data: { retained: { fields: [null, "value"] } },
+ title: "Dependency finding",
+ },
+ },
+ ],
+ };
+ assert.deepEqual(storeExternalFindings(database, reordered, updated), {
+ findings: [first.findings[0]],
+ counts: { created: 0, updated: 0, unchanged: 1 },
+ });
+ assert.equal(
+ database
+ .prepare("SELECT evidence_json FROM external_source_reports WHERE id = ?")
+ .get(first.findings[0].id)!.evidence_json,
+ originalJson,
+ );
+ reordered.findings[0].evidence.title = "Updated source finding";
+ const changed = storeExternalFindings(database, reordered, updated);
+ assert.deepEqual(changed.counts, { created: 0, updated: 1, unchanged: 0 });
+ assert.deepEqual(changed.findings[0], {
+ ...first.findings[0],
+ evidence: reordered.findings[0].evidence,
+ version: 2,
+ updatedAt: updated,
+ });
+ const all = listExternalFindings(database, { repository }).findings;
+ assert.equal(all.length, 2);
+ assert.deepEqual(
+ all.find(({ id }) => id === first.findings[1].id),
+ first.findings[1],
+ );
+});
+
+test("repository, provider and source namespaces keep distinct finding identities", (t) => {
+ const database = open(t);
+ const input = batch();
+ input.findings = input.findings.slice(0, 1);
+ const inputs = [
+ input,
+ { ...input, repository: join(repository, "another") },
+ { ...input, provider: "another-scanner" },
+ { ...input, sourceKey: "project-two" },
+ ];
+ const stored = inputs.map(
+ (item) => storeExternalFindings(database, item, created).findings[0],
+ );
+ assert.equal(new Set(stored.map(({ id }) => id)).size, 4);
+ assert.equal(
+ listExternalFindings(database, { repository }).findings.length,
+ 3,
+ );
+ assert.deepEqual(
+ listExternalFindings(database, { repository, id: stored[0].id }).findings,
+ [stored[0]],
+ );
+ assert.deepEqual(
+ listExternalFindings(database, { repository, id: stored[1].id }).findings,
+ [],
+ );
+});
+
+test("invalid batches and failed writes preserve all previously stored evidence", (t) => {
+ const database = open(t);
+ const input = batch();
+ storeExternalFindings(database, input, created);
+ const before = listExternalFindings(database, { repository });
+ const duplicate = {
+ ...input,
+ findings: [
+ input.findings[0],
+ { ...input.findings[0], evidence: { changed: true } },
+ ],
+ };
+ assert.throws(
+ () => storeExternalFindings(database, duplicate, updated),
+ /duplicate source_finding_id/u,
+ );
+ assert.deepEqual(listExternalFindings(database, { repository }), before);
+ database.exec(`CREATE TRIGGER fail_external_insert BEFORE INSERT ON external_source_reports
+ WHEN NEW.source_finding_id = 'fail'
+ BEGIN SELECT RAISE(ABORT, 'synthetic write failure'); END;`);
+ assert.throws(
+ () =>
+ storeExternalFindings(
+ database,
+ {
+ ...input,
+ findings: [
+ { ...input.findings[0], evidence: { changed: true } },
+ { source_finding_id: "inserted-before-failure", evidence: {} },
+ { source_finding_id: "fail", evidence: {} },
+ ],
+ },
+ updated,
+ ),
+ /synthetic write failure/u,
+ );
+ assert.deepEqual(listExternalFindings(database, { repository }), before);
+});
+
+test("a Python database at the preceding schema upgrades in Node and remains readable in Python", async () => {
+ const directory = await temporary.create("external-migration-");
+ const path = join(directory, "workbench.sqlite3");
+ execFileSync(process.env.PYTHON ?? "python", [
+ "-I",
+ "-B",
+ "-X",
+ "utf8",
+ "-c",
+ `import sqlite3, sys
+sys.path.insert(0, sys.argv[1])
+import workbench_db
+workbench_db.MIGRATIONS = tuple(item for item in workbench_db.MIGRATIONS if item[0] < 53)
+with sqlite3.connect(sys.argv[2]) as db:
+ db.row_factory = sqlite3.Row
+ workbench_db.apply_migrations(db)`,
+ fileURLToPath(new URL("../../scripts/", import.meta.url)),
+ path,
+ ]);
+ const database = new DatabaseSync(path);
+ try {
+ assert.equal(
+ database
+ .prepare("SELECT MAX(version) AS version FROM schema_migrations")
+ .get()!.version,
+ 52,
+ );
+ applyMigrations(database);
+ assert.equal(
+ database
+ .prepare("SELECT MAX(version) AS version FROM schema_migrations")
+ .get()!.version,
+ migrations.at(-1)!.version,
+ );
+ storeExternalFindings(database, batch(), created);
+ applyMigrations(database);
+ assert.equal(
+ listExternalFindings(database, { repository }).findings.length,
+ 2,
+ );
+ } finally {
+ database.close();
+ }
+ const evidence = execFileSync(
+ process.env.PYTHON ?? "python",
+ [
+ "-I",
+ "-B",
+ "-X",
+ "utf8",
+ "-c",
+ `import json, sqlite3, sys
+sys.path.insert(0, sys.argv[1])
+from workbench_db import apply_migrations
+with sqlite3.connect(sys.argv[2]) as db:
+ db.row_factory = sqlite3.Row
+ apply_migrations(db)
+ print(json.dumps([[row["source_finding_id"], json.loads(row["evidence_json"])]
+ for row in db.execute("SELECT source_finding_id, evidence_json FROM external_source_reports ORDER BY source_finding_id")]))`,
+ fileURLToPath(new URL("../../scripts/", import.meta.url)),
+ path,
+ ],
+ { encoding: "utf8" },
+ );
+ assert.deepEqual(
+ JSON.parse(evidence),
+ batch().findings.map(({ source_finding_id, evidence }) => [
+ source_finding_id,
+ evidence,
+ ]),
+ );
+});
diff --git a/plugins/codex-security/shared/workbench-migrations.json b/plugins/codex-security/shared/workbench-migrations.json
index 1e6a725b0..1fbcef4a9 100644
--- a/plugins/codex-security/shared/workbench-migrations.json
+++ b/plugins/codex-security/shared/workbench-migrations.json
@@ -434,5 +434,12 @@
"ALTER TABLE scan_severity_assessments ADD COLUMN classification_runs INTEGER CHECK (classification_runs BETWEEN 1 AND 10);",
"ALTER TABLE scan_severity_classifications ADD COLUMN classification_runs INTEGER CHECK (classification_runs BETWEEN 1 AND 10);"
]
+ },
+ {
+ "version": 53,
+ "name": "store external source findings without assessments",
+ "statements": [
+ "CREATE TABLE external_source_reports (\n id TEXT PRIMARY KEY,\n repository TEXT NOT NULL,\n provider TEXT NOT NULL,\n source_key TEXT NOT NULL,\n source_finding_id TEXT NOT NULL,\n evidence_json TEXT NOT NULL,\n version INTEGER NOT NULL,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL,\n UNIQUE (repository, provider, source_key, source_finding_id)\n);"
+ ]
}
]
diff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.py
index 9381029f2..4107a8d0e 100644
--- a/plugins/codex-security/tests/test_workbench_db.py
+++ b/plugins/codex-security/tests/test_workbench_db.py
@@ -72,6 +72,7 @@ EXPECTED_TABLES = {
"deep_scan_runs",
"deep_scan_worker_threads",
"deep_scan_workers",
+ "external_source_reports",
"finding_decisions",
"finding_dedupe_group_members",
"finding_dedupe_groups",
diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
index f1f7ec12d..b4975606c 100644
--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
@@ -94,6 +94,7 @@ EXPECTED_MIGRATIONS = [
(50, "persist authorized source excerpt scopes"),
(51, "preserve deep scan worker thread history"),
(52, "record severity classification review counts"),
+ (53, "store external source findings without assessments"),
]
diff --git a/sdk/typescript/scripts/fixtures/package-consumer.ts b/sdk/typescript/scripts/fixtures/package-consumer.ts
index 3ee57f218..1b96af1c9 100644
--- a/sdk/typescript/scripts/fixtures/package-consumer.ts
+++ b/sdk/typescript/scripts/fixtures/package-consumer.ts
@@ -1,6 +1,10 @@
import {
CodexSecurity,
prepareExternalPublication,
+ prepareNormalizedLocalImport,
+ listLocalExternalFindings,
+ type NormalizedLocalImportOptions,
+ type StoredExternalFinding,
prepareNormalizedExternalPublication,
type ExternalFindings,
type ExternalPublicationOptions,
@@ -370,3 +374,12 @@ const localOptions: ScanOptions = {
hosted: {},
};
void localOptions;
+
+export async function importLocalEvidence(
+ input: ExternalFindings,
+ options: NormalizedLocalImportOptions,
+): Promise<StoredExternalFinding[]> {
+ const prepared = await prepareNormalizedLocalImport(input, options);
+ await prepared.import();
+ return listLocalExternalFindings({ repository: options.repository });
+}
diff --git a/sdk/typescript/scripts/package-dist-files.mjs b/sdk/typescript/scripts/package-dist-files.mjs
index ad7d207f4..252a25d97 100644
--- a/sdk/typescript/scripts/package-dist-files.mjs
+++ b/sdk/typescript/scripts/package-dist-files.mjs
@@ -24,6 +24,7 @@ const distModules = [
"cloud-endpoint",
"cloud-publish",
"external-findings",
+ "external-findings-local",
"scanner-import",
"external-findings-publish",
"external-import-contract",
diff --git a/sdk/typescript/src/external-findings-local.ts b/sdk/typescript/src/external-findings-local.ts
new file mode 100644
index 000000000..cf863e53d
--- /dev/null
+++ b/sdk/typescript/src/external-findings-local.ts
@@ -0,0 +1,610 @@
+import { lstat, readFile, realpath, stat } from "node:fs/promises";
+import { dirname, isAbsolute, join, resolve, sep } from "node:path";
+import { isNumberObject } from "node:util/types";
+import { environmentEntry } from "./auth.js";
+import { CodexSecurityError } from "./errors.js";
+import type {
+ ExternalFindings,
+ ExternalSourceRepository,
+} from "./external-findings.js";
+import {
+ cloudRepositoryUrlKey,
+ repositoryUrlKey,
+} from "./external-import-contract.js";
+import type {
+ ExternalFindingEvidence,
+ Provider,
+} from "./external-import-models.js";
+import { parseGitRemote } from "./git-remote.js";
+import { isRecord } from "./record.js";
+import {
+ bundledPluginRoot,
+ canonicalConfigPath,
+ canonicalizeModelSafePath,
+ expandHome,
+ requireOutputOutsideRepositories,
+ runWorkbench,
+ workbenchEnvironment,
+ type WorkbenchCommandOptions,
+} from "./runtime.js";
+import {
+ abortable,
+ gitMarkerRoot,
+ gitOutput,
+ gitProtectionRoots,
+ normalizeRepository,
+ nullIfMissingPath,
+} from "./targets.js";
+
+/** A scanner source record, independent of a Codex assessment or completed scan. */
+export interface StoredExternalFinding {
+ id: string;
+ repository: string;
+ provider: Provider;
+ sourceKey: string;
+ sourceFindingId: string;
+ evidence: ExternalFindingEvidence;
+ assessment: { state: "not_assessed" };
+ version: number;
+ createdAt: string;
+ updatedAt: string;
+}
+
+export interface NormalizedLocalImportOptions {
+ /** Local checkout or source directory to associate with these records. */
+ repository: string;
+ provider: Provider;
+ /** Stable scanner organization/project/branch namespace across imports. */
+ sourceKey: string;
+}
+
+export interface LocalExternalImportDependencies {
+ environment?: NodeJS.ProcessEnv;
+ signal?: AbortSignal;
+ runWorkbench?: typeof runWorkbench;
+ /** Repository references supplied by a reader outside its projected evidence. */
+ sourceRepositories?: readonly ExternalSourceRepository[];
+}
+
+export interface LocalExternalImportPreview extends ExternalFindings {
+ repository: string;
+ source: { provider: Provider; source_key: string };
+}
+
+export interface LocalExternalImportResult {
+ repository: string;
+ read: number;
+ ready: number;
+ excluded: ExternalFindings["excluded"];
+ findings: StoredExternalFinding[];
+ counts: { created: number; updated: number; unchanged: number };
+}
+
+export interface PreparedLocalExternalImport {
+ preview: LocalExternalImportPreview;
+ import(): Promise<LocalExternalImportResult>;
+}
+
+function requireIdentity(
+ value: unknown,
+ field: string,
+): asserts value is string {
+ if (typeof value !== "string" || !value.trim() || value.includes("\0"))
+ throw new CodexSecurityError(
+ `${field} must be nonempty and contain no NUL characters.`,
+ );
+}
+
+/** Compare transport forms without changing retained scanner evidence. */
+function remoteRepositoryKey(
+ value: string,
+ provider: Provider,
+): string | undefined {
+ if (/^[A-Za-z]:[\\/]/u.test(value)) return undefined;
+ let remote;
+ try {
+ remote = parseGitRemote(value);
+ } catch {
+ return undefined;
+ }
+ if (
+ !remote ||
+ ![
+ "http:",
+ "https:",
+ "ssh:",
+ "git:",
+ "ftp:",
+ "ftps:",
+ "git+ssh:",
+ "ssh+git:",
+ ].includes(remote.url.protocol)
+ )
+ return undefined;
+ const url = remote.url;
+ const ssh = ["ssh:", "git+ssh:", "ssh+git:"].includes(url.protocol);
+ const protocol = url.protocol === "http:" ? "http:" : "https:";
+ const githubSshOverHttps =
+ ssh &&
+ url.hostname.toLowerCase() === "ssh.github.com" &&
+ url.port === "443";
+ const hostname = githubSshOverHttps ? "github.com" : url.hostname;
+ const port =
+ githubSshOverHttps ||
+ (ssh && url.port === "22") ||
+ (url.protocol === "git:" && url.port === "9418") ||
+ (url.protocol === "ftp:" && url.port === "21") ||
+ (url.protocol === "ftps:" && url.port === "990")
+ ? ""
+ : url.port;
+ // A GitHub source identifies a case-insensitive repository on custom Enterprise
+ // hosts too; other providers can refer to VCS hosts with case-sensitive paths.
+ const key = provider === "github" ? cloudRepositoryUrlKey : repositoryUrlKey;
+ return key(
+ `${protocol}//${hostname}${port ? `:${port}` : ""}/${remote.path}`,
+ );
+}
+
+async function bareMetadataRoots(
+ repository: string,
+ signal?: AbortSignal,
+): Promise<string[]> {
+ const roots: string[] = [];
+ let directory = repository;
+ for (;;) {
+ signal?.throwIfAborted();
+ const present = async (name: string) =>
+ (await abortable(
+ () => lstat(join(directory, name)).catch(nullIfMissingPath),
+ signal,
+ )) !== null;
+ // Git also discovers bare repositories and linked metadata without .git.
+ const head = await abortable(
+ () => lstat(join(directory, "HEAD")).catch(nullIfMissingPath),
+ signal,
+ );
+ if (
+ (head?.isFile() || head?.isSymbolicLink()) &&
+ ((await present("commondir")) ||
+ ((await present("objects")) && (await present("refs"))))
+ )
+ roots.push(directory);
+ const parent = dirname(directory);
+ if (parent === directory) return roots;
+ directory = parent;
+ }
+}
+
+async function metadataPointer(
+ path: string,
+ prefix: string,
+ context: { directory: string; environment: NodeJS.ProcessEnv },
+ signal?: AbortSignal,
+): Promise<string | undefined> {
+ const metadata = await abortable(
+ () => stat(path).catch(nullIfMissingPath),
+ signal,
+ );
+ if (!metadata?.isFile()) return undefined;
+ const text = await abortable(() => readFile(path, "utf8"), signal);
+ if (!text.startsWith(prefix) || (prefix === "" && text.length === 0))
+ return undefined;
+ const payload = text.slice(prefix.length).replace(/[\r\n]+$/u, "");
+ if (prefix !== "" && payload.length === 0) return undefined;
+ // Git checks emptiness before consuming the path as a NUL-terminated string.
+ const value = payload.split("\0", 1)[0]!;
+ // Preserve filesystem traversal through links before '..'.
+ let target = isAbsolute(value) ? value : `${dirname(path)}${sep}${value}`;
+ if (process.platform === "win32") {
+ const drive = /^[A-Za-z]:(?![\\/])/u.exec(value)?.[0].toUpperCase();
+ if (drive !== undefined) {
+ const directory =
+ context.directory.slice(0, 2).toUpperCase() === drive
+ ? context.directory
+ : environmentEntry(context.environment, `=${drive}`) || `${drive}\\`;
+ target = resolve(directory, value.slice(2));
+ } else if (isAbsolute(value)) target = resolve(context.directory, value);
+ }
+ return (
+ (await abortable(
+ () => realpath(target).catch(nullIfMissingPath),
+ signal,
+ )) ?? undefined
+ );
+}
+
+async function associatedMetadataRoots(
+ directory: string,
+ context: { directory: string; environment: NodeJS.ProcessEnv },
+ signal?: AbortSignal,
+ configurationDirectory?: string,
+): Promise<string[]> {
+ const head = await abortable(
+ () => lstat(join(directory, "HEAD")).catch(nullIfMissingPath),
+ signal,
+ );
+ if (!head || (!head.isFile() && !head.isSymbolicLink())) return [];
+ const commonMarker = join(directory, "commondir");
+ const hasCommon = await abortable(
+ () => lstat(commonMarker).catch(nullIfMissingPath),
+ signal,
+ );
+ const common = hasCommon
+ ? await metadataPointer(commonMarker, "", context, signal)
+ : undefined;
+ if (hasCommon && common === undefined) return [];
+ const storage = common ?? directory;
+ for (const name of ["objects", "refs"]) {
+ const metadata = await abortable(
+ () => stat(join(storage, name)).catch(nullIfMissingPath),
+ signal,
+ );
+ if (!metadata?.isDirectory()) return [];
+ }
+ const roots = common === undefined ? [directory] : [directory, common];
+ if (
+ process.platform === "win32" &&
+ configurationDirectory !== undefined &&
+ configurationDirectory !== context.directory
+ ) {
+ // Git rereads commondir after changing to the discovered repository root.
+ roots.push(
+ ...(await associatedMetadataRoots(
+ directory,
+ {
+ ...context,
+ directory: await abortable(
+ () => canonicalConfigPath(configurationDirectory),
+ signal,
+ ),
+ },
+ signal,
+ )),
+ );
+ }
+ return roots;
+}
+
+async function localGitProtectionRoots(
+ repository: string,
+ environment: NodeJS.ProcessEnv,
+ signal?: AbortSignal,
+): Promise<string[]> {
+ const roots = new Set(
+ await abortable(() => gitProtectionRoots(repository, signal), signal),
+ );
+ const context = {
+ directory: await abortable(() => canonicalConfigPath(repository), signal),
+ environment,
+ };
+ const paths = new Set([resolve(repository), context.directory]);
+ for (const path of paths) {
+ for (const root of await bareMetadataRoots(path, signal)) {
+ roots.add(root);
+ for (const associated of await associatedMetadataRoots(
+ root,
+ context,
+ signal,
+ root,
+ ))
+ roots.add(associated);
+ }
+ let directory = path;
+ for (;;) {
+ signal?.throwIfAborted();
+ const marker = join(directory, ".git");
+ const metadata = await abortable(
+ () => stat(marker).catch(nullIfMissingPath),
+ signal,
+ );
+ if (metadata !== null) {
+ roots.add(directory);
+ const gitDirectory = metadata.isDirectory()
+ ? await abortable(() => realpath(marker), signal)
+ : await metadataPointer(marker, "gitdir: ", context, signal);
+ if (gitDirectory !== undefined)
+ for (const associated of await associatedMetadataRoots(
+ gitDirectory,
+ context,
+ signal,
+ directory,
+ ))
+ roots.add(associated);
+ }
+ const parent = dirname(directory);
+ if (parent === directory) break;
+ directory = parent;
+ }
+ }
+ for (const root of [...roots])
+ roots.add(await abortable(() => canonicalConfigPath(root), signal));
+ return [...roots];
+}
+
+async function hasGitMetadata(
+ repository: string,
+ signal?: AbortSignal,
+): Promise<boolean> {
+ return (
+ (await abortable(
+ () => gitMarkerRoot(repository, signal, "nearest"),
+ signal,
+ )) !== null || (await bareMetadataRoots(repository, signal)).length !== 0
+ );
+}
+
+async function requireMatchingRepository(
+ repository: string,
+ provider: Provider,
+ sources: readonly ExternalSourceRepository[],
+ protectedRoots: WorkbenchCommandOptions["protectedRoot"],
+ environment: WorkbenchCommandOptions["environment"],
+ signal?: AbortSignal,
+): Promise<void> {
+ if (sources.length === 0) return;
+ if (!(await hasGitMetadata(repository, signal))) return;
+ const gitProtectedRoots = [
+ ...(typeof protectedRoots === "string"
+ ? [protectedRoots]
+ : (protectedRoots ?? [])),
+ await abortable(() => canonicalConfigPath(process.cwd()), signal),
+ ];
+ const readGit = (args: readonly string[]) =>
+ abortable(
+ () =>
+ gitOutput(
+ repository,
+ args,
+ signal,
+ { LC_ALL: "C" },
+ undefined,
+ "utf8",
+ gitProtectedRoots,
+ environment,
+ ),
+ signal,
+ );
+ let remoteNames: string;
+ try {
+ remoteNames = await readGit(["remote"]);
+ } catch (error) {
+ signal?.throwIfAborted();
+ // A local source directory need not be a Git checkout.
+ if (
+ error instanceof Error &&
+ "code" in error &&
+ error.code === 128 &&
+ "stderr" in error &&
+ typeof error.stderr === "string" &&
+ (error.stderr.trimEnd() ===
+ "fatal: not a git repository (or any of the parent directories): .git" ||
+ /^fatal: not a git repository \(or any parent up to mount point [^\r\n]+\)\r?\nStopping at filesystem boundary \(GIT_DISCOVERY_ACROSS_FILESYSTEM not set\)\.$/u.test(
+ error.stderr.trimEnd(),
+ ))
+ )
+ return;
+ throw error;
+ }
+ const remotes: string[] = [];
+ for (const name of remoteNames.split(/\r?\n/u).filter(Boolean)) {
+ const urls = await readGit(["remote", "get-url", "--all", "--", name]);
+ remotes.push(...urls.split(/\r?\n/u));
+ }
+ const githubRepositories = new Set(
+ sources.flatMap((source) => {
+ if (source.platform !== "github") return [];
+ const key = remoteRepositoryKey(source.url, "github");
+ return key === undefined ? [] : [key];
+ }),
+ );
+ for (const source of sources) {
+ const githubKey = remoteRepositoryKey(source.url, "github");
+ const sourceProvider =
+ source.platform === "github" ||
+ (githubKey !== undefined && githubRepositories.has(githubKey))
+ ? "github"
+ : provider;
+ const remoteKeys = new Set(
+ remotes.flatMap((remote) => {
+ const key = remoteRepositoryKey(remote, sourceProvider);
+ return key === undefined ? [] : [key];
+ }),
+ );
+ if (remoteKeys.size === 0) continue;
+ const key = remoteRepositoryKey(source.url, sourceProvider);
+ if (key === undefined || !remoteKeys.has(key))
+ throw new CodexSecurityError(
+ `Imported repository ${JSON.stringify(source.url)} does not match a configured remote of ${repository}. Select the matching local repository.`,
+ );
+ }
+}
+
+async function localContext(
+ selectedRepository: string,
+ dependencies: LocalExternalImportDependencies,
+ mode: "import" | "read" = "import",
+) {
+ dependencies.signal?.throwIfAborted();
+ const environment = workbenchEnvironment(
+ dependencies.environment ?? process.env,
+ );
+ const path = expandHome(selectedRepository, environment);
+ const repository =
+ mode === "read"
+ ? await abortable(() => canonicalConfigPath(path), dependencies.signal)
+ : await normalizeRepository(path, dependencies.signal);
+ const protectedRoot = [
+ ...new Set([
+ repository,
+ ...(await abortable(
+ () => localGitProtectionRoots(path, environment, dependencies.signal),
+ dependencies.signal,
+ )),
+ ...(await abortable(
+ () =>
+ localGitProtectionRoots(
+ process.cwd(),
+ environment,
+ dependencies.signal,
+ ),
+ dependencies.signal,
+ )),
+ ]),
+ ];
+ requireOutputOutsideRepositories(
+ protectedRoot,
+ await abortable(
+ () => canonicalizeModelSafePath(environment.CODEX_SECURITY_STATE_DIR),
+ dependencies.signal,
+ ),
+ "runtime",
+ );
+ return {
+ repository,
+ options: {
+ pluginRoot: await abortable(
+ () => bundledPluginRoot(),
+ dependencies.signal,
+ ),
+ environment,
+ protectedRoot,
+ signal: dependencies.signal,
+ failureMessage: "Could not access imported local findings",
+ },
+ };
+}
+
+/** Prepare scanner evidence without writing state or performing a security assessment. */
+export async function prepareNormalizedLocalImport(
+ input: ExternalFindings,
+ options: NormalizedLocalImportOptions,
+ dependencies: LocalExternalImportDependencies = {},
+): Promise<PreparedLocalExternalImport> {
+ dependencies.signal?.throwIfAborted();
+ requireIdentity(options.sourceKey, "Source key");
+ const current = structuredClone(input);
+ const identities = new Set<string>();
+ for (const finding of current.findings) {
+ requireIdentity(finding.source_finding_id, "Source finding id");
+ if (identities.has(finding.source_finding_id))
+ throw new CodexSecurityError(
+ `Duplicate source finding id ${JSON.stringify(finding.source_finding_id)}.`,
+ );
+ identities.add(finding.source_finding_id);
+ if (!isRecord(finding.evidence))
+ throw new CodexSecurityError("Imported evidence must be an object.");
+ }
+ const { repository, options: workbenchOptions } = await localContext(
+ options.repository,
+ dependencies,
+ );
+ const sources = new Map<string, ExternalSourceRepository>();
+ for (const { evidence } of current.findings) {
+ const url = evidence.details?.repository?.url;
+ if (url) sources.set(url, { url });
+ }
+ for (const source of dependencies.sourceRepositories ?? []) {
+ // A reader's explicit platform also applies to the same URL in typed evidence.
+ if (sources.get(source.url)?.platform !== "github")
+ sources.set(source.url, source);
+ }
+ await requireMatchingRepository(
+ repository,
+ options.provider,
+ [...sources.values()],
+ workbenchOptions.protectedRoot,
+ workbenchOptions.environment,
+ dependencies.signal,
+ );
+ dependencies.signal?.throwIfAborted();
+ const preview = {
+ ...structuredClone(current),
+ repository,
+ source: { provider: options.provider, source_key: options.sourceKey },
+ };
+ const payload = JSON.stringify(
+ {
+ repository,
+ provider: options.provider,
+ sourceKey: options.sourceKey,
+ findings: current.findings,
+ },
+ (_key, value: unknown) => {
+ if (isNumberObject(value)) value = +value;
+ if (typeof value === "number" && !Number.isFinite(value))
+ throw new TypeError(
+ "Stored finding JSON cannot contain non-finite numbers.",
+ );
+ return value;
+ },
+ );
+ return {
+ preview,
+ async import() {
+ dependencies.signal?.throwIfAborted();
+ const protectedRoot = [
+ ...new Set([
+ ...workbenchOptions.protectedRoot,
+ ...(await abortable(
+ () =>
+ localGitProtectionRoots(
+ process.cwd(),
+ workbenchOptions.environment,
+ dependencies.signal,
+ ),
+ dependencies.signal,
+ )),
+ ]),
+ ];
+ requireOutputOutsideRepositories(
+ protectedRoot,
+ await abortable(
+ () =>
+ canonicalizeModelSafePath(
+ workbenchOptions.environment.CODEX_SECURITY_STATE_DIR,
+ ),
+ dependencies.signal,
+ ),
+ "runtime",
+ );
+ const result = await abortable(() => {
+ dependencies.signal?.throwIfAborted();
+ return (dependencies.runWorkbench ?? runWorkbench)(
+ { ...workbenchOptions, protectedRoot },
+ ["store-external-findings"],
+ payload,
+ );
+ }, dependencies.signal);
+ return {
+ repository,
+ read: current.read,
+ ready: current.findings.length,
+ excluded: current.excluded,
+ findings: result["findings"] as unknown as StoredExternalFinding[],
+ counts: result["counts"] as LocalExternalImportResult["counts"],
+ };
+ },
+ };
+}
+
+/** Read imported source records, including evidence that has not been assessed by Codex. */
+export async function listLocalExternalFindings(
+ selection: { repository: string; id?: string },
+ dependencies: LocalExternalImportDependencies = {},
+): Promise<StoredExternalFinding[]> {
+ if (selection.id !== undefined) requireIdentity(selection.id, "Finding id");
+ const { repository, options } = await localContext(
+ selection.repository,
+ dependencies,
+ "read",
+ );
+ const result = await abortable(() => {
+ dependencies.signal?.throwIfAborted();
+ return (dependencies.runWorkbench ?? runWorkbench)(
+ options,
+ ["list-external-findings"],
+ JSON.stringify({ repository, id: selection.id }),
+ );
+ }, dependencies.signal);
+ return result["findings"] as unknown as StoredExternalFinding[];
+}
diff --git a/sdk/typescript/src/external-findings.ts b/sdk/typescript/src/external-findings.ts
index 3c4a0436c..763260e8c 100644
--- a/sdk/typescript/src/external-findings.ts
+++ b/sdk/typescript/src/external-findings.ts
@@ -1,11 +1,17 @@
import type { ExternalFindingEvidence } from "./external-import-models.js";
-/** A provider occurrence and its evidence, before Cloud assigns a source report. */
+/** A provider occurrence and its evidence, independent of a Codex assessment. */
export interface ExternalFinding {
source_finding_id: string;
evidence: ExternalFindingEvidence;
}
+/** Repository provenance retained by a reader outside the published evidence. */
+export interface ExternalSourceRepository {
+ url: string;
+ platform?: "github";
+}
+
export interface ExternalFindings {
read: number;
findings: ExternalFinding[];
diff --git a/sdk/typescript/src/index.ts b/sdk/typescript/src/index.ts
index 626ac10c7..9fd0f8bbe 100644
--- a/sdk/typescript/src/index.ts
+++ b/sdk/typescript/src/index.ts
@@ -216,7 +216,23 @@ export type {
ExternalPublicationResult,
PreparedExternalPublication,
} from "./external-findings-publish.js";
-export type { ExternalFinding, ExternalFindings } from "./external-findings.js";
+export {
+ prepareNormalizedLocalImport,
+ listLocalExternalFindings,
+} from "./external-findings-local.js";
+export type {
+ StoredExternalFinding,
+ NormalizedLocalImportOptions,
+ LocalExternalImportDependencies,
+ LocalExternalImportPreview,
+ LocalExternalImportResult,
+ PreparedLocalExternalImport,
+} from "./external-findings-local.js";
+export type {
+ ExternalFinding,
+ ExternalFindings,
+ ExternalSourceRepository,
+} from "./external-findings.js";
export type {
ExternalFindingEvidence,
FindingImportRequest,
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index a9c22f5e1..8dc7fb4ec 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -1635,6 +1635,8 @@ export async function runWorkbench(
"database-info",
"store-findings",
"list-stored-findings",
+ "store-external-findings",
+ "list-external-findings",
"find-potential-duplicates",
"store-dedupe-groups",
"list-dedupe-groups",
diff --git a/sdk/typescript/src/targets.ts b/sdk/typescript/src/targets.ts
index 3da90aa14..2a87c8256 100644
--- a/sdk/typescript/src/targets.ts
+++ b/sdk/typescript/src/targets.ts
@@ -757,6 +757,9 @@ export function gitOutput(
signal?: AbortSignal,
environment?: NodeJS.ProcessEnv,
workingDirectory?: string,
+ encoding?: "utf8",
+ protectedRoots?: string | readonly string[],
+ sourceEnvironment?: NodeJS.ProcessEnv,
): Promise<string>;
export function gitOutput(
repository: string,
@@ -765,6 +768,8 @@ export function gitOutput(
environment: NodeJS.ProcessEnv | undefined,
workingDirectory: string | undefined,
encoding: "buffer",
+ protectedRoots?: string | readonly string[],
+ sourceEnvironment?: NodeJS.ProcessEnv,
): Promise<Buffer>;
export async function gitOutput(
repository: string,
@@ -773,14 +778,27 @@ export async function gitOutput(
environment: NodeJS.ProcessEnv = {},
workingDirectory = repository,
encoding: "utf8" | "buffer" = "utf8",
+ protectedRoots: string | readonly string[] = [],
+ sourceEnvironment: NodeJS.ProcessEnv = process.env,
): Promise<string | Buffer> {
throwIfAborted(signal);
const command = await resolveTrustedExecutable(
"git",
isolatedGitEnvironment(
- args[0] === "rev-parse" || args[0] === "config" || args[0] === "ls-files",
+ args[0] === "rev-parse" ||
+ args[0] === "config" ||
+ args[0] === "ls-files" ||
+ (args[0] === "remote" &&
+ (args.length === 1 ||
+ (args[1] === "get-url" && args[2] === "--all"))),
+ sourceEnvironment,
),
- (await gitMarkerRoot(repository, signal, "outermost")) ?? repository,
+ [
+ (await gitMarkerRoot(repository, signal, "outermost")) ?? repository,
+ ...(typeof protectedRoots === "string"
+ ? [protectedRoots]
+ : protectedRoots),
+ ],
);
if (command === null)
throw new Error("Git is not available on a trusted PATH.");
diff --git a/sdk/typescript/tests-ts/external-findings-local-helpers.ts b/sdk/typescript/tests-ts/external-findings-local-helpers.ts
new file mode 100644
index 000000000..a4b2f36a1
--- /dev/null
+++ b/sdk/typescript/tests-ts/external-findings-local-helpers.ts
@@ -0,0 +1,31 @@
+import { execFileSync } from "node:child_process";
+import { mkdir, mkdtemp, realpath, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+/** Real Git checkout and separate state, shared by normalized and vendor import tests. */
+export async function localFindingsFixture(options: { remote?: string } = {}) {
+ const root = await mkdtemp(
+ join(await realpath(tmpdir()), "codex-security-external-local-"),
+ );
+ const repository = join(root, "repository");
+ await mkdir(repository);
+ execFileSync("git", ["init", "--quiet", repository]);
+ if (options.remote !== undefined)
+ execFileSync("git", [
+ "-C",
+ repository,
+ "remote",
+ "add",
+ "origin",
+ options.remote,
+ ]);
+ const stateDirectory = join(root, "state");
+ return {
+ root,
+ repository,
+ stateDirectory,
+ environment: { ...process.env, CODEX_SECURITY_STATE_DIR: stateDirectory },
+ dispose: () => rm(root, { recursive: true, force: true }),
+ };
+}
diff --git a/sdk/typescript/tests-ts/external-findings-local.test.ts b/sdk/typescript/tests-ts/external-findings-local.test.ts
new file mode 100644
index 000000000..09fc8be6e
--- /dev/null
+++ b/sdk/typescript/tests-ts/external-findings-local.test.ts
@@ -0,0 +1,1942 @@
+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 } 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 {
+ 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("persists sparse evidence through the native helper and reads it in a separate process", async () => {
+ const { repository, stateDirectory, environment } = await fixture();
+ const normalized = input();
+ 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 },
+ });
+ 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, {
+ 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",
+ "-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);
+ },
+ );
+
+ test.each(["repository", "prepare-state", "read-state", "prepare-metadata"])(
+ "cancels %s resolution while its filesystem lookup is pending",
+ async (operation) => {
+ const { repository, stateDirectory, environment } = await fixture();
+ 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"))}
+ : ${JSON.stringify(stateDirectory)};
+ 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 (operation === "prepare-metadata" || path !== heldPath)
+ return originalRealpath(path, ...args);
+ enteredLookup();
+ return lookup;
+ },
+ lstat: (path, ...args) => {
+ if (operation !== "prepare-metadata" || 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(
+ { 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) {
+ const gate = 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 = { read: 0, findings: [], excluded: [] };
+ 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("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.each(["bare", "linked", "ordinary"] as const)(
+ "protects selected %s roots through nested paths and aliases",
+ async (kind) => {
+ const { root, repository, environment } = await fixture();
+ const owner = join(root, "owner");
+ const common = join(root, "common.git");
+ if (kind === "linked") {
+ execFileSync("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,
+ ]);
+ }
+ 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 selections = [
+ owner,
+ nested,
+ join(owner, "to-selected"),
+ join(alias, "to-selected"),
+ ];
+ if (kind === "bare") {
+ const inner = join(owner, "inner-worktree");
+ execFileSync("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 } = 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",
+ );
+ execFileSync("git", ["init", "--quiet", "--bare", common]);
+ execFileSync("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(
+ execFileSync("git", ["-C", repository, "remote"], {
+ encoding: "utf8",
+ }).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 } = 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);
+ execFileSync("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.
+ execFileSync("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 } = await fixture();
+ const repository = join(root, "linked");
+ const common = join(root, "common.git");
+ await mkdir(repository);
+ execFileSync("git", ["init", "--quiet", "--bare", common]);
+ execFileSync("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 } = 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");
+ execFileSync("git", [
+ "-C",
+ repository,
+ "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 } = 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");
+ execFileSync("git", [
+ "-C",
+ repository,
+ "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(["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 } = await fixture({
+ remote,
+ });
+ const dependencies = {
+ environment,
+ sourceRepositories: [{ url: "https://github.com/example/project" }],
+ };
+ await prepareNormalizedLocalImport(
+ input(),
+ { repository, ...source },
+ dependencies,
+ );
+ execFileSync("git", [
+ "-C",
+ repository,
+ "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 } = 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)]);
+ execFileSync("git", ["init", "--bare", "--quiet", bare]);
+ execFileSync("git", [
+ "-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);
+ });
+
+ test("does not impose Cloud field-size limits on normalized local evidence", async () => {
+ const { repository, environment } = await fixture();
+ const data = input();
+ data.findings[0]!.evidence.description = "x".repeat(65537);
+ data.findings[0]!.evidence.locations = Array.from(
+ { length: 101 },
+ (_, index) => ({ path: `src/file-${index}.ts` }),
+ );
+ const result = await (
+ await prepareNormalizedLocalImport(
+ data,
+ { repository, ...source },
+ { environment },
+ )
+ ).import();
+ expect(
+ (
+ await listLocalExternalFindings(
+ { repository, id: result.findings[0]!.id },
+ { environment },
+ )
+ )[0]!.evidence,
+ ).toEqual(data.findings[0]!.evidence);
+ });
+});