Skip to content

Commit 8cde997

Browse files
committed
fix(guards): close the core/Lab directory bypass and the release dry-run dead end
Two false-confidence defects found while auditing main..dev. Neither came from that range; both are mechanisms the range is relying on. The core/Lab guard matched only specifiers containing "/lab/". Since src/lab/index.ts exists, `import("../lab")` resolves to the Lab entrypoint while matching none of its three patterns: source : void import("../lab"); Guard 1 detects : false The self-test that claims to protect the guard re-declared its own copy of the regex, so it proved a local literal behaved rather than that the guard did -- a copy cannot fail when the original drifts. The predicate is now a single shared function that both the guard and its self-test call, and it matches the bare directory specifier. The release helper's documented "re-run with --publish" path could never complete. A dry run bumps, commits, and pushes the version deliberately, so the second invocation hits `npm version <same>` and exits "Version not changed" before dispatching anything. The bump and the push are now treated as a desired end state rather than as actions that must happen every time, so the second run reuses the release commit the first one made.
1 parent c5fc969 commit 8cde997

2 files changed

Lines changed: 59 additions & 18 deletions

File tree

‎scripts/release.ts‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,36 @@ console.log("→ privacy scan");
380380
await runLoud(["bun", "run", "privacy:scan"]);
381381

382382
// 2. Bump package.json only; the workflow creates the version tag after npm publish.
383-
console.log(`→ bump package.json → ${version}`);
384-
await runLoud(["npm", "version", version, "--no-git-tag-version"]);
383+
//
384+
// A dry run bumps and pushes exactly like a real one, because the point of the dry run is to
385+
// exercise the workflow against the REAL release commit. That makes the second invocation
386+
// re-enter with package.json already at `version`, where `npm version <same>` exits
387+
// "Version not changed" — so the documented "re-run with --publish" path could never
388+
// complete. Treat an already-correct version as satisfied rather than as an error: the
389+
// bump is a desired end state, not an action that must happen every time.
390+
const currentVersion = JSON.parse(await Bun.file("package.json").text()).version as string;
391+
if (currentVersion === version) {
392+
console.log(`→ package.json already at ${version}; leaving it alone`);
393+
} else {
394+
console.log(`→ bump package.json → ${version}`);
395+
await runLoud(["npm", "version", version, "--no-git-tag-version"]);
396+
}
385397

386-
// 3. Commit + push the version bump.
387-
await runLoud(["git", "add", "package.json"]);
388-
await runLoud(["git", "commit", "-m", `release: v${version}`]);
398+
// 3. Commit + push the version bump — only if it is not already committed and pushed. On the
399+
// --publish re-run of a dry run there is nothing to commit, and `git commit` with an empty
400+
// index fails, which would strand the release just as surely as the bump did.
401+
const pendingBump = (await capture(["git", "status", "--porcelain", "package.json"])).trim() !== "";
402+
if (pendingBump) {
403+
await runLoud(["git", "add", "package.json"]);
404+
await runLoud(["git", "commit", "-m", `release: v${version}`]);
405+
}
389406
const releaseSha = await capture(["git", "rev-parse", "HEAD"]);
390-
console.log(`→ push origin ${branch}`);
391-
await runLoud(["git", "push", "origin", branch]);
407+
if (pendingBump) {
408+
console.log(`→ push origin ${branch}`);
409+
await runLoud(["git", "push", "origin", branch]);
410+
} else {
411+
console.log(`→ release commit ${releaseSha.slice(0, 9)} already pushed; reusing it`);
412+
}
392413

393414
// 4. Wait for the pushed release commit to pass CI, then dispatch the Release workflow.
394415
console.log(`→ wait for Cross-platform CI (${releaseSha})`);

‎tests/core-lab-boundary.test.ts‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,29 @@ function firstLabPath(entry: string): string[] | null {
100100
return null;
101101
}
102102

