"use strict"; const test = require("node:test"); const assert = require("node:assert"); const crypto = require("crypto"); const { canonicalStoreDigestInput, canonicalStoreSigningPayload, computeStoreDigestFromRaw, parseEd25519PublicKey, verifyStoreSignature, expectedGitBlobSha1, verifyGitBlobSha1, verifyListedContentHash, imageRegistryForRef, resolveAllowedTemplateRegistries, validateTemplateImageRefInput, resolveTemplateStoreTrustConfig, BUILTIN_VENDOR_PUBLIC_KEY, buildStoreSyncDiff, DEFAULT_TEMPLATE_IMAGE_REGISTRIES, } = require("./template-store-trust"); function keyPair() { return crypto.generateKeyPairSync("ed25519"); } function rawPublicKey(keyObject) { const jwk = keyObject.publicKey.export({ format: "jwk" }); return Buffer.from(jwk.x, "base64").toString("base64"); } function signPayload(privateKey, payload) { return crypto.sign(null, Buffer.from(payload, "utf8"), privateKey).toString("base64"); } function makeSignedStore({ repo = "antonndev/ADTemplates", files, privateKey, tamper, generatedAt = "2026-09-04T00:00:00Z" } = {}) { const computed = computeStoreDigestFromRaw(repo, files); const payload = canonicalStoreSigningPayload(repo, computed.digest, generatedAt); const signatureFile = { version: 2, repo, digest: tamper && tamper.digest ? "f".repeat(64) : computed.digest, generatedAt, signature: signPayload(privateKey, payload), }; return { signatureFile, computed }; } const sampleFiles = [ { relPath: "games/paper.json", raw: '{"name":"paper"}' }, { relPath: "media/ffmpeg.json", raw: '{"name":"ffmpeg"}' }, ]; // --- canonical digest ------------------------------------------------------ test("digest input is order-independent and path-keyed", () => { const a = canonicalStoreDigestInput("antonndev/ADTemplates", [ { relPath: "a/one.json", sha256: "a".repeat(64) }, { relPath: "b/two.json", sha256: "b".repeat(64) }, ]); const b = canonicalStoreDigestInput("antonndev/ADTemplates", [ { relPath: "b/two.json", sha256: "b".repeat(64) }, { relPath: "a/one.json", sha256: "a".repeat(64) }, ]); assert.strictEqual(a, b); assert.ok(a.includes("a".repeat(64) + " a/one.json")); }); test("digest changes with repo and content", () => { const base = computeStoreDigestFromRaw("antonndev/ADTemplates", sampleFiles); const otherRepo = computeStoreDigestFromRaw("evil/repo", sampleFiles); const otherContent = computeStoreDigestFromRaw("antonndev/ADTemplates", [ { relPath: "games/paper.json", raw: '{"name":"paper-malicious"}' }, { relPath: "media/ffmpeg.json", raw: '{"name":"ffmpeg"}' }, ]); assert.notStrictEqual(base.digest, otherRepo.digest); assert.notStrictEqual(base.digest, otherContent.digest); }); // --- key parsing -------------------------------------------------------------- test("parses raw 32-byte and SPKI base64 public keys to the same fingerprint", () => { const { publicKey } = keyPair(); const spkiBase64 = publicKey.export({ type: "spki", format: "der" }).toString("base64"); const rawBase64 = rawPublicKey({ publicKey }); const fromSpki = parseEd25519PublicKey(spkiBase64); const fromRaw = parseEd25519PublicKey(rawBase64); assert.ok(fromSpki && fromRaw); assert.strictEqual(fromSpki.fingerprint, fromRaw.fingerprint); assert.strictEqual(parseEd25519PublicKey("not base64 !!!"), null); assert.strictEqual(parseEd25519PublicKey(""), null); // RSA SPKI blobs must not pass as Ed25519 keys. const rsa = crypto.generateKeyPairSync("rsa", { modulusLength: 2048 }); assert.strictEqual( parseEd25519PublicKey(rsa.publicKey.export({ type: "spki", format: "der" }).toString("base64")), null ); }); // --- signature verification ------------------------------------------------------ test("valid signature over fetched bytes verifies", () => { const own = keyPair(); const pinned = parseEd25519PublicKey(rawPublicKey(own)); const { signatureFile } = makeSignedStore({ files: sampleFiles, privateKey: own.privateKey }); const result = verifyStoreSignature({ signatureFile, repo: "antonndev/ADTemplates", files: sampleFiles, pinnedKey: pinned, }); assert.strictEqual(result.verified, true); assert.strictEqual(result.reason, ""); assert.strictEqual(result.generatedAt, "2026-09-04T00:00:00Z"); }); test("generatedAt is bound by the signature — re-dating or dropping it fails closed", () => { const own = keyPair(); const pinned = parseEd25519PublicKey(rawPublicKey(own)); const { signatureFile, computed } = makeSignedStore({ files: sampleFiles, privateKey: own.privateKey }); // Replay vector: old legitimately-signed content re-served with a forged // future timestamp must NOT verify — the timestamp is inside the payload. const reDated = verifyStoreSignature({ signatureFile: { ...signatureFile, generatedAt: "2030-01-01T00:00:00.000Z" }, repo: "antonndev/ADTemplates", files: sampleFiles, pinnedKey: pinned, }); assert.strictEqual(reDated.verified, false); assert.match(reDated.reason, /signature verification failed/); const undated = verifyStoreSignature({ signatureFile: { ...signatureFile, generatedAt: "" }, repo: "antonndev/ADTemplates", files: sampleFiles, pinnedKey: pinned, }); assert.strictEqual(undated.verified, false); assert.match(undated.reason, /no generatedAt/); // The legitimate signer can move the timestamp forward by re-signing. const future = makeSignedStore({ files: sampleFiles, privateKey: own.privateKey, generatedAt: "2030-01-01T00:00:00.000Z" }); const resigned = verifyStoreSignature({ signatureFile: future.signatureFile, repo: "antonndev/ADTemplates", files: sampleFiles, pinnedKey: pinned, }); assert.strictEqual(resigned.verified, true); assert.strictEqual(resigned.generatedAt, "2030-01-01T00:00:00.000Z"); // v1 signatures (pre-generatedAt binding) are rejected outright. const legacy = verifyStoreSignature({ signatureFile: { ...signatureFile, version: 1 }, repo: "antonndev/ADTemplates", files: sampleFiles, pinnedKey: pinned, }); assert.strictEqual(legacy.verified, false); assert.match(legacy.reason, /unsupported store signature version/); assert.ok(computed.digest); }); test("tampered content, wrong key, wrong repo and missing signature all fail closed", () => { const own = keyPair(); const pinned = parseEd25519PublicKey(rawPublicKey(own)); const other = keyPair(); const good = makeSignedStore({ files: sampleFiles, privateKey: own.privateKey }); const tamperedContent = verifyStoreSignature({ signatureFile: good.signatureFile, repo: "antonndev/ADTemplates", files: [{ relPath: "games/paper.json", raw: '{"name":"paper-pwned"}' }, sampleFiles[1]], pinnedKey: pinned, }); assert.strictEqual(tamperedContent.verified, false); assert.match(tamperedContent.reason, /digest does not match/); const wrongKey = verifyStoreSignature({ signatureFile: good.signatureFile, repo: "antonndev/ADTemplates", files: sampleFiles, pinnedKey: parseEd25519PublicKey(rawPublicKey(other)), }); assert.strictEqual(wrongKey.verified, false); assert.match(wrongKey.reason, /digest does not match|signature verification failed/); const wrongRepo = verifyStoreSignature({ signatureFile: good.signatureFile, repo: "evil/repo", files: sampleFiles, pinnedKey: pinned, }); assert.strictEqual(wrongRepo.verified, false); assert.match(wrongRepo.reason, /repo/); assert.strictEqual( verifyStoreSignature({ signatureFile: null, repo: "antonndev/ADTemplates", files: sampleFiles, pinnedKey: pinned }).verified, false ); assert.strictEqual( verifyStoreSignature({ signatureFile: good.signatureFile, repo: "antonndev/ADTemplates", files: sampleFiles, pinnedKey: null }).verified, false ); assert.strictEqual( verifyStoreSignature({ signatureFile: { ...good.signatureFile, signature: Buffer.from("junk").toString("base64") }, repo: "antonndev/ADTemplates", files: sampleFiles, pinnedKey: pinned, }).verified, false ); // A signature file naming a different key fingerprint than the pinned one is // refused even before crypto work — it was produced for someone else's key. assert.match( verifyStoreSignature({ signatureFile: { ...good.signatureFile, publicKeyFingerprint: "0".repeat(64) }, repo: "antonndev/ADTemplates", files: sampleFiles, pinnedKey: pinned, }).reason, /different public key/ ); }); // --- git blob sha1 ------------------------------------------------------------------ test("git blob sha1 matches git's own hashing of blob content", () => { // printf 'hello' | git hash-object --stdin assert.strictEqual(expectedGitBlobSha1("hello"), "b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0"); // printf 'hello\n' | git hash-object --stdin assert.strictEqual(expectedGitBlobSha1("hello\n"), "ce013625030ba8dba906f756967f9e9ca394464a"); assert.strictEqual(verifyGitBlobSha1("hello", "b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0"), true); assert.strictEqual(verifyGitBlobSha1("hello!", "b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0"), false); // Non-40-hex declared hashes carry nothing verifiable — pass through. assert.strictEqual(verifyGitBlobSha1("hello", ""), true); assert.strictEqual(verifyGitBlobSha1("hello", "not-a-sha"), true); }); // --- listed content hash cross-check ------------------------------------------------- test("verifyListedContentHash checks both listing formats and fails closed", () => { // GitHub tree API: 40-hex git blob SHA-1, case-insensitive. assert.deepStrictEqual(verifyListedContentHash("hello", "b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0"), { verified: true }); assert.deepStrictEqual(verifyListedContentHash("hello", "B6FC4C620B67D95F953A5C1C1230AAAB5DB5A1B0"), { verified: true }); const gitMismatch = verifyListedContentHash("hello!", "b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0"); assert.strictEqual(gitMismatch.verified, false); assert.notStrictEqual(gitMismatch.unverifiable, true); // jsDelivr flat listing: base64-encoded SHA-256 of the raw bytes. The base64 // vector is derived from the well-known sha256("hello") hex, which stays the // ground truth. const helloSha256B64 = Buffer.from("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", "hex").toString("base64"); assert.deepStrictEqual(verifyListedContentHash("hello", helloSha256B64), { verified: true }); const helloNewlineB64 = Buffer.from(crypto.createHash("sha256").update("hello\n").digest("hex"), "hex").toString("base64"); assert.deepStrictEqual(verifyListedContentHash("hello\n", helloNewlineB64), { verified: true }); // Base64 is case-sensitive: flipping one character decodes to different // bytes — a hard mismatch, not an unverifiable hash. const tampered = (helloSha256B64[0] === "A" ? "B" : "A") + helloSha256B64.slice(1); const tamperedResult = verifyListedContentHash("hello", tampered); assert.strictEqual(tamperedResult.verified, false); assert.notStrictEqual(tamperedResult.unverifiable, true); // Nothing checkable declared — empty, garbage, or a base64 blob that is not // 32 bytes ("AAAA"; a 64-hex git sha256 blob hash among them) — is reported // unverifiable so the caller warns instead of silently treating the file as // verified. for (const declared of ["", " ", "not-a-sha", "AAAA", "f".repeat(64)]) { const result = verifyListedContentHash("hello", declared); assert.strictEqual(result.verified, false, declared); assert.strictEqual(result.unverifiable, true, declared); assert.ok(result.reason, declared); } assert.match(verifyListedContentHash("hello", "").reason, /no content hash/); }); // --- image registry allowlist ---------------------------------------------------------- test("registry is only the first component when it looks like a host", () => { assert.strictEqual(imageRegistryForRef("ubuntu"), "docker.io"); assert.strictEqual(imageRegistryForRef("library/ubuntu"), "docker.io"); assert.strictEqual(imageRegistryForRef("ghcr.io/pterodactyl/installers:debian"), "ghcr.io"); assert.strictEqual(imageRegistryForRef("localhost:5000/foo/bar"), "localhost:5000"); assert.strictEqual(imageRegistryForRef("registry.mycorp.io/team/app@sha256:" + "a".repeat(64)), "registry.mycorp.io"); }); test("default allowlist covers public registries; env replaces it", () => { assert.strictEqual(validateTemplateImageRefInput("ubuntu", null), null); assert.strictEqual(validateTemplateImageRefInput("ghcr.io/a/b:1", null), null); assert.match(validateTemplateImageRefInput("registry.evil.io/a/b", null), /not allowed/); assert.match(validateTemplateImageRefInput("", null), /missing/); assert.match(validateTemplateImageRefInput("registry.evil.io/a b", null), /whitespace/); const custom = resolveAllowedTemplateRegistries("registry.mycorp.io, docker.io"); assert.deepStrictEqual(custom, ["registry.mycorp.io", "docker.io"]); assert.strictEqual(validateTemplateImageRefInput("registry.mycorp.io/team/app", custom), null); assert.match(validateTemplateImageRefInput("ghcr.io/a/b", custom), /not allowed/); assert.deepStrictEqual(resolveAllowedTemplateRegistries(""), DEFAULT_TEMPLATE_IMAGE_REGISTRIES); }); // --- trust config resolution ---------------------------------------------------------- test("trust config: env wins, mode defaults to enforce with or without a key", () => { // No env/file config: the vendor key pinned in the distribution is the // anchor, so a stock panel verifies store content out of the box. assert.deepStrictEqual(resolveTemplateStoreTrustConfig({ env: {} }), { publicKey: BUILTIN_VENDOR_PUBLIC_KEY, mode: "enforce", autoUpdate: true, pinnedRef: "", }); // The built-in key parses as a real Ed25519 public key. assert.ok(parseEd25519PublicKey(BUILTIN_VENDOR_PUBLIC_KEY), "built-in vendor key must parse"); // A pinned key (env or file) replaces the built-in one. assert.strictEqual( resolveTemplateStoreTrustConfig({ env: { ADPANEL_TEMPLATE_STORE_SIGNING_PUBKEY: "abc" } }).publicKey, "abc" ); assert.strictEqual( resolveTemplateStoreTrustConfig({ env: {}, trustFile: { publicKey: "k" } }).publicKey, "k" ); // Key pinned: enforce. assert.strictEqual(resolveTemplateStoreTrustConfig({ env: { ADPANEL_TEMPLATE_STORE_SIGNING_PUBKEY: "abc" } }).mode, "enforce"); // Explicit warn is the only way to get the permissive posture — with or // without a key. assert.strictEqual( resolveTemplateStoreTrustConfig({ env: { ADPANEL_TEMPLATE_STORE_SIGNATURE_MODE: "warn" } }).mode, "warn" ); assert.strictEqual( resolveTemplateStoreTrustConfig({ env: { ADPANEL_TEMPLATE_STORE_SIGNING_PUBKEY: "abc", ADPANEL_TEMPLATE_STORE_SIGNATURE_MODE: "warn" } }).mode, "warn" ); // An unrecognized mode string must never fall into the permissive mode. assert.strictEqual(resolveTemplateStoreTrustConfig({ env: { ADPANEL_TEMPLATE_STORE_SIGNATURE_MODE: "yolo" } }).mode, "enforce"); const fromFile = resolveTemplateStoreTrustConfig({ env: {}, trustFile: { publicKey: "k", pinnedRef: "v2026.09", autoUpdate: false } }); assert.strictEqual(fromFile.mode, "enforce"); assert.strictEqual(fromFile.pinnedRef, "v2026.09"); assert.strictEqual(fromFile.autoUpdate, false); const envOverridesFile = resolveTemplateStoreTrustConfig({ env: { ADPANEL_TEMPLATE_STORE_PINNED_REF: "main" }, trustFile: { pinnedRef: "v2026.09" }, }); assert.strictEqual(envOverridesFile.pinnedRef, "main"); }); // --- sync diff --------------------------------------------------------------------------- test("sync diff reports added, removed and changed store files", () => { const before = { games: { files: [{ name: "paper.json", sha256: "a".repeat(64) }, { name: "fabric.json", sha256: "b".repeat(64) }] }, media: { files: [{ name: "ffmpeg.json", sha256: "c".repeat(64) }] }, }; const after = { games: { files: [{ name: "paper.json", sha256: "a".repeat(64) }, { name: "fabric.json", sha256: "d".repeat(64) }] }, media: { files: [] }, hosting: { files: [{ name: "nginx.json", sha256: "e".repeat(64) }] }, }; assert.deepStrictEqual(buildStoreSyncDiff(before, after), { added: ["hosting/nginx.json"], removed: ["media/ffmpeg.json"], changed: ["games/fabric.json"], }); });