From 9cb28c714529130a9abf01db489335f764cc853a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 3 Dec 2026 21:37:06 -0700 Subject: names: /lnurlp-fetch fetches a Lightning address's pay parameters for a browser that can't. "another thing is coinos cannot reach speed.app wallet, not sure why" (Shark, relayed by Adam 2026-12-04). speed.app answers /.well-known/lnurlp/ with a 301 to appapi.tryspeed.com that carries no Access-Control-Allow-Origin, and a browser won't follow a cross-origin redirect without it: from v3.coinos.io the fetch fails ("Failed to fetch"), though the target itself allows any origin. GET /lnurlp-fetch?url=https:///.well-known/lnurlp/ fetches it server-side with the link previews' guards (public hosts only, rechecked after every redirect, capped body), https only, that one path only, and returns the pay parameters (200), the host's own error (404) or 502. Rate-limited, cached 5 minutes (30 s for failures). New lnurl-proxy.test.js passes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VMan6WoqaQDYdpWkatPCar --- names/lnurl-proxy.test.js | 35 +++++++++++++++++++++++++++++++++++ names/preview.js | 37 +++++++++++++++++++++++++++++++++++++ names/server.js | 23 ++++++++++++++++++++++- 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 names/lnurl-proxy.test.js (limited to 'names') diff --git a/names/lnurl-proxy.test.js b/names/lnurl-proxy.test.js new file mode 100644 index 0000000..930844a --- /dev/null +++ b/names/lnurl-proxy.test.js @@ -0,0 +1,35 @@ +// bun test names/lnurl-proxy.test.js +import { test, expect } from 'bun:test'; +import { fetchLnurlPay } from './preview.js'; + +const ok = async (s) => { try { const u = new URL(s); return /internal/.test(u.hostname) ? null : u; } catch { return null; } }; +const pay = { tag: 'payRequest', callback: 'https://api.example/cb', minSendable: 1000, maxSendable: 1e9, metadata: '[]' }; +const site = (routes) => async (url) => { + const r = routes[url]; + if (!r) return new Response('nope', { status: 404 }); + if (r.to) return new Response('', { status: 301, headers: { location: r.to } }); + return Response.json(r.body, { status: r.status || 200 }); +}; + +test('follows a redirect to the pay parameters (speed.app)', async () => { + const f = site({ 'https://speed.example/.well-known/lnurlp/bob': { to: 'https://api.example/.well-known/lnurlp/*/speed/bob' }, + 'https://api.example/.well-known/lnurlp/*/speed/bob': { body: pay } }); + expect(await fetchLnurlPay('https://speed.example/.well-known/lnurlp/bob', f, ok)).toEqual(pay); +}); +test('only a Lightning address path, only https', async () => { + const f = site({ 'https://a.example/anything': { body: pay } }); + expect(await fetchLnurlPay('https://a.example/anything', f, ok)).toBeNull(); + expect(await fetchLnurlPay('http://a.example/.well-known/lnurlp/bob', f, ok)).toBeNull(); +}); +test('a redirect into a private host is refused', async () => { + const f = site({ 'https://a.example/.well-known/lnurlp/bob': { to: 'https://x.internal/secret' } }); + expect(await fetchLnurlPay('https://a.example/.well-known/lnurlp/bob', f, ok)).toBeNull(); +}); +test("the host's own error comes back as an error", async () => { + const f = site({ 'https://a.example/.well-known/lnurlp/bob': { body: { status: 'ERROR', reason: 'no such user' }, status: 404 } }); + expect(await fetchLnurlPay('https://a.example/.well-known/lnurlp/bob', f, ok)).toEqual({ status: 'ERROR', reason: 'no such user' }); +}); +test('anything that is not pay parameters is dropped', async () => { + const f = site({ 'https://a.example/.well-known/lnurlp/bob': { body: { hello: 1 } } }); + expect(await fetchLnurlPay('https://a.example/.well-known/lnurlp/bob', f, ok)).toBeNull(); +}); diff --git a/names/preview.js b/names/preview.js index b9af975..4979532 100644 --- a/names/preview.js +++ b/names/preview.js @@ -131,3 +131,40 @@ export async function fetchPreview(str) { } return null; } + +// A Lightning address's pay parameters (LUD-16 step one), for the wallet when +// the browser can't fetch them itself: some hosts answer +// /.well-known/lnurlp/ with a redirect that carries no CORS header +// (speed.app → appapi.tryspeed.com, 2026-12-04), and a browser refuses to +// follow it. Same guards as previews: public hosts only, rechecked after every +// redirect, a capped body. Only that one path, only JSON; null when unusable. +export const LNURLP_PATH = /^\/\.well-known\/lnurlp\/[A-Za-z0-9._+-]{1,64}$/; +export async function fetchLnurlPay(str, fetchFn = fetch, resolve = publicUrl) { + let u = await resolve(str); + if (!u || u.protocol !== 'https:' || !LNURLP_PATH.test(u.pathname)) return null; + for (let hop = 0; u && hop < MAX_HOPS; hop++) { + let res; + try { + res = await fetchFn(u.href, { + redirect: 'manual', + signal: AbortSignal.timeout(TIMEOUT_MS), + headers: { 'user-agent': 'Mozilla/5.0 (compatible; coinos-lnurl/1.0; +https://coinos.io)', accept: 'application/json' }, + }); + } catch { return null; } + if (res.status >= 300 && res.status < 400 && res.headers.get('location')) { + let next; + try { next = new URL(res.headers.get('location'), u.href).href; } catch { return null; } + try { await res.body?.cancel(); } catch {} + u = await resolve(next); + if (u && u.protocol !== 'https:') return null; + continue; + } + let d; + try { d = JSON.parse(await readCapped(res)); } catch { return null; } + if (!d || typeof d !== 'object') return null; + if (d.tag === 'payRequest' && typeof d.callback === 'string' && /^https:\/\//.test(d.callback)) return d; + if (String(d.status).toUpperCase() === 'ERROR') return { status: 'ERROR', reason: String(d.reason || 'error').slice(0, 300) }; + return null; + } + return null; +} diff --git a/names/server.js b/names/server.js index f12b08f..d49c390 100644 --- a/names/server.js +++ b/names/server.js @@ -27,7 +27,7 @@ import { HDKey } from '@scure/bip32'; import { mnemonicToSeedSync } from '@scure/bip39'; import { lnBackend } from '../bridge/ln.js'; import { ArkManager } from '../coinosv3/src/ark/manager.js'; -import { fetchPreview } from './preview.js'; +import { fetchPreview, fetchLnurlPay } from './preview.js'; import { dnsUpdate } from './dns-update.js'; import { translate, translateDeepl, languages } from './translate.js'; import { decodeAddress, arkIdFromServerPubkey } from '../coinosv3/src/ark/proto.js'; @@ -1317,6 +1317,7 @@ function rateOk(ip, limit = 10) { // HTTP // --------------------------------------------------------------------------- +const lnurlFetches = new Map(); // /lnurlp-fetch: url → { d, until } const json = (body, status = 200) => { if (status >= 400) log(`→ ${status} ${JSON.stringify(body).slice(0, 120)}`); return new Response(JSON.stringify(body), { @@ -1460,6 +1461,26 @@ Bun.serve({ // A link in a post, as the card its page describes for itself (Open // Graph). Cached a day either way: a page with nothing to show is asked // again only after an hour. Public data; the CDN may cache it too. + // A Lightning address's pay parameters fetched for the wallet when the + // browser can't (a redirect without CORS: speed.app). GET ?url=https:// + // host/.well-known/lnurlp/name → the host's JSON, or 502. + if (url.pathname === '/lnurlp-fetch' && req.method === 'GET') { + if (!rateOk(ip, 60)) return json({ error: 'rate limited' }, 429); + const target = (url.searchParams.get('url') || '').trim(); + if (!/^https:\/\/\S{4,300}$/.test(target)) return json({ error: 'bad url' }, 400); + let hit = lnurlFetches.get(target); + if (!hit || Date.now() > hit.until) { + const d = await fetchLnurlPay(target).catch(() => null); + hit = { d, until: Date.now() + (d?.tag ? 300_000 : 30_000) }; + lnurlFetches.set(target, hit); + if (lnurlFetches.size > 2000) lnurlFetches.delete(lnurlFetches.keys().next().value); + } + return new Response(JSON.stringify(hit.d || { status: 'ERROR', reason: "couldn't reach that address" }), { + status: hit.d?.tag ? 200 : hit.d ? 404 : 502, + headers: { 'content-type': 'application/json', 'access-control-allow-origin': '*', 'cache-control': 'no-store' }, + }); + } + if (url.pathname === '/preview' && req.method === 'GET') { if (!rateOk(ip, 120)) return json({ error: 'rate limited' }, 429); const target = (url.searchParams.get('url') || '').trim(); -- cgit v1.2.3