Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
POST /admin/lift lifts a live mute or ban for the owner or a moderator, same-origin, and logs who did it - route and test only #367
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/356-post-admin-lift-lifts-a-live-mute-or-ban → main 1 commit fork 1 thread · 1 unresolved
3 files
changed +73
−1
Software/so101/src/admin-relay.test.ts +28
| @@ -123,3 +123,31 @@ test("/status names who is driving: null, then alice, then the owner after a tak | ||
| 123 | 123 | sim.stop(true); |
| 124 | 124 | } |
| 125 | 125 | }); |
| 126 | ||
| 127 | test("POST /admin/lift lifts a mute or ban for the owner or a moderator, logged; others are refused", async () => { | |
| 128 | const base = await boot(); | |
| 129 | const owner = as("trevato", "admin"); | |
| 130 | const bob = as("bob", "user"); | |
| 131 | await fetch(`${base}/admin`, form(owner, { action: "mod-add", name: "bob" })); | |
| 132 | lobby!.chat.mute("u1", "carol", null); | |
| 133 | lobby!.chat.mute("u2", "bob", null); | |
| 134 | lobby!.accounts.ban({ id: "u3", name: "dave", owner: false }, null, "trevato"); | |
| 135 | lobby!.accounts.ban({ id: "u4", name: "erin", owner: false }, null, "trevato"); | |
| 136 | const lift = (h: Record<string, string>, body: Record<string, string>) => fetch(`${base}/admin/lift`, form(h, body)); | |
| 137 | expect((await lift(as("eve", "user"), { kind: "ban", id: "u3" })).status).toBe(403); | |
| 138 | expect((await lift({ ...bob, origin: "http://evil.example" }, { kind: "ban", id: "u3" })).status).toBe(403); | |
| 139 | expect((await lift(bob, { kind: "kick", id: "u3" })).status).toBe(400); | |
| 140 | expect((await lift(bob, { kind: "ban", id: "bad id" })).status).toBe(400); | |
| 141 | expect((await lift(bob, { kind: "ban", id: "nobody" })).status).toBe(404); | |
| 142 | expect((await lift(bob, { kind: "mute", id: "u2" })).status).toBe(403); // a moderator's own mute stays with the owner | |
| 143 | const ok = await lift(bob, { kind: "ban", id: "u3" }); | |
| 144 | expect(ok.status).toBe(303); | |
| 145 | expect(ok.headers.get("location")).toBe("/admin"); | |
| 146 | expect(lobby!.accounts.bans().map((b) => b.id)).toEqual(["u4"]); | |
| 147 | expect(lobby!.admin.log.at(-1)).toMatchObject({ what: "unbanned dave", by: { name: "bob" } }); | |
| 148 | expect((await lift(bob, { kind: "ban", id: "u3" })).status).toBe(404); | |
| 149 | expect((await lift(owner, { kind: "mute", id: "u2" })).status).toBe(303); | |
| 150 | expect((await lift(owner, { kind: "mute", id: "u1" })).status).toBe(303); | |
| 151 | expect(lobby!.chat.muted.size).toBe(0); | |
| 152 | expect(lobby!.admin.log.at(-1)?.what).toBe("unmuted carol"); | |
| 153 | }); |
Software/so101/src/admin-routes.ts +43
| @@ -3,6 +3,7 @@ import type { IncomingMessage, ServerResponse } from "node:http"; | ||
| 3 | 3 | import type { Person } from "./identity.ts"; |
| 4 | 4 | import type { Lobby } from "./lobby.ts"; |
| 5 | 5 | import { AdminError, changeFromForm } from "./admin.ts"; |
| 6 | import { sameOrigin } from "./relay.ts"; | |
| 6 | 7 | import { adminCsp, DONE, renderAdmin, renderRefusal, type Done } from "./admin-page.ts"; |
| 7 | 8 | |
| 8 | 9 | /** What a POST needs once the caller is known to be allowed on the page. */ |
| @@ -122,3 +123,45 @@ export async function handleAdmin(lobby: Lobby, req: IncomingMessage, res: Serve | ||
| 122 | 123 | res.writeHead(303, { location: `/admin?done=${done}`, "cache-control": "no-store" }); |
| 123 | 124 | res.end(); |
| 124 | 125 | } |
| 126 | ||
| 127 | /** A user id as the chat and the accounts know it. */ | |
| 128 | const USER_ID = /^[A-Za-z0-9_-]{1,64}$/; | |
| 129 | ||
| 130 | /** Answer a plain JSON refusal. */ | |
| 131 | function refuse(res: ServerResponse, status: number, error: string): void { | |
| 132 | res.writeHead(status, { "content-type": "application/json", "cache-control": "no-store" }); | |
| 133 | res.end(JSON.stringify({ error })); | |
| 134 | } | |
| 135 | ||
| 136 | /** | |
| 137 | * POST /admin/lift: the owner or a verified moderator lifts a live mute or ban. A moderator may not lift one on the | |
| 138 | * owner or another moderator. Logged with who did it, then back to /admin. | |
| 139 | */ | |
| 140 | export async function handleLift(lobby: Lobby, req: IncomingMessage, res: ServerResponse, readBody: ReadBody): Promise<void> { | |
| 141 | if (req.method !== "POST") { | |
| 142 | res.setHeader("allow", "POST"); | |
| 143 | return refuse(res, 405, "POST"); | |
| 144 | } | |
| 145 | if (!sameOrigin(req)) return refuse(res, 403, "only this page may lift"); | |
| 146 | const who = lobby.who(req); | |
| 147 | if (!who || !(who.owner || (lobby.verified(req)?.id === who.id && lobby.admin.isModerator(who.name)))) return refuse(res, 403, "only the owner or a moderator may lift"); | |
| 148 | let body: Buffer; | |
| 149 | try { | |
| 150 | body = await readBody(req, MAX_FORM); | |
| 151 | } catch { | |
| 152 | res.setHeader("connection", "close"); | |
| 153 | return refuse(res, 413, "that form is too large"); | |
| 154 | } | |
| 155 | const form = new URLSearchParams(body.toString("utf8")); | |
| 156 | const kind = form.get("kind"); | |
| 157 | const id = form.get("id") ?? ""; | |
| 158 | if ((kind !== "mute" && kind !== "ban") || !USER_ID.test(id)) return refuse(res, 400, "lift a mute or ban by user id"); | |
| 159 | const name = kind === "mute" ? lobby.chat.muted.get(id) : lobby.accounts.bans().find((b) => b.id === id)?.name; | |
| 160 | if (name === undefined) return refuse(res, 404, `that ${kind} is not in force`); | |
| 161 | if (!who.owner && lobby.admin.isModerator(name)) return refuse(res, 403, `only the owner can lift a ${kind} on a moderator`); | |
| 162 | if (kind === "mute") lobby.chat.unmute(id); | |
| 163 | else lobby.accounts.unban(id); | |
| 164 | lobby.admin.record(who, `${kind === "mute" ? "unmuted" : "unbanned"} ${name}`); | |
| 165 | res.writeHead(303, { location: "/admin", "cache-control": "no-store" }); | |
| 166 | res.end(); | |
| 167 | } |
Software/so101/src/relay.ts +2 −1
| @@ -66,7 +66,7 @@ import { acceptsGzip, GzipCache, worthCompressing } from "./compress.ts"; | ||
| 66 | 66 | import { AccountError, sessionCookie, visitor } from "./accounts.ts"; |
| 67 | 67 | import { CameraSocket } from "./camera-socket.ts"; |
| 68 | 68 | import { OFFLINE_FRAME } from "./offline-frame.ts"; |
| 69 | import { handleAdmin } from "./admin-routes.ts"; | |
| 69 | import { handleAdmin, handleLift } from "./admin-routes.ts"; | |
| 70 | 70 | import { parseFps, type FeedOptions } from "./camera-feed.ts"; |
| 71 | 71 | import { CameraError, CameraHub } from "./cameras.ts"; |
| 72 | 72 | import { VideoFeed, type VideoSources } from "./video-feed.ts"; |
| @@ -564,6 +564,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 564 | 564 | } |
| 565 | 565 | const cameraAdmin = ADMIN_CAMERA.exec(url.pathname); |
| 566 | 566 | if (cameraAdmin) return await administer(req, res, cameraAdmin[1] === undefined ? null : Number(cameraAdmin[1])); |
| 567 | if (lobby && url.pathname === "/admin/lift") return await handleLift(lobby, req, res, readBody); | |
| 567 | 568 | if (lobby && (url.pathname === "/admin" || url.pathname === "/admin/health")) return await handleAdmin(lobby, req, res, url, readBody); |
| 568 | 569 | if (url.pathname === LEADER) return await leaderCalibration(req, res); |
| 569 | 570 | if (url.pathname === "/join") { |
new above
offline
relay.ts and admin-routes.ts now import each other. It works at call time, but move sameOrigin into its own small module to break the cycle.