aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lnurl-payload.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-11-13 16:11:02 -0700
committerAdam Soltys <[email protected]>2026-11-13 16:11:02 -0700
commitb8b409910f71976959135b336b409c4923cf4c87 (patch)
tree21115643c3bed3c967a9bdc2fe1c75fa9cdfa07d /lib/lnurl-payload.ts
parent589519f2556b2d434ed23f26c705df73b6149d7c (diff)
downloadcoinos-server-b8b409910f71976959135b336b409c4923cf4c87.tar.gz
coinos-server-b8b409910f71976959135b336b409c4923cf4c87.zip
lnurl: fetch user-supplied URLs through the SSRF guard, relay only protocol fields
Lightning address sends (pay() and POST /send/<lnaddr>) 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 <[email protected]>
Diffstat (limited to 'lib/lnurl-payload.ts')
-rw-r--r--lib/lnurl-payload.ts60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/lnurl-payload.ts b/lib/lnurl-payload.ts
new file mode 100644
index 00000000..6248d213
--- /dev/null
+++ b/lib/lnurl-payload.ts
@@ -0,0 +1,60 @@
+import { bech32 } from "bech32";
+
+const invalid = (): never => { throw new Error("Invalid LNURL response"); };
+const object = (v: any) => v !== null && typeof v === "object" && !Array.isArray(v);
+const amount = (v: any, min: number) => Number.isSafeInteger(v) && v >= min;
+const callback = (v: any) => {
+ if (typeof v !== "string") return invalid();
+ const u = new URL(v);
+ if (!["http:", "https:"].includes(u.protocol) || u.username || u.password) invalid();
+ return v;
+};
+
+// Return only protocol fields: never relay an arbitrary upstream JSON object.
+export function lnurlPayload(v: any): any {
+ if (!object(v)) return invalid();
+ if (v.status === "ERROR" && typeof v.reason === "string")
+ return { status: "ERROR", reason: "LNURL service returned an error" };
+ if (v.status === "OK" && !v.tag && !v.pr) return { status: "OK" };
+ if (v.tag === "payRequest") {
+ if (!amount(v.minSendable, 1) || !amount(v.maxSendable, v.minSendable) || typeof v.metadata !== "string") invalid();
+ let metadata: any;
+ try { metadata = JSON.parse(v.metadata); } catch { return invalid(); }
+ if (!Array.isArray(metadata) || !metadata.every((e: any) => Array.isArray(e) && e.length >= 2 && typeof e[0] === "string") ||
+ metadata.filter((e: any) => e[0] === "text/plain" && typeof e[1] === "string").length !== 1) invalid();
+ const out: any = { tag: v.tag, callback: callback(v.callback), minSendable: v.minSendable, maxSendable: v.maxSendable, metadata: v.metadata };
+ if (amount(v.commentAllowed, 0)) out.commentAllowed = v.commentAllowed;
+ if (v.allowsNostr === true && typeof v.nostrPubkey === "string" && /^[a-f0-9]{64}$/i.test(v.nostrPubkey)) {
+ out.allowsNostr = true; out.nostrPubkey = v.nostrPubkey;
+ }
+ return out;
+ }
+ if (v.tag === "withdrawRequest") {
+ if (typeof v.k1 !== "string" || !v.k1 || typeof v.defaultDescription !== "string" ||
+ !amount(v.minWithdrawable, 0) || !amount(v.maxWithdrawable, v.minWithdrawable)) invalid();
+ return { tag: v.tag, callback: callback(v.callback), k1: v.k1, defaultDescription: v.defaultDescription,
+ minWithdrawable: v.minWithdrawable, maxWithdrawable: v.maxWithdrawable };
+ }
+ if (typeof v.pr === "string") {
+ let decoded: any;
+ try { decoded = bech32.decode(v.pr, 20000); } catch { return invalid(); }
+ if (!/^ln(bc|tb|bcrt|sb)[0-9]*[munp]?$/.test(decoded.prefix) || decoded.words.length < 111 ||
+ !Array.isArray(v.routes) || v.routes.length !== 0) invalid();
+ const out: any = { pr: v.pr, routes: [] };
+ if (v.disposable === false || v.disposable === true) out.disposable = v.disposable;
+ if (typeof v.verify === "string") out.verify = callback(v.verify);
+ const a = v.successAction;
+ if (object(a)) {
+ if (a.tag === "message" && typeof a.message === "string" && a.message.length <= 144)
+ out.successAction = { tag: a.tag, message: a.message };
+ else if (a.tag === "url" && typeof a.description === "string" && a.description.length <= 144)
+ out.successAction = { tag: a.tag, description: a.description, url: callback(a.url) };
+ else if (a.tag === "aes" && typeof a.description === "string" && a.description.length <= 144 &&
+ typeof a.ciphertext === "string" && a.ciphertext.length <= 4096 && typeof a.iv === "string" && /^[A-Za-z0-9+/]{22}==$/.test(a.iv))
+ out.successAction = { tag: a.tag, description: a.description, ciphertext: a.ciphertext, iv: a.iv };
+ else invalid();
+ }
+ return out;
+ }
+ return invalid();
+}