Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

A moderator who bans someone on the same address bars themselves #334

Parked opened this
Closed without merging

smith/arm:smith/209-a-moderator-who-bans-someone-on-the-same → main 5 commits fork 5 threads · 5 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +41 −0
Software/so101/src/accounts.ts +5
@@ -171,6 +171,11 @@ export class Accounts {
171171 return Boolean(this.db.query("SELECT 1 FROM bans WHERE id = ? OR key = ? OR ip = ? LIMIT 1").get(id, name?.toLowerCase() ?? "\0", ip || "\0"));
172172 }
173173
174 /** The address an account was last seen at, or "" when unknown. */
175 addressOf(id: string): string {
176 return this.db.query<{ ip: string }, [string]>("SELECT ip FROM users WHERE id = ?").get(id)?.ip ?? "";
177 }
178
174179 /** Bar a person: their account, their name, and the address and browser they were last seen with. */
175180 ban(who: Person, seen: Pick<Visitor, "ip" | "ua"> | null, by: string): Ban {
176181 const row = this.db.query<UserRow, [string]>("SELECT id, name, ip, ua FROM users WHERE id = ?").get(who.id);
Software/so101/src/lobby.test.ts +30
@@ -488,6 +488,25 @@ describe("lobby", () => {
488488 expect((await alice.next((m) => m.type === "error")).error).toBe("only the owner can do that");
489489 });
490490
491 test("a moderator cannot ban someone on their own address; no ban row is written; another address works", async () => {
492 const admin = new Admin();
493 admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["bob", "carol"] });
494 const port = await start(undefined, undefined, admin);
495 const troll = await enter(port, "troll", "203.0.113.66");
496 const bob = await join(port, { ...BOB, "x-forwarded-for": "203.0.113.66" });
497 const carol = await join(port, { ...CAROL, "x-forwarded-for": "192.0.2.77" });
498 const room = await join(port, troll.as);
499 await room.first((m) => m.type === "hello");
500
501 bob.send({ type: "ban", id: troll.body.you.id, on: true });
502 expect((await bob.next((m) => m.type === "error")).error).toContain("would bar you");
503 expect(lobby!.accounts.bans()).toEqual([]);
504
505 carol.send({ type: "ban", id: troll.body.you.id, on: true });
506 expect((await room.next((m) => m.type === "hello")).banned).toBe(true);
507 expect(lobby!.accounts.bans()).toEqual([expect.objectContaining({ name: "troll", by: "carol" })]);
508 });
509
491510 test("report: a signed-in user reports a line; only they hear it, owner and moderators get the list, a guest and a banned user are refused", async () => {
492511 const admin = new Admin();
493512 admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["bob"] });
@@ -792,6 +811,17 @@ describe("lobby", () => {
792811 expect((await enter(port, "another", "203.0.113.66")).status).toBe(200);
793812 });
794813
814 test("a ban that would cover the banner's own address is refused; the owner is never covered", () => {
815 const lobby = new Lobby({ identityKey: KEY, turns: new Turns(), chat: new Chat(), admin: new Admin(), accounts: new Accounts() });
816 const mod = { id: "u-bob", name: "bob", owner: false };
817 const here = { ip: "203.0.113.7", ua: "x", token: null } as any;
818 expect(lobby.banCoversBanner(mod, here, "203.0.113.7")).toBe(true);
819 expect(lobby.banCoversBanner(mod, here, "198.51.100.1")).toBe(false);
820 expect(lobby.banCoversBanner(mod, here, "")).toBe(false);
821 expect(lobby.banCoversBanner({ ...mod, owner: true }, here, "203.0.113.7")).toBe(false);
822 lobby.close();
823 });
824
795825 test("ban: the driver's turn ends at once, the next in line drives, and the banned person is out of the queue and refused", async () => {
796826 const port = await start();
797827 const alice = await join(port, ALICE);
Software/so101/src/lobby.ts +6
@@ -171,6 +171,11 @@ export class Lobby {
171171 return !who.owner && !this.local && this.accounts.banned(who.id, who.name, from?.ip ?? null);
172172 }
173173
174 /** Whether banning a person seen at this address would bar the banner too (the owner is never barred). */
175 banCoversBanner(banner: Person, bannerFrom: Visitor | null, targetIp: string): boolean {
176 return !banner.owner && !this.local && Boolean(targetIp) && targetIp === bannerFrom?.ip;
177 }
178
174179 /** Why this person may not command the arm right now, or null when they may (and then it counts as activity). */
175180 refusal(who: Who, req?: IncomingMessage): { status: number; error: string } | null {
176181 if (this.local) return null;
@@ -420,6 +425,7 @@ export class Lobby {
420425 if (!person) return this.send(seat, { type: "error", error: "nobody here by that id" });
421426 if (person.owner) return this.send(seat, { type: "error", error: who.owner ? "the owner cannot be banned" : refused });
422427 if (!who.owner && this.admin.isModerator(person.name)) return this.send(seat, { type: "error", error: refused });
428 if (this.banCoversBanner(who, seat.from, target?.from.ip || this.accounts.addressOf(id))) return this.send(seat, { type: "error", error: "that address is yours too: the ban would bar you" });
423429 this.accounts.ban({ id: person.id, name: person.name, owner: false }, target?.from ?? null, who.name);
424430 this.turns.end(id); // their turn ends, or they leave the line
425431 this.broadcast({ type: "said", message: this.chat.system(`${person.name} was banned`) });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs