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
smith/arm:smith/209-a-moderator-who-bans-someone-on-the-same → main 5 commits fork 5 threads · 5 unresolved
3 files
changed +41
−0
Software/so101/src/accounts.ts +5
| @@ -171,6 +171,11 @@ export class Accounts { | ||
| 171 | 171 | return Boolean(this.db.query("SELECT 1 FROM bans WHERE id = ? OR key = ? OR ip = ? LIMIT 1").get(id, name?.toLowerCase() ?? "\0", ip || "\0")); |
| 172 | 172 | } |
| 173 | 173 | |
| 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 | ||
| 174 | 179 | /** Bar a person: their account, their name, and the address and browser they were last seen with. */ |
| 175 | 180 | ban(who: Person, seen: Pick<Visitor, "ip" | "ua"> | null, by: string): Ban { |
| 176 | 181 | 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", () => { | ||
| 488 | 488 | expect((await alice.next((m) => m.type === "error")).error).toBe("only the owner can do that"); |
| 489 | 489 | }); |
| 490 | 490 | |
| 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 | ||
| 491 | 510 | 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 () => { |
| 492 | 511 | const admin = new Admin(); |
| 493 | 512 | admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["bob"] }); |
| @@ -792,6 +811,17 @@ describe("lobby", () => { | ||
| 792 | 811 | expect((await enter(port, "another", "203.0.113.66")).status).toBe(200); |
| 793 | 812 | }); |
| 794 | 813 | |
| 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 | ||
| 795 | 825 | 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 () => { |
| 796 | 826 | const port = await start(); |
| 797 | 827 | const alice = await join(port, ALICE); |
Software/so101/src/lobby.ts +6
| @@ -171,6 +171,11 @@ export class Lobby { | ||
| 171 | 171 | return !who.owner && !this.local && this.accounts.banned(who.id, who.name, from?.ip ?? null); |
| 172 | 172 | } |
| 173 | 173 | |
| 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 | ||
| 174 | 179 | /** Why this person may not command the arm right now, or null when they may (and then it counts as activity). */ |
| 175 | 180 | refusal(who: Who, req?: IncomingMessage): { status: number; error: string } | null { |
| 176 | 181 | if (this.local) return null; |
| @@ -420,6 +425,7 @@ export class Lobby { | ||
| 420 | 425 | if (!person) return this.send(seat, { type: "error", error: "nobody here by that id" }); |
| 421 | 426 | if (person.owner) return this.send(seat, { type: "error", error: who.owner ? "the owner cannot be banned" : refused }); |
| 422 | 427 | 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" }); | |
| 423 | 429 | this.accounts.ban({ id: person.id, name: person.name, owner: false }, target?.from ?? null, who.name); |
| 424 | 430 | this.turns.end(id); // their turn ends, or they leave the line |
| 425 | 431 | this.broadcast({ type: "said", message: this.chat.system(`${person.name} was banned`) }); |
new above
offline