From 0da5daabc9037f1cfabbc8d4cf3da06fcb2ffafc Mon Sep 17 00:00:00 2001 From: Snipzil Date: Wed, 21 Oct 2026 22:54:26 -0400 Subject: [PATCH] fix(account): removing 2FA by email also needs the password, and never for staff A recovery link (7dee290) removed 2FA on the email alone, and a password reset is email alone too, so a player's mailbox was their account, 2FA or not. For staff, 2FA removed and turned on again with another app made whoever had the mailbox active staff. - a 2FA reset by email also takes the account's current password, and is refused for 7 days after a password reset by email (emailPasswordResetAt), so the two cannot be chained. - never for an account with a staff role: they ask the admin, whose temporary password can turn 2FA off. - at most three recovery emails an hour for one account, from any address. The address is still never confirmed, nor shown or changed on the account page. Co-Authored-By: Claude Opus 5.5 --- lib/manage.js | 45 ++++++++++++++++++++++++++++++++++++++++----- public/recover.html | 4 ++-- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lib/manage.js b/lib/manage.js index 6f38084..130df36 100644 --- a/lib/manage.js +++ b/lib/manage.js @@ -25,6 +25,7 @@ const totp = require("./totp.js"); const recoveryMail = require("./recoverymail.js"); const world = require("./world.js"); const chatStyles = require("./chatstyles.js"); +const roles = require("./roles.js"); // the account page at /account: what a player can do about their own // account from a browser. they sign in with the game's username and @@ -64,6 +65,20 @@ const failuresByAddress = limits.limiter(config.failedLoginsPerTenMinutes, TEN_M const WRONG_PASSWORDS_ALLOWED = 5; const wrongPasswords = limits.limiter(WRONG_PASSWORDS_ALLOWED, TEN_MINUTES_MS); const recoveryRequests = limits.limiter(5, TEN_MINUTES_MS); +// recovery mail sent for one account, from any address: a few an hour. +const recoveryMailsPerAccount = limits.limiter(3, 3600000); +// a 2FA reset by email waits this long after a password reset by email, so +// that someone who has only the mailbox cannot do one after the other. +const EMAIL_RESET_GAP_MS = 7 * 24 * 3600000; +// why a 2FA reset by email cannot be done for this account now, or null. +// it also needs the account's password (recover/complete). staff ask the +// admin (lib/panel.js), whose 2FA reset ends their role's use until they +// turn it on again: a mailbox must not make someone staff. +const twoStepResetProblem = account => roles.roleOf(account) + ? "Staff accounts cannot remove 2FA by email. Ask the admin." + : account.emailPasswordResetAt && Date.now() - account.emailPasswordResetAt < EMAIL_RESET_GAP_MS + ? "2FA cannot be removed by email within 7 days of a password reset by email." + : null; // what may be done from the page of a ban (above). const BANNED_MAY = ["password", "twoStepStart", "twoStepConfirm", "twoStepOff", "forgetDevices", "delete", "appeal"]; // and from the page of a rejected username: the same, with a new name in @@ -564,7 +579,8 @@ const handle = async (req, res, url, from, web) => { const account = accounts.byName(String(body.username || "")); const email = String(body.email || "").trim().toLowerCase(); const kind = body.kind === "twoStep" ? "twoStep" : "password"; - if (account && account.recoveryEmail && account.recoveryEmail === email && recoveryMail.available()) { + if (account && account.recoveryEmail && account.recoveryEmail === email && recoveryMail.available() + && recoveryMailsPerAccount.allow(account.id)) { const token = crypto.randomBytes(32).toString("hex"); account.recoveryTokens = (account.recoveryTokens || []).filter(entry => entry.expires > Date.now()); account.recoveryTokens.push({hash: recoveryHash(token), kind, expires: Date.now() + RECOVERY_TOKEN_MS}); @@ -584,7 +600,8 @@ const handle = async (req, res, url, from, web) => { const token = String(body.token || ""); const account = [...accounts.all()].find(candidate => (candidate.recoveryTokens || []).some(entry => entry.hash === recoveryHash(token) && entry.expires > Date.now())); const entry = account && account.recoveryTokens.find(candidate => candidate.hash === recoveryHash(token) && candidate.expires > Date.now()); - reply(entry ? 200 : 400, entry ? {kind: entry.kind} : {error: "This recovery link is invalid or expired."}); + const refused = entry && entry.kind === "twoStep" ? twoStepResetProblem(account) : null; + reply(entry && !refused ? 200 : 400, entry && !refused ? {kind: entry.kind} : {error: refused || "This recovery link is invalid or expired."}); return; } case "POST /account/api/recover/complete": { @@ -601,10 +618,28 @@ const handle = async (req, res, url, from, web) => { reply(400, {error: problem === "weak_password" ? "That password is too easy to guess." : "Use 10+ characters with an uppercase letter, lowercase letter and digit."}); return; } + account.emailPasswordResetAt = Date.now(); } - else if (totp.isOn(account)) { - totp.turnOff(account); - accounts.revokeTokens(account); + else { + const refused = twoStepResetProblem(account); + if (refused) { + reply(400, {error: refused}); + return; + } + // the mailbox and the password: the link alone does not remove 2FA. + if (!from.local && failuresByAddress.blocked(from.key)) { + reply(429, {error: "Too many attempts. Try later."}); + return; + } + if (await accounts.authenticateAsync(account.username, body.password) !== account) { + failuresByAddress.record(from.key); + reply(401, {error: "Wrong password."}); + return; + } + if (totp.isOn(account)) { + totp.turnOff(account); + accounts.revokeTokens(account); + } } account.recoveryTokens = []; accounts.save(account); diff --git a/public/recover.html b/public/recover.html index 4804298..73c9e07 100644 --- a/public/recover.html +++ b/public/recover.html @@ -4,5 +4,5 @@

Account recovery

We’ll send a one-time recovery link to the email saved on your account.

-

Back to sign in

- +

Back to sign in

+