feat(dashboard): unify secure staff administration

Provide a single /dashboard entry point for owner and staff tools, with Discord OAuth sign-in for approved owner accounts and active staff. Add owner-managed dashboard access grants, staff server health and update visibility, filtering, moderation improvements, item controls, and per-animal clothing restrictions.

Replace staff-triggered password and 2FA resets with email-verified account recovery. Persist validated recovery addresses, use short-lived hashed recovery tokens, expose recovery flows in registration and account pages, and keep mail delivery disabled until its webhook configuration is present.

Cover dashboard aliases, recovery restrictions, Discord access, monitoring, item locks, wear rules, and staff behavior with regression tests.
This commit is contained in:
Snipzil committed 2026-10-19 10:09:35 -04:00
1 parent 4997315239
commit 7dee29010a
27 files changed
+2303 -346

No files matched your search

+72 -1
View File
@@ -22,6 +22,7 @@ const masterpieces = require("./masterpieces.js");
const defpacks = require("./defpacks.js");
const support = require("./support.js");
const totp = require("./totp.js");
const recoveryMail = require("./recoverymail.js");
const world = require("./world.js");
// the account page at /account: what a player can do about their own
@@ -50,6 +51,8 @@ const discordCookie = (value, maxAgeSeconds) =>
const SESSION_HOURS = 12;
const TEN_MINUTES_MS = 600000;
const MAX_NOTE = 120;
const RECOVERY_TOKEN_MS = 30 * 60 * 1000;
const recoveryHash = token => crypto.createHash("sha256").update(token).digest("hex");
// wrong passwords before further tries from an address are refused for a
// while, as for the launcher's sign-in (lib/http.js).
@@ -59,6 +62,7 @@ const failuresByAddress = limits.limiter(config.failedLoginsPerTenMinutes, TEN_M
// at it and is then signed out.
const WRONG_PASSWORDS_ALLOWED = 5;
const wrongPasswords = limits.limiter(WRONG_PASSWORDS_ALLOWED, TEN_MINUTES_MS);
const recoveryRequests = limits.limiter(5, TEN_MINUTES_MS);
// 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
@@ -142,6 +146,7 @@ const bannedProfile = (account, ban) => {
appeal: appeal && {createdAt: appeal.createdAt, text: appeal.text, status: appeal.status, reply: appeal.reply, handledAt: appeal.handledAt},
appealLimit: bans.MAX_APPEAL,
twoStep: twoStepOf(account),
recoveryEmail: account.recoveryEmail ? `${account.recoveryEmail.slice(0, 2)}…${account.recoveryEmail.slice(account.recoveryEmail.lastIndexOf("@"))}` : "",
};
};
@@ -177,6 +182,7 @@ const profile = (account, from) => {
banned: null,
locked: account.locked,
twoStep: twoStepOf(account),
recoveryEmail: account.recoveryEmail ? `${account.recoveryEmail.slice(0, 2)}…${account.recoveryEmail.slice(account.recoveryEmail.lastIndexOf("@"))}` : "",
discord: account.discord,
discordLinked: Boolean(account.discordId),
discordLinking: discord.linking(),
@@ -204,7 +210,7 @@ const profile = (account, from) => {
// hash, the 2FA secret, device passes), and what other tables hold of it.
// what others wrote about it (reports, a moderator's notes) is not theirs
// to be handed, and is not here.
const SECRET_FIELDS = ["salt", "hash", "totp", "totpSetup", "devices", "modNotes"];
const SECRET_FIELDS = ["salt", "hash", "totp", "totpSetup", "devices", "modNotes", "recoveryTokens"];
const dataOf = account => ({
exportedAt: new Date().toISOString(),
server: config.baseUrl,
@@ -524,6 +530,10 @@ const handle = async (req, res, url, from, web) => {
web.send(res, 200, captcha.withScriptVersion(await fs.promises.readFile(path.join(config.publicDir, "account.html"))), "text/html; charset=utf-8");
return;
}
if (route === "GET /account/recover") {
web.send(res, 200, captcha.withScriptVersion(await fs.promises.readFile(path.join(config.publicDir, "recover.html"))), "text/html; charset=utf-8");
return;
}
if (req.method === "POST" && !sameOrigin(req)) {
reply(403, {error: "forbidden"});
return;
@@ -531,6 +541,67 @@ const handle = async (req, res, url, from, web) => {
const body = req.method === "POST" ? await web.readJson(req) : {};
switch (route) {
case "POST /account/api/recover/request": {
// Always give the same reply: neither account names nor email addresses
// can be discovered from this endpoint.
if (!from.local && !recoveryRequests.allow(from.key)) {
reply(429, {ok: "If the account and recovery email match, we sent a recovery link."});
return;
}
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()) {
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});
accounts.save(account);
const link = new URL(`/account/recover?token=${token}`, config.siteUrl).href;
const sent = await recoveryMail.send({to: account.recoveryEmail, subject: "Burrow account recovery",
text: `A ${kind === "twoStep" ? "2FA reset" : "password reset"} was requested for ${account.username}. Open this link within 30 minutes:\n${link}\n\nIf this was not you, ignore this email.`});
if (!sent) {
account.recoveryTokens = account.recoveryTokens.filter(entry => entry.hash !== recoveryHash(token));
accounts.save(account);
}
}
reply(200, {ok: "If the account and recovery email match, we sent a recovery link."});
return;
}
case "POST /account/api/recover/check": {
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."});
return;
}
case "POST /account/api/recover/complete": {
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());
if (!entry) {
reply(400, {error: "This recovery link is invalid or expired."});
return;
}
if (entry.kind === "password") {
const problem = accounts.setPassword(account, body.password);
if (problem) {
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;
}
}
else if (totp.isOn(account)) {
totp.turnOff(account);
accounts.revokeTokens(account);
}
account.recoveryTokens = [];
accounts.save(account);
signOutOthers(account, null);
endStaffSessions(account);
world.moderation.actions.kick(account);
console.log(`[recovery] ${logText(entry.kind)} reset for "${logText(account.username)}" after email verification from ${logText(from.address)}`);
reply(200, {ok: entry.kind === "password" ? "Password reset. You can sign in now." : "2FA reset. You can sign in now."});
return;
}
case "POST /account/api/login": {
const key = String(body.username || "").trim().toLowerCase();
if (!from.local && failuresByAddress.blocked(from.key)) {