Wire format (internal/message): sealed boxes to the listing's device key (X25519 + HKDF + ChaCha20-Poly1305), padded, proof-of-work stamped, with signed owner replies and address grants. Relay validation, bounded queues, gossip on per-region message topics and sync (store, p2p). Owner side (internal/inbox, API): inbox with hidden link messages and a per-day new-thread limit, replies, grants, and a private per-listing address. Public POST/GET /api/v1/messages for buyers. Browser: noble-based sealing and reply verification checked against a Go fixture, contact form, conversation view, message list, de/en strings. Docs: PROTOCOL.md messaging section matches the implementation. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
197 lines
8.8 KiB
TypeScript
197 lines
8.8 KiB
TypeScript
import assert from "node:assert/strict";
|
||
import { readFileSync } from "node:fs";
|
||
import { after, before, test } from "node:test";
|
||
|
||
import type { Listing } from "../src/verify.ts";
|
||
import { DIFFICULTY, openPage, signListing, startGateway, type Gateway } from "./helpers.ts";
|
||
|
||
// Produced by the Go implementation; verifies at NOW-equivalent time.
|
||
const fixture = JSON.parse(
|
||
readFileSync(new URL("../../internal/listing/testdata/fixture.json", import.meta.url), "utf8"),
|
||
) as { listing: Listing };
|
||
const goSigned = fixture.listing;
|
||
|
||
const XSS_TITLE = `<img src=x onerror="window.__pwned=1">Haus`;
|
||
const XSS_DESC = `<script>window.__pwned=2</script>\nZweite Zeile`;
|
||
|
||
const tampered = { ...structuredClone(goSigned), id: "ff".repeat(16) } as Listing;
|
||
tampered.title = "Gefälschter Titel";
|
||
const hostile = signListing({ title: XSS_TITLE, description: XSS_DESC, features: ["<b>fett</b>"] });
|
||
|
||
let gw: Gateway;
|
||
before(async () => {
|
||
gw = await startGateway({ listings: [goSigned, tampered, hostile] });
|
||
});
|
||
after(async () => {
|
||
await gw.close();
|
||
});
|
||
|
||
const cardTitles = (doc: Document) =>
|
||
[...doc.querySelectorAll(".card h3")].map((e) => e.textContent);
|
||
|
||
test("search shows verified listings and hides forged ones", async () => {
|
||
const page = openPage(gw.url);
|
||
try {
|
||
await page.waitFor(() => page.document.querySelectorAll(".card").length === 2, "two cards");
|
||
const titles = cardTitles(page.document);
|
||
assert.ok(titles.includes(goSigned.title!), "the Go-signed listing is shown");
|
||
assert.ok(!titles.includes("Gefälschter Titel"), "the forged listing must not be shown");
|
||
assert.match(page.document.querySelector(".warn")!.textContent!, /1 result\(s\) hidden/);
|
||
assert.match(page.document.querySelector(".card .badge.ok")!.textContent!, /Signature verified/);
|
||
assert.match(page.document.body.textContent!, /€1,234\.50 \/ month/);
|
||
assert.match(page.document.getElementById("status")!.textContent!, /2 connected nodes/);
|
||
} finally {
|
||
page.close();
|
||
}
|
||
});
|
||
|
||
test("hostile listing text is rendered as text, never as HTML", async () => {
|
||
const page = openPage(gw.url);
|
||
try {
|
||
await page.waitFor(() => page.document.querySelectorAll(".card").length === 2, "cards");
|
||
const titles = cardTitles(page.document);
|
||
assert.ok(titles.includes(XSS_TITLE), "the markup is shown literally");
|
||
assert.equal(page.document.querySelector("img"), null, "no <img> may be created from listing text");
|
||
assert.equal((page.window as unknown as { __pwned?: number }).__pwned, undefined);
|
||
|
||
page.window.location.hash = `#/l/${hostile.author}/${hostile.id}`;
|
||
await page.waitFor(() => page.document.querySelector("h1") !== null, "detail view");
|
||
assert.equal(page.document.querySelector("h1")!.textContent, XSS_TITLE);
|
||
assert.equal(page.document.querySelector("img"), null);
|
||
assert.equal(page.document.querySelectorAll("main script").length, 0);
|
||
assert.equal(page.document.querySelector(".chips b"), null, "feature markup must stay text");
|
||
assert.equal(page.document.querySelector(".desc")!.textContent, XSS_DESC);
|
||
assert.equal((page.window as unknown as { __pwned?: number }).__pwned, undefined);
|
||
} finally {
|
||
page.close();
|
||
}
|
||
});
|
||
|
||
test("detail view shows the content and a passing verification report", async () => {
|
||
const page = openPage(gw.url, `#/l/${goSigned.author}/${goSigned.id}`);
|
||
try {
|
||
await page.waitFor(() => page.document.querySelector("h1") !== null, "detail");
|
||
const text = page.document.body.textContent!;
|
||
assert.equal(page.document.querySelector("h1")!.textContent, goSigned.title);
|
||
assert.ok(text.includes("Zeile 2 – Überraschung \u{1F3E0}"), "unicode survives");
|
||
assert.equal(page.document.querySelector(".desc")!.textContent, goSigned.description);
|
||
assert.equal(page.document.querySelectorAll(".checks li.ok").length, 6);
|
||
assert.equal(page.document.querySelectorAll(".checks li.bad").length, 0);
|
||
assert.ok(page.document.querySelector("a[href^='https://www.openstreetmap.org/']")!.getAttribute("rel")!.includes("noopener"));
|
||
assert.match(text, /Contact the owner/);
|
||
assert.ok(page.document.querySelector("form.compose textarea"), "the contact form is offered");
|
||
} finally {
|
||
page.close();
|
||
}
|
||
});
|
||
|
||
test("detail view refuses to show a forged listing", async () => {
|
||
const page = openPage(gw.url, `#/l/${tampered.author}/${tampered.id}`);
|
||
try {
|
||
await page.waitFor(() => page.document.querySelector("#app .error") !== null, "warning");
|
||
const text = page.document.body.textContent!;
|
||
assert.match(text, /could not be verified/);
|
||
assert.ok(!text.includes("Gefälschter Titel"), "forged content must not be displayed");
|
||
assert.equal(page.document.querySelector("h1"), null);
|
||
assert.ok(page.document.querySelectorAll(".checks li.bad").length >= 1);
|
||
} finally {
|
||
page.close();
|
||
}
|
||
});
|
||
|
||
test("a gateway answering with a different valid listing is caught", async () => {
|
||
const other = signListing({ title: "Andere Anzeige" });
|
||
const liar = await startGateway({ listings: [], swap: other });
|
||
try {
|
||
// Ask for goSigned, but the gateway returns `other`, which is validly signed.
|
||
const page = openPage(liar.url, `#/l/${goSigned.author}/${goSigned.id}`);
|
||
await page.waitFor(() => page.document.querySelector("#app .error") !== null, "warning");
|
||
assert.match(page.document.body.textContent!, /could not be verified/);
|
||
assert.ok(!page.document.body.textContent!.includes("Andere Anzeige"));
|
||
page.close();
|
||
} finally {
|
||
await liar.close();
|
||
}
|
||
});
|
||
|
||
test("a missing listing says so", async () => {
|
||
const page = openPage(gw.url, `#/l/${"a".repeat(64)}/${"b".repeat(32)}`);
|
||
try {
|
||
await page.waitFor(() => /does not exist/.test(page.document.body.textContent!), "not found text");
|
||
} finally {
|
||
page.close();
|
||
}
|
||
});
|
||
|
||
test("filters are translated into the API's units", async () => {
|
||
const page = openPage(gw.url);
|
||
try {
|
||
await page.waitFor(() => page.document.querySelectorAll(".card").length > 0, "initial results");
|
||
const set = (name: string, value: string) =>
|
||
((page.document.querySelector(`[name=${name}]`) as HTMLInputElement).value = value);
|
||
set("kind", "rent");
|
||
set("geohash", "U1X");
|
||
set("max_price", "1000");
|
||
set("min_area", "42.5");
|
||
set("min_rooms", "2.5"); // browsers normalise a typed "2,5" to this for type=number
|
||
gw.queries.length = 0;
|
||
page.document.querySelector("form")!.dispatchEvent(new page.window.Event("submit", { cancelable: true }));
|
||
await page.waitFor(() => gw.queries.length > 0, "a search request");
|
||
|
||
const q = new URLSearchParams(gw.queries.at(-1));
|
||
assert.equal(q.get("kind"), "rent");
|
||
assert.equal(q.get("geohash"), "u1x", "lower-cased");
|
||
assert.equal(q.get("max_price"), "100000", "euros become cents");
|
||
assert.equal(q.get("min_area"), "43", "rounded up");
|
||
assert.equal(q.get("min_rooms"), "5", "2.5 rooms is stored as 5");
|
||
assert.equal(q.get("limit"), "20");
|
||
assert.match(page.window.location.hash, /geohash=U1X|geohash=u1x/i);
|
||
} finally {
|
||
page.close();
|
||
}
|
||
});
|
||
|
||
test("an invalid area is reported without calling the API", async () => {
|
||
const before = gw.queries.length;
|
||
const page = openPage(gw.url, "#/?geohash=u1xa'%20OR%201=1");
|
||
try {
|
||
await page.waitFor(() => page.document.querySelector("#app .error") !== null, "hint");
|
||
assert.equal(gw.queries.length, before, "no request should be sent for an invalid prefix");
|
||
} finally {
|
||
page.close();
|
||
}
|
||
});
|
||
|
||
test("the page still works when the node's status is unavailable", async () => {
|
||
const down = await startGateway({ listings: [goSigned], statusFails: true });
|
||
try {
|
||
const page = openPage(down.url, "", "de");
|
||
await page.waitFor(() => /nicht erreichbar/.test(page.document.getElementById("status")!.textContent!), "status error (German)");
|
||
// With no difficulty known the verifier still checks everything else.
|
||
await page.waitFor(() => page.document.querySelectorAll(".card").length === 1, "card");
|
||
assert.match(page.document.querySelector(".badge")!.textContent!, /Signatur geprüft/);
|
||
assert.equal(page.document.documentElement.lang, "de");
|
||
page.close();
|
||
} finally {
|
||
await down.close();
|
||
}
|
||
});
|
||
|
||
test("the difficulty reported by the node is enforced as a minimum", async () => {
|
||
// A listing without any proof-of-work, from a gateway claiming difficulty 8.
|
||
const lazy = signListing({ title: "Ohne Arbeit" });
|
||
const weak = structuredClone(lazy);
|
||
assert.ok(DIFFICULTY > 0);
|
||
// Re-signing is impossible here, so simply check the real one passes and a forged PoW fails.
|
||
weak.pow += 1;
|
||
const g = await startGateway({ listings: [lazy, weak] });
|
||
try {
|
||
const page = openPage(g.url);
|
||
await page.waitFor(() => page.document.querySelectorAll(".card").length >= 1, "cards");
|
||
assert.equal(page.document.querySelectorAll(".card").length, 1, "the altered one is hidden");
|
||
page.close();
|
||
} finally {
|
||
await g.close();
|
||
}
|
||
});
|