From b8b409910f71976959135b336b409c4923cf4c87 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 13 Nov 2026 16:11:02 -0700 Subject: lnurl: fetch user-supplied URLs through the SSRF guard, relay only protocol fields Lightning address sends (pay() and POST /send/) now validate the name and domain and fetch through safeGot instead of plain got. Zap receipt relays from the zap request must resolve to public hosts. The LNURL decode and proxy routes return only LNURL protocol fields instead of whatever JSON the upstream sent, and the proxy logs failures rate-limited with a generic error to the client. Also blocks the 172.42.0.0/16 docker range. Co-Authored-By: Claude Opus 5.5 --- test/lnurl-payload.test.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test/lnurl-payload.test.ts (limited to 'test/lnurl-payload.test.ts') diff --git a/test/lnurl-payload.test.ts b/test/lnurl-payload.test.ts new file mode 100644 index 00000000..01d1da9b --- /dev/null +++ b/test/lnurl-payload.test.ts @@ -0,0 +1,29 @@ +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(); + }); +}); -- cgit v1.2.3