From a07e326bc9dae4a3df02ce8f08cb4f71f094392e Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 2 Dec 2026 13:45:51 -0700 Subject: rate limits: key per-account limits on the user, add them to on-chain sends The token-keyed limits on /lightning/quote and GET /invoice/:id used the first 50 characters of the Authorization header, which are the same for every JWT, so all signed-in users shared one bucket. /lightning/quote now limits per user id (checked after auth), and GET /invoice/:id per IP like the other invoice routes. /bitcoin/fee and /bitcoin/send get a per-user limit of 10 a minute, and repeated fee-estimate failures log once per user per 10 minutes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VMan6SSfVopM1ZzXKZmLYU --- index.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'index.ts') diff --git a/index.ts b/index.ts index 81cb2743..1658dee4 100644 --- a/index.ts +++ b/index.ts @@ -23,6 +23,20 @@ import rates from "$routes/rates"; import square from "$routes/square"; import users from "$routes/users"; +// Per-account limit. Runs at preHandler, after auth (preValidation) has set +// req.user, so the bucket is the real user id. Keying on a slice of the +// Authorization header doesn't work: every token starts with the same JWT +// header + `{"id":"` prefix, so all users would share one bucket. +const perUser = (name: string, max: number, timeWindow: number) => ({ + rateLimit: { + max, + timeWindow, + hook: "preHandler", + keyGenerator: (req) => + `${name}:${req.user?.id || (req.headers["cf-connecting-ip"] as string) || req.ip}`, + }, +}); + try { getLocations(); getFx(); @@ -46,7 +60,7 @@ app.get("/rates", rates.index); app.get("/locations", locations.list); -app.get("/invoice/:id", { config: { rateLimit: { max: 30, timeWindow: 10000, keyGenerator: (req) => { const ip = (req.headers["cf-connecting-ip"] as string) || req.ip; const token = (req.headers.authorization || "").slice(0, 50); return token ? `invoice:${token}` : `invoice:${ip}`; } } } }, invoices.get); +app.get("/invoice/:id", { config: { rateLimit: { max: 30, timeWindow: 10000, keyGenerator: (req) => `invoice:${(req.headers["cf-connecting-ip"] as string) || req.ip}` } } }, invoices.get); app.get("/invoices", auth, invoices.list); app.post("/invoice", { ...optional, config: { rateLimit: { max: 30, timeWindow: 10000, keyGenerator: (req) => `invoicecreate:${(req.headers["cf-connecting-ip"] as string) || req.ip}` } } }, invoices.create); app.post("/invoice/:id", { ...optional, config: { rateLimit: { max: 30, timeWindow: 10000, keyGenerator: (req) => `invoiceupdate:${(req.headers["cf-connecting-ip"] as string) || req.ip}` } } }, invoices.update); @@ -94,13 +108,7 @@ app.post( "/lightning/quote", { ...auth, - config: { - rateLimit: { - max: 20, - timeWindow: 10000, - keyGenerator: (req) => `lnquote:${(req.headers.authorization || "").slice(0, 50) || (req.headers["cf-connecting-ip"] as string) || req.ip}`, - }, - }, + config: perUser("lnquote", 20, 10000), }, payments.quote, ); @@ -124,8 +132,8 @@ app.get("/pay/:username/:amount", lnurl.pay); app.post("/freeze", payments.freeze); app.post("/confirm", payments.confirm); -app.post("/bitcoin/fee", auth, payments.fee); -app.post("/bitcoin/send", auth, payments.send); +app.post("/bitcoin/fee", { ...auth, config: perUser("btcfee", 10, 60000) }, payments.fee); +app.post("/bitcoin/send", { ...auth, config: perUser("btcsend", 10, 60000) }, payments.send); app.get("/account/:id", auth, users.account); app.post("/account/:id", auth, users.updateAccount); -- cgit v1.2.3