Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Anyone who is not the owner or a moderator gets 404 on GET and POST /admin, not 401/403 #175
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/137-anyone-who-is-not-the-owner-or-a-moderat → main 3 commits fork 1 thread · 1 unresolved
4 files
changed +18
−16
Software/so101/src/admin-page.ts +4 −6
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | |
| 7 | 7 | import { createHash } from "node:crypto"; |
| 8 | 8 | import { ANNOUNCEMENT_CHARS, type AdminSettings, type LogEntry } from "./admin.ts"; |
| 9 | import type { Who } from "./identity.ts"; | |
| 9 | import type { Person, Who } from "./identity.ts"; | |
| 10 | 10 | import type { Snapshot } from "./turns.ts"; |
| 11 | 11 | |
| 12 | 12 | export const esc = (text: string): string => text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`); |
| @@ -84,11 +84,9 @@ ${body} | ||
| 84 | 84 | </html> |
| 85 | 85 | `; |
| 86 | 86 | |
| 87 | /** For anyone who is not the owner. */ | |
| 88 | export function renderRefusal(who: Who): string { | |
| 89 | const what = who | |
| 90 | ? `<p>Signed in as ${esc(who.name)}. Only the owner can change how the arm is shared.</p>` | |
| 91 | : `<p>Only the owner can change how the arm is shared.</p><p><a href="/__forge/signin?to=%2Fadmin">Sign in</a></p>`; | |
| 87 | /** For a signed-in moderator who tried to change something: the page is theirs to see, not to edit. */ | |
| 88 | export function renderRefusal(who: Person): string { | |
| 89 | const what = `<p>Signed in as ${esc(who.name)}. Only the owner can change how the arm is shared.</p>`; | |
| 92 | 90 | return page("Admin · SO-101", `<header><h1>Admin</h1></header><section>${what}<p><a href="/">Back to the arm</a></p></section>`); |
| 93 | 91 | } |
| 94 | 92 |
Software/so101/src/admin-relay.test.ts +7 −4
| @@ -47,14 +47,17 @@ test("a moderator looks at /admin and changes nothing; the owner names and remov | ||
| 47 | 47 | let base = await boot(); |
| 48 | 48 | const owner = as("trevato", "admin"); |
| 49 | 49 | const bob = as("bob", "user"); |
| 50 | expect((await fetch(`${base}/admin`, { headers: bob })).status).toBe(403); | |
| 50 | expect((await fetch(`${base}/admin`, { headers: bob })).status).toBe(404); | |
| 51 | expect((await fetch(`${base}/admin`)).status).toBe(404); | |
| 52 | expect((await fetch(`${base}/admin`, { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded" }, body: "action=pause" })).status).toBe(404); | |
| 53 | expect((await fetch(`${base}/admin`, form(bob, { action: "pause" }))).status).toBe(404); | |
| 51 | 54 | expect((await fetch(`${base}/admin`, form(owner, { action: "mod-add", name: "Bob" }))).status).toBe(303); |
| 52 | 55 | const seen = await fetch(`${base}/admin`, { headers: bob }); |
| 53 | 56 | expect(seen.status).toBe(200); |
| 54 | 57 | expect(await seen.text()).toContain("read-only"); |
| 55 | 58 | expect((await fetch(`${base}/admin`, form(bob, { action: "pause" }))).status).toBe(403); |
| 56 | 59 | expect((await fetch(`${base}/admin`, form(bob, { action: "mod-add", name: "eve" }))).status).toBe(403); |
| 57 | expect((await fetch(`${base}/admin`, { headers: as("eve", "user") })).status).toBe(403); | |
| 60 | expect((await fetch(`${base}/admin`, { headers: as("eve", "user") })).status).toBe(404); | |
| 58 | 61 | |
| 59 | 62 | await relay!.close(); |
| 60 | 63 | lobby!.close(); |
| @@ -63,7 +66,7 @@ test("a moderator looks at /admin and changes nothing; the owner names and remov | ||
| 63 | 66 | const html = await (await fetch(`${base}/admin`, { headers: owner })).text(); |
| 64 | 67 | expect(html).toContain("moderators → bob"); |
| 65 | 68 | await fetch(`${base}/admin`, form(owner, { action: "mod-remove", name: "bob" })); |
| 66 | expect((await fetch(`${base}/admin`, { headers: bob })).status).toBe(403); | |
| 69 | expect((await fetch(`${base}/admin`, { headers: bob })).status).toBe(404); | |
| 67 | 70 | }); |
| 68 | 71 | |
| 69 | 72 | test("the arm server set on /admin is the one the relay uses, at once; a bad one is refused", async () => { |
| @@ -88,7 +91,7 @@ test("a guest who types a moderator's name is not that moderator; the Remove but | ||
| 88 | 91 | const joined = await fetch(`${base}/join`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ name: "bob" }) }); |
| 89 | 92 | expect(joined.status).toBe(200); |
| 90 | 93 | const cookie = joined.headers.get("set-cookie")!.split(";")[0]!; |
| 91 | expect((await fetch(`${base}/admin`, { headers: { cookie } })).status).toBe(403); | |
| 94 | expect((await fetch(`${base}/admin`, { headers: { cookie } })).status).toBe(404); | |
| 92 | 95 | expect((await fetch(`${base}/admin`, { headers: as("bob", "user") })).status).toBe(200); |
| 93 | 96 | expect(await (await fetch(`${base}/admin`, { headers: owner })).text()).toContain('aria-label="Remove moderator bob"'); |
| 94 | 97 | }); |
Software/so101/src/admin-routes.ts +4 −2
| @@ -16,7 +16,7 @@ export interface AdminSession { | ||
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | * Answer 401/403 to anyone not allowed, and the page itself to GET and HEAD. Returns the session for any other | |
| 19 | * Answer 404 to anyone not allowed, and the page itself to GET and HEAD. Returns the session for any other | |
| 20 | 20 | * method by an allowed caller, and null when the response has been sent. |
| 21 | 21 | */ |
| 22 | 22 | export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerResponse, url: URL): AdminSession | null { |
| @@ -28,7 +28,9 @@ export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerRe | ||
| 28 | 28 | const canEdit = Boolean(who?.owner); |
| 29 | 29 | // a moderator is named by forge username and must hold that forge identity: a guest who types the same name is not them |
| 30 | 30 | if (!who || !(canEdit || (lobby.verified(req)?.id === who.id && lobby.admin.isModerator(who.name)))) { |
| 31 | html(who ? 403 : 401, renderRefusal(who)); | |
| 31 | // the page is no secret to a moderator, but to anyone else it does not exist: the answer is the ordinary not-found one | |
| 32 | res.writeHead(404, { "content-type": "application/json", "cache-control": "no-store" }); | |
| 33 | res.end(JSON.stringify({ error: "no such route" })); | |
| 32 | 34 | return null; |
| 33 | 35 | } |
| 34 | 36 | const show = (status: number, done: Done | null, error: string | null) => |
Software/so101/src/lobby.test.ts +3 −4
| @@ -543,10 +543,9 @@ describe("lobby", () => { | ||
| 543 | 543 | const port = await start(undefined, new Chat(), new Admin({ file })); |
| 544 | 544 | const page = (headers: Record<string, string> = {}) => fetch(`http://127.0.0.1:${port}/admin`, { headers }); |
| 545 | 545 | const anonPage = await page(); |
| 546 | expect(anonPage.status).toBe(401); | |
| 547 | expect(await anonPage.text()).toContain("/__forge/signin?to=%2Fadmin"); | |
| 548 | expect((await page(ALICE)).status).toBe(403); | |
| 549 | expect((await form(port, { action: "pause" }, ALICE)).status).toBe(403); | |
| 546 | expect(anonPage.status).toBe(404); | |
| 547 | expect((await page(ALICE)).status).toBe(404); | |
| 548 | expect((await form(port, { action: "pause" }, ALICE)).status).toBe(404); | |
| 550 | 549 | expect((await form(port, { action: "pause" }, { ...OWNER, "sec-fetch-site": "cross-site" })).status).toBe(403); |
| 551 | 550 | |
| 552 | 551 | const alice = await join(port, ALICE); |
new above
offline
Doc comment above says 404 now, good; but renderRefusal(who) is still called below in refuse() with a signed-in moderator only, so its anonymous branch is dead. Remove it.