import assert from "node:assert/strict" import { test } from "node:test" import { redact, privateSecretFingerprint, tokenDigest, accessGeneration } from "./security.js" import { mkdtemp, unlink, rmdir, writeFile, readFile } from "node:fs/promises" import { tmpdir } from "node:os" import { join } from "node:path" test("fast existing-state reads retain immediate revocation and corruption refusal", async () => { const directory = await mkdtemp(join(tmpdir(), "mcp-current-state-")), previous = process.env.MCP_STATE_DIR try { process.env.MCP_STATE_DIR = directory const original = await accessGeneration() await writeFile(join(directory, "access-generation"), Buffer.from(original, "hex").map(value => value ^ 1)) assert.notEqual(await accessGeneration(), original) const proof = await privateSecretFingerprint("value", "fresh-key") await writeFile(join(directory, "encryption-key"), Buffer.alloc(32, 7)) assert.notEqual(await privateSecretFingerprint("value", "fresh-key"), proof) await writeFile(join(directory, "access-generation"), Buffer.from("broken")) await assert.rejects(accessGeneration(), { code: "STATE_UNAVAILABLE" }) assert.equal((await readFile(join(directory, "access-generation"))).toString(), "broken") } finally { if (previous === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previous await unlink(join(directory, "access-generation")); await unlink(join(directory, "encryption-key")); await rmdir(directory) } }) test("routing authentication keys are masked in structured and textual results", () => { const result = redact({ name: "ospf-lab", "auth-key": "fixture-ospf-key", nested: [{ "tcp-md5-key": "fixture-bgp-key", authenticationKey: "fixture-auth-key", authKeyRef: "reference-id" }], message: 'auth-key="fixture-text-key" tcp-md5-key=fixture-text-md5' }) as { name: string; nested: Record[]; message: string } assert.equal(result.name, "ospf-lab") assert.equal(result.nested[0].authKeyRef, "reference-id") for (const secret of ["fixture-ospf-key", "fixture-bgp-key", "fixture-auth-key", "fixture-text-key", "fixture-text-md5"]) assert.ok(!JSON.stringify(result).includes(secret)) }) test("credential proofs are stable on one installation and separated by secret, domain and installation", async () => { const first=await mkdtemp(join(tmpdir(),"mcp-secret-proof-")),second=await mkdtemp(join(tmpdir(),"mcp-secret-proof-")),previous=process.env.MCP_STATE_DIR try{ process.env.MCP_STATE_DIR=first const value=await privateSecretFingerprint("human-chosen-value","backup-storage") assert.equal(await privateSecretFingerprint("human-chosen-value","backup-storage"),value) assert.notEqual(value,tokenDigest("human-chosen-value")) assert.notEqual(await privateSecretFingerprint("different-value","backup-storage"),value) assert.notEqual(await privateSecretFingerprint("human-chosen-value","different-domain"),value) process.env.MCP_STATE_DIR=second assert.notEqual(await privateSecretFingerprint("human-chosen-value","backup-storage"),value) }finally{ if(previous===undefined)delete process.env.MCP_STATE_DIR;else process.env.MCP_STATE_DIR=previous for(const directory of [first,second]){await unlink(join(directory,"encryption-key"));await rmdir(directory)} } })