diff --git a/scripts/run-and-validate-provider.ts b/scripts/run-and-validate-provider.ts index 597989d8..09511f4a 100644 --- a/scripts/run-and-validate-provider.ts +++ b/scripts/run-and-validate-provider.ts @@ -1,7 +1,7 @@ import { spawn } from "node:child_process"; import { randomBytes } from "node:crypto"; import { constants } from "node:fs"; -import { access, appendFile, lstat } from "node:fs/promises"; +import { access, appendFile, lstat, stat } from "node:fs/promises"; import path from "node:path"; import { @@ -274,12 +274,21 @@ async function runProviderInSandbox( bwrapArguments.push("--dir", directory); } bwrapArguments.push("--ro-bind", workspaceRoot, workspaceRoot); - if (await exists(path.join(workspaceRoot, ".git"))) { + const repositoryMetadata = path.join(workspaceRoot, ".git"); + const metadataKind = await entryKind(repositoryMetadata); + if (metadataKind === "directory") { bwrapArguments.push( "--size", String(PROVIDER_MASK_BYTES), - "--tmpfs", path.join(workspaceRoot, ".git"), - "--remount-ro", path.join(workspaceRoot, ".git"), + "--tmpfs", repositoryMetadata, + "--remount-ro", repositoryMetadata, ); + } else if (metadataKind === "other") { + // 1103/11-04. A linked worktree or a submodule checkout has a one-line + // `gitdir:` file here, and a tmpfs cannot be mounted on a file. /dev/null + // is bound over it instead. bubblewrap mounts binds nodev, so the provider + // can neither read the line nor write the path; the directory it names + // lies outside the workspace and is never bound into the sandbox. + bwrapArguments.push("--ro-bind", "/dev/null", repositoryMetadata); } bwrapArguments.push( "--ro-bind", candidateRoot, "/candidate", @@ -597,6 +606,16 @@ function missingDestinationAncestors(target: string): string[] { return ancestors.reverse(); } +/** What `target` is once symlinks are followed, as bubblewrap will see it. */ +async function entryKind(target: string): Promise<"directory" | "other" | null> { + try { + return (await stat(target)).isDirectory() ? "directory" : "other"; + } catch (error) { + if (hasErrorCode(error, "ENOENT")) return null; + throw error; + } +} + async function exists(target: string): Promise { try { await lstat(target); diff --git a/tests/system/ci-runner/ci-artifact-contract.test.ts b/tests/system/ci-runner/ci-artifact-contract.test.ts index 1eefff9a..3aa99b33 100644 --- a/tests/system/ci-runner/ci-artifact-contract.test.ts +++ b/tests/system/ci-runner/ci-artifact-contract.test.ts @@ -764,6 +764,44 @@ describe("candidate archive and provider upload boundaries", { timeout: PROCESS_ await expect(readFile(canary, "utf8")).resolves.toBe("host-secret\n"); }); + // 1103/11-04. In a linked worktree or a submodule checkout `.git` is a + // one-line `gitdir:` file. The sandbox hid `.git` with a tmpfs, which + // bubblewrap cannot mount on a file, so every provider run in a worktree + // ended with "Can't mkdir …/.git: Not a directory" before the provider ran. + it.each(["directory", "file"] as const)( + "hides repository metadata from the provider when .git is a %s", + async (shape) => { + const fixture = await createProviderFixture(); + await rm(fixture.reportPath); + const metadata = path.join(fixture.root, ".git"); + await rm(metadata, { recursive: true, force: true }); + if (shape === "directory") { + await mkdir(metadata); + await writeFile(path.join(metadata, "HEAD"), "ref: refs/heads/fixture\n"); + } else { + await writeFile(metadata, "gitdir: /repository/.git/worktrees/fixture\n"); + } + const providerScript = path.join(fixture.root, "provider-git-metadata.mjs"); + // Exit 9 when the provider can see what the metadata says, exit 8 when + // it can change it. + await writeFile(providerScript, [ + "import { appendFileSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs';", + `const metadata = ${JSON.stringify(metadata)};`, + "const directory = statSync(metadata).isDirectory();", + "let visible = false;", + "try { visible = directory ? readdirSync(metadata).length > 0 : readFileSync(metadata).length > 0; } catch {}", + "if (visible) process.exit(9);", + "try { appendFileSync(directory ? `${metadata}/HEAD` : metadata, 'mutated\\n'); process.exit(8); } catch {}", + providerV2WriterSource({ importFs: false }), + ].join("\n")); + const result = runProviderSupervisor(fixture, { + command: `node ${JSON.stringify(providerScript)}`, + sealedPath: path.join(fixture.root, "provider-evidence/vulnerability-report.json"), + }); + expect(result.status, result.stderr).toBe(0); + }, + ); + it("applies effective aggregate cgroup limits without exposing command or credentials", async () => { const fixture = await createProviderFixture(); await rm(fixture.reportPath); @@ -2184,6 +2222,27 @@ async function createProviderFixture() { }; } +/** + * Top-level entries the provider base fixture leaves behind: build output, + * installed modules (linked separately below), and other checkouts or scratch + * trees that can live inside a checkout. `.claude/worktrees/*` and + * `.worktrees/*` are other worktrees of this repository and `.tmp/*` holds + * removal-drill fixtures, each with its own node_modules; copying them filled + * every /tmp/ci-provider-upload-* root with those trees, and the suite failed + * with timeouts and ENOTEMPTY clean-ups (1103/11-04). None of them is tracked, + * so the candidate's tracked-file inventory does not miss them. + */ +const PROVIDER_BASE_FIXTURE_UNCOPIED_ENTRIES: readonly string[] = Object.freeze([ + ".release", + "artifacts", + "dist", + "node_modules", + ".claude", + ".worktrees", + ".tmp", + "graphify-out", +]); + async function ensureProviderBaseFixture(): Promise { if (providerBaseRoot) return providerBaseRoot; const sourceRoot = process.cwd(); @@ -2194,7 +2253,7 @@ async function ensureProviderBaseFixture(): Promise { const relative = path.relative(sourceRoot, source); if (!relative) return true; const first = relative.split(path.sep)[0]; - return ![".release", "artifacts", "dist", "node_modules"].includes(first ?? ""); + return !PROVIDER_BASE_FIXTURE_UNCOPIED_ENTRIES.includes(first ?? ""); }, }); // The release evidence, minus the trace and Storybook trees. Copying the