103+
/**
104+
* Guard 1's predicate, defined ONCE so the test that claims to protect it actually calls it.
105+
*
106+
* It previously lived inline in the assertion while the self-test below re-declared its own
107+
* copy of the regex — so the self-test proved a local literal behaved, not that the guard did.
108+
* A copy cannot fail when the original drifts, which is the specific way a guard rots.
109+
*
110+
* The trailing-slash forms are not sufficient either: `src/lab/index.ts` exists, so
111+
* `import("../lab")` resolves to the Lab entrypoint while matching none of them. The
112+
* directory specifier is matched explicitly.
113+
*/
114+
export function namesLabDirectly(source: string): boolean {
115+
return /^\s*(?:import|export)\s+(?!type\b)[^;]*?["'][^"']*\/lab(?:\/|["'])/m.test(source)
116+
|| /^\s*import\s+["'][^"']*\/lab(?:\/|["'])/m.test(source)
117+
// A protected file may lazily reach Lab through a handler it imports, but must not
118+
// name Lab itself -- not even dynamically, and not as a bare directory.
119+
|| /\bimport\s*\(\s*["'][^"']*\/lab(?:\/|["'])/.test(source);
120+
}
121+
103122
describe("core / Compatibility Lab boundary", () => {
104123
// Guard 1: the obvious case, a direct import.
105124
test.each(PROTECTED)("%s has no direct src/lab import", file => {
106-
const source = readFileSync(resolve(repoRoot, file), "utf8");
107-
const direct = /^\s*(?:import|export)\s+(?!type\b)[^;]*?["'][^"']*\/lab\//m.test(source)
108-
|| /^\s*import\s+["'][^"']*\/lab\//m.test(source)
109-
// A protected file may lazily reach Lab through a handler it imports, but must not
110-
// name Lab itself -- not even dynamically.
111-
|| /\bimport\s*\(\s*["'][^"']*\/lab\//.test(source);
112-
expect(direct).toBe(false);
125+
expect(namesLabDirectly(readFileSync(resolve(repoRoot, file), "utf8"))).toBe(false);
113126
});
114127

115128
// Guard 2: the case that actually caused this work. The original defect reached Lab
@@ -156,11 +169,18 @@ describe("boundary guard cannot be defeated", () => {
156169
// -- lazy loading is the remedy, not the defect. Guard 1 is what stops a protected file
157170
// from naming Lab dynamically, so the coverage moves there rather than disappearing.
158171
test("guard 1 forbids a direct dynamic Lab import in a protected file", () => {
159-
const direct = (source: string) => /\bimport\s*\(\s*["'][^"']*\/lab\//.test(source);
160-
expect(direct('void import("../lab/paths");')).toBe(true);
161-
expect(direct('const m = await import("./management/lab-routes");')).toBe(false);
172+
// Calls the SAME predicate the guard uses, so a drift in one cannot pass in the other.
173+
expect(namesLabDirectly('void import("../lab/paths");')).toBe(true);
174+
// A bare directory specifier resolves to src/lab/index.ts and must be caught too --
175+
// matching only `/lab/` left this shape as a silent way through.
176+
expect(namesLabDirectly('void import("../lab");')).toBe(true);
177+
expect(namesLabDirectly('import "../lab";')).toBe(true);
178+
expect(namesLabDirectly('import { x } from "../lab";')).toBe(true);
179+
// A module whose NAME merely contains "lab" is not Lab.
180+
expect(namesLabDirectly('const m = await import("./management/lab-routes");')).toBe(false);
181+
expect(namesLabDirectly('import { x } from "./collaboration";')).toBe(false);
162182
for (const file of PROTECTED) {
163-
expect(direct(readFileSync(resolve(repoRoot, file), "utf8"))).toBe(false);
183+
expect(namesLabDirectly(readFileSync(resolve(repoRoot, file), "utf8"))).toBe(false);
164184
}
165185
});
166186

0 commit comments

Comments
 (0)