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 <[email protected]>
This commit is contained in:
SnipzilandClaude Opus 5.5 committed 2026-10-21 22:54:26 -04:00
1 parent 3b53c8dab8
commit 0da5daabc9
2 files changed
+42 -7

No files matched your search

+40 -5
View File
@@ -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);
+2 -2
View File
@@ -4,5 +4,5 @@
<style>@font-face{font-family:d;src:url('/fonts/CCDigitalDelivery-Regular.otf')}*{box-sizing:border-box}body{margin:0;padding:48px 16px;background:#000;color:#fff;font:16px/1.45 d,system-ui}main{max-width:420px;margin:auto;padding:28px;background:#0d0d0d;border:1px solid #2e2e2e;border-radius:18px}.logo{height:62px}h1{font-size:25px;margin:10px 0}p,label{color:#aaa}label{display:block;margin:14px 0 5px}input,select,button{width:100%;font:inherit;color:#fff;background:#000;border:1px solid #444;border-radius:12px;padding:10px 12px}button{margin-top:20px;background:#fff;color:#000;font-weight:bold;cursor:pointer}.status{min-height:24px;margin:14px 0 0;color:#aaa}a{color:#fff}</style></head>
<body><main><img class="logo" src="/logo.png" alt=""><h1>Account recovery</h1>
<div id="request"><p>We’ll send a one-time recovery link to the email saved on your account.</p><form id="request-form"><label>Username<input id="username" autocomplete="username" required></label><label>Recovery email<input id="email" type="email" autocomplete="email" required></label><label>What do you need to recover?<select id="kind"><option value="password">Password</option><option value="twoStep">2FA</option></select></label><button>Send recovery link</button></form></div>
<div id="complete" hidden><p id="intro"></p><form id="complete-form"><label id="password-label">New password<input id="password" type="password" autocomplete="new-password" minlength="10"></label><button>Complete recovery</button></form></div><p class="status" id="status" role="status"></p><p><a href="/account">Back to sign in</a></p></main>
<script>"use strict";const $=id=>document.getElementById(id),token=new URLSearchParams(location.search).get('token');const post=async(path,body)=>{const r=await fetch(path,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});return {ok:r.ok,data:await r.json()}};const status=(text)=>$('status').textContent=text;if(token){post('/account/api/recover/check',{token}).then(r=>{if(!r.ok){status(r.data.error);return}$('request').hidden=true;$('complete').hidden=false;const two=r.data.kind==='twoStep';$('intro').textContent=two?'Confirm that you want to remove 2FA from this account.':'Choose a new password.';$('password-label').hidden=two;$('password').required=!two}).catch(()=>status('The server did not answer.'))}$('request-form').addEventListener('submit',async e=>{e.preventDefault();const r=await post('/account/api/recover/request',{username:$('username').value,email:$('email').value,kind:$('kind').value});status(r.data.ok||'Could not start recovery.')});$('complete-form').addEventListener('submit',async e=>{e.preventDefault();const r=await post('/account/api/recover/complete',{token,password:$('password').value});status(r.data.ok||r.data.error||'Could not complete recovery.')});</script></body></html>
<div id="complete" hidden><p id="intro"></p><form id="complete-form"><label id="password-label"><span id="password-text">New password</span><input id="password" type="password" autocomplete="new-password" minlength="10" required></label><button>Complete recovery</button></form></div><p class="status" id="status" role="status"></p><p><a href="/account">Back to sign in</a></p></main>
<script>"use strict";const $=id=>document.getElementById(id),token=new URLSearchParams(location.search).get('token');const post=async(path,body)=>{const r=await fetch(path,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});return {ok:r.ok,data:await r.json()}};const status=(text)=>$('status').textContent=text;if(token){post('/account/api/recover/check',{token}).then(r=>{if(!r.ok){status(r.data.error);return}$('request').hidden=true;$('complete').hidden=false;const two=r.data.kind==='twoStep';$('intro').textContent=two?'Enter your password to remove 2FA from this account.':'Choose a new password.';$('password-text').textContent=two?'Your password':'New password';$('password').autocomplete=two?'current-password':'new-password';$('password').minLength=two?1:10}).catch(()=>status('The server did not answer.'))}$('request-form').addEventListener('submit',async e=>{e.preventDefault();const r=await post('/account/api/recover/request',{username:$('username').value,email:$('email').value,kind:$('kind').value});status(r.data.ok||'Could not start recovery.')});$('complete-form').addEventListener('submit',async e=>{e.preventDefault();const r=await post('/account/api/recover/complete',{token,password:$('password').value});status(r.data.ok||r.data.error||'Could not complete recovery.')});</script></body></html>