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

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

smith/arm:smith/137-anyone-who-is-not-the-owner-or-a-moderat → main 3 commits fork 1 thread · 1 unresolved

  • Merged. smith/137-anyone-who-is-not-the-owner-or-a-moderat was merged into main as 3854e9a.

Review

Sign in to review this change.
4 files changed +18 −16
Software/so101/src/admin-page.ts +4 −6
@@ -6,7 +6,7 @@
66
77 import { createHash } from "node:crypto";
88 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";
1010 import type { Snapshot } from "./turns.ts";
1111
1212 export const esc = (text: string): string => text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
@@ -84,11 +84,9 @@ ${body}
8484 </html>
8585 `;
8686
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>`;
9290 return page("Admin · SO-101", `<header><h1>Admin</h1></header><section>${what}<p><a href="/">Back to the arm</a></p></section>`);
9391 }
9492
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
4747 let base = await boot();
4848 const owner = as("trevato", "admin");
4949 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);
5154 expect((await fetch(`${base}/admin`, form(owner, { action: "mod-add", name: "Bob" }))).status).toBe(303);
5255 const seen = await fetch(`${base}/admin`, { headers: bob });
5356 expect(seen.status).toBe(200);
5457 expect(await seen.text()).toContain("read-only");
5558 expect((await fetch(`${base}/admin`, form(bob, { action: "pause" }))).status).toBe(403);
5659 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);
5861
5962 await relay!.close();
6063 lobby!.close();
@@ -63,7 +66,7 @@ test("a moderator looks at /admin and changes nothing; the owner names and remov
6366 const html = await (await fetch(`${base}/admin`, { headers: owner })).text();
6467 expect(html).toContain("moderators → bob");
6568 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);
6770 });
6871
6972 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
8891 const joined = await fetch(`${base}/join`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ name: "bob" }) });
8992 expect(joined.status).toBe(200);
9093 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);
9295 expect((await fetch(`${base}/admin`, { headers: as("bob", "user") })).status).toBe(200);
9396 expect(await (await fetch(`${base}/admin`, { headers: owner })).text()).toContain('aria-label="Remove moderator bob"');
9497 });
Software/so101/src/admin-routes.ts +4 −2
@@ -16,7 +16,7 @@ export interface AdminSession {
1616 }
1717
1818 /**
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
2020 * method by an allowed caller, and null when the response has been sent.
2121 */
2222 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
2828 const canEdit = Boolean(who?.owner);
2929 // a moderator is named by forge username and must hold that forge identity: a guest who types the same name is not them
3030 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

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.

32 res.writeHead(404, { "content-type": "application/json", "cache-control": "no-store" });
33 res.end(JSON.stringify({ error: "no such route" }));
3234 return null;
3335 }
3436 const show = (status: number, done: Done | null, error: string | null) =>
Software/so101/src/lobby.test.ts +3 −4
@@ -543,10 +543,9 @@ describe("lobby", () => {
543543 const port = await start(undefined, new Chat(), new Admin({ file }));
544544 const page = (headers: Record<string, string> = {}) => fetch(`http://127.0.0.1:${port}/admin`, { headers });
545545 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);
550549 expect((await form(port, { action: "pause" }, { ...OWNER, "sec-fetch-site": "cross-site" })).status).toBe(403);
551550
552551 const alice = await join(port, ALICE);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs