mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-19 09:37:50 -05:00
fix(staff): a Discord sign-in asks for the 2FA code, and the bot needs one too
Since7dee290a Discord account linked to a staff member opened the moderator panel for 12 hours on Discord's word alone, with no password and no 2FA code, though the normal sign-in asks for both every time; and sinceb11ae1dthe bot took /moderate and /burrow commands from that Discord account alone. The link outlives a new password and "sign out everywhere", so a taken-over Discord account was enough to act as the staff member. The admin dashboard's Discord sign-in (adminDashboardAccess) skipped its password the same way. - /mod/discord/callback and /admin/discord/callback no longer sign in: they keep a pending sign-in for 5 minutes (a Strict cookie) and the page asks for the linked account's 2FA code (POST .../api/discord/code). Five wrong codes and it must start again at Discord. The admin one also needs the account to have 2FA on and not be locked. - the bot's commands and buttons count only for a staff member with a panel session that took a code (modpanel.discordBot.commandActor): the password sign-in, or Discord and a code; not the game's token sign-in. A new password, signing out everywhere or 2FA turned off end those sessions (forget), and so the bot's use. Whose Discord it is still keeps linked staff out of /discord moderation, and lets them close tickets. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
83c2fc3b0b
commit
3b53c8dab8
5 files changed
+278
-25
No files matched your search
+73
-8
@@ -119,7 +119,22 @@ const cookieHeader = (token, maxAgeSeconds) =>
|
||||
const discordCookie = (value, maxAgeSeconds) =>
|
||||
`${DISCORD_COOKIE}=${value}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${maxAgeSeconds}${config.secureCookies ? "; Secure" : ""}`;
|
||||
|
||||
const signIn = res => {
|
||||
// a Discord sign-in waiting for the 2FA code of the account it is linked
|
||||
// to: SHA-256 of the token in its cookie -> {id, expiry, tries}. Discord
|
||||
// alone does not open this panel, as the password alone does not open the
|
||||
// moderator panel.
|
||||
const pendingDiscord = new Map();
|
||||
const PENDING_COOKIE = "ajc_admin_pending";
|
||||
const PENDING_MINUTES = 5;
|
||||
const PENDING_TRIES = 5;
|
||||
const pendingCookieHeader = (token, maxAgeSeconds) =>
|
||||
`${PENDING_COOKIE}=${token}; Path=/; HttpOnly; SameSite=Strict; Max-Age=${maxAgeSeconds}${config.secureCookies ? "; Secure" : ""}`;
|
||||
const pendingToken = req => (new RegExp(`(?:^|;\\s*)${PENDING_COOKIE}=([0-9a-f]{48})(?:;|$)`).exec(req.headers.cookie || "") || [])[1] || null;
|
||||
// may this account still sign in here with Discord?
|
||||
const discordAdmin = account => Boolean(account && account.adminDashboardAccess && account.discordId && totp.isOn(account) && !account.locked);
|
||||
|
||||
// `extraCookies` go out with the session's own.
|
||||
const signIn = (res, extraCookies = []) => {
|
||||
for (const [hash, expiry] of sessions) {
|
||||
if (expiry < Date.now()) {
|
||||
sessions.delete(hash);
|
||||
@@ -127,7 +142,7 @@ const signIn = res => {
|
||||
}
|
||||
const token = crypto.randomBytes(24).toString("hex");
|
||||
sessions.set(sha256(token), Date.now() + SESSION_HOURS * 3600000);
|
||||
res.setHeader("Set-Cookie", cookieHeader(token, SESSION_HOURS * 3600));
|
||||
res.setHeader("Set-Cookie", [cookieHeader(token, SESSION_HOURS * 3600), ...extraCookies]);
|
||||
};
|
||||
|
||||
const signOut = (req, res) => {
|
||||
@@ -692,13 +707,25 @@ const handle = async (req, res, url, from, web) => {
|
||||
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 discordLinks.finishAdmin(url.searchParams.get("state"), nonce, url.searchParams.get("code"));
|
||||
if (result.ok) {
|
||||
signIn(res);
|
||||
console.log(`[admin] ${logText(result.account.username)} signed in with Discord from ${logText(from.address)}`);
|
||||
// Discord has said who this is; the panel opens once that account's 2FA
|
||||
// code is given too (POST /admin/api/discord/code).
|
||||
if (result.ok && !discordAdmin(result.account)) {
|
||||
result.ok = false;
|
||||
result.error = totp.isOn(result.account) ? "denied" : "twostep";
|
||||
}
|
||||
const adminCookie = res.getHeader("Set-Cookie");
|
||||
res.setHeader("Set-Cookie", adminCookie ? [adminCookie, discordCookie("", 0)] : discordCookie("", 0));
|
||||
res.writeHead(302, {Location: `/dashboard/admin?discord=${result.ok ? "ok" : result.error}`});
|
||||
const cookies = [discordCookie("", 0)];
|
||||
if (result.ok) {
|
||||
for (const [hash, pending] of pendingDiscord) {
|
||||
if (pending.expiry < Date.now()) {
|
||||
pendingDiscord.delete(hash);
|
||||
}
|
||||
}
|
||||
const token = crypto.randomBytes(24).toString("hex");
|
||||
pendingDiscord.set(sha256(token), {id: result.account.id, expiry: Date.now() + PENDING_MINUTES * 60000, tries: 0});
|
||||
cookies.push(pendingCookieHeader(token, PENDING_MINUTES * 60));
|
||||
}
|
||||
res.setHeader("Set-Cookie", cookies);
|
||||
res.writeHead(302, {Location: `/dashboard/admin?discord=${result.ok ? "code" : result.error}`});
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
@@ -760,6 +787,44 @@ const handle = async (req, res, url, from, web) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// the second half of a Discord sign-in: the linked account's 2FA code.
|
||||
case "POST /admin/api/discord/code": {
|
||||
if (tooManyFailures(from.key)) {
|
||||
reply(429, {error: "Too many attempts. Try again in a few minutes."});
|
||||
return;
|
||||
}
|
||||
const token = pendingToken(req);
|
||||
const hash = token ? sha256(token) : null;
|
||||
const pending = hash ? pendingDiscord.get(hash) : null;
|
||||
const account = pending && pending.expiry > Date.now() ? accounts.byId(pending.id) : null;
|
||||
if (!discordAdmin(account)) {
|
||||
if (hash) {
|
||||
pendingDiscord.delete(hash);
|
||||
}
|
||||
res.setHeader("Set-Cookie", pendingCookieHeader("", 0));
|
||||
reply(401, {error: "The Discord sign-in expired. Sign in with Discord again.", expired: true});
|
||||
return;
|
||||
}
|
||||
if (!totp.verify(account, String(body.code || ""))) {
|
||||
recordFailure(from.key);
|
||||
console.log(`[admin] wrong 2FA code after Discord for ${logText(account.username)} from ${logText(from.address)}`);
|
||||
pending.tries++;
|
||||
if (pending.tries >= PENDING_TRIES) {
|
||||
pendingDiscord.delete(hash);
|
||||
res.setHeader("Set-Cookie", pendingCookieHeader("", 0));
|
||||
reply(401, {error: "Too many wrong codes. Sign in with Discord again.", expired: true});
|
||||
return;
|
||||
}
|
||||
reply(401, {error: "Wrong code."});
|
||||
return;
|
||||
}
|
||||
pendingDiscord.delete(hash);
|
||||
signIn(res, [pendingCookieHeader("", 0)]);
|
||||
console.log(`[admin] ${logText(account.username)} signed in with Discord and a code from ${logText(from.address)}`);
|
||||
reply(200, {ok: true});
|
||||
return;
|
||||
}
|
||||
|
||||
case "POST /admin/api/logout":
|
||||
signOut(req, res);
|
||||
reply(200, {ok: true});
|
||||
|
||||
Reference in new issue
Block a user