fix(bind-scope): write the contract and entry through the atomic writer (#182)

This commit is contained in:
Dery Ferdika
2026-11-13 10:37:28 +07:00
committed by GitHub
parent 7eaadc1dc3
commit 308dc008ea
2 changed files with 82 additions and 4 deletions
+5 -4
View File
@@ -17,6 +17,7 @@ import {
writeFileSync,
} from "node:fs";
import { basename, dirname, join, relative, resolve, sep } from "node:path";
import { writeFileAtomicSync } from "./protector.js";
export const BIND_SCOPE_SCHEMA = "bla-bind-scope/v1" as const;
export const BIND_SCOPE_FILE_NAME = "bind-scope.json" as const;
@@ -603,14 +604,14 @@ export function applyBindScope(
const rewritten = injectRuntimeIntoEntry(original, snippet);
if (!options.dryRun) {
mkdirSync(dirname(contractPath), { recursive: true });
writeFileSync(
// Same atomic, link-guarded writer the rest of the engine uses: a crash must
// not leave a truncated contract or entry copy behind.
writeFileAtomicSync(
contractPath,
JSON.stringify(validated.contract, null, 2) + "\n",
"utf8",
);
mkdirSync(dirname(entryOut), { recursive: true });
writeFileSync(entryOut, rewritten, "utf8");
writeFileAtomicSync(entryOut, rewritten, "utf8");
}
// Dry-run synthesizes lint against the in-memory rewrite (no disk read).
+77
View File
@@ -0,0 +1,77 @@
import { existsSync, linkSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { BIND_SCOPE_RUNTIME_MARKER, applyBindScope } from "../src/engine/bind-scope.js";
const dirs: string[] = [];
const dir = (): string => {
const d = mkdtempSync(join(tmpdir(), "bla-bind-atomic-"));
dirs.push(d);
return d;
};
afterEach(() => {
for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true });
});
const ENTRY = `"use strict";
module.exports.unlock = function unlock(key) {
return key === "ok" ? "FLAG{in_scope}" : null;
};
`;
function hardlinkOrSkip(target: string, linkPath: string): boolean {
try {
linkSync(target, linkPath);
return true;
} catch {
return false;
}
}
describe("bind-scope writes through the shared atomic writer", () => {
it("writes the contract and the rewritten entry into outDir", () => {
const src = dir();
const out = dir();
writeFileSync(join(src, "license.cjs"), ENTRY);
const report = applyBindScope({
directory: src,
entryPath: join(src, "license.cjs"),
outDir: out,
domains: ["allowed.example"],
validFrom: "2020-01-01T00:00:00.000Z",
validUntil: "2099-01-01T00:00:00.000Z",
});
expect(report.status).toBe("ok");
expect(existsSync(join(out, "bind-scope.json"))).toBe(true);
expect(readFileSync(join(out, "license.cjs"), "utf8")).toContain(BIND_SCOPE_RUNTIME_MARKER);
});
it("refuses to write the contract through a hardlinked path", () => {
const src = dir();
const out = dir();
const outside = join(dir(), "outside-target.json");
writeFileSync(join(src, "license.cjs"), ENTRY);
writeFileSync(outside, "keep me");
if (!hardlinkOrSkip(outside, join(out, "bind-scope.json"))) {
// Filesystem without hardlink support: nothing to assert here.
return;
}
expect(() =>
applyBindScope({
directory: src,
entryPath: join(src, "license.cjs"),
outDir: out,
domains: ["allowed.example"],
validFrom: "2020-01-01T00:00:00.000Z",
validUntil: "2099-01-01T00:00:00.000Z",
}),
).toThrow(/Refusing to write through a symlink or hardlinked output/);
expect(readFileSync(outside, "utf8")).toBe("keep me");
});
});