diff --git a/lib/devices.js b/lib/devices.js index d6eebea..95e7dfc 100644 --- a/lib/devices.js +++ b/lib/devices.js @@ -308,6 +308,13 @@ const unban = id => { return ban; }; +// one ban as kept, with the account it was made with (accountId), or null: +// for the moderator panel to see whose it is before lifting it. +const banById = id => { + const row = db().prepare("SELECT * FROM device_bans WHERE id = ?").get(Number(id)); + return row ? banRow(row) : null; +}; + // the bans, newest first: their ids, and the accounts that used them. const MAX_LISTED_ACCOUNTS = 20; const list = () => db().prepare("SELECT * FROM device_bans ORDER BY id DESC").all().map(banRow) @@ -418,4 +425,4 @@ const noteLauncher = (token, hasIds) => { }; const launcherOutdated = token => outdatedTokens.has(tokenKey(token)); -module.exports = {noteLauncher, launcherOutdated, load, read, banOf, banOfAccount, seen, alts, knownFor, banAccount, accountsUnder, unban, list, idsFor, bansOfAccount, search, bannedId, alerts, alertsOf, forget, clientHash}; +module.exports = {noteLauncher, launcherOutdated, load, read, banOf, banOfAccount, seen, alts, knownFor, banAccount, accountsUnder, unban, banById, list, idsFor, bansOfAccount, search, bannedId, alerts, alertsOf, forget, clientHash}; diff --git a/lib/manage.js b/lib/manage.js index a69b55c..32eef7c 100644 --- a/lib/manage.js +++ b/lib/manage.js @@ -111,6 +111,13 @@ const signOutOthers = (account, req) => { } }; +// 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}); @@ -292,6 +299,7 @@ const act = (account, body, req, from, res) => { } 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."}; } @@ -311,6 +319,7 @@ const act = (account, body, req, from, res) => { // 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)}`); @@ -364,6 +373,7 @@ const act = (account, body, req, from, res) => { } 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."}; @@ -467,6 +477,7 @@ const act = (account, body, req, from, res) => { 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": { @@ -479,6 +490,7 @@ const act = (account, body, req, from, res) => { 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}; diff --git a/lib/modpanel.js b/lib/modpanel.js index 5a52258..cee7603 100644 --- a/lib/modpanel.js +++ b/lib/modpanel.js @@ -153,13 +153,16 @@ const maskedAddress = ban => ({ const wholeNumber = (value, min, max) => (Number.isInteger(value) && value >= min && value <= max ? value : null); -// the permission (lib/roles.js) each action needs. +// the permission (lib/roles.js) each action needs. an action that is not +// here is refused (act), so that a new one cannot go unchecked. const ACTION_PERMISSIONS = { ban: "player.ban", banReason: "player.ban", unban: "player.ban", silence: "chat.silence", unsilence: "chat.silence", kick: "player.kick", message: "player.warn", warn: "player.warn", unwarn: "player.warn", rejectName: "player.names", acceptName: "player.names", clearBio: "player.names", clearWallPosts: "player.names", banAddress: "address.ban", + // a computer's ban, as an address's, keeps out everyone who uses it + banDevices: "address.ban", unbanDevices: "address.ban", summon: "player.summon", goto: "player.goto", note: "player.view", deleteNote: "player.view", }; @@ -180,8 +183,11 @@ const act = (mod, target, body) => { if (!roles.outranks(mod, target)) { return {error: "Staff of your rank or above are for someone higher up to deal with."}; } - const permission = ACTION_PERMISSIONS[body.action]; - if (permission && !roles.can(mod, permission)) { + const permission = Object.hasOwn(ACTION_PERMISSIONS, body.action) ? ACTION_PERMISSIONS[body.action] : null; + if (!permission) { + return {error: "Unknown action."}; + } + if (!roles.can(mod, permission)) { return {error: "Your role cannot do that."}; } switch (body.action) { @@ -421,6 +427,23 @@ const handle = async (req, res, url, from, web) => { } reply(result.ok ? 200 : 400, result.ok ? {ok: result.ok} : {error: result.error}); }; + // a route's permission (lib/roles.js), as act() checks an action's: a + // role without it is refused here, whatever its page shows. + const may = permission => { + if (roles.can(mod, permission)) { + return true; + } + reply(403, {error: "Your role cannot do that."}); + return false; + }; + // why the moderator may not act on `account`, as act() refuses: not on + // themselves, nor on staff of their rank or above. null when they may. + const refusalFor = account => { + if (!account || roles.outranks(mod, account)) { + return null; + } + return {error: account === mod ? "That is you." : "Staff of your rank or above are for someone higher up to deal with."}; + }; switch (route) { // who is signed in here, their role and what it allows: for a client to @@ -564,7 +587,12 @@ const handle = async (req, res, url, from, web) => { reply(200, {reports: reports.list(url.searchParams.get("handled") === "1"), open: reports.openCount()}); return; + // a report dealt with: for the roles that act on players (a warning is + // the least of it), not for one that only looks. case "POST /mod/api/report": + if (!may("player.warn")) { + return; + } done(reports.markHandled(Number(body.id)) ? {ok: "Marked as dealt with."} : {error: "No such report waiting."}, "report dealt with", "", `#${Number(body.id)}`); return; @@ -576,10 +604,32 @@ const handle = async (req, res, url, from, web) => { } case "POST /mod/api/request": { if (body.action === "appealApprove" || body.action === "appealDeny") { + // a ban decision: for the roles that ban, and one that also lifts the + // ban on an address only for those that ban addresses. + const appeal = bans.waitingAppeal(body.id); + const liftsAddress = body.action === "appealApprove" && appeal && appeal.addressKey && bans.addressBan(appeal.addressKey); + if (!may("player.ban") || (liftsAddress && !may("address.ban"))) { + return; + } + const refusal = appeal ? refusalFor(accounts.byId(appeal.accountId)) : null; + if (refusal) { + done(refusal); + return; + } const result = staff.answerAppeal(body.id, body.action === "appealApprove", body.reply, mod.username); done(result, body.action === "appealApprove" ? "appeal approved" : "appeal denied", result.username, bans.clean(body.reply, bans.MAX_REPLY)); } else if (body.action === "approve" || body.action === "deny") { + // a new name: for the roles that judge names. + if (!may("player.names")) { + return; + } + const request = support.nameRequestById(body.id); + const refusal = request ? refusalFor(accounts.byId(request.accountId)) : null; + if (refusal) { + done(refusal); + return; + } const result = staff.answerNameRequest(body.id, body.action === "approve"); done(result, body.action === "approve" ? "name request approved" : "name request denied", result.username); } @@ -603,8 +653,18 @@ const handle = async (req, res, url, from, web) => { return; } - // A device ban lifted (lib/devices.js). + // A device ban lifted (lib/devices.js): for the roles that give them + // (act's unbanDevices), and not one made with staff of their rank or above. case "POST /mod/api/device": { + if (!may("address.ban")) { + return; + } + const ban = devices.banById(body.id); + const refusal = ban ? refusalFor(accounts.byId(ban.accountId)) : null; + if (refusal) { + done(refusal); + return; + } const result = staff.unbanDevices(body.id, mod.username); done(result, "hardware ban lifted", result.username || ""); return; @@ -626,9 +686,19 @@ const handle = async (req, res, url, from, web) => { return; } + // a painting judged: for the roles that judge what players make, and + // never their own or one by staff of their rank or above. case "POST /mod/api/masterpiece": { + if (!may("items.approve")) { + return; + } const id = String(body.id || ""); const maker = masterpieces.creatorOf(id); + const refusal = refusalFor(maker); + if (refusal) { + done(refusal); + return; + } const result = body.action === "approve" ? (world.approveMasterpiece(id) ? {ok: "Approved."} : {error: "No such painting waiting."}) : body.action === "reject" || body.action === "remove" diff --git a/lib/roles.js b/lib/roles.js index 20b308a..e621b94 100644 --- a/lib/roles.js +++ b/lib/roles.js @@ -17,15 +17,15 @@ const totp = require("./totp.js"); const PERMISSIONS = { "player.view": "see players, chat, reports, requests, bans and paintings in the moderator panel, and keep notes on players (!note, !notes)", "staff.list": "see which staff are online (!staff)", - "player.warn": "warn a player, take a warning back and send them a message (!warn, !unwarn, !warnings, !message)", + "player.warn": "warn a player, take a warning back, send them a message and mark a report dealt with (!warn, !unwarn, !warnings, !message)", "player.goto": "go to the room a player is in (!goto)", "chat.silence": "silence and unsilence a player (!silence)", "player.kick": "disconnect a player (!kick)", "player.summon": "bring a player to the room you are in (!summon)", "staff.invisible": "go unseen in rooms, as the game's moderators could (!invisible)", - "player.ban": "ban, unban and change a ban's reason", - "player.names": "reject or accept a name, clear a bio, remove a player's Jammer Wall posts", - "address.ban": "ban the address a player last signed in from", + "player.ban": "ban, unban, change a ban's reason and answer ban appeals", + "player.names": "reject or accept a name, answer requests for a new one, clear a bio, remove a player's Jammer Wall posts", + "address.ban": "ban the address a player last signed in from, or the computers they use, and lift those bans", "items.approve": "approve or turn away the items and pictures players make, when the admin has asked for that", "items.create": "make new items from the game's art, change and delete them, lock items out of the item finder and take items from players (!item, !lock, !unlock, !locked, !take)", }; diff --git a/public/mod.html b/public/mod.html index 7e721ee..6b4314c 100644 --- a/public/mod.html +++ b/public/mod.html @@ -1295,7 +1295,7 @@ if (handled) { box.append(el("div", `Dealt with on ${when(report.handledAt)}`, "hint")); } - else { + else if (permissions.has("player.warn")) { buttons.append(button("Dealt with", async () => { const result = await api("report", {id: report.id}); setStatus($("report-status"), said(result), !result.ok); @@ -1342,9 +1342,17 @@ reply.setAttribute("aria-label", `Reply to ${appeal.username}`); reply.style.width = "100%"; const buttons = el("div", undefined, "buttons"); - buttons.append(button("Unban", () => answer({action: "appealApprove", id: appeal.accountId, reply: reply.value}), "primary"), - button("Deny", () => answer({action: "appealDeny", id: appeal.accountId, reply: reply.value})), - button("Player", () => openPlayer(appeal.username)), button("Chat history", () => openChat(appeal.username))); + // answering is a ban decision, and lifting an address's ban with + // it is for the roles that ban addresses (lib/modpanel.js). + const answers = permissions.has("player.ban"); + if (answers && (!appeal.ofAddress || permissions.has("address.ban"))) { + buttons.append(button("Unban", () => answer({action: "appealApprove", id: appeal.accountId, reply: reply.value}), "primary")); + } + if (answers) { + buttons.append(button("Deny", () => answer({action: "appealDeny", id: appeal.accountId, reply: reply.value}))); + } + buttons.append(button("Player", () => openPlayer(appeal.username)), button("Chat history", () => openChat(appeal.username))); + reply.hidden = !answers; box.append(reply, buttons); return box; })); @@ -1359,7 +1367,7 @@ if (handled) { box.append(el("div", `${request.status === "approved" ? "Approved" : "Denied"} on ${when(request.handledAt)}`, "hint")); } - else { + else if (permissions.has("player.names")) { const buttons = el("div", undefined, "buttons"); buttons.append(button("Approve", () => answer({action: "approve", id: request.id}), "primary"), button("Deny", () => answer({action: "deny", id: request.id}))); box.append(buttons); @@ -1418,11 +1426,11 @@ setStatus($("masterpiece-status"), said(result), !result.ok); refresh(); }, className); - if (painting.status === "waiting") { - buttons.append(does("Approve", "approve", "primary"), does("Reject", "reject", "danger")); - } - else { - buttons.append(does("Remove", "remove", "danger")); + // judging paintings is for the roles that judge what players make + if (permissions.has("items.approve")) { + buttons.append(...(painting.status === "waiting" + ? [does("Approve", "approve", "primary"), does("Reject", "reject", "danger")] + : [does("Remove", "remove", "danger")])); } buttons.append(button("Player", () => openPlayer(painting.creator))); box.append(buttons); @@ -1467,11 +1475,13 @@ $("addresses").replaceChildren(...data.addresses.map(ban => { const row = el("tr"); const buttons = el("td"); - buttons.append(button("Unban", async () => { - const result = await api("address", {id: ban.id}); - setStatus($("ban-status"), said(result), !result.ok); - refresh(); - })); + if (permissions.has("address.ban")) { + buttons.append(button("Unban", async () => { + const result = await api("address", {id: ban.id}); + setStatus($("ban-status"), said(result), !result.ok); + refresh(); + })); + } row.append(el("td", ban.address), el("td", ban.reason), el("td", ban.by || "—"), el("td", when(ban.createdAt)), buttons); return row; })); @@ -1479,14 +1489,17 @@ $("device-bans").replaceChildren(...data.devices.map(ban => { const row = el("tr"); const buttons = el("td"); - buttons.append(button("Unban", async () => { - if (!window.confirm(`Lift the hardware ban made with ${ban.username}?`)) { - return; - } - const result = await api("device", {id: ban.id}); - setStatus($("ban-status"), said(result), !result.ok); - refresh(); - }), " ", button("Player", () => openPlayer(ban.username))); + if (permissions.has("address.ban")) { + buttons.append(button("Unban", async () => { + if (!window.confirm(`Lift the hardware ban made with ${ban.username}?`)) { + return; + } + const result = await api("device", {id: ban.id}); + setStatus($("ban-status"), said(result), !result.ok); + refresh(); + }), " "); + } + buttons.append(button("Player", () => openPlayer(ban.username))); row.append(el("td", ban.username || "—"), el("td", ban.reason), el("td", ban.by || "—"), el("td", when(ban.createdAt)), deviceAccounts(ban.accounts), deviceIds(ban), buttons); return row; @@ -2108,7 +2121,8 @@ silence: "chat.silence", unsilence: "chat.silence", kick: "player.kick", message: "player.warn", warn: "player.warn", unwarn: "player.warn", rejectName: "player.names", acceptName: "player.names", clearBio: "player.names", - clearWallPosts: "player.names", banAddress: "address.ban", summon: "player.summon", goto: "player.goto", + clearWallPosts: "player.names", banAddress: "address.ban", banDevices: "address.ban", unbanDevices: "address.ban", + summon: "player.summon", goto: "player.goto", }; const applyPermissions = async () => { const result = await api("me"); diff --git a/test/staff.js b/test/staff.js index 4cdfbe8..6c763e8 100644 --- a/test/staff.js +++ b/test/staff.js @@ -246,6 +246,30 @@ const run = async () => { // searching by address (against ban evasion) is for the roles that ban addresses assert.strictEqual((await halPage("/mod/api/players?by=address&q=127.0.0.")).status, 403); assert.ok(Array.isArray((await modPage("/mod/api/players?by=address&q=127.0.0.")).data.players)); + // the panel's other ban decisions are for the roles that ban: a helper + // may not answer an appeal, judge a new name or a painting, nor place or + // lift a hardware ban, and is refused before anything is looked up. + for (const [route, body] of [ + ["/mod/api/request", {action: "appealApprove", id: 1}], ["/mod/api/request", {action: "appealDeny", id: 1}], + ["/mod/api/request", {action: "approve", id: 1}], ["/mod/api/request", {action: "deny", id: 1}], + ["/mod/api/masterpiece", {action: "approve", id: "x"}], ["/mod/api/masterpiece", {action: "remove", id: "x"}], + ["/mod/api/device", {id: 1}], + ]) { + assert.strictEqual((await halPage(route, body)).status, 403, `${route} ${JSON.stringify(body)}`); + } + for (const action of ["banDevices", "unbanDevices"]) { + assert.strictEqual((await halPage("/mod/api/act", {username: "Pip", action, reason: "x"})).data.error, "Your role cannot do that."); + } + // a moderator's role lets them through, to the thing itself + assert.strictEqual((await modPage("/mod/api/request", {action: "appealApprove", id: 999999})).data.error, "No such appeal waiting."); + assert.strictEqual((await modPage("/mod/api/request", {action: "approve", id: 999999})).data.error, "No such request waiting."); + assert.strictEqual((await modPage("/mod/api/masterpiece", {action: "approve", id: "x"})).data.error, "No such painting waiting."); + assert.strictEqual((await modPage("/mod/api/device", {id: 999999})).data.error, "No such hardware ban."); + assert.strictEqual((await modPage("/mod/api/act", {username: "Pip", action: "unbanDevices"})).data.error, "None of their hardware is banned."); + // a report is closed by whoever may act on one, a helper too + assert.strictEqual((await halPage("/mod/api/report", {id: 999999})).data.error, "No such report waiting."); + // an action the panel does not know is refused, never let through unchecked + assert.strictEqual((await modPage("/mod/api/act", {username: "Pip", action: "giveEverything"})).data.error, "Unknown action."); // a warning from the panel, and on the player's page assert.match((await modPage("/mod/api/act", {username: "Pip", action: "warn", reason: "scamming", note: "fake trade"})).data.ok, /^Pip warned \(scamming\): warning 1\./); assert.match((await modPage("/mod/api/act", {username: "Pip", action: "warn", reason: "nonsense"})).data.error, /reason/); @@ -365,6 +389,27 @@ const run = async () => { assert.strictEqual(strings[11134], "--Message From Moderator--\n\n"); assert.ok(!strings[11135].includes("\\n")); + // --- the account page ends the moderator panel's sessions too: a new + // password, or signing out everywhere, leaves no panel session behind. + // and a moderator does not answer their own request for a new name. + for (const username of ["Mira", "Hal"]) { + usedStep[username]++; + } + await waitForStep(Math.max(usedStep.Mira, usedStep.Hal) - 1); + const miraAccount = webSession(); + assert.strictEqual((await miraAccount("/account/api/login", {username: "Mira", password: people.Mira, code: codeAt(secrets.Mira, usedStep.Mira)})).status, 200); + assert.ok((await miraAccount("/account/api/act", {action: "requestName", newName: "Miranda", reason: "a longer name"})).data.ok); + const asked = (await modPage("/mod/api/requests")).data.names.find(request => request.oldName === "Mira"); + assert.strictEqual((await modPage("/mod/api/request", {action: "approve", id: asked.id})).data.error, "That is you."); + assert.strictEqual((await modPage("/mod/api/me")).status, 200); + assert.strictEqual((await miraAccount("/account/api/act", {action: "password", current: people.Mira, password: "KettlePass21"})).data.ok, "Password changed."); + assert.strictEqual((await modPage("/mod/api/me")).status, 401); + const halAccount = webSession(); + assert.strictEqual((await halAccount("/account/api/login", {username: "Hal", password: people.Hal, code: codeAt(secrets.Hal, usedStep.Hal)})).status, 200); + assert.strictEqual((await halPage("/mod/api/me")).status, 200); + assert.strictEqual((await halAccount("/account/api/act", {action: "signOutEverywhere"})).data.ok, "Signed out everywhere."); + assert.strictEqual((await halPage("/mod/api/me")).status, 401); + for (const {client} of [mira, hal, pip, annBack]) { client.socket.destroy(); }