diff options
| author | Adam Soltys <[email protected]> | 2026-11-25 09:25:18 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-11-25 09:25:18 -0700 |
| commit | 0f3cb69da812dc81b2a5d939b1eebf1146ee2369 (patch) | |
| tree | afbf4fe0da8049684960f45a7d43cee36a5dd069 | |
| parent | 6d5f69a23b8e52513825465a06911d67702edec3 (diff) | |
| download | coinos-server-0f3cb69da812dc81b2a5d939b1eebf1146ee2369.tar.gz coinos-server-0f3cb69da812dc81b2a5d939b1eebf1146ee2369.zip | |
confirm: accept only canonical txids from the node notify scripts
Lowercase and validate the txid so a mixed-case txid cannot slip past the
case-sensitive dedupe keys, refuse requests that came through Cloudflare or
the onion, and require the TCP peer to be a node container or loopback so
internal relays cannot reach it either. Validate type and wallet.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VMan6SEnsbshZrUDtTsxAX
| -rw-r--r-- | lib/notifier.ts | 35 | ||||
| -rw-r--r-- | routes/payments.ts | 22 |
2 files changed, 56 insertions, 1 deletions
diff --git a/lib/notifier.ts b/lib/notifier.ts new file mode 100644 index 00000000..adc09457 --- /dev/null +++ b/lib/notifier.ts @@ -0,0 +1,35 @@ +import config from "$config"; +import dns from "node:dns"; + +// POST /confirm credits deposits, so only the wallet-notify scripts may call +// it. They run inside the bitcoin and liquid node containers and connect to +// us directly, so the TCP peer is one of those nodes (or ourselves, for the +// catch-up sweep in lib/payments.ts). Checking the peer address, rather than +// the absence of proxy headers, also stops requests relayed by another +// internal service such as the UI's server-side forwarding routes. +const hosts = [config.bitcoin?.host, config.liquid?.host].filter( + (h): h is string => typeof h === "string" && h.length > 0, +); + +let notifiers = new Set<string>(); +const resolve = async () => { + const next = new Set<string>(); + for (const host of hosts) { + try { + for (const { address } of await dns.promises.lookup(host.split(":")[0], { + all: true, + })) + next.add(address); + } catch {} + } + if (next.size) notifiers = next; +}; +await resolve(); +setInterval(resolve, 30000).unref(); + +const loopback = (ip: string) => ip === "::1" || ip.startsWith("127."); + +export const isNotifier = (req): boolean => { + const peer = (req.socket?.remoteAddress || "").replace(/^::ffff:/, ""); + return loopback(peer) || notifiers.has(peer); +}; diff --git a/routes/payments.ts b/routes/payments.ts index 9cc31434..491ee3cc 100644 --- a/routes/payments.ts +++ b/routes/payments.ts @@ -8,6 +8,7 @@ import ln from "$lib/ln"; import { platformFee, quoteMax, quoteRoutingFee } from "$lib/lnquote"; import { err, l, quiet, shortError, suppressed, warn } from "$lib/logging"; import mqtt from "$lib/mqtt"; +import { isNotifier } from "$lib/notifier"; import { build, completePayment, @@ -551,8 +552,27 @@ export default { async confirm(req, res) { const { - body: { txid, wallet, type }, + body: { wallet, type }, } = req; + // Canonical lowercase txid. bitcoind accepts any hex case, but the + // credited:/payment: dedupe keys are case-sensitive, so a mixed-case txid + // would re-credit an already-credited output. + const txid = String(req.body?.txid ?? "").toLowerCase(); + if (!/^[0-9a-f]{64}$/.test(txid)) return res.code(400).send("invalid txid"); + // Only our own wallet notify scripts may call this (they hit app:3119 + // directly). Anything that came through Cloudflare or the onion carries + // cf-connecting-ip (set in lib/app.ts for tor) or x-forwarded-for. + if (req.headers["cf-connecting-ip"] || req.headers["x-forwarded-for"]) + return res.code(403).send("forbidden"); + // Headers alone are not enough: the UI's /post/<path> route relays to us + // without them. Require the TCP peer to be a node container or ourselves. + if (!isNotifier(req)) { + warn("confirm refused from", req.socket?.remoteAddress, txid, wallet); + return res.code(403).send("forbidden"); + } + if (type !== PaymentType.bitcoin && type !== PaymentType.liquid) + return res.code(400).send("invalid type"); + if (typeof wallet !== "string") return res.code(400).send("invalid wallet"); try { const node = rpc({ ...config[type], wallet }); |
