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

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

Chat refuses links (unless the host is allowed), shouting and a repeated line, each with a short reason to the sender only #340

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/241-chat-refuses-links-unless-the-host-is-al → main 4 commits fork 2 threads · 2 unresolved

  • Merged. smith/241-chat-refuses-links-unless-the-host-is-al was merged into main as eca73cb.

Review

Sign in to review this change.
5 files changed +188 −0
Software/README.md +10
@@ -482,6 +482,16 @@ its live pose and the cameras. Driving needs a name and a turn.
482482 phrases; a chat line holding one whole is refused with an error and never
483483 posted. Case, accents and look-alike digits (`h3ck`) are folded; the owner is
484484 not held to it. The list is in `words.json` beside the arm's data.
485 - Three more chat rules refuse a line before it is posted, with a short reason
486 told to the sender only (nothing is broadcast): a **link** whose host (or a
487 parent domain of it) is not on the owner's allowlist ("links are only allowed
488 to hosts the owner has approved"); **shouting**, a line of 12 or more letters
489 that is over 70 % capitals ("please do not shout"); and the same person's
490 **identical line within 60 s** ("you just said that"). The owner is exempt.
491 The owner sets the allowlist over `/live` with
492 `{"type":"hosts","hosts":["example.org"]}`: hostnames only, at most 100, each
493 at most 100 characters, otherwise the owner gets an error. It is kept in
494 `hosts.json` beside `words.json`. There is no admin-page control for it yet.
485495 - A ban bars the person's account, their name and the address they were
486496 last seen at: their turn ends, they leave the queue, and they cannot
487497 chat, queue or make a new name from that address (or from the browser
Software/so101/src/chat-rules.test.ts added +50
@@ -0,0 +1,50 @@
1 import { describe, expect, test } from "bun:test";
2 import { mkdtempSync } from "node:fs";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { ChatRules } from "./chat-rules.ts";
6
7 describe("chat rules", () => {
8 test("links: refused unless the host (or a parent) is allowed", () => {
9 const r = new ChatRules();
10 for (const t of ["see http://evil.com/x", "go to www.evil.com", "evil.example.org now", "https://evil.com:8080"]) expect(() => r.check("a", t)).toThrow(/links/);
11 r.set(["Example.org"]);
12 expect(() => r.check("a", "see https://docs.example.org/a?b=1")).not.toThrow();
13 expect(() => r.check("a", "example.org.")).not.toThrow();
14 expect(() => r.check("a", "https://notexample.org")).toThrow(/links/);
15 expect(() => r.check("a", "Hi. Nice arm.")).not.toThrow();
16 });
17
18 test("shouting: 12+ letters over 70 % capitals; OK and short lines pass", () => {
19 const r = new ChatRules();
20 expect(() => r.check("a", "STOP DOING THAT RIGHT NOW")).toThrow(/shout/);
21 for (const [i, t] of ["OK", "HELLO THERE", "Hello There my friend", "WOW 1234567890 ok"].entries()) expect(() => r.check(`u${i}`, t)).not.toThrow();
22 });
23
24 test("repeat: the same sender's identical line within 60 s", () => {
25 const r = new ChatRules();
26 r.check("a", "hello", 1000);
27 expect(() => r.check("a", " Hello ", 30_000)).toThrow(/said that/);
28 expect(() => r.check("b", "hello", 30_000)).not.toThrow();
29 expect(() => r.check("a", "hello", 61_001)).not.toThrow();
30 });
31
32 test("a refused line is not remembered as said", () => {
33 const r = new ChatRules();
34 expect(() => r.check("a", "http://x.com", 0)).toThrow();
35 expect(() => r.check("a", "http://x.com", 1)).toThrow(/links/);
36 });
37
38 test("the allowlist is validated and survives a reload", () => {
39 const r = new ChatRules();
40 expect(() => r.set("a.com")).toThrow(/list/);
41 expect(() => r.set([1])).toThrow(/text/);
42 expect(() => r.set(["not a host"])).toThrow(/hostname/);
43 expect(() => r.set(["https://a.com"])).toThrow(/hostname/);
44 expect(() => r.set(Array(101).fill("a.com"))).toThrow(/100/);
45 expect(() => r.set(["a".repeat(101)])).toThrow(/100/);
46 const file = join(mkdtempSync(join(tmpdir(), "so101-hosts-")), "hosts.json");
47 expect(new ChatRules(file).set(["A.com", "a.com", ""])).toEqual(["a.com"]);
48 expect(new ChatRules(file).hosts).toEqual(["a.com"]);
49 });
50 });
Software/so101/src/chat-rules.ts added +82
@@ -0,0 +1,82 @@
1 /**
2 * The chat's other house rules, checked before a line is posted: no link unless its host is on the owner's
3 * allowlist, no shouting (12 or more letters, over 70 % capitals), and no repeating your own line within 60 s.
4 * A refusal is a ChatError, which only the sender is told. The allowlist is one JSON file beside the word list.
5 */
6
7 import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
8 import { dirname } from "node:path";
9 import { ChatError } from "./chat.ts";
10
11 export const MAX_HOSTS = 100;
12 export const MAX_HOST_CHARS = 100;
13 export const SHOUT_MIN_LETTERS = 12;
14 export const SHOUT_RATIO = 0.7;
15 export const REPEAT_MS = 60_000;
16 const HOST = /^(?=.{1,100}$)[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/;
17 const LINK = /(?:\b[a-z][a-z0-9+.-]*:\/\/|\bwww\.)([^\s/?#:]+)|\b((?:[a-z0-9-]+\.)+[a-z]{2,})(?=$|[\s/?#:.,;!)\]]|:\d)/gi;
18
19 function cleanHost(h: string): string {
20 return h.toLowerCase().replace(/\.$/, "");
21 }
22
23 export class ChatRules {
24 private list: string[] = [];
25 private readonly file: string | undefined;
26 private readonly lastSaid = new Map<string, { text: string; at: number }>();
27
28 constructor(file?: string) {
29 this.file = file;
30 if (!file) return;
31 try {
32 const stored = JSON.parse(readFileSync(file, "utf8"));
33 if (Array.isArray(stored)) this.list = stored.filter((h): h is string => typeof h === "string" && HOST.test(h)).slice(0, MAX_HOSTS);
34 } catch {
35 /* none yet, or unreadable: an empty list */
36 }
37 }
38
39 get hosts(): string[] {
40 return [...this.list];
41 }
42
43 /** Replace the allowlist. Throws ChatError unless it is a short list of hostnames. */
44 set(input: unknown): string[] {
45 if (!Array.isArray(input) || input.length > MAX_HOSTS) throw new ChatError(`the allowlist is a list of at most ${MAX_HOSTS} hosts`);
46 const seen = new Set<string>();
47 for (const h of input) {
48 if (typeof h !== "string" || h.length > MAX_HOST_CHARS) throw new ChatError(`each host is text of at most ${MAX_HOST_CHARS} characters`);
49 const host = cleanHost(h.trim());
50 if (!host) continue;
51 if (!HOST.test(host)) throw new ChatError(`"${h.slice(0, 20)}" is not a hostname`);
52 seen.add(host);
53 }
54 this.list = [...seen];
55 this.save();
56 return this.hosts;
57 }
58
59 /** Throw a ChatError with the reason if this line may not be posted by this sender; otherwise note it. */
60 check(senderId: string, text: string, now = Date.now()): void {
61 for (const m of text.matchAll(LINK)) {
62 const host = cleanHost(m[1] ?? m[2]!);
63 if (!this.list.some((a) => host === a || host.endsWith(`.${a}`))) throw new ChatError("links are only allowed to hosts the owner has approved");
64 }
65 const letters = [...text].filter((c) => c.toLowerCase() !== c.toUpperCase());
66 if (letters.length >= SHOUT_MIN_LETTERS && letters.filter((c) => c === c.toUpperCase()).length / letters.length > SHOUT_RATIO) {
67 throw new ChatError("please do not shout");
68 }

The line is remembered here, before the post succeeds. Expose a record step and call it after chat.post so a later refusal does not eat the retry.

69 const key = text.trim().replace(/\s+/g, " ").toLowerCase();
70 const last = this.lastSaid.get(senderId);
71 if (last && last.text === key && now - last.at < REPEAT_MS) throw new ChatError("you just said that");
72 this.lastSaid.set(senderId, { text: key, at: now });
73 if (this.lastSaid.size > 1000) for (const [id, v] of this.lastSaid) if (now - v.at >= REPEAT_MS) this.lastSaid.delete(id);
74 }
75
76 private save(): void {
77 if (!this.file) return;
78 mkdirSync(dirname(this.file), { recursive: true });
79 writeFileSync(`${this.file}.tmp`, JSON.stringify(this.list));
80 renameSync(`${this.file}.tmp`, this.file);
81 }
82 }
Software/so101/src/lobby.test.ts +26
@@ -735,6 +735,32 @@ describe("lobby", () => {
735735 expect((await torque(port, forged)).status).toBe(403);
736736 });
737737
738 test("chat rules: a link, shouting and a repeat are refused to the sender alone; the owner sets hosts and is exempt", async () => {
739 const port = await start();
740 const owner = await join(port, OWNER);
741 const alice = await join(port, ALICE);
742 const bob = await join(port, BOB);
743 await alice.first((m) => m.type === "hello");
744 alice.send({ type: "hosts", hosts: ["ok.org"] });
745 expect((await alice.next((m) => m.type === "error")).error).toContain("only the owner");
746 owner.send({ type: "hosts", hosts: ["not a host"] });
747 expect((await owner.next((m) => m.type === "error")).error).toContain("hostname");
748 owner.send({ type: "hosts", hosts: ["ok.org"] });
749 expect(await owner.next((m) => m.type === "hosts" && m.hosts.length === 1)).toMatchObject({ hosts: ["ok.org"] });
750 alice.send({ type: "post", text: "see http://bad.example/x" });
751 expect((await alice.next((m) => m.type === "error")).error).toContain("links");
752 alice.send({ type: "post", text: "SHOUTING AT EVERYONE HERE" });
753 expect((await alice.next((m) => m.type === "error")).error).toContain("shout");
754 alice.send({ type: "post", text: "see https://ok.org/x" });
755 await alice.next((m) => m.type === "said");
756 alice.send({ type: "post", text: "see https://ok.org/x" });
757 expect((await alice.next((m) => m.type === "error")).error).toContain("said that");
758 owner.send({ type: "post", text: "http://bad.example SHOUTING AT EVERYONE" });
759 await owner.next((m) => m.type === "said");
760 await bob.first((m) => m.type === "said" && m.message.from?.name === "trevato");
761 expect(bob.heard.some((m: any) => m.type === "said" && /bad\.example\/x|SHOUTING AT EVERYONE HERE/.test(m.message.text))).toBe(false);
762 });
763
738764 test("word filter: only the owner sets it, a listed word is refused in chat, the owner is not held to it", async () => {
739765 const port = await start();
740766 const owner = await join(port, OWNER);
Software/so101/src/lobby.ts +20
@@ -19,6 +19,7 @@
1919 * {"type": "removed"} owner: the latest removed lines, answered with {"type": "removed", "lines": [...]}
2020 * {"type": "restore", "id": 12} owner: put back a removed line, or one the crew hid
2121 * {"type": "dismiss", "id": 12} owner: dismiss a mute suggestion
22 * {"type": "hosts", "hosts": ["…"]} owner: hostnames chat may link to; any other link is refused
2223 * {"type": "ban", "id": "<user id>", "on": true|false} owner or a moderator (never the owner or a moderator): bars their account, name and address; they still watch
2324 * {"type": "words", "words": ["…"]} owner: the chat's word filter; a line with a listed word is refused
2425 * {"type": "report", "id": 12} signed in, not banned: report a chat line; answered to you alone with {"type": "reported", "id"}, a repeat too
@@ -33,6 +34,7 @@
3334 * `publish`es to everyone: the arm's status, so a page needs this one socket for the pose, the chat and the queue.
3435 */
3536
37 import { dirname, join } from "node:path";
3638 import type { IncomingMessage } from "node:http";
3739 import type { Duplex } from "node:stream";
3840 import WebSocket, { WebSocketServer } from "ws";
@@ -41,6 +43,7 @@ import { Admin, AdminError } from "./admin.ts";
4143 import { frameBytes, TOO_BIG_CLOSE } from "./frame-limit.ts";
4244 import { Chat, ChatError } from "./chat.ts";
4345 import { Moderator } from "./moderator.ts";
46 import { ChatRules } from "./chat-rules.ts";
4447 import { Reports } from "./reports.ts";
4548 import { WordFilter } from "./word-filter.ts";
4649 import { identify, LOCAL, type Person, type Who } from "./identity.ts";
@@ -63,6 +66,8 @@ export interface LobbyOptions {
6366 chatFile?: string;
6467 /** The word filter's file; none keeps it in memory. */
6568 wordsFile?: string;
69 /** The link allowlist's file; by default `hosts.json` beside the word list. */
70 hostsFile?: string;
6671 /** The chat reports' file; none keeps them in memory. */
6772 reportsFile?: string;
6873 /** The admin settings' file; none keeps them in memory. */
@@ -102,6 +107,7 @@ export class Lobby {
102107 readonly accounts: Accounts;
103108 readonly admin: Admin;
104109 readonly filter: WordFilter;
110 readonly rules: ChatRules;
105111 /** What each open page last reported about its own link. */
106112 readonly reports: ViewerReports;
107113 /** Chat lines people reported, for the owner and moderators. */
@@ -130,6 +136,7 @@ export class Lobby {
130136 this.chat = options.chat ?? new Chat({ file: options.chatFile });
131137 this.accounts = options.accounts ?? new Accounts();
132138 this.filter = new WordFilter(options.wordsFile);
139 this.rules = new ChatRules(options.hostsFile ?? (options.wordsFile ? join(dirname(options.wordsFile), "hosts.json") : undefined));
133140 this.reports = new ViewerReports(this.now);
134141 this.lineReports = new Reports(options.reportsFile);
135142 this.silentMs = options.silentMs ?? 15_000;
@@ -288,6 +295,7 @@ export class Lobby {
288295 }
289296 if (who?.owner) {
290297 this.send(seat, this.wordsList());
298 this.send(seat, this.hostsList());
291299 this.send(seat, this.adminState());
292300 this.send(seat, this.crewState());
293301 }
@@ -349,6 +357,7 @@ export class Lobby {
349357 return;
350358 case "post": {
351359 if (!who.owner && typeof m.text === "string" && this.filter.blocks(m.text)) throw new ChatError("that word is not allowed here");
360 if (!who.owner && typeof m.text === "string") this.rules.check(who.id, m.text);
352361 const said = this.chat.post(who, m.text);
353362 this.broadcast({ type: "said", message: said });
354363 void this.moderator?.check(said);
@@ -382,6 +391,13 @@ export class Lobby {
382391 for (const s of this.seats) if (s.who?.owner) this.send(s, this.wordsList());
383392 return;
384393 }
394 case "hosts": {
395 const hosts = this.rules.set(m.hosts);
396 this.admin.record(who, `link allowlist → ${hosts.length} host${hosts.length === 1 ? "" : "s"}`);
397 this.broadcastAdmin();
398 for (const s of this.seats) if (s.who?.owner) this.send(s, this.hostsList());
399 return;
400 }
385401 case "mute":
386402 return this.muteUser(seat, who, m);
387403 case "ban":
@@ -550,6 +566,10 @@ export class Lobby {
550566 return { type: "banned", banned: this.accounts.bans().map(({ id, name, at, by }) => ({ id, name, at, by })) };
551567 }
552568
569 private hostsList() {
570 return { type: "hosts", hosts: this.rules.hosts };
571 }
572
553573 private wordsList() {
554574 return { type: "words", words: this.filter.words };
555575 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs