Files

239 lines
10 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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();
}
});
const SHARE = "/ip4/8.8.8.8/tcp/4001/p2p/12D3KooWTest";
test("the invite page shows the command to join through this node", async () => {
const g = await startGateway({ listings: [], share: [SHARE, "/ip4/1.1.1.1/tcp/4001/p2p/12D3KooWRelay/p2p-circuit/p2p/12D3KooWTest"] });
try {
const page = openPage(g.url, "#/invite");
await page.waitFor(() => page.document.querySelector(".invite-command") !== null, "the command");
assert.equal(page.document.querySelector(".invite-command")!.textContent, `allmende run --bootstrap ${SHARE}`);
assert.match(page.document.querySelector("ul")!.textContent!, /p2p-circuit/, "the second address is listed");
page.close();
} finally {
await g.close();
}
});
test("the invite page says so when the node has no address to share, and for an older node", async () => {
for (const share of [[], undefined]) {
const g = await startGateway({ listings: [], ...(share ? { share } : {}) });
try {
const page = openPage(g.url, "#/invite", "de");
await page.waitFor(() => /noch keine Adresse/.test(page.document.body.textContent!), "the explanation");
assert.equal(page.document.querySelector(".invite-command"), null);
page.close();
} finally {
await g.close();
}
}
});
test("an address in the invite page is shown as text, never as HTML", async () => {
const g = await startGateway({ listings: [], share: [SHARE, `<img src=x onerror="window.__pwned=3">`] });
try {
const page = openPage(g.url, "#/invite");
await page.waitFor(() => page.document.querySelector("ul code") !== null, "the list");
assert.equal(page.document.querySelector("ul img"), null);
assert.equal((page.window as unknown as { __pwned?: number }).__pwned, undefined);
page.close();
} finally {
await g.close();
}
});