aboutsummaryrefslogtreecommitdiffstats
path: root/routes/users.ts
diff options
context:
space:
mode:
Diffstat (limited to 'routes/users.ts')
-rw-r--r--routes/users.ts861
1 files changed, 629 insertions, 232 deletions
diff --git a/routes/users.ts b/routes/users.ts
index 34c2a655..77ddbdc6 100644
--- a/routes/users.ts
+++ b/routes/users.ts
@@ -1,21 +1,28 @@
-import { createReadStream } from "node:fs";
-import { appendFile, unlink } from "node:fs/promises";
+import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
import config from "$config";
import { requirePin } from "$lib/auth";
-import { db, g, ga, s } from "$lib/db";
+import { db, g, gf, s } from "$lib/db";
import { err, l, warn } from "$lib/logging";
+import { isDefaultName } from "$lib/default-names";
+import { upgradeLegacyNsec } from "$lib/legacynsec";
import { mail, templates } from "$lib/mail";
-import { getNostrUser, getProfile, serverPubkey } from "$lib/nostr";
-import { reconcile } from "$lib/payments";
+import { getNostrUser, getProfile, serverPubkey2 } from "$lib/nostr";
+import deleteUser from "$lib/deluser";
import register from "$lib/register";
+import { assertNameFree } from "$lib/names";
+import { isReserved } from "$lib/reserved";
import { emit } from "$lib/sockets";
import upload from "$lib/upload";
import { bail, fail, fields, getUser, pick } from "$lib/utils";
import whitelist from "$lib/whitelist";
-import rpc from "@coinos/rpc";
+
import { $ } from "bun";
+import got from "got";
import jwt from "jsonwebtoken";
import { getPublicKey, nip19, verifyEvent } from "nostr-tools";
+import { hexToBytes } from "@noble/hashes/utils";
import { authenticator } from "otplib";
import { v4 } from "uuid";
@@ -25,6 +32,116 @@ import type { ProfilePointer } from "nostr-tools/nip19";
const { host } = new URL(process.env.URL);
const relay = encodeURIComponent(config.publicRelay);
+// reCAPTCHA v3 is score-based: siteverify answers success:true for any
+// validly minted token, bot or not, and puts the verdict in `score` (0 = bot,
+// 1 = human). Checking `success` alone let a headless-browser signup wave
+// through on 2026-10-18 with a fresh token on every request. `actions` and
+// the hostname check stop a token minted for another page or site from being
+// replayed here.
+const verifyRecaptcha = async (
+ response,
+ req?,
+ { actions = [] as string[], minScore = 0 } = {},
+) => {
+ const { recaptcha: secret } = config;
+ if (!secret) return true;
+
+ // Skip captcha for Tor users accessing via .onion
+ const reqHost = req?.headers?.["x-forwarded-host"] || req?.headers?.["host"] || "";
+ if (reqHost.endsWith(".onion")) return true;
+
+ // Skip captcha for allowlisted User-Agents (prefix match)
+ const uaRaw = req?.headers?.["user-agent"] || req?.headers?.["User-Agent"];
+ const ua = Array.isArray(uaRaw) ? uaRaw[0] : uaRaw;
+ if (typeof ua === "string") {
+ const prefixes = await db.sMembers("nocaptcha_ua");
+ const uaLower = ua.toLowerCase();
+ if (prefixes.some((p) => uaLower.startsWith(p.toLowerCase()))) {
+ return true;
+ }
+ }
+
+ // Check for valid API key to bypass captcha
+ const apiKey = req?.headers?.["x-api-key"];
+ if (apiKey && (await db.sIsMember("apikeys", apiKey))) {
+ return true;
+ }
+
+ const { username } = req.body;
+ if (username &&
+ await db.sIsMember("nocaptcha", username.toLowerCase().replace(/\s/g, ""))
+ )
+ return true;
+
+ if (!response) return false;
+
+ try {
+ const ip = req?.headers?.["cf-connecting-ip"] || req?.socket?.remoteAddress;
+ const { success, score, action, hostname } = await got
+ .post("https://www.google.com/recaptcha/api/siteverify", {
+ form: {
+ secret,
+ response,
+ remoteip: ip,
+ },
+ // Bound the wait so a slow/unreachable Google can't hang the login.
+ timeout: { request: 6000 },
+ })
+ .json();
+ // A real verdict from Google: fail CLOSED only when the captcha itself is
+ // invalid (success === false).
+ if (!!config.adminpass && response === config.adminpass) return true;
+ if (!success) return false;
+ if (hostname && hostname !== host && !hostname.endsWith(`.${host}`)) {
+ warn("recaptcha wrong hostname", ip, hostname);
+ return false;
+ }
+ if (actions.length && !actions.includes(action)) {
+ warn("recaptcha wrong action", ip, action);
+ return false;
+ }
+ if (action === "register") l("recaptcha register score", ip, score);
+ if (typeof score === "number" && score < minScore) {
+ warn("recaptcha low score", ip, action, score);
+ return false;
+ }
+ return true;
+ } catch (e) {
+ // Fail OPEN on an INFRASTRUCTURE error (network blip, timeout, Google 5xx).
+ // Reaching Google is not part of authentication; failing closed here turned
+ // a transient coinos<->Google connectivity hiccup into a login outage —
+ // users got "failed captcha" until it cleared (2026-08-25 incident). The
+ // password and the per-IP login rate limits (ipCount>30 / ipFailures>20 ->
+ // 429) still gate the request; captcha is only bot-mitigation, so allowing
+ // it through when the verifier is unreachable is the safer failure mode.
+ warn(
+ "recaptcha verify unreachable — allowing login (fail-open)",
+ (e as any)?.code ?? (e as any)?.message ?? String(e),
+ );
+ return true;
+ }
+};
+
+// Cloudflare Turnstile, on trial next to reCAPTCHA for the signup page:
+// reCAPTCHA v3 scores the 2026-10 farm 0.9, so this only logs its verdict
+// until we've seen whether it tells the farm apart. Returns null when the
+// verifier can't be reached so callers can tell "unknown" from "failed".
+const verifyTurnstile = async (token, ip) => {
+ if (!config.turnstile || !token) return null;
+ try {
+ const r: any = await got
+ .post("https://challenges.cloudflare.com/turnstile/v0/siteverify", {
+ form: { secret: config.turnstile, response: token, remoteip: ip },
+ timeout: { request: 6000 },
+ })
+ .json();
+ return r;
+ } catch (e) {
+ warn("turnstile verify unreachable", (e as any)?.code ?? String(e));
+ return null;
+ }
+};
+
export default {
upload,
@@ -32,21 +149,10 @@ export default {
const { user } = req;
try {
user.balance = await g(`balance:${user.id}`);
- user.locked = await ga(`balance:${user.id}`);
-
- if (user.locked) {
- const blacklisted = await db.sIsMember(
- "blacklist",
- user?.username?.toLowerCase().trim(),
- );
-
- const whitelisted = await db.sIsMember(
- "whitelist",
- user?.username?.toLowerCase().trim(),
- );
-
- if (!blacklisted || whitelisted) user.locked = 0;
- }
+ // The password-encrypted nostr key is only handed out by /login, after
+ // the password (and 2FA) check. A bearer token alone mustn't be enough
+ // to fetch it and brute-force or decrypt it offline.
+ user.hasnsec = !!(user.nsec || user.cipher);
user.prompt = !!user.prompt;
if (user.pubkey) user.npub = nip19.npubEncode(user.pubkey);
@@ -58,42 +164,6 @@ export default {
}
},
- async list(req, res) {
- const { user } = req;
- if (!user.admin) fail("unauthorized");
-
- const users = [];
-
- for await (const k of db.scanIterator({ MATCH: "balance:*" })) {
- const uid = k.split(":")[1];
- const user = await getUser(uid);
-
- if (!user) {
- await db.del(`balance:${uid}`);
- continue;
- }
-
- user.balance = await g(k);
-
- const payments = await db.lRange(`${uid}:payments`, 0, -1);
-
- let total = 0;
- for (const pid of payments) {
- const p = await g(`payment:${pid}`);
- if (!p) continue;
- total += p.amount;
- if (p.amount < 0)
- total -= (p.fee || 0) + (p.ourfee || 0) + (p.tip || 0);
- else total += p.tip || 0;
- }
-
- user.expected = total;
- users.push(user);
- }
-
- res.send(users);
- },
-
async get(req, res) {
let {
params: { key },
@@ -115,8 +185,7 @@ export default {
const user = await getNostrUser(key);
res.send(pick(user, fields));
} catch (e) {
- err("problem getting user", key, e.message);
- res.code(500).send(e.message);
+ res.code(404).send("User not found");
}
},
@@ -127,7 +196,128 @@ export default {
if (!body.user) fail("no user object provided");
let { user } = body;
- const fields = ["pubkey", "password", "username", "picture", "fresh"];
+ // Per-IP registration cap. Nothing else throttles /register (the
+ // recaptcha check only runs on login; the fastify limiters cover
+ // /login and /send), so one VPN exit created ~500 empty accounts at
+ // 3/min on 2026-09-08 without tripping anything. Five an hour is far
+ // above any household; an internal/private source IP (proxy without
+ // the Cloudflare header) is exempt so a misrouted request can't lock
+ // everyone out. Counted before validation so failed attempts count too.
+ if (ip && !/^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|::1$|fc|fd)/.test(ip)) {
+ const k = `register:ip:${ip}`;
+ const n = await db.incr(k);
+ if (n === 1) await db.expire(k, 3600);
+ if (n > 5) {
+ warn("registration cap hit", ip, n);
+ fail("Too many accounts created from this address, try again later");
+ }
+
+ // Same idea one level up: a residential proxy pool hands out a fresh
+ // address per request, but they cluster in a few ISP ranges. All
+ // signups site-wide normally run ~25/hour, so 10/hour from one IPv4
+ // /16 or IPv6 /48 only bites a farm.
+ const range = ip.includes(":")
+ ? ip.split(":").slice(0, 3).join(":")
+ : ip.split(".").slice(0, 2).join(".");
+ const rk = `register:range:${range}`;
+ const rn = await db.incr(rk);
+ if (rn === 1) await db.expire(rk, 3600);
+ if (rn > 10) {
+ warn("registration range cap hit", ip, range, rn);
+ fail("Too many accounts created from this network, try again later");
+ }
+
+ // The farm came back spread over ~90 ranges an hour, each staying
+ // under the hourly cap, so also cap a range per day. Counted on
+ // attempts that got past the hourly caps.
+ const dk = `register:range:day:${range}`;
+ const dn = await db.incr(dk);
+ if (dn === 1) await db.expire(dk, 86400);
+ if (dn > 5) {
+ warn("registration daily range cap hit", ip, range, dn);
+ fail("Too many accounts created from this network, try again later");
+ }
+ }
+
+ // Forged browser User-Agent. Every engine that reports AppleWebKit also
+ // reports the "(KHTML, like Gecko)" token right behind it, so a UA that
+ // claims AppleWebKit without it was assembled by hand. Across 4.8M
+ // logged requests this matches 2,366: the c<epoch> registration bot
+ // (1,203 accounts from 596 rotating residential IPs — which is exactly
+ // why the per-IP cap above never touched it), PetalBot, Bytespider, and
+ // nothing else. Real browsers carry the token; non-browser clients that
+ // register legitimately (python-requests, curl, damus, our own Bun SSR)
+ // never claim AppleWebKit at all, so none of them match.
+ const ua = String(headers["user-agent"] || "");
+ if (ua.includes("AppleWebKit/") && !ua.includes("KHTML")) {
+ warn("registration blocked, forged user-agent", ip, ua.slice(0, 80));
+ fail("Registration unavailable");
+ }
+
+ // The signup form has always sent a recaptcha token and /register has
+ // always discarded it — it isn't even in the `fields` pick below — so
+ // the captcha only ever gated login. Verify it when one is supplied.
+ // Supplied-and-invalid is the only rejection: an ABSENT token still
+ // passes, because half of all registrations are API clients that never
+ // had one and requiring it would break them. verifyRecaptcha keeps its
+ // own bypasses (onion host, x-api-key, allowlisted UA) and fails open
+ // when Google is unreachable.
+ // Our own signup page (SSR, Bun UA) always sends a token on the
+ // clearnet; only the .onion page, which reaches us without a
+ // cf-connecting-ip, and the fund sweep (internal x-api-key, bypassed
+ // inside verifyRecaptcha) legitimately don't. So from the web UI an
+ // absent token is a scripted submit, not an API client.
+ if (!user.recaptcha && ip && ua.startsWith("Bun/")) {
+ if (!(await verifyRecaptcha(undefined, req))) {
+ warn("registration blocked, missing captcha", ip, user.username);
+ fail("Failed captcha");
+ }
+ }
+
+ if (
+ user.recaptcha &&
+ !(await verifyRecaptcha(user.recaptcha, req, {
+ actions: ["register"],
+ minScore: 0.5,
+ }))
+ ) {
+ warn("registration blocked, failed captcha", ip, user.username);
+ fail("Failed captcha");
+ }
+
+ if (ip && ua.startsWith("Bun/")) {
+ const t = await verifyTurnstile(user.turnstile, ip);
+ l(
+ "turnstile register",
+ ip,
+ user.username,
+ !user.turnstile
+ ? "missing"
+ : t
+ ? `${t.success ? "pass" : "fail"} ${t.hostname || ""} ${(t["error-codes"] || []).join(",")}`
+ : "unverified",
+ );
+ }
+
+ // Site-wide cap on signups that kept the auto-filled animal+NN name.
+ // The 2026-10 farm passes reCAPTCHA (0.9) and Cloudflare's challenge
+ // from thousands of residential IPs, but never changes the default
+ // name; real users keep it ~8 times a day. Hitting the cap only asks
+ // for a different name.
+ if (isDefaultName(user.username)) {
+ const k = `register:default:${Math.floor(Date.now() / 3600000)}`;
+ const n = await db.incr(k);
+ if (n === 1) await db.expire(k, 7200);
+ if (n > 10) {
+ warn("registration default-name cap hit", ip, user.username, n);
+ fail("Please choose your own username");
+ }
+ }
+
+ // No pubkey: nothing here proves the caller holds that key, and register()
+ // points `user:<pubkey>` at the new account. Nostr accounts come from
+ // /nostrAuth, which verifies a signed challenge first.
+ const fields = ["password", "username", "picture", "fresh"];
user = await register(pick(user, fields), ip);
const payload = { id: user.id };
@@ -135,7 +325,7 @@ export default {
l("registered new user", user.username);
- res.send({ ...pick(user, whitelist), sk: user.sk, token });
+ res.send({ ...pick(user, whitelist), nsec: user.nsec, sk: user.sk, token });
} catch (e) {
err("problem registering", e.message);
res.code(500).send(e.message);
@@ -152,9 +342,12 @@ export default {
return res.code(401).send("2fa required");
}
+ // Fresh seed on disable so a seed that leaked while 2FA was on is dead
+ // when it's re-enabled.
user.twofa = false;
+ user.otpsecret = authenticator.generateSecret();
await s(`user:${id}`, user);
- emit(username, "user", user);
+ emit(username, "user", pick(user, whitelist));
emit(username, "otpsecret", user.otpsecret);
l("disabled 2fa", username);
res.send({});
@@ -171,7 +364,7 @@ export default {
if (isValid) {
user.twofa = true;
await s(`user:${id}`, user);
- emit(username, "user", user);
+ emit(username, "user", pick(user, whitelist));
} else {
return res.code(500).send("Invalid token");
}
@@ -179,7 +372,6 @@ export default {
l("enabled 2fa", username);
res.send({});
} catch (e) {
- console.log(e);
bail(res, e.message);
}
},
@@ -199,32 +391,40 @@ export default {
const valid = /^[\p{L}\p{N}]{2,24}$/u;
if (!valid.test(username))
fail("Usernames can only have letters and numbers");
+ if (isReserved(username)) fail("Invalid username");
+ if (username?.includes("undefined")) fail("Invalid username");
let exists;
let { pubkey } = body;
if (pubkey) {
pubkey = pubkey.trim();
+ if (pubkey.startsWith("npub")) pubkey = nip19.decode(pubkey).data;
exists = await getUser(pubkey);
+ const un = user.username.toLowerCase().replace(/\s/g, "");
const existingUsername = exists?.username
?.toLowerCase()
.replace(/\s/g, "");
- if (exists && username !== existingUsername) {
- warn("key in use", pubkey, existingUsername);
+ if (exists && un !== existingUsername) {
+ warn("key in use", pubkey, username, existingUsername);
if (exists.anon) await db.del(`user:${pubkey}`);
else fail("Key in use by another account");
}
const event = JSON.parse(body.event);
const challenge = event.tags.find((t) => t[0] === "challenge")[1];
- const c = await g(`challenge:${challenge}`);
+ // Single-use: GETDEL claims the challenge atomically, so the identical
+ // signed event can't be replayed within the 5-minute TTL (it is also
+ // written to the request log, so a log reader could otherwise reuse it).
+ // Burn it on read rather than on success — a failed attempt costs the
+ // client one extra GET /challenge, and two racing replays cannot both win.
+ const c = await db.getDel(`challenge:${challenge}`);
if (!c) fail("Invalid or expired challenge");
if (!verifyEvent(event) || event.pubkey !== pubkey)
fail("Invalid signature or challenge mismatch.");
pubkey = pubkey.replace(/\s*/g, "");
- if (pubkey.startsWith("npub")) pubkey = nip19.decode(pubkey).data;
if (pubkey.length !== 64) fail(`Invalid pubkey ${pubkey}`);
await db.del(`user:${user.pubkey}`);
user.pubkey = pubkey;
@@ -237,18 +437,20 @@ export default {
if (user.pin === "delete") user.pin = undefined;
if (username) {
- exists = await db.exists(`user:${username}`);
const currentUsername = user.username.replace(/\s/g, "").toLowerCase();
+ if (username !== currentUsername) {
+ exists = await db.exists(`user:${username}`);
- if (currentUsername !== username && exists) {
- err("username taken", username, currentUsername);
- fail("Username taken");
- } else if (username) {
- if (currentUsername !== username)
+ if (exists) {
+ err("username taken", username, currentUsername);
+ fail("Username taken");
+ } else {
+ // A name the v3 registrar holds is taken too (see lib/names.ts).
+ await assertNameFree(username, user.pubkey);
l("changing username", currentUsername, username);
-
- await db.del(`user:${currentUsername}`);
- user.username = username;
+ await db.del(`user:${currentUsername}`);
+ user.username = username;
+ }
}
}
@@ -265,6 +467,7 @@ export default {
"language",
"locktime",
"memoPrompt",
+ "migrated",
"nip5",
"notify",
"nsec",
@@ -281,23 +484,28 @@ export default {
"twofa",
];
+ if (body.email) {
+ user.verified = false;
+ user.notify = false;
+ }
+
for (const a of attributes) {
if (typeof body[a] !== "undefined") user[a] = body[a];
}
user.fresh = false;
- user.tip = Math.max(0, Math.min(1000, parseInt(user.tip)));
+ user.tip = Math.max(0, Math.min(1000, Number.parseInt(user.tip)));
if (password && password === confirm) {
user.password = await Bun.password.hash(password, {
algorithm: "bcrypt",
- cost: 4,
+ cost: 12,
});
}
user.haspin = !!user.pin;
if (user.destination) user.destination = user.destination.trim();
- await s(`user:${user.pubkey}`, user.id);
+ if (user.pubkey) await s(`user:${user.pubkey}`, user.id);
await s(
`user:${user.username.toLowerCase().replace(/\s/g, "")}`,
user.id,
@@ -306,8 +514,8 @@ export default {
await s(`user:${user.id}`, user);
if (user.nip5) await db.sAdd("nip5", `${user.username}:${user.pubkey}`);
- emit(user.id, "user", user);
- res.send({ user });
+ emit(user.id, "user", pick(user, whitelist));
+ res.send({ user: pick(user, whitelist) });
} catch (e) {
warn("failed to update", user.username, e.message);
bail(res, e.message);
@@ -316,22 +524,61 @@ export default {
async login(req, res) {
try {
- let { username, password, token: twofa } = req.body;
+ let { username, password, token: twofa, recaptcha } = req.body;
+ const ip = req.headers["cf-connecting-ip"] || req.socket.remoteAddress;
+
+ const ipKey = `ip:${ip}:login`;
+ const ipCount = await db.incr(ipKey);
+ if (ipCount === 1) await db.expire(ipKey, 10);
+ if (ipCount > 30) return res.code(429).send({});
+
+ // Fail closed: an unset adminpass must never authenticate, and an omitted
+ // password field must never coincide with an unset value (undefined ===
+ // undefined). Both the configured value and the supplied one must be
+ // non-empty and match exactly.
+ const isAdmin =
+ !!config?.adminpass && !!password && password === config.adminpass;
+
+ if (!isAdmin) {
+ const recaptchaOk = await verifyRecaptcha(recaptcha, req, {
+ actions: ["login", "register"],
+ minScore: 0.3,
+ });
+ if (!recaptchaOk) {
+ return res.code(401).send("failed captcha");
+ }
+ }
username = username.toLowerCase().replace(/\s/g, "");
+ username = username.split("@")[0];
+
+ const ipFailKey = `ip:${ip}:login:fail`;
+ const ipFailures = await g(ipFailKey);
+ if (!isAdmin && ipFailures > 20) return res.code(429).send({});
+
let user = await getUser(username);
- if (password !== config?.adminpass) {
- if (
- !user ||
- !user.password ||
- !(await Bun.password.verify(password, user.password))
- ) {
- // warn("invalid username or password attempt", username);
- await appendFile(
- "failedlogins.txt",
- `${Date.now()} : ${username} : ${password}\n`,
- );
+ if (!isAdmin) {
+ // changeid leaves the literal "reset" in place of a hash, so no
+ // password can ever match. Say so, letting the client point the
+ // owner at support instead of a generic login failure. This runs
+ // after the captcha and the per-IP failure cap, and counts as a
+ // failure itself.
+ if (user?.password === "reset") {
+ await db.incrBy(ipFailKey, 1);
+ if ((await db.ttl(ipFailKey)) < 0) await db.expire(ipFailKey, 600);
+ return res.code(401).send("account reset");
+ }
+
+ let verified = false;
+ try {
+ if (user?.password)
+ verified = await Bun.password.verify(password, user.password);
+ } catch (e) {}
+
+ if (!user || !verified) {
+ await db.incrBy(ipFailKey, 1);
+ if ((await db.ttl(ipFailKey)) < 0) await db.expire(ipFailKey, 600);
return res.code(401).send({});
}
@@ -340,17 +587,49 @@ export default {
(typeof twofa === "undefined" ||
!authenticator.check(twofa, user.otpsecret))
) {
+ await db.incrBy(ipFailKey, 1);
+ if ((await db.ttl(ipFailKey)) < 0) await db.expire(ipFailKey, 600);
return res.code(401).send("2fa required");
}
+
+ // Transparent bcrypt upgrade: legacy accounts were hashed at cost 4
+ // (~1,300 guesses/s). Now that we hold the verified plaintext, re-hash
+ // at the current cost so the stored hash strengthens on next login.
+ const cost = Number.parseInt(
+ user.password.match(/^\$2[aby]\$(\d{2})\$/)?.[1] ?? "0",
+ 10,
+ );
+ let changed = false;
+ if (cost < 12) {
+ user.password = await Bun.password.hash(password, {
+ algorithm: "bcrypt",
+ cost: 12,
+ });
+ changed = true;
+ }
+
+ if (await upgradeLegacyNsec(user, password)) changed = true;
+ if (changed) await s(`user:${user.id}`, user);
}
+ // Evicted accounts can't use a token anyway (see lib/auth.ts), so don't
+ // mint one — checked after the password so it can't be used to probe.
+ if (!isAdmin && (await db.sIsMember("evicted", user.id)))
+ return res.code(403).send("account disabled");
+
if (username !== "coinos")
l("logged in", username, req.headers["cf-connecting-ip"]);
const payload = { id: user.id };
const token = jwt.sign(payload, config.jwt);
- res.cookie("token", token, { expires: new Date(Date.now() + 432000000) });
- user = pick(user, whitelist);
+ res.cookie("token", token, {
+ expires: new Date(Date.now() + 432000000),
+ path: "/",
+ httpOnly: true,
+ secure: true,
+ sameSite: "lax",
+ });
+ user = { ...pick(user, whitelist), hasnsec: !!(user.nsec || user.cipher), nsec: user.nsec };
res.send({ user, token });
} catch (e) {
err("login error", e.message, req.socket.remoteAddress);
@@ -366,9 +645,18 @@ export default {
async nostrAuth(req, res) {
try {
- const { event, challenge, twofa } = req.body;
+ const { event, challenge, twofa, recaptcha } = req.body;
const ip = req.headers["cf-connecting-ip"];
- const c = await g(`challenge:${challenge}`);
+ const recaptchaOk = await verifyRecaptcha(recaptcha, req, {
+ actions: ["login", "register"],
+ minScore: 0.3,
+ });
+ if (!recaptchaOk) {
+ return res.code(401).send("failed captcha");
+ }
+ // Single-use — see the GETDEL note on the other challenge consumer. The
+ // captcha is checked above, so a captcha failure doesn't burn a challenge.
+ const c = await db.getDel(`challenge:${challenge}`);
const { pubkey: key, kind } = event;
if (kind !== 27235) fail("Invalid event");
if (!c) fail("Invalid or expired login challenge");
@@ -401,25 +689,21 @@ export default {
}
const { username } = user;
- if (
- await db.sIsMember(
- "compromised",
- username.replace(/\s/g, "").toLowerCase(),
- )
- ) {
- warn("compromised nostr auth attempt");
- fail("unauthorized");
- }
-
l("nostr login", username, ip);
const payload = { id: user.id };
const token = jwt.sign(payload, config.jwt);
- res.cookie("token", token, { expires: new Date(Date.now() + 432000000) });
- user = pick(user, whitelist);
+ res.cookie("token", token, {
+ expires: new Date(Date.now() + 432000000),
+ path: "/",
+ httpOnly: true,
+ secure: true,
+ sameSite: "lax",
+ });
+ user = { ...pick(user, whitelist), hasnsec: !!(user.nsec || user.cipher) };
res.send({ user, token });
} catch (e) {
- err("login error", e.message, req.socket.remoteAddress);
+ err("nostr login error", e.message, req.socket.remoteAddress);
res.code(401).send({});
}
},
@@ -441,7 +725,6 @@ export default {
await db.sAdd(`${id}:subscriptions`, JSON.stringify(subscription));
res.send(subscription);
} catch (e) {
- console.log(e);
bail(res, e.message);
}
},
@@ -453,7 +736,6 @@ export default {
await db.sRem(`${id}:subscriptions`, JSON.stringify(subscription));
res.send(subscription);
} catch (e) {
- console.log(e);
bail(res, e.message);
}
},
@@ -473,6 +755,42 @@ export default {
}
},
+ // The encrypted nostr key, for clients that need to reveal or sign with it
+ // after a session that didn't start with a password login. Re-checking the
+ // password keeps a bearer token alone from fetching it (see /me).
+ async nsec(req, res) {
+ const {
+ body: { password },
+ user,
+ } = req;
+ const ip = req.headers["cf-connecting-ip"] || req.socket.remoteAddress;
+ const ipFailKey = `ip:${ip}:login:fail`;
+
+ try {
+ if (!user.nsec && !user.cipher) fail("nsec not available");
+ if ((await g(ipFailKey)) > 20) return res.code(429).send({});
+
+ let verified = false;
+ try {
+ if (user.password && password)
+ verified = await Bun.password.verify(password, user.password);
+ } catch (e) {}
+
+ if (!verified) {
+ await db.incrBy(ipFailKey, 1);
+ if ((await db.ttl(ipFailKey)) < 0) await db.expire(ipFailKey, 600);
+ return res.code(401).send("invalid password");
+ }
+
+ if (await upgradeLegacyNsec(user, password)) await s(`user:${user.id}`, user);
+ if (!user.nsec) fail("nsec could not be decrypted");
+
+ res.send({ nsec: user.nsec });
+ } catch (e) {
+ bail(res, e.message);
+ }
+ },
+
async pin(req, res) {
const {
body: { pin },
@@ -484,6 +802,10 @@ export default {
async otpsecret(req, res) {
try {
await requirePin(req);
+ // Only needed to enrol. Once 2FA is on, the seed never leaves the
+ // server again — otherwise a stolen token on a PIN-less account is
+ // enough to mint valid codes.
+ if (req.user.twofa) fail("2FA is already enabled");
const { otpsecret, username } = req.user;
res.send({ secret: otpsecret, username });
} catch (e) {
@@ -506,7 +828,7 @@ export default {
for (const { ref } of (
await Promise.all(
- payments.reverse().map(async (id) => await g(`payment:${id}`)),
+ payments.reverse().map(async (id) => await gf(`payment:${id}`)),
)
).filter((p) => p && p.type === PaymentType.internal && p.ref)) {
if (ref === id) continue;
@@ -550,6 +872,7 @@ export default {
params: { username },
headers: { authorization },
} = req;
+ fail("Unauthorized");
username = username.toLowerCase();
if (!authorization?.includes(config.admin))
return res.code(401).send("unauthorized");
@@ -569,47 +892,100 @@ export default {
res.send({});
},
+ // Authenticated self-delete: wipes every key tied to the calling user via
+ // lib/deluser (the same sweep as scripts/delall.ts, offers and archive
+ // included). Gated by typing the username (works for password and nostr
+ // accounts alike) plus the PIN when one is set, and refuses while a balance
+ // remains so a user can't accidentally destroy funds.
+ async deleteSelf(req, res) {
+ try {
+ const { user } = req;
+ const { id, username } = user;
+
+ const confirm = (req.body?.confirm || "").toString().trim().toLowerCase();
+ if (confirm !== username.toLowerCase())
+ fail("Type your username to confirm account deletion");
+ await requirePin(req);
+
+ // Allow deletion through dust (often too small to withdraw); only block
+ // a meaningful balance so nobody destroys real funds by accident. Sum
+ // EVERY account (main + sub-accounts) — the sweep wipes each
+ // sub-account's balance:/pending: keys unconditionally, so a guard that
+ // only checked the main account let a user with funds parked in a
+ // sub-account pass and silently destroy them.
+ const aids = await db.lRange(`${id}:accounts`, 0, -1);
+ if (!aids.includes(id)) aids.push(id);
+ let total = 0;
+ for (const aid of aids) {
+ total += Number(await db.get(`balance:${aid}`)) || 0;
+ total += Number(await db.get(`pending:${aid}`)) || 0;
+ }
+ if (total > 10000)
+ fail("Withdraw your balance before deleting your account");
+
+ const { deleted, disabled } = await deleteUser(user);
+
+ l("user self-deleted", username, id, `${deleted} keys, ${disabled} offers`);
+ try {
+ res.clearCookie("token");
+ } catch {}
+ res.send({ deleted: true });
+ } catch (e) {
+ bail(res, e.message);
+ }
+ },
+
async reset(req, res) {
const {
body: { code, username, password },
user: u,
} = req;
- const admin = u?.admin;
- let id;
- let user;
+ try {
+ let id;
+ let user;
- if (admin) {
+ // Non-admins must not be able to tell this admin-only endpoint exists.
+ // Log the probe (so exploit-watch can flag it) but return a generic 404
+ // identical to an unregistered route — previously it returned "disabled",
+ // which confirmed the endpoint AND its admin-gating to attackers.
+ if (u?.username !== config.admin) {
+ err("password reset failed disabled", req.headers["cf-connecting-ip"]);
+ return res
+ .code(404)
+ .send({ message: "Route POST:/reset not found", error: "Not Found", statusCode: 404 });
+ }
id = await g(`user:${username.toLowerCase().replace(/\s/g, "")}`);
user = await g(`user:${id}`);
- } else {
- id = await g(`reset:${code}`);
- user = await g(`user:${id}`);
- }
- if (!user) fail("user not found");
+ if (!user) fail("user not found");
- warn(
- "password reset",
- user.username,
- code,
- req.headers["cf-connecting-ip"],
- );
+ warn(
+ "password reset",
+ user.username,
+ code,
+ req.headers["cf-connecting-ip"],
+ );
- try {
user.pin = null;
user.nsec = null;
+ user.cipher = null;
+ user.salt = null;
user.password = await Bun.password.hash(password, {
algorithm: "bcrypt",
- cost: 4,
+ cost: 12,
});
await s(`user:${id}`, user);
await db.del(`reset:${code}`);
res.send(pick(user, whitelist));
+
+ for await (const k of db.scanIterator({ MATCH: "ip:*:login:fail" })) {
+ await db.del(k);
+ }
} catch (e) {
- err("password reset failed", e.message);
+ err("password reset failed", e.message, req.headers["cf-connecting-ip"]);
bail(res, e.message);
}
},
@@ -685,7 +1061,7 @@ export default {
await s(`user:${id}`, user);
await s(`email:${email.toLowerCase()}`, id);
- res.send(pick(user, whitelist));
+ res.send(pick(user, fields));
} catch (e) {
bail(res, e.message);
}
@@ -716,28 +1092,13 @@ export default {
}
},
- async hidepay(req, res) {
- const {
- body: { username },
- } = req;
- const u = await getUser(username);
- u.hidepay = true;
- await s(`user:${u.id}`, u);
- res.send({});
- },
-
- async unlimit(req, res) {
- const {
- body: { username },
- } = req;
- const u = await getUser(username);
- u.unlimited = true;
- await s(`user:${u.id}`, u);
- res.send({});
- },
-
async account(req, res) {
const { id } = req.params;
+ const { id: uid } = req.user;
+
+ const pos = await db.lPos(`${uid}:accounts`, id);
+ if (pos == null) fail("account not found");
+
const account = await g(`account:${id}`);
if (account) account.balance = await g(`balance:${id}`);
res.send(account);
@@ -758,45 +1119,17 @@ export default {
res.send(accounts.reverse());
} catch (e) {
- console.log(e);
bail(res, e.message);
}
},
async createAccount(req, res) {
try {
- const { fingerprint, pubkey, name, seed, type } = req.body;
+ const { name, type } = req.body;
const { user } = req;
- const { id: uid } = user;
const id = v4();
- const account = { id, name, seed, type, uid, descriptors: [] };
-
- let node = rpc(config[type]);
-
- await node.createWallet({
- wallet_name: id,
- descriptors: true,
- disable_private_keys: true,
- load_on_startup: true,
- });
-
- node = rpc({ ...config[type], wallet: id });
-
- for (const i of [0, 1]) {
- const desc = `wpkh([${fingerprint}]${pubkey}/${i}/*)`;
- const { checksum } = await node.getDescriptorInfo(desc);
- account.descriptors.push({
- desc: `${desc}#${checksum}`,
- range: [0, 100],
- next_index: 0,
- timestamp: "now",
- internal: i === 1,
- active: true,
- });
- }
-
- await node.importDescriptors(account.descriptors);
+ const account = { id, name, type, uid: user.id };
await db
.multi()
@@ -806,10 +1139,6 @@ export default {
.lPush(`${user.id}:accounts`, id)
.exec();
- reconcile(account, true);
-
- await appendFile("/bitcoin.conf", `wallet=${id}\n`);
-
res.send(account);
} catch (e) {
bail(res, e.message);
@@ -835,17 +1164,10 @@ export default {
try {
const { id } = req.body;
const { id: uid } = req.user;
- const { type } = await g(`account:${id}`);
+ const account = await g(`account:${id}`);
const pos = await db.lPos(`${uid}:accounts`, id);
- if (!(type && pos != null)) fail("account not found");
-
- try {
- const node = rpc({ ...config[type], wallet: id });
- await node.unloadWallet(id);
- } catch (e) {
- warn("failed to unload wallet", id);
- }
+ if (!(account && pos != null)) fail("account not found");
await db
.multi()
@@ -857,39 +1179,44 @@ export default {
res.send({ ok: true });
} catch (e) {
- console.log(e);
bail(res, e.message);
}
},
- async printer(req, res) {
- const { ssid, key, username, password } = req.body;
- const dir = "./printer";
- const path = `${dir}/config.txt`;
-
+ async flash(req, res) {
+ // Build in a per-request scratch dir. A shared ./printer/config.txt and
+ // ./littlefs.img let two overlapping requests hand one user the other's
+ // wifi key and API token.
+ const dir = await mkdtemp(join(tmpdir(), "flash-"));
try {
- await unlink(path);
- await appendFile(path, `${ssid}\n`, "utf8");
- await appendFile(path, `${key}\n`, "utf8");
- await appendFile(path, `${username}\n`, "utf8");
- await appendFile(path, `${password}\n`, "utf8");
-
- const output = "./littlefs.img";
- await $`./mklittlefs -c ${dir} -p 256 -b 4096 -s 0x20000 ${output}`;
-
+ const { ssid, key, token } = req.body;
+ if (!ssid || !key || !token) fail("ssid, key and token required");
+ const cfg = `${ssid.trim()}\n${key.trim()}\n${token.trim()}\n`;
+ const src = join(dir, "fs");
+ const img = join(dir, "littlefs.img");
+ await mkdir(src);
+ await writeFile(join(src, "config.txt"), cfg, "utf8");
+ await $`./mklittlefs -c ${src} -p 256 -b 4096 -s 0x20000 ${img}`;
res.header("Content-Type", "application/octet-stream");
- res.header("Content-Disposition", "attachment; filename=littlefs.img");
- console.log("sending back");
- return res.send(createReadStream(output));
- } catch (error) {
- res.code(500).send({ error: "Failed to generate LittleFS image" });
+ // Read it fully before the finally below removes the directory.
+ return res.send(await readFile(img));
+ } catch (e) {
+ bail(res, e.message);
+ } finally {
+ await rm(dir, { recursive: true, force: true });
}
},
async app(req, res) {
const { pubkey } = req.params;
const { user } = req;
+ if (!/^[0-9a-f]{64}$/.test(pubkey))
+ return res.code(404).send({ error: "connection not found" });
const app = await g(`app:${pubkey}`);
+ // Missing records must 404, not crash into a 500: clients (Damus one-click
+ // setup) probe this endpoint and treat 404 as "not configured yet, create
+ // one" — anything else aborts their whole connection flow.
+ if (!app) return res.code(404).send({ error: "connection not found" });
if (app.uid !== user.id) fail("unauthorized");
const lud16 = `${user.username}@${host}`;
@@ -898,13 +1225,13 @@ export default {
const payments = await Promise.all(
pids.map(async (pid) => {
- const p = await g(`payment:${pid}`);
- p.user = await g(`user:${p.uid}`);
+ const p = await gf(`payment:${pid}`);
+ if (p) p.user = pick((await g(`user:${p.uid}`)) || {}, fields);
return p;
}),
);
- app.nwc = `nostr+walletconnect://${serverPubkey}?relay=${relay}&secret=${app.secret}&lud16=${lud16}`;
+ app.nwc = `nostr+walletconnect://${serverPubkey2}?relay=${relay}&secret=${app.secret}&lud16=${lud16}`;
app.payments = payments.filter((p) => p);
res.send(app);
@@ -913,26 +1240,31 @@ export default {
async apps(req, res) {
const { user } = req;
const pubkeys = await db.sMembers(`${user.id}:apps`);
- const apps = await Promise.all(pubkeys.map((p) => g(`app:${p}`)));
+ // Drop set members whose app record was deleted (e.g. an invalidation
+ // sweep) so one stale pubkey can't 500 the whole listing.
+ const apps = (await Promise.all(pubkeys.map((p) => g(`app:${p}`)))).filter(
+ Boolean,
+ );
const lud16 = `${user.username}@${host}`;
await Promise.all(
apps.map(async (a) => {
if (a.secret)
- a.nwc = `nostr+walletconnect://${serverPubkey}?relay=${relay}&secret=${a.secret}&lud16=${lud16}`;
+ a.nwc = `nostr+walletconnect://${serverPubkey2}?relay=${relay}&secret=${a.secret}&lud16=${lud16}`;
const pids = await db.lRange(`${a.pubkey}:payments`, 0, -1);
let payments = await Promise.all(
- pids.map((pid) => g(`payment:${pid}`)),
+ pids.map((pid) => gf(`payment:${pid}`)),
);
payments = payments.filter((p) => p);
a.spent = payments.reduce(
(a, b) =>
a +
- (Math.abs(parseInt(b.amount || 0)) +
- parseInt(b.fee || 0) +
- parseInt(b.ourfee || 0)),
+ (Math.abs(Number.parseInt(b.amount || 0)) +
+ Number.parseInt(b.tip || 0) +
+ Number.parseInt(b.fee || 0) +
+ Number.parseInt(b.ourfee || 0)),
0,
);
}),
@@ -955,10 +1287,64 @@ export default {
const { user } = req;
const uid = user.id;
- let app = await g(pubkey);
+ if (secret) pubkey = getPublicKey(hexToBytes(secret));
+
+ // The pubkey names `app:<pubkey>` and `<pubkey>:payments`, so it must be
+ // a real NWC key: a user id here would hand the caller that user's
+ // payment list through GET /app.
+ if (!/^[0-9a-f]{64}$/.test(pubkey)) fail("Invalid pubkey");
+ // App records live under `app:<pubkey>`. Looking up the bare pubkey made
+ // this ownership check ineffective for normal NWC keys and allowed an
+ // authenticated user to overwrite another connection's configuration.
+ let app = await g(`app:${pubkey}`);
if (app && uid !== app.uid) fail("Unauthorized");
- if (secret) pubkey = getPublicKey(secret);
+
+ // Rotation is mandatory after a credential disclosure: the NWC pubkey is
+ // derived from the bearer secret, so re-registering a retired or
+ // quarantined pubkey would re-authorize the exact secret that was
+ // disclosed (we store client-supplied secrets, so they're part of any DB
+ // leak). Reject it and make the client mint a fresh secret — a new secret
+ // yields a new pubkey and never trips this check.
+ const credentialCutoff = Number(await g("nwc:credential-cutoff")) || 0;
+ const retired =
+ (app &&
+ credentialCutoff > 0 &&
+ (!Number(app.created) || Number(app.created) < credentialCutoff)) ||
+ (await db.sIsMember("nwc:quarantined", pubkey));
+ if (retired)
+ return res.code(403).send({
+ error:
+ "This connection's key was retired for security reasons; connect again with a newly generated secret",
+ });
+
+ const validRenewals = new Set([
+ "daily",
+ "weekly",
+ "monthly",
+ "yearly",
+ "never",
+ ]);
+ if (budget_renewal && !validRenewals.has(budget_renewal))
+ fail("Invalid budget renewal period");
+
+ for (const [label, value] of [
+ ["spending budget", max_amount],
+ ["maximum fee", max_fee],
+ ]) {
+ if (
+ value !== undefined &&
+ value !== null &&
+ value !== "" &&
+ (!Number.isFinite(Number(value)) || Number(value) < 0)
+ )
+ fail(`Invalid ${label}`);
+ }
+
+ // Notifications are opt-in (default off): most users don't consume them and
+ // publishing payment_sent/payment_received for every app would flood the
+ // relay. A client enables them by sending notify:"true" when creating the
+ // connection. (Reverted a brief default-on change, 2026-06-06.)
notify = String(notify) === "true";
app = {
@@ -973,14 +1359,16 @@ export default {
secret,
};
- if (!app?.created) app.created = Date.now();
+ // Refresh the creation epoch on every owner post: this record passed the
+ // retirement check above, so the owner updating it (or minting it fresh)
+ // asserts it's live and keeps it clear of the credential cutoff.
+ app.created = Date.now();
await s(`app:${pubkey}`, app);
await db.sAdd(`${uid}:apps`, pubkey);
res.send({});
} catch (e) {
- console.log(e);
bail(res, e.message);
}
},
@@ -990,13 +1378,15 @@ export default {
const { user } = req;
const uid = user.id;
const { pubkey } = req.body;
- const app = await g(pubkey);
- if (app && uid !== app.uid) fail("Unauthorized");
+ const app = await g(`app:${pubkey}`);
+ if (app && uid !== app.uid) {
+ warn(app.uid, uid);
+ fail("Unauthorized");
+ }
await db.sRem(`${uid}:apps`, pubkey);
await db.del(`app:${pubkey}`);
res.send({});
} catch (e) {
- console.log(e);
bail(res, e.message);
}
},
@@ -1041,4 +1431,11 @@ export default {
const liquid = await g(`credit:liquid:${id}`);
res.send({ bitcoin, lightning, liquid });
},
+
+ async ro(req, res) {
+ const { user } = req;
+ const payload = { id: `${user.id}-ro` };
+ const token = jwt.sign(payload, config.jwt);
+ res.send(token);
+ },
};