abuse reports in the browser: notice, visitor tips, operator form and queue
A listing page says when the node hides the listing and who flagged it. A visitor can tip off the operator (proof-of-work, nothing is hidden by it); the signed-in operator can sign or withdraw a report from the page and works through the tips at #/mine/reports. e2e covers the real binaries. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -56,7 +56,7 @@ test("a first message is encrypted, mined and sent; the conversation opens", asy
|
||||
// Revisiting the listing offers the conversation instead of a second form.
|
||||
p.window.location.hash = detail;
|
||||
await p.waitFor(() => /already written/.test(text(p)), "already written note");
|
||||
assert.equal(p.document.querySelector("textarea"), null);
|
||||
assert.equal(p.document.querySelector("section.contact textarea"), null);
|
||||
} finally {
|
||||
p.close();
|
||||
}
|
||||
|
||||
+22
-1
@@ -176,6 +176,12 @@ export interface Gateway {
|
||||
mailboxes: Map<string, unknown[]>;
|
||||
/** Make POST /api/v1/messages answer with this status and error. */
|
||||
postFails?: { status: number; error: string };
|
||||
/** What GET /api/v1/reports answers; unset, the route does not exist (an older node). */
|
||||
reports?: unknown;
|
||||
/** Bodies POSTed to /api/v1/signals (a visitor's tips), in order. */
|
||||
tips: string[];
|
||||
/** Make POST /api/v1/signals answer with this status and error. */
|
||||
tipFails?: { status: number; error: string };
|
||||
/** What GET /api/v1/messages/required answers (bits); unset, the route does not exist (an older node). */
|
||||
requiredBits?: number;
|
||||
/** Refuse the next POST once with 429, asking for this many bits of proof-of-work. */
|
||||
@@ -209,6 +215,7 @@ export async function startGateway(opts: GatewayOptions): Promise<Gateway> {
|
||||
queries,
|
||||
posted,
|
||||
mailboxes,
|
||||
tips: [],
|
||||
close: () => new Promise((ok) => server.close(() => ok())),
|
||||
};
|
||||
const server: Server = createServer((req, res) => {
|
||||
@@ -222,9 +229,23 @@ export async function startGateway(opts: GatewayOptions): Promise<Gateway> {
|
||||
res.writeHead(200, { "Content-Type": "text/tab-separated-values" });
|
||||
return void res.end(readFileSync(new URL("../static/places.tsv", import.meta.url)));
|
||||
}
|
||||
if (url.pathname === "/api/v1/reports" && req.method === "GET") {
|
||||
return gateway.reports === undefined ? json(404, { error: "not found" }) : json(200, gateway.reports);
|
||||
}
|
||||
if (url.pathname === "/api/v1/signals" && req.method === "POST") {
|
||||
let body = "";
|
||||
req.on("data", (c) => (body += c));
|
||||
req.on("end", () => {
|
||||
if (gateway.tipFails) return json(gateway.tipFails.status, { error: gateway.tipFails.error });
|
||||
gateway.tips.push(body);
|
||||
res.writeHead(202);
|
||||
res.end();
|
||||
});
|
||||
return;
|
||||
}
|
||||
// The owner's token-protected routes are played by the test, if it sets `owner`.
|
||||
const writesListing = req.method !== "GET" && /^\/api\/v1\/listings(\/[0-9a-f]{32})?$/.test(url.pathname);
|
||||
if (writesListing || /^\/api\/v1\/(mine|inbox|regions|blocked)|^\/api\/v1\/listings\/[0-9a-f]{32}\/address$/.test(url.pathname)) {
|
||||
if (writesListing || /^\/api\/v1\/(mine|inbox|regions|blocked|signals|reports)|^\/api\/v1\/listings\/[0-9a-f]{32}\/address$/.test(url.pathname)) {
|
||||
let body = "";
|
||||
req.on("data", (c) => (body += c));
|
||||
req.on("end", () => {
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { createHash } from "node:crypto";
|
||||
import { afterEach, beforeEach, test } from "node:test";
|
||||
|
||||
import { canonicalize } from "../src/jcs.ts";
|
||||
import { NOW, newParty, openPage, startGateway, type Gateway, type Page } from "./helpers.ts";
|
||||
|
||||
const TOKEN = "cd34".repeat(16);
|
||||
const seller = newParty({ title: "Altbau in Ottensen" });
|
||||
const ref = { author: seller.author, id: seller.listing.id };
|
||||
const detail = `#/l/${ref.author}/${ref.id}`;
|
||||
|
||||
let gw: Gateway;
|
||||
let page: Page | undefined;
|
||||
let calls: { method: string; path: string; body: string; auth: string }[];
|
||||
|
||||
beforeEach(async () => {
|
||||
gw = await startGateway({ listings: [seller.listing] });
|
||||
calls = [];
|
||||
});
|
||||
afterEach(async () => {
|
||||
page?.close();
|
||||
page = undefined;
|
||||
await gw.close();
|
||||
});
|
||||
|
||||
const open = (hash: string) => (page = openPage(gw.url, hash));
|
||||
const text = () => page!.document.body.textContent ?? "";
|
||||
const qa = (sel: string) => [...page!.document.querySelectorAll(sel)];
|
||||
const button = (label: string) => qa("button").find((b) => b.textContent === label) as HTMLButtonElement | undefined;
|
||||
|
||||
/** Fills the first form of the report panel (the visitor's) and submits it. */
|
||||
function tipOff(category: string, note: string): void {
|
||||
const form = page!.document.querySelector("section.report details.report-form form")!;
|
||||
(form.querySelector("select") as HTMLSelectElement).value = category;
|
||||
(form.querySelector("textarea") as HTMLTextAreaElement).value = note;
|
||||
form.dispatchEvent(new page!.window.Event("submit", { cancelable: true }));
|
||||
}
|
||||
|
||||
const zeroBits = (s: string): number => {
|
||||
let bits = 0;
|
||||
for (const byte of createHash("sha256").update(s).digest()) {
|
||||
if (byte === 0) {
|
||||
bits += 8;
|
||||
continue;
|
||||
}
|
||||
return bits + Math.clz32(byte) - 24;
|
||||
}
|
||||
return bits;
|
||||
};
|
||||
|
||||
test("a listing the node hides says so, and who flagged it", async () => {
|
||||
gw.reports = { hidden: true, trusted: { scam: 2, illegal: 1 }, others: 3, signals: 0 };
|
||||
open(detail);
|
||||
await page!.waitFor(() => /hidden from search/.test(text()), "the notice");
|
||||
assert.match(text(), /scam \(2\)/);
|
||||
assert.match(text(), /illegal content \(1\)/);
|
||||
assert.match(text(), /3 more reports/);
|
||||
assert.match(text(), /Altbau in Ottensen/, "the listing itself is still shown");
|
||||
});
|
||||
|
||||
test("a listing without reports shows no notice, and an older node without the route still works", async () => {
|
||||
gw.reports = { hidden: false, trusted: {}, others: 0, signals: 0 };
|
||||
open(detail);
|
||||
await page!.waitFor(() => button("Send report") !== undefined, "the report form");
|
||||
assert.doesNotMatch(text(), /hidden from search/);
|
||||
page!.close();
|
||||
|
||||
gw.reports = undefined; // 404
|
||||
open(detail);
|
||||
await page!.waitFor(() => button("Send report") !== undefined, "the report form without a summary");
|
||||
});
|
||||
|
||||
test("a visitor's tip is mined, canonical and sent to the node, and nothing else happens", async () => {
|
||||
open(detail);
|
||||
await page!.waitFor(() => button("Send report") !== undefined, "the report form");
|
||||
tipOff("scam", "Will Vorkasse nach Spanien");
|
||||
await page!.waitFor(() => gw.tips.length === 1, "the tip to arrive");
|
||||
|
||||
const body = gw.tips[0]!;
|
||||
const sent = JSON.parse(body) as Record<string, unknown>;
|
||||
assert.equal(body, canonicalize(sent), "the node only accepts the canonical form");
|
||||
assert.deepEqual(sent.listing, ref);
|
||||
assert.equal(sent.category, "scam");
|
||||
assert.equal(sent.note, "Will Vorkasse nach Spanien");
|
||||
assert.equal(sent.v, 1);
|
||||
assert.ok(zeroBits(body) >= 6, "it did the proof-of-work the node asked for");
|
||||
assert.ok(Math.abs((sent.created_at as number) - NOW) < 60, "dated by the page's clock");
|
||||
|
||||
await page!.waitFor(() => /Thank you/.test(text()), "thanks");
|
||||
assert.equal(button("Send report"), undefined, "the form is gone once it was sent");
|
||||
});
|
||||
|
||||
test("a refused tip is explained and the text is kept", async () => {
|
||||
gw.tipFails = { status: 429, error: "too many requests from this address; try again in a minute" };
|
||||
open(detail);
|
||||
await page!.waitFor(() => button("Send report") !== undefined, "the report form");
|
||||
tipOff("other", "Sieht komisch aus");
|
||||
await page!.waitFor(() => /too many requests/.test(text()), "the node's explanation");
|
||||
assert.equal(button("Send report")!.disabled, false, "the visitor can try again");
|
||||
assert.equal(page!.document.querySelector<HTMLTextAreaElement>("section.report textarea")!.value, "Sieht komisch aus");
|
||||
});
|
||||
|
||||
test("only the signed-in operator gets the form that signs a report", async () => {
|
||||
open(detail);
|
||||
await page!.waitFor(() => button("Send report") !== undefined, "the report form");
|
||||
assert.doesNotMatch(text(), /Report as operator/, "a visitor must not be offered it");
|
||||
page!.close();
|
||||
|
||||
gw.owner = (r) => {
|
||||
calls.push({ method: r.method, path: r.path, body: r.body, auth: r.auth });
|
||||
return r.method === "POST" && r.path === "/api/v1/reports" ? { status: 200, body: {} } : undefined;
|
||||
};
|
||||
open(`#/login/${TOKEN}`);
|
||||
await page!.waitFor(() => page!.window.location.hash === "#/mine", "signed in");
|
||||
page!.window.location.hash = detail;
|
||||
await page!.waitFor(() => /Report as operator/.test(text()), "the operator's form");
|
||||
|
||||
const form = qa("section.report details.report-form form")[1]!;
|
||||
(form.querySelector("select") as HTMLSelectElement).value = "illegal";
|
||||
(form.querySelector("textarea") as HTMLTextAreaElement).value = "bestätigt";
|
||||
form.dispatchEvent(new page!.window.Event("submit", { cancelable: true }));
|
||||
await page!.waitFor(() => calls.some((c) => c.method === "POST" && c.path === "/api/v1/reports"), "the report");
|
||||
|
||||
const post = calls.find((c) => c.method === "POST" && c.path === "/api/v1/reports")!;
|
||||
assert.equal(post.auth, `Bearer ${TOKEN}`);
|
||||
assert.deepEqual(JSON.parse(post.body), { listing: ref, category: "illegal", note: "bestätigt" });
|
||||
|
||||
button("Withdraw my report")!.click();
|
||||
await page!.waitFor(() => calls.filter((c) => c.method === "POST" && c.path === "/api/v1/reports").length === 2, "the withdrawal");
|
||||
assert.equal(JSON.parse(calls.filter((c) => c.method === "POST")[1]!.body).category, "withdrawn");
|
||||
});
|
||||
|
||||
test("the operator's queue lists tips by listing; they can be turned into a report or dismissed", async () => {
|
||||
const tips = [
|
||||
{ id: 1, author: ref.author, listing: ref.id, title: "Altbau in Ottensen", category: "scam", note: "Vorkasse", created_at: 1, work: 19 },
|
||||
{ id: 2, author: ref.author, listing: ref.id, title: "Altbau in Ottensen", category: "other", note: "", created_at: 2, work: 14 },
|
||||
];
|
||||
gw.owner = (r) => {
|
||||
calls.push({ method: r.method, path: r.path, body: r.body, auth: r.auth });
|
||||
if (r.method === "GET" && r.path === "/api/v1/signals") return { status: 200, body: tips };
|
||||
if (r.method === "POST" && r.path === "/api/v1/reports") return { status: 200, body: {} };
|
||||
if (r.method === "DELETE") return { status: 204 };
|
||||
return undefined;
|
||||
};
|
||||
open(`#/login/${TOKEN}`);
|
||||
await page!.waitFor(() => page!.window.location.hash === "#/mine", "signed in");
|
||||
page!.window.location.hash = "#/mine/reports";
|
||||
await page!.waitFor(() => /Vorkasse/.test(text()), "the tips");
|
||||
assert.equal(qa("section.panel").length, 1, "two tips about one listing make one card");
|
||||
assert.match(text(), /Effort 19 bits/);
|
||||
assert.match(text(), /Altbau in Ottensen/);
|
||||
|
||||
button("Report as “scam”")!.click();
|
||||
await page!.waitFor(() => calls.some((c) => c.method === "POST" && c.path === "/api/v1/reports"), "the report");
|
||||
assert.deepEqual(JSON.parse(calls.find((c) => c.path === "/api/v1/reports")!.body), {
|
||||
listing: ref, category: "scam", note: "Vorkasse",
|
||||
});
|
||||
|
||||
button("Dismiss")!.click();
|
||||
await page!.waitFor(() => calls.some((c) => c.method === "DELETE"), "the dismissal");
|
||||
assert.equal(calls.find((c) => c.method === "DELETE")!.path, `/api/v1/signals/${ref.author}/${ref.id}`);
|
||||
});
|
||||
|
||||
test("without signing in the queue leads to the sign-in", async () => {
|
||||
open("#/mine/reports");
|
||||
await page!.waitFor(() => /sign in|Sign in|token/i.test(text()), "the sign-in");
|
||||
});
|
||||
Reference in New Issue
Block a user