"use strict"; const test = require("node:test"); const assert = require("node:assert"); const fs = require("fs"); const os = require("os"); const path = require("path"); const { generateMasterSecret, persistMasterExclusive, resolvePanelHmacSecret, } = require("./panel-hmac-secret"); function tmpDir() { return fs.mkdtempSync(path.join(os.tmpdir(), "panel-hmac-test-")); } // Silence the resolver's operational warnings during tests while capturing // them so tests can assert the recovery guidance is present. function captureWarns(fn) { const warns = []; const orig = console.warn; console.warn = (...args) => { warns.push(args.join(" ")); }; try { return { result: fn(), warns }; } finally { console.warn = orig; } } test("generated master is 256-bit hex", () => { assert.match(generateMasterSecret(), /^[0-9a-f]{64}$/); }); test("env secret always wins and never touches the persistence file", () => { const dir = tmpDir(); const filePath = path.join(dir, "panel-hmac-secret.json"); const result = resolvePanelHmacSecret({ envSecret: " operator-secret ", filePath }); assert.deepStrictEqual(result, { secret: "operator-secret", source: "env" }); assert.strictEqual(fs.existsSync(filePath), false, "env resolution must not create the file"); }); test("first resolve generates and persists; second resolve reuses the file", () => { const dir = tmpDir(); const filePath = path.join(dir, "panel-hmac-secret.json"); const first = resolvePanelHmacSecret({ envSecret: "", filePath }); assert.strictEqual(first.source, "generated"); assert.match(first.secret, /^[0-9a-f]{64}$/); const onDisk = JSON.parse(fs.readFileSync(filePath, "utf8")); assert.strictEqual(onDisk.secret, first.secret); assert.ok(onDisk.generatedAt, "generatedAt metadata missing"); const mode = fs.statSync(filePath).mode & 0o777; assert.strictEqual(mode, 0o600, `persisted secret must be 0600, got ${mode.toString(8)}`); const second = resolvePanelHmacSecret({ envSecret: "", filePath }); assert.deepStrictEqual(second, { secret: first.secret, source: "file" }); }); test("persistMasterExclusive never overwrites: loser converges on winner", () => { const dir = tmpDir(); const filePath = path.join(dir, "panel-hmac-secret.json"); assert.strictEqual(persistMasterExclusive(filePath, "winner-secret"), true); // A concurrent boot racing for the same path must lose and NOT overwrite. assert.strictEqual(persistMasterExclusive(filePath, "loser-secret"), false); const onDisk = JSON.parse(fs.readFileSync(filePath, "utf8")); assert.strictEqual(onDisk.secret, "winner-secret"); }); test("corrupt persisted file fails closed and is left untouched", () => { const dir = tmpDir(); const filePath = path.join(dir, "panel-hmac-secret.json"); fs.writeFileSync(filePath, "{not json at all", "utf8"); const { result, warns } = captureWarns(() => resolvePanelHmacSecret({ envSecret: "", filePath })); assert.deepStrictEqual(result, { secret: "", source: "" }); assert.ok(warns.some(w => w.includes("not valid JSON") && w.includes("delete it to generate a new master")), "warning must explain recovery"); assert.strictEqual(fs.readFileSync(filePath, "utf8"), "{not json at all", "corrupt file must not be overwritten"); }); test("persisted file with a blank secret fails closed", () => { const dir = tmpDir(); const filePath = path.join(dir, "panel-hmac-secret.json"); fs.writeFileSync(filePath, JSON.stringify({ secret: " " }), "utf8"); const { result } = captureWarns(() => resolvePanelHmacSecret({ envSecret: "", filePath })); assert.deepStrictEqual(result, { secret: "", source: "" }); }); test("unwritable persistence location degrades to disabled signing, no throw", () => { const dir = tmpDir(); // Parent path is a regular FILE, so mkdir/link underneath must fail. const blocker = path.join(dir, "not-a-dir"); fs.writeFileSync(blocker, "x", "utf8"); const filePath = path.join(blocker, "panel-hmac-secret.json"); const { result, warns } = captureWarns(() => resolvePanelHmacSecret({ envSecret: "", filePath })); assert.deepStrictEqual(result, { secret: "", source: "" }); assert.ok(warns.some(w => w.includes("Cannot persist")), "warning must explain degradation"); }); test("unreadable persisted file (EACCES-style) fails closed without regeneration", () => { const dir = tmpDir(); const filePath = path.join(dir, "panel-hmac-secret.json"); // Simulate an unreadable target: a DIRECTORY where a file is expected. // Reading it throws EISDIR, exercising the non-ENOENT read error path. fs.mkdirSync(filePath); const { result, warns } = captureWarns(() => resolvePanelHmacSecret({ envSecret: "", filePath })); assert.deepStrictEqual(result, { secret: "", source: "" }); assert.ok(warns.some(w => w.includes("Cannot read"))); // The directory must still exist (nothing tried to overwrite it). assert.strictEqual(fs.statSync(filePath).isDirectory(), true); });