From 2fef93f3f9bf646d66625deb09c8b5b278a32cae Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 6 Nov 2026 23:15:17 -0700 Subject: nwcpush only pushes to push services and public hosts. A subscription is whatever a stranger POSTs to /register, and delivering one is this server making a request to the URL in it: unchecked, anyone could point it at an address inside our own network — an interactsh (oast.pro) probe registered one under the coinos service key (2026-11-06). Now a browser subscription must name a browser push service (FCM/jmt Google, Mozilla, Apple, Windows); a UnifiedPush endpoint must be https on a public host, its name re-resolved at send time (a name can be re-pointed after registering) and refused if any address is private, loopback, link-local, CGNAT or multicast, with redirects not followed; an fcm: endpoint must be a token. /register refuses the rest (400); at delivery a failing endpoint counts as dead (410) so stored ones are dropped, except a name that merely didn't resolve just then (503, kept). Audited against the live registrations: 760 kept; dropped are the probe, example.com, a 10.x ntfy, an IP-literal private endpoint, two hosts that no longer resolve and one unparseable endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VMan6Ts5Tktega8VbLg31q --- nwcpush/server.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'nwcpush') diff --git a/nwcpush/server.js b/nwcpush/server.js index 82257da..7f4cfe1 100644 --- a/nwcpush/server.js +++ b/nwcpush/server.js @@ -19,6 +19,8 @@ import { readFileSync, writeFileSync, renameSync, mkdirSync, existsSync } from 'node:fs'; import { dirname, join } from 'node:path'; +import { lookup } from 'node:dns/promises'; +import { isIP } from 'node:net'; import { SimplePool } from 'nostr-tools/pool'; import { verifyEvent } from 'nostr-tools/pure'; import webpush from 'web-push'; @@ -125,11 +127,64 @@ async function deliverFCM(sub, payload, opts) { throw err; } +// Where a push may go. A subscription is whatever a stranger POSTs to +// /register, and delivering one is this server making an HTTP request to the +// URL in it — so an unchecked endpoint let anyone point us at an address +// inside our own network (an interactsh probe registered one, 2026-11-06). +// browser push: only the browsers' push services; +// UnifiedPush: https on a public host, re-checked against what the name +// resolves to at send time (a name can be re-pointed after registering), +// redirects not followed; +// fcm: a token, nothing more. +// Checked at /register and again at delivery, where a failure counts as a +// dead endpoint (410) so stored ones that don't pass are dropped. +const PUSH_SERVICES = /(^|\.)(fcm\.googleapis\.com|android\.googleapis\.com|jmt\d*\.google\.com|push\.services\.mozilla\.com|push\.apple\.com|notify\.windows\.com)$/i; +function privateIp(ip) { + if (isIP(ip) === 4) { + const [a, b] = ip.split('.').map(Number); + return a === 0 || a === 10 || a === 127 || (a === 100 && b >= 64 && b <= 127) || (a === 169 && b === 254) + || (a === 172 && b >= 16 && b <= 31) || (a === 192 && b === 168) || (a === 198 && (b === 18 || b === 19)) || a >= 224; + } + const v = ip.toLowerCase(); + if (v.startsWith('::ffff:')) return privateIp(v.slice(7)); + return v === '::' || v === '::1' || v.startsWith('fc') || v.startsWith('fd') || v.startsWith('fe8') || v.startsWith('fe9') + || v.startsWith('fea') || v.startsWith('feb') || v.startsWith('ff'); +} +function endpointShape(sub) { + const ep = sub && typeof sub.endpoint === 'string' ? sub.endpoint : ''; + if (!ep || ep.length > 2048) return false; + if (isUP(sub) && ep.startsWith(FCM_PREFIX)) return /^fcm:[A-Za-z0-9_:\-]{20,4096}$/.test(ep); + let u; + try { u = new URL(ep); } catch { return false; } + if (u.protocol !== 'https:' || u.username || u.password || (u.port && u.port !== '443')) return false; + const host = u.hostname.replace(/^\[|\]$/g, ''); + if (!isUP(sub)) return PUSH_SERVICES.test(host); + if (isIP(host)) return !privateIp(host); + return host.includes('.') && !/(^|\.)(localhost|local|internal|lan|home|corp|localdomain)$/i.test(host); +} +// true: fine; false: never (bad shape, or a private address); null: the name +// didn't resolve just now — refused at /register, but at delivery a passing +// DNS hiccup mustn't cost a working registration +async function endpointSafe(sub) { + if (!endpointShape(sub)) return false; + if (!isUP(sub) || isFCM(sub)) return true; + const host = new URL(sub.endpoint).hostname.replace(/^\[|\]$/g, ''); + if (isIP(host)) return true; + let addrs; + try { addrs = await lookup(host, { all: true }); } catch { return null; } + if (!addrs.length) return null; + return addrs.every((a) => !privateIp(a.address)); +} + async function deliver(sub, payload, opts = {}) { + const safe = await endpointSafe(sub); + if (safe === false) throw Object.assign(new Error('endpoint refused'), { statusCode: 410 }); + if (safe === null) throw Object.assign(new Error('endpoint did not resolve'), { statusCode: 503 }); if (!isUP(sub)) return webpush.sendNotification(sub, payload, opts); if (isFCM(sub)) return deliverFCM(sub, payload, opts); const res = await fetch(sub.endpoint, { method: 'POST', + redirect: 'manual', headers: { 'content-type': 'application/json' }, body: payload, signal: AbortSignal.timeout(8000), @@ -681,6 +736,7 @@ const server = Bun.serve({ if (url.pathname === '/register' && req.method === 'POST') { const body = await req.json().catch(() => null); const sub_ = body?.subscription; + if (sub_ && (await endpointSafe(sub_)) !== true) return json({ error: 'push endpoint refused' }, 400); const hasNwc = Array.isArray(body?.servicePubkeys); const pks = (body?.servicePubkeys || []).filter((p) => /^[0-9a-f]{64}$/.test(p)); // user-facing notification watch (messages + payments) — independent of -- cgit v1.2.3