Files
burrow-server/lib/manage.js
T
Burrowdev 80383e8dec feat: chat filters cover bios
The server refuses a bio that breaks its filter and hides one saved before.
The client filter now runs on your own card too.
2026-10-17 14:35:29 +03:00

605 lines
25 KiB
JavaScript

"use strict";
const crypto = require("crypto");
const fs = require("fs");
const path = require("path");
const accounts = require("./accounts.js");
const config = require("../config.js");
const limits = require("./limits.js");
const achievements = require("./achievements.js");
const badges = require("./badges.js");
const bans = require("./bans.js");
const captcha = require("./captcha.js");
const chatlog = require("./chatlog.js");
const chatFilter = require("./chatfilter.js");
const erase = require("./erase.js");
const masterpieces = require("./masterpieces.js");
const defpacks = require("./defpacks.js");
const support = require("./support.js");
const totp = require("./totp.js");
const world = require("./world.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
// password; that sets a cookie only this page's requests send back
// (HttpOnly, SameSite=Strict, Path=/account). sessions live in memory, so
// restarting the server signs everyone out of the page (not out of the game).
//
// A locked account can still sign in here: this is where it is unlocked.
//
// A banned account, or anyone signing in from a banned address, gets another
// page altogether: what the ban is and why, one appeal, and the way out
// (signing out, or deleting the account). the password and two-step sign-in
// stay within reach, in case the account is in other hands. nothing else of
// the account is sent or may be changed until the ban is over.
const COOKIE = "ajc_account";
// A device that gave its two-step code keeps this for 30 days (lib/totp.js).
const DEVICE_COOKIE = "ajc_device";
const SESSION_HOURS = 12;
const TEN_MINUTES_MS = 600000;
const MAX_NOTE = 120;
// wrong passwords before further tries from an address are refused for a
// while, as for the launcher's sign-in (lib/http.js).
const failuresByAddress = limits.limiter(config.failedLoginsPerTenMinutes, TEN_MINUTES_MS);
// the password is asked for again before the changes that matter. A page
// left open, or one that fell into other hands, gets this many wrong tries
// at it and is then signed out.
const WRONG_PASSWORDS_ALLOWED = 5;
const wrongPasswords = limits.limiter(WRONG_PASSWORDS_ALLOWED, 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
// place of an appeal.
const RENAMING_MAY = ["password", "twoStepStart", "twoStepConfirm", "twoStepOff", "forgetDevices", "delete", "newName"];
// things that write to the database, per account.
const writesPerTenMinutes = limits.limiter(60, TEN_MINUTES_MS);
const sessions = new Map(); // SHA-256 of a session token -> {id: account id, expiry: ms}
const sha256 = text => crypto.createHash("sha256").update(text).digest("hex");
const cookieToken = req => {
const match = new RegExp(`(?:^|;\\s*)${COOKIE}=([0-9a-f]{48})(?:;|$)`).exec(req.headers.cookie || "");
return match ? match[1] : null;
};
const cookieHeader = (token, maxAgeSeconds, name = COOKIE) =>
`${name}=${token}; Path=/account; HttpOnly; SameSite=Strict; Max-Age=${maxAgeSeconds}${config.secureCookies ? "; Secure" : ""}`;
const deviceCookie = req => {
const match = new RegExp(`(?:^|;\\s*)${DEVICE_COOKIE}=([0-9a-f]{48})(?:;|$)`).exec(req.headers.cookie || "");
return match ? match[1] : null;
};
const signedInAs = req => {
const token = cookieToken(req);
const session = token && sessions.get(sha256(token));
return session && session.expiry > Date.now() ? accounts.byId(session.id) : null;
};
const signIn = (res, account, otherCookies = []) => {
for (const [hash, session] of sessions) {
if (session.expiry < Date.now()) {
sessions.delete(hash);
}
}
const token = crypto.randomBytes(24).toString("hex");
sessions.set(sha256(token), {id: account.id, expiry: Date.now() + SESSION_HOURS * 3600000});
res.setHeader("Set-Cookie", [cookieHeader(token, SESSION_HOURS * 3600), ...otherCookies]);
};
// ends the account's sessions on this page, all but the one making `req`.
const signOutOthers = (account, req) => {
const keep = req && cookieToken(req) ? sha256(cookieToken(req)) : null;
for (const [hash, session] of sessions) {
if (session.id === account.id && hash !== keep) {
sessions.delete(hash);
}
}
};
const twoStepOf = account => ({on: totp.isOn(account), since: account.totp ? account.totp.since : null,
recoveryLeft: account.totp ? account.totp.recovery.length : 0, devices: totp.freshDevices(account).length, deviceDays: totp.DEVICE_DAYS});
// the ban in the way of this account from where it is asked about: its own,
// or else the one on that address. {kind, permanent, seconds, reason,
// since, addressKey}, or null.
const banFor = (account, from) => {
const own = world.moderation.banOf(account);
if (own) {
return {kind: "account", permanent: own.seconds === world.moderation.PERMANENT, seconds: own.seconds, reason: own.reason, since: own.since, addressKey: ""};
}
const address = bans.addressBan(from.address);
return address ? {kind: "address", permanent: true, seconds: world.moderation.PERMANENT, reason: address.reason, since: address.createdAt, addressKey: address.key} : null;
};
// all the page of a ban is given.
const bannedProfile = (account, ban) => {
const appeal = bans.appealOf(account);
return {
username: account.username,
banned: {kind: ban.kind, permanent: ban.permanent, seconds: ban.seconds, reason: ban.reason, since: ban.since},
appeal: appeal && {createdAt: appeal.createdAt, text: appeal.text, status: appeal.status, reply: appeal.reply, handledAt: appeal.handledAt},
appealLimit: bans.MAX_APPEAL,
twoStep: twoStepOf(account),
};
};
const profile = (account, from) => {
const ban = banFor(account, from);
if (ban) {
return bannedProfile(account, ban);
}
// A rejected username (lib/moderation.js): a new one before anything else.
if (account.nameRejected) {
return {username: account.username, banned: null, nameRejected: true, twoStep: twoStepOf(account)};
}
const avatar = account.avatars.find(candidate => candidate.perUserAvId === account.activeAvatar) || account.avatars[0];
return {
username: account.username,
createdAt: account.createdAt,
lastSeen: account.lastSeen || account.lastLogin || null,
online: Boolean(world.findOnline(account.username)),
logins: account.numLogins || 0,
playtime: account.playtime,
playtimePublic: account.playtimePublic,
animals: account.avatars.length,
animalType: avatar ? avatar.type : null,
items: account.items.length,
denItems: account.denItems.length,
pets: account.pets.length,
buddies: account.buddies.length,
banned: null,
locked: account.locked,
twoStep: twoStepOf(account),
discord: account.discord,
bio: account.bio,
bioLimit: badges.MAX_BIO,
badges: badges.of(account).map(badge => badge.label),
features: Object.fromEntries(accounts.FEATURES.map(name => [name, accounts.featureOn(account, name)])),
history: account.logins,
denVisits: account.denVisits,
trades: account.tradeHistory,
buddyList: account.buddies.map(key => {
const buddy = accounts.byName(key);
return {name: buddy ? buddy.username : key, online: Boolean(buddy && world.findOnline(buddy.username)), note: account.buddyNotes[key] || ""};
}).sort((one, other) => one.name.localeCompare(other.name)),
blocked: account.blocked.map(key => (accounts.byName(key) || {username: key}).username).sort(),
achievements: achievements.progress(account),
offers: {incoming: world.tradeOffers.incoming(account), outgoing: world.tradeOffers.outgoing(account)},
nameRequests: support.nameRequestsOf(account),
feedback: support.feedbackOf(account),
};
};
// everything kept about the account, for its owner to take away: the
// account as it is stored, without what would let someone in (the password's
// 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"];
const dataOf = account => ({
exportedAt: new Date().toISOString(),
server: config.baseUrl,
account: {...Object.fromEntries(Object.entries(account).filter(([field]) => !SECRET_FIELDS.includes(field))), twoStepOn: totp.isOn(account)},
chat: chatlog.saidBy(account),
banRecord: bans.historyOf(account),
appeal: bans.appealOf(account),
usernameRequests: support.nameRequestsOf(account),
feedback: support.feedbackOf(account),
paintings: masterpieces.madeBy(account),
});
const NAME_ERRORS = {
invalid_username: "3 to 20 letters, digits, spaces, dots or dashes, with at least three letters.",
username_taken: "Username taken.",
name_not_allowed: "That name is not allowed.",
same_name: "That is already your username.",
};
const OFFER_ERRORS = {
unavailable: "You cannot trade with them.",
not_listed: "No longer on their trade list.",
nothing: "Pick something to give.",
not_yours: "You no longer have one of those.",
too_many: "Too many offers waiting.",
};
const FEEDBACK_ERRORS = {
empty: "Write something first.",
unknown_kind: "Pick feedback or a bug report.",
too_many: "Too many today. Try tomorrow.",
};
// is `password` the account's own? A wrong one counts, and after a few the
// page is signed out (the caller of `act` sees to that).
const passwordAgain = (account, password) => {
if (wrongPasswords.blocked(account.id)) {
return false;
}
const right = accounts.authenticate(account.username, password) === account;
if (!right) {
wrongPasswords.record(account.id);
}
return right;
};
// one change to the signed-in account. returns {ok: text} or {error: text}.
const act = (account, body, req, from, res) => {
const ban = banFor(account, from);
if (ban && !BANNED_MAY.includes(body.action)) {
return {error: "Not while banned."};
}
if (!ban && account.nameRejected && !RENAMING_MAY.includes(body.action)) {
return {error: "Choose a new username first."};
}
switch (body.action) {
// in place of a rejected username, at once and under the sign-up rules.
case "newName": {
if (!account.nameRejected || ban) {
return {error: "Your username was not rejected."};
}
const wanted = typeof body.newName === "string" ? body.newName.trim() : "";
if (wanted.toLowerCase() === account.username.toLowerCase()) {
return {error: NAME_ERRORS.same_name};
}
world.disconnect(account);
const problem = accounts.rename(account, wanted);
if (problem) {
return {error: NAME_ERRORS[problem] || NAME_ERRORS.invalid_username};
}
delete account.nameRejected;
accounts.save(account);
console.log(`[account] a rejected username was replaced by "${account.username}" from ${from.address}`);
return {ok: "Saved."};
}
// one for each account, ever: an answer that wants a follow-up says
// where to go with it.
case "appeal": {
if (!ban) {
return {error: "You are not banned."};
}
const result = bans.appeal(account, body.text, ban.addressKey);
return result.error ? {error: result.error === "empty" ? "Write something first." : "Already appealed."} : {ok: "Sent."};
}
case "password": {
// asked for again: a page left open is not enough to take an account over.
if (!passwordAgain(account, body.current)) {
return {error: "Wrong password."};
}
if (accounts.setPassword(account, body.password)) {
return {error: "Passwords need 10 characters or more, with a capital letter, a small letter and a digit."};
}
world.moderation.actions.kick(account);
signOutOthers(account, req);
console.log(`[account] "${account.username}" changed their password from ${from.address}`);
return {ok: "Password changed."};
}
case "requestName": {
const result = support.requestName(account, body.newName, body.reason);
return result.error ? {error: NAME_ERRORS[result.error]} : {ok: `Requested "${result.request.newName}".`};
}
case "withdrawName":
return support.withdrawNameRequest(account) ? {ok: "Withdrawn."} : {error: "No request waiting."};
case "feedback": {
const result = support.sendFeedback(account, String(body.kind), body.text);
return result.error ? {error: FEEDBACK_ERRORS[result.error]} : {ok: body.kind === "bug" ? "Bug report sent." : "Feedback sent."};
}
case "lock": {
account.locked = body.locked === true;
if (account.locked) {
// out of the game now, and no saved sign-in gets back in.
accounts.revokeTokens(account);
world.moderation.actions.kick(account);
}
accounts.save(account);
console.log(`[account] "${account.username}" ${account.locked ? "locked" : "unlocked"} from ${from.address}`);
return {ok: account.locked ? "Locked." : "Unlocked."};
}
case "feature": {
if (!accounts.FEATURES.includes(body.feature) || typeof body.on !== "boolean") {
return {error: "Unknown setting."};
}
account.features[body.feature] = body.on;
accounts.save(account);
return {ok: "Saved."};
}
// two-step sign-in (lib/totp.js). turning it on: the password again,
// then a secret for their authenticator app, then a code from the app
// to show it took. saved sign-ins end, so every device passes it once.
case "twoStepStart": {
if (totp.isOn(account)) {
return {error: "2FA is already on."};
}
if (!passwordAgain(account, body.current)) {
return {error: "Wrong password."};
}
const setup = totp.begin(account);
accounts.save(account);
return {ok: "Scan the code.", setup};
}
case "twoStepConfirm": {
const recovery = totp.confirm(account, body.code);
if (!recovery) {
accounts.save(account);
return {error: "Wrong code."};
}
accounts.revokeTokens(account);
accounts.save(account);
console.log(`[account] "${account.username}" turned two-step sign-in on from ${from.address}`);
return {ok: "2FA is on.", recovery};
}
// turning it off: the password and a code (or a recovery code).
case "twoStepOff": {
if (!totp.isOn(account)) {
return {error: "2FA is not on."};
}
if (!passwordAgain(account, body.current)) {
return {error: "Wrong password."};
}
if (!totp.verify(account, String(body.code || ""))) {
accounts.save(account);
return {error: "Wrong code."};
}
totp.turnOff(account);
accounts.save(account);
res.setHeader("Set-Cookie", cookieHeader("", 0, DEVICE_COOKIE));
console.log(`[account] "${account.username}" turned two-step sign-in off from ${from.address}`);
return {ok: "2FA is off."};
}
case "forgetDevices":
totp.forgetDevices(account);
accounts.save(account);
return {ok: "Trusted devices forgotten."};
// their bio, for the Bio tab of their card in the game. empty takes it off.
case "bio": {
// typed words others read: not while they cannot chat.
if (world.moderation.isSilenced(account)) {
return {error: "You cannot change your bio while silenced."};
}
const bio = badges.bioText(body.text);
if (bio === null) {
return {error: `At most ${badges.MAX_BIO} characters on ${badges.MAX_BIO_LINES} lines.`};
}
// the server's chat filter, as for anything else players write to
// each other (lib/chat-filter.txt).
const rule = chatFilter.broken(bio);
if (rule) {
console.log(`[filter] "${account.username}" bio not saved (${rule}): ${JSON.stringify(bio)}`);
return {error: "Your bio has words that are not allowed here."};
}
account.bio = bio;
accounts.save(account);
return {ok: bio ? "Saved." : "Removed."};
}
// whether their card in the game shows how long they have played.
case "playtimePublic":
account.playtimePublic = body.on === true;
accounts.save(account);
return {ok: account.playtimePublic ? "Others can see your playtime on your card." : "Your playtime is private."};
// their Discord username, for their card in the game. empty takes it off.
case "discord": {
const name = badges.discordName(body.name);
if (name === null) {
return {error: "Not a Discord username."};
}
account.discord = name;
accounts.save(account);
return {ok: name ? "Saved." : "Removed."};
}
// the player's own words about a buddy; only they see them.
case "buddyNote": {
const key = String(body.name || "").toLowerCase();
if (!account.buddies.includes(key)) {
return {error: "Not a buddy."};
}
const note = String(body.note || "").replace(/[\u0000-\u001f\u007f]/g, " ").trim().slice(0, MAX_NOTE);
if (note) {
account.buddyNotes[key] = note;
}
else {
delete account.buddyNotes[key];
}
accounts.save(account);
return {ok: "Saved."};
}
// blocking and unblocking, as the game's own buttons do it.
case "block":
case "unblock": {
const target = accounts.byName(String(body.name || "").trim());
if (!target || target === account) {
return {error: "No such player."};
}
return world.setBlocked(account, target, body.action === "block")
? {ok: body.action === "block" ? "Blocked." : "Unblocked."}
: {error: body.action === "block" ? "Already blocked, or your list is full." : "Not blocked."};
}
// an offer for something on another player's trade list, left for them
// to answer on their own page.
case "offer": {
const problem = world.tradeOffers.make(account, accounts.byName(String(body.to || "").trim()), body.wanted, body.offered);
return problem ? {error: OFFER_ERRORS[problem]} : {ok: "Offer sent."};
}
case "answerOffer": {
const problem = world.tradeOffers.answer(account, body.id, body.accept === true, world.refreshInventories);
return problem ? {error: problem === "gone" ? "That offer is no longer possible." : "No such offer."}
: {ok: body.accept === true ? "Traded." : "Declined."};
}
case "withdrawOffer":
return world.tradeOffers.withdraw(account, String(body.to || ""), body.id) ? {ok: "Withdrawn."} : {error: "No such offer."};
case "signOutEverywhere":
accounts.revokeTokens(account);
totp.forgetDevices(account);
accounts.save(account);
world.moderation.actions.kick(account);
signOutOthers(account, req);
return {ok: "Signed out everywhere."};
// for good: the password again, and the username typed out.
case "delete": {
if (!passwordAgain(account, body.current)) {
return {error: "Wrong password."};
}
if (String(body.confirm || "").trim().toLowerCase() !== account.username.toLowerCase()) {
return {error: "Username does not match."};
}
const name = account.username;
world.disconnect(account);
signOutOthers(account, null);
erase(account);
console.log(`[account] "${name}" deleted their account from ${from.address}`);
return {ok: "Account deleted.", deleted: true};
}
default:
return {error: "Unknown action."};
}
};
const PAGE_HEADERS = {
"Cache-Control": "no-store",
"X-Frame-Options": "DENY",
"X-Content-Type-Options": "nosniff",
"Referrer-Policy": "no-referrer",
"Content-Security-Policy": captcha.policy("default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; "
+ "form-action 'none'; frame-ancestors 'none'; base-uri 'none'"),
};
// A request that changes something must come from this server's own page.
const sameOrigin = limits.originAllowed;
// `from` is {address, local}; `web` is {send, readJson} from lib/http.js.
const handle = async (req, res, url, from, web) => {
for (const [name, value] of Object.entries(PAGE_HEADERS)) {
res.setHeader(name, value);
}
const reply = (status, body) => web.send(res, status, body);
const route = `${req.method} ${url.pathname.replace(/\/$/, "") || "/account"}`;
if (route === "GET /account") {
web.send(res, 200, captcha.withScriptVersion(await fs.promises.readFile(path.join(config.publicDir, "account.html"))), "text/html; charset=utf-8");
return;
}
if (req.method === "POST" && !sameOrigin(req)) {
reply(403, {error: "forbidden"});
return;
}
const body = req.method === "POST" ? await web.readJson(req) : {};
switch (route) {
case "POST /account/api/login": {
const key = String(body.username || "").trim().toLowerCase();
if (!from.local && failuresByAddress.blocked(from.key)) {
reply(429, {error: "Too many attempts. Try later."});
return;
}
// looked up first: with AJ_REGISTER=1 authenticate() would make the account.
const known = accounts.byName(key);
const captchaProblem = await captcha.check({captcha: body.captcha, captcha_pass: body.captchaPass}, from, "login", known);
if (captchaProblem) {
reply(captchaProblem === "captcha_unavailable" ? 503 : 401, {error: captcha.MESSAGES[captchaProblem], captcha: true});
return;
}
const account = known && await accounts.authenticateAsync(known.username, body.password) === known ? known : null;
if (!account) {
failuresByAddress.record(from.key);
if (known) {
accounts.recordLogin(known, "web", from.address, false);
accounts.save(known);
}
reply(401, {error: "Wrong username or password."});
return;
}
// two-step sign-in: a code after the password, unless this browser
// gave one in the last 30 days (lib/totp.js).
const cookies = [];
if (totp.isOn(account) && !totp.deviceTrusted(account, deviceCookie(req))) {
if (!body.code) {
reply(401, {error: "Enter your 2FA code.", twoStep: true, captchaPass: captcha.issuePass(account, from)});
return;
}
if (!totp.verify(account, String(body.code))) {
failuresByAddress.record(from.key);
accounts.recordLogin(account, "web", from.address, false);
accounts.save(account);
reply(401, {error: "Wrong code.", twoStep: true, captchaPass: captcha.issuePass(account, from)});
return;
}
if (body.rememberDevice === true) {
cookies.push(cookieHeader(totp.trustDevice(account, "web"), totp.DEVICE_DAYS * 86400, DEVICE_COOKIE));
}
}
accounts.recordLogin(account, "web", from.address);
accounts.save(account);
signIn(res, account, cookies);
reply(200, {ok: true});
return;
}
case "POST /account/api/logout": {
const token = cookieToken(req);
if (token) {
sessions.delete(sha256(token));
}
res.setHeader("Set-Cookie", cookieHeader("", 0));
reply(200, {ok: true});
return;
}
}
const account = signedInAs(req);
if (!account) {
reply(401, {error: "Sign in first."});
return;
}
switch (route) {
// A copy of their data, as a file. also from the page of a ban.
case "GET /account/api/export":
if (!writesPerTenMinutes.allow(account.id)) {
reply(429, {error: "Too many. Try later."});
return;
}
res.setHeader("Content-Disposition", `attachment; filename="burrow-data-${account.id}.json"`);
web.send(res, 200, JSON.stringify(dataOf(account), null, 2));
return;
case "GET /account/api/me":
reply(200, profile(account, from));
return;
// for the trade offer form: what the player could give, and what
// another player has up for trade.
case "GET /account/api/tradable": {
if (banFor(account, from) || account.nameRejected) {
reply(403, {error: "Not now."});
return;
}
const other = accounts.byName(url.searchParams.get("of") || "");
reply(200, {mine: world.tradeOffers.owned(account), theirs: other && other !== account ? world.tradeOffers.listed(other) : [],
found: Boolean(other && other !== account)});
return;
}
case "POST /account/api/act": {
if (!writesPerTenMinutes.allow(account.id)) {
reply(429, {error: "Too many changes. Try later."});
return;
}
const result = act(account, body, req, from, res);
// too many wrong passwords on this page: it is signed out.
if (!result.deleted && wrongPasswords.blocked(account.id)) {
signOutOthers(account, null);
res.setHeader("Set-Cookie", cookieHeader("", 0));
reply(401, {error: "Too many wrong passwords. Sign in again."});
return;
}
if (result.deleted) {
res.setHeader("Set-Cookie", cookieHeader("", 0));
}
reply(result.ok ? 200 : 400, result.ok && !result.deleted ? {...result, account: profile(account, from)} : result);
return;
}
default:
reply(404, {error: "not_found"});
}
};
// called when an account's name changes or it is deleted: its page sessions end.
const forget = account => signOutOthers(account, null);
module.exports = {handle, forget};