aboutsummaryrefslogtreecommitdiffstats
path: root/lib/safe-fetch.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-11-25 09:25:18 -0700
committerAdam Soltys <[email protected]>2026-11-25 09:25:18 -0700
commitcdd086afc3044d9c8beefb0cc1abc307d1e4557e (patch)
treeb0397b911bf34d4af2b0b68ff5bcd05a60f93cfe /lib/safe-fetch.ts
parent0f3cb69da812dc81b2a5d939b1eebf1146ee2369 (diff)
downloadcoinos-server-cdd086afc3044d9c8beefb0cc1abc307d1e4557e.tar.gz
coinos-server-cdd086afc3044d9c8beefb0cc1abc307d1e4557e.zip
webhooks, zaps: send user-supplied URLs through the SSRF guard
Invoice webhooks must be plain http(s) strings and are posted with safePost (checked, pinned address, no redirects). Zap requests resolve the recipient lud16/lud06 and callback through safeGot instead of fetching them as-is. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VMan6SEnsbshZrUDtTsxAX
Diffstat (limited to 'lib/safe-fetch.ts')
-rw-r--r--lib/safe-fetch.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/safe-fetch.ts b/lib/safe-fetch.ts
index 2a9c7870..b1d082f6 100644
--- a/lib/safe-fetch.ts
+++ b/lib/safe-fetch.ts
@@ -165,6 +165,42 @@ const fetchOnce = (url: string, redirectsLeft: number): Promise<any> =>
// body. Throws on a bad scheme/URL or a host that resolves into a blocked range.
export const safeGot = async (url: string): Promise<any> => fetchOnce(url, MAX_REDIRECTS);
+// POST JSON to a user-supplied URL (merchant webhooks) with the same
+// protections as safeGot: http(s) only, the socket pinned to an address that
+// was checked, and no redirects at all (a redirect could turn a public URL
+// into an internal one). The response body is discarded; resolves with the
+// status code.
+export const safePost = (url: unknown, json: unknown): Promise<number> =>
+ new Promise((resolve, reject) => {
+ if (typeof url !== "string" || url.length > 2048)
+ return reject(new Error("invalid url"));
+ let u: URL;
+ try { u = new URL(url); } catch { return reject(new Error("invalid url")); }
+ if (u.protocol !== "http:" && u.protocol !== "https:")
+ return reject(new Error("unsupported scheme"));
+ const literal = u.hostname.replace(/^\[|\]$/g, "");
+ if (net.isIP(literal) && isBlockedIp(literal))
+ return reject(new Error(`blocked host ${literal}`));
+
+ const body = Buffer.from(JSON.stringify(json));
+ const mod = u.protocol === "https:" ? https : http;
+ const req = mod.request(
+ url,
+ {
+ method: "POST",
+ lookup: pinnedLookup as any,
+ headers: { "content-type": "application/json", "content-length": body.length },
+ },
+ (res) => {
+ res.resume();
+ resolve(res.statusCode || 0);
+ },
+ );
+ req.setTimeout(TIMEOUT_MS, () => req.destroy(new Error("request timed out")));
+ req.on("error", reject);
+ req.end(body);
+ });
+
// For URLs we hand to a library that does its own connecting (nostr relays
// over WebSocket): allow only ws/wss to a host whose every resolved address
// is public. Not rebind-proof like safeGot, but keeps user-supplied relay