import { describe, expect, test } from "bun:test"; import { bech32 } from "bech32"; import { lnurlPayload } from "../lib/lnurl-payload"; import { safeGot } from "../lib/safe-fetch"; const pay = { tag: "payRequest", callback: "https://example.com/pay", minSendable: 1000, maxSendable: 2000, metadata: '[["text/plain","coffee"]]' }; describe("LNURL response boundary", () => { test("keeps supported pay fields and strips unrelated data", () => { expect(lnurlPayload({ ...pay, secret: "internal data", commentAllowed: 100 })).toEqual({ ...pay, commentAllowed: 100 }); }); test("rejects unrelated and malformed JSON payloads", () => { for (const v of [null, [], "hello", { version: "docker" }, { ...pay, minSendable: -1 }, { ...pay, maxSendable: 1 }, { ...pay, metadata: "{}" }, { ...pay, callback: "file:///etc/passwd" }, { ...pay, metadata: '[["image/png;base64","abc"]]' }]) expect(() => lnurlPayload(v)).toThrow(); }); test("withdrawal and status responses expose only protocol fields", () => { const w = { tag: "withdrawRequest", callback: "https://example.com/withdraw", k1: "challenge", defaultDescription: "withdraw", minWithdrawable: 0, maxWithdrawable: 1000 }; expect(lnurlPayload({ ...w, secret: "hidden" })).toEqual(w); expect(lnurlPayload({ status: "OK", secret: "hidden" })).toEqual({ status: "OK" }); expect(lnurlPayload({ status: "ERROR", reason: "sensitive upstream error", secret: "hidden" }).reason).toBe("LNURL service returned an error"); }); test("requires a checksummed invoice and strips extras", () => { expect(() => lnurlPayload({ pr: "not an invoice", routes: [] })).toThrow(); const pr = bech32.encode("lnbc10n", Array(120).fill(0), 20000); expect(lnurlPayload({ pr, routes: [], secret: "hidden", successAction: { tag: "message", message: "thanks", secret: "hidden" } })).toEqual({ pr, routes: [], successAction: { tag: "message", message: "thanks" } }); }); test("blocks internal literal targets before fetching", async () => { for (const url of ["http://172.42.0.13:3119/health", "http://127.0.0.1", "http://169.254.169.254", "http://10.1.2.3", "http://[::1]", "http://[::ffff:127.0.0.1]", "file:///etc/passwd"]) await expect(safeGot(url)).rejects.toThrow(); }); });