Files
OmniRoute/tests/unit/windows-cert-identity-7275.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966)

Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.

One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.

Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.

* fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff

The gate shells out to `git diff` through execFileSync with Node's default 1 MB
maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to
overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing
anything. 64 MB is far above any real PR and costs nothing when unused.
2026-08-29 01:17:40 -03:00

157 lines
6.6 KiB
TypeScript

/**
* Regression test for #7275 — Windows cert check/uninstall used a hardcoded
* legacy hostname (`daily-cloudcode-pa.googleapis.com`) instead of the real
* generated CA's identity.
*
* Root cause: `checkCertInstalledWindows()`/`uninstallCertWindows()` queried
* the Windows Root store by the literal legacy hostname regardless of the
* `certPath` passed in — it only "worked" because that hostname happens to be
* the CA's own `commonName` today (`generate.ts` derives it from
* `ANTIGRAVITY_TARGET.hosts[0]`), a coincidence with no shared symbol coupling
* the two. `installCertWindows()` was already correct (keys off `certPath`).
*
* Fix: both functions now derive a SHA-1 thumbprint straight from the
* `certPath` file (via the exported `certutilThumbprint()`, reusing the same
* `getCertFingerprint()` logic {@link checkCertInstalledMac} already uses) —
* the same identity `installCertWindows()` installs, independent of
* `ANTIGRAVITY_TARGET.hosts` ordering/content.
*
* Methodology: a real `certutil` stub on PATH captures argv (no
* `child_process` mocking), with `process.platform` forced to `win32` before
* the module is imported (`IS_WIN` is a load-time const) and a fake — but
* real-file — cert so `getCertFingerprint()` runs unmodified.
*/
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import crypto from "node:crypto";
const LEGACY_HARDCODED_HOST = "daily-cloudcode-pa.googleapis.com";
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform")!;
const originalPath = process.env.PATH;
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-7275-"));
const binDir = path.join(tmpRoot, "bin");
fs.mkdirSync(binDir, { recursive: true });
const captureFile = path.join(tmpRoot, "certutil-argv.log");
fs.writeFileSync(captureFile, "");
// A real executable on PATH — not a child_process mock — so
// checkCertInstalledWindows() exercises the genuine execFile() code path.
const certutilStubPath = path.join(binDir, "certutil");
fs.writeFileSync(
certutilStubPath,
`#!/usr/bin/env node
const fs = require("fs");
fs.appendFileSync(${JSON.stringify(captureFile)}, process.argv.slice(2).join(" ") + "\\n");
process.exit(0);
`,
{ mode: 0o755 }
);
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
process.env.PATH = `${binDir}${path.delimiter}${originalPath}`;
// Imported AFTER forcing win32: IS_WIN inside install.ts is a load-time const.
const { checkCertInstalled, certutilThumbprint, buildWindowsDelstoreScript } =
await import("../../src/mitm/cert/install.ts");
test.after(() => {
Object.defineProperty(process, "platform", originalPlatformDescriptor);
process.env.PATH = originalPath;
fs.rmSync(tmpRoot, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
function fakeCertFile(seed: string): string {
const der = crypto.createHash("sha256").update(seed).digest();
const pem =
"-----BEGIN CERTIFICATE-----\n" +
der
.toString("base64")
.match(/.{1,64}/g)!
.join("\n") +
"\n-----END CERTIFICATE-----\n";
const certPath = path.join(tmpRoot, `${seed}.crt`);
fs.writeFileSync(certPath, pem);
return certPath;
}
test("checkCertInstalledWindows() keys off the real cert's thumbprint, not the legacy hardcoded host", async () => {
const certPath = fakeCertFile("probe-a");
const expectedThumbprint = certutilThumbprint(certPath);
const isInstalled = await checkCertInstalled(certPath);
assert.equal(isInstalled, true, "the certutil stub always exits 0 → should report installed");
const capturedArgv = fs.readFileSync(captureFile, "utf8").trim().split("\n").at(-1)!;
// Exact-equality is strictly stronger than a negative substring check: an argv
// that IS `-store Root <thumbprint>` cannot also carry the legacy hostname.
assert.equal(
capturedArgv,
`-store Root ${expectedThumbprint}`,
"certutil must be queried with the real cert's thumbprint, not the legacy hostname"
);
});
test("checkCertInstalledWindows() tracks certPath — a different cert yields a different query", async () => {
fs.writeFileSync(captureFile, "");
const certPathB = fakeCertFile("probe-b");
const thumbprintA = certutilThumbprint(fakeCertFile("probe-a"));
const thumbprintB = certutilThumbprint(certPathB);
assert.notEqual(thumbprintA, thumbprintB, "sanity: distinct cert content → distinct thumbprint");
await checkCertInstalled(certPathB);
const capturedArgv = fs.readFileSync(captureFile, "utf8").trim().split("\n").at(-1)!;
assert.equal(capturedArgv, `-store Root ${thumbprintB}`);
});
test("buildWindowsDelstoreScript() embeds the cert's own thumbprint, not the legacy hardcoded host", () => {
const certPath = fakeCertFile("probe-c");
const thumbprint = certutilThumbprint(certPath);
const script = buildWindowsDelstoreScript(thumbprint);
// Assert on the certId argument certutil actually receives, rather than on a
// negative substring scan of the whole script: pinning the extracted value to
// the real thumbprint proves no other identity (legacy hostname included) can
// be the one passed to -delstore.
const certIdArg = script.match(/'-delstore'\s*,\s*'Root'\s*,\s*'([^']+)'/)?.[1];
assert.equal(
certIdArg,
thumbprint,
"delstore must target the cert's own thumbprint as its certId argument"
);
});
test("check and uninstall derive identity from the SAME source (certutilThumbprint(certPath)) — immune to ANTIGRAVITY_TARGET.hosts reordering", async () => {
fs.writeFileSync(captureFile, "");
const certPath = fakeCertFile("probe-d");
await checkCertInstalled(certPath);
const checkArgv = fs.readFileSync(captureFile, "utf8").trim().split("\n").at(-1)!;
const checkThumbprint = checkArgv.replace("-store Root ", "");
const delstoreScript = buildWindowsDelstoreScript(certutilThumbprint(certPath));
assert.ok(
delstoreScript.includes(checkThumbprint),
"uninstall must target the exact same identity the check used — " +
"no coincidence-based coupling through a hardcoded hostname"
);
});
test("source no longer hardcodes the legacy host for the Windows check/uninstall paths", () => {
const source = fs.readFileSync(
new URL("../../src/mitm/cert/install.ts", import.meta.url),
"utf8"
);
// Only allowed inside the doc-comment explaining the historical bug — never
// as a live argv literal passed to certutil.
assert.ok(
!source.includes(`"${LEGACY_HARDCODED_HOST}"`),
"the legacy hostname must not appear as a string literal anywhere in install.ts"
);
});