mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-18 17:27:50 -05:00
From the 18 October security audit (S-9, S-10). - the moderator panel only refused a role the actions in its permission map. hardware bans and lifting them were missing from it, and answering ban appeals (which lifts the ban, the address's too), name requests, paintings and reports were checked for player.view alone, so a Helper could do all of them. each now needs its own permission (address.ban, player.ban, player.names, items.approve, player.warn), with the rank check the other actions have, and an action missing from the map is refused. the page hides what the role cannot do. - a new password, "Sign out everywhere", locking the account, turning 2FA off or deleting the account now also ends its moderator panel sessions, as the admin panel's reset already did, so a stolen panel session does not outlive them. - test/staff.js covers both, and fails without either fix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
677 lines
29 KiB
JavaScript
677 lines
29 KiB
JavaScript
"use strict";
|
|
|
|
const {logText} = require("./logtext.js");
|
|
const customItems = require("./customitems.js");
|
|
const itemControl = require("./itemcontrol.js");
|
|
const playerItems = require("./playeritems.js");
|
|
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 discord = require("./discord.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";
|
|
// a Discord link under way (lib/discord.js): the browser that started it
|
|
// must be the one that comes back. Lax, as Discord's redirect back is from
|
|
// another site, which the page's own (Strict) cookie is not sent from.
|
|
const DISCORD_COOKIE = "ajc_dlink";
|
|
const discordCookie = (value, maxAgeSeconds) =>
|
|
`${DISCORD_COOKIE}=${value}; Path=/account/discord; HttpOnly; SameSite=Lax; Max-Age=${maxAgeSeconds}${config.secureCookies ? "; Secure" : ""}`;
|
|
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);
|
|
}
|
|
}
|
|
};
|
|
|
|
// ends the account's sessions in the moderator panel (lib/modpanel.js, which
|
|
// needs this file, so it is asked for when used): with the others when the
|
|
// password changes or the owner signs out everywhere, and when the account
|
|
// locks or turns 2FA off, so that a session from before does not come back
|
|
// with it.
|
|
const endStaffSessions = account => require("./modpanel.js").forget(account);
|
|
|
|
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,
|
|
// what they made in the game's Item Maker (lib/playeritems.js)
|
|
customItems: playerItems.madeBy(account).map(entry => ({kind: entry.kind, id: entry.id, name: entry.name,
|
|
art: require("./itemart.js").urlOf(entry, "/account/api/itemart"), artWaiting: Boolean(entry.artPending), look: entry.artMode || "sticker"})),
|
|
picturesStopped: Boolean(account.artBlocked),
|
|
customItemLimits: {perDay: playerItems.PER_DAY, inAll: playerItems.IN_ALL},
|
|
pets: account.pets.length,
|
|
buddies: account.buddies.length,
|
|
banned: null,
|
|
locked: account.locked,
|
|
twoStep: twoStepOf(account),
|
|
discord: account.discord,
|
|
discordLinked: Boolean(account.discordId),
|
|
discordLinking: discord.linking(),
|
|
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", "modNotes"];
|
|
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 "${logText(account.username)}" from ${logText(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."};
|
|
}
|
|
const passwordProblem = accounts.setPassword(account, body.password);
|
|
if (passwordProblem) {
|
|
return {error: passwordProblem === "weak_password" ? "That password is too easy to guess: it is a common one, or has your username in it."
|
|
: "Passwords need 10 characters or more, with a capital letter, a small letter and a digit."};
|
|
}
|
|
world.moderation.actions.kick(account);
|
|
signOutOthers(account, req);
|
|
endStaffSessions(account);
|
|
console.log(`[account] "${logText(account.username)}" changed their password from ${logText(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);
|
|
endStaffSessions(account);
|
|
}
|
|
accounts.save(account);
|
|
console.log(`[account] "${logText(account.username)}" ${logText(account.locked ? "locked" : "unlocked")} from ${logText(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);
|
|
// confirm() drops a setup older than 15 minutes: no code can match it now.
|
|
return {error: account.totpSetup ? "Wrong code." : "Setup timed out. Turn 2FA on again and scan the new QR code."};
|
|
}
|
|
accounts.revokeTokens(account);
|
|
accounts.save(account);
|
|
console.log(`[account] "${logText(account.username)}" turned two-step sign-in on from ${logText(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);
|
|
endStaffSessions(account);
|
|
res.setHeader("Set-Cookie", cookieHeader("", 0, DEVICE_COOKIE));
|
|
console.log(`[account] "${logText(account.username)}" turned two-step sign-in off from ${logText(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.
|
|
// their own custom items: a new name, or gone (from everyone who has
|
|
// one). a picture is sent to /api/itemart.
|
|
case "itemLook":
|
|
return playerItems.setLook(account, String(body.kind), body.id, String(body.look));
|
|
case "itemRename":
|
|
return playerItems.rename(account, String(body.kind), body.id, body.name);
|
|
case "itemDelete": {
|
|
const entry = customItems.get(String(body.kind), body.id);
|
|
if (!entry || entry.ownerId !== account.id) {
|
|
return {error: "That is not one of your items."};
|
|
}
|
|
const result = itemControl.deleteCustom(entry.kind, entry.id, world);
|
|
return result.error ? result : {ok: `Deleted ${entry.name}.`};
|
|
}
|
|
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] "${logText(account.username)}" bio not saved (${logText(rule)}): ${logText(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 account, for their card in the game: it is linked by
|
|
// signing in to Discord (GET /account/discord/link), and only taken off
|
|
// here, with the booster badge it brought.
|
|
case "discord":
|
|
if (String(body.name || "") !== "") {
|
|
return {error: "Link your Discord account instead."};
|
|
}
|
|
discord.unlink(account);
|
|
return {ok: "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);
|
|
endStaffSessions(account);
|
|
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);
|
|
endStaffSessions(account);
|
|
erase(account);
|
|
console.log(`[account] "${logText(name)}" deleted their account from ${logText(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;
|
|
}
|
|
}
|
|
|
|
// back from Discord (lib/discord.js): the state and the nonce must be the
|
|
// ones the link began with. the page then says how it went.
|
|
if (route === "GET /account/discord/callback") {
|
|
const nonce = (new RegExp(`(?:^|;\\s*)${DISCORD_COOKIE}=([0-9a-f]{48})(?:;|$)`).exec(req.headers.cookie || "") || [])[1];
|
|
const result = url.searchParams.get("error") ? {error: "cancelled"}
|
|
: await discord.finish(url.searchParams.get("state"), nonce, url.searchParams.get("code"));
|
|
res.setHeader("Set-Cookie", discordCookie("", 0));
|
|
res.writeHead(302, {Location: `/account?discord=${result.ok ? "linked" : result.error}`});
|
|
res.end();
|
|
return;
|
|
}
|
|
|
|
const account = signedInAs(req);
|
|
if (!account) {
|
|
reply(401, {error: "Sign in first."});
|
|
return;
|
|
}
|
|
switch (route) {
|
|
// the page's "Link Discord": off to Discord's sign-in, which comes back
|
|
// to /account/discord/callback.
|
|
case "GET /account/discord/link": {
|
|
if (!discord.linking() || banFor(account, from)) {
|
|
res.writeHead(302, {Location: "/account?discord=off"});
|
|
res.end();
|
|
return;
|
|
}
|
|
const {url: to, nonce} = discord.start(account);
|
|
res.setHeader("Set-Cookie", discordCookie(nonce, 600));
|
|
res.writeHead(302, {Location: to});
|
|
res.end();
|
|
return;
|
|
}
|
|
// 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, signedInAs};
|