diff options
Diffstat (limited to 'lib/safe-fetch.ts')
| -rw-r--r-- | lib/safe-fetch.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/safe-fetch.ts b/lib/safe-fetch.ts index 5b1059e9..b1d082f6 100644 --- a/lib/safe-fetch.ts +++ b/lib/safe-fetch.ts @@ -26,6 +26,7 @@ const blockedV4 = (ip: string): boolean => { if (a === 127) return true; // loopback if (a === 169 && b === 254) return true; // link-local + cloud metadata (169.254.169.254) if (a === 172 && b >= 16 && b <= 31) return true; // private + if (a === 172 && b === 42) return true; // cs Docker network uses 172.42.0.0/16 if (a === 192 && b === 168) return true; // private if (a === 100 && b >= 64 && b <= 127) return true; // CGNAT 100.64/10 (incl. tailscale) if (a >= 224) return true; // multicast / reserved @@ -163,3 +164,55 @@ const fetchOnce = (url: string, redirectsLeft: number): Promise<any> => // Fetch a user-supplied URL with SSRF protection and return the parsed JSON // 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 +// lists from pointing us at internal services. +export const safeRelayUrl = async (raw: string): Promise<boolean> => { + let u: globalThis.URL; + try { u = new globalThis.URL(raw); } catch { return false; } + if (u.protocol !== "ws:" && u.protocol !== "wss:") return false; + const host = u.hostname.replace(/^\[|\]$/g, ""); + if (net.isIP(host)) return !isBlockedIp(host); + try { + const addrs = await dns.promises.lookup(host, { all: true }); + return addrs.length > 0 && addrs.every((a) => !isBlockedIp(a.address)); + } catch { return false; } +}; |
