From 308dc008ea079fa1ba44d5104f52c6ebad08e1fe Mon Sep 17 00:00:00 2001 From: Dery Ferdika <114888155+DeryFerd@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:37:28 +0700 Subject: [PATCH] fix(bind-scope): write the contract and entry through the atomic writer (#182) --- src/engine/bind-scope.ts | 9 ++-- tests/bind-scope-atomic-write.test.ts | 77 +++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 tests/bind-scope-atomic-write.test.ts diff --git a/src/engine/bind-scope.ts b/src/engine/bind-scope.ts index bb808dc..6642fd0 100644 --- a/src/engine/bind-scope.ts +++ b/src/engine/bind-scope.ts @@ -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). diff --git a/tests/bind-scope-atomic-write.test.ts b/tests/bind-scope-atomic-write.test.ts new file mode 100644 index 0000000..75cade6 --- /dev/null +++ b/tests/bind-scope-atomic-write.test.ts @@ -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"); + }); +}); \ No newline at end of file