import assert from "node:assert/strict"; import crypto from "node:crypto"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { createRequire } from "node:module"; import { fileURLToPath } from "node:url"; import ts from "../../web/node_modules/typescript/lib/typescript.js"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const reportPath = path.join(root, "tests/golden/M13-03A/sandbox-scope-report.json"); const manifestPath = path.join(root, "tests/golden/M13-03A/manifest.json"); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "m13-03a-sandbox-")); const hashFile = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"); for (const name of ["asset-path", "capability-gates", "scripting-platform"]) { const source = fs.readFileSync(path.join(root, `web/protocol/${name}.ts`), "utf8"); const output = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }, fileName: `${name}.ts` }).outputText .replace('require("./asset-path")', 'require("./asset-path.cjs")') .replace('require("./capability-gates")', 'require("./capability-gates.cjs")'); fs.writeFileSync(path.join(temporary, `${name}.cjs`), output); } const protocol = createRequire(import.meta.url)(path.join(temporary, "scripting-platform.cjs")); try { const scope = { schemaVersion: 1, dom: false, hostWorker: false, opfs: false, indexedDB: false, network: false }; const accepted = protocol.parseScriptSandboxScope(scope); const blocked = Object.fromEntries(["dom", "hostWorker", "opfs", "indexedDB", "network"].map((capability) => { try { protocol.parseScriptSandboxScope({ ...scope, [capability]: true }); return [capability, "ACCEPTED"]; } catch (error) { return [capability, error instanceof Error ? error.message.split(":", 1)[0] : String(error)]; } })); assert.deepEqual(accepted, scope); assert.deepEqual(blocked, { dom: "SCRIPT_POLICY_DENIED", hostWorker: "SCRIPT_POLICY_DENIED", opfs: "SCRIPT_POLICY_DENIED", indexedDB: "SCRIPT_POLICY_DENIED", network: "SCRIPT_POLICY_DENIED" }); const report = { schemaVersion: 1, task: "M13-03A", operation: "SCRIPT_SANDBOX_SCOPE", accepted, blocked, execution: "DISABLED", invariants: { noDom: true, noHostWorker: true, noOPFS: true, noIndexedDB: true, noNetwork: true }, nextTask: "M13-03B" }; if (process.env.UPDATE_M13_03A_REPORT === "1") { fs.mkdirSync(path.dirname(reportPath), { recursive: true }); fs.writeFileSync(reportPath, JSON.stringify(report, null, 2) + "\n"); } assert.deepEqual(JSON.parse(fs.readFileSync(reportPath, "utf8")), report); const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); assert.deepEqual({ schemaVersion: manifest.schemaVersion, task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { schemaVersion: 1, task: "M13-03A", parentTask: "M13-02F", nextTask: "M13-03B" }); for (const artifact of Object.values(manifest.artifacts)) assert.equal(hashFile(path.join(root, artifact.path)), artifact.sha256, `artifact hash mismatch ${artifact.path}`); process.stdout.write(`script-sandbox-scope-ok dom=SCRIPT_POLICY_DENIED hostWorker=SCRIPT_POLICY_DENIED opfs=SCRIPT_POLICY_DENIED indexedDB=SCRIPT_POLICY_DENIED network=SCRIPT_POLICY_DENIED execution=DISABLED next=${manifest.nextTask}\n`); } finally { fs.rmSync(temporary, { recursive: true, force: true }); }