askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
reader.test.ts · 10.1 KB · 191 lines
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { |
| 3 | LIMITS, |
| 4 | blocksToText, |
| 5 | buildReaderHtml, |
| 6 | escapeHtml, |
| 7 | footFor, |
| 8 | readerDataUrl, |
| 9 | safeHttpUrl, |
| 10 | tldrUpdateScript, |
| 11 | validateExtraction, |
| 12 | type Extracted, |
| 13 | } from "../src/reader.js"; |
| 14 | |
| 15 | const base = (blocks: unknown[], extra: Record<string, unknown> = {}) => ({ mode: "article", url: "https://example.com/post", title: "A Post", byline: "Ann", siteName: "Example", lang: "en", blocks, ...extra }); |
| 16 | const P = (x: string) => ({ t: "p", runs: [{ x }] }); |
| 17 | |
| 18 | describe("safeHttpUrl", () => { |
| 19 | it("allows plain http(s) only", () => { |
| 20 | expect(safeHttpUrl("https://a.test/x?y=1")).toBe("https://a.test/x?y=1"); |
| 21 | for (const bad of ["javascript:alert(1)", "data:text/html,<script>", "file:///etc/passwd", "blob:https://a.test/x", "//a.test", "https://u:p@a.test/", "", null, 5]) { |
| 22 | expect(safeHttpUrl(bad)).toBeNull(); |
| 23 | } |
| 24 | }); |
| 25 | }); |
| 26 | |
| 27 | describe("validateExtraction — everything from the page is untrusted", () => { |
| 28 | it("accepts well-formed data and normalizes it", () => { |
| 29 | const ex = validateExtraction(base([{ t: "h", level: 1, runs: [{ x: "Heading" }] }, P("Hello"), { t: "ul", items: [[{ x: "a" }], [{ x: "b" }]] }]))!; |
| 30 | expect(ex.blocks).toHaveLength(3); |
| 31 | expect(ex.blocks[0]).toMatchObject({ t: "h", level: 2 }); // the title is the only h1 |
| 32 | expect(ex.title).toBe("A Post"); |
| 33 | }); |
| 34 | it("drops unknown block types, raw HTML fields, and non-http links/images", () => { |
| 35 | const ex = validateExtraction( |
| 36 | base([ |
| 37 | { t: "script", text: "alert(1)" }, |
| 38 | { t: "html", html: "<img src=x onerror=alert(1)>" }, |
| 39 | { t: "p", runs: [{ x: "link", href: "javascript:alert(1)" }, { x: "ok", href: "https://ok.test/" }], html: "<b>nope</b>" }, |
| 40 | { t: "img", src: "javascript:alert(1)", alt: "x" }, |
| 41 | { t: "img", src: "data:image/svg+xml;base64,PHN2Zz4=", alt: "x" }, |
| 42 | { t: "img", src: "https://img.test/a.png", alt: "fine" }, |
| 43 | ]), |
| 44 | )!; |
| 45 | expect(ex.blocks.map((b) => b.t)).toEqual(["p", "img"]); |
| 46 | const p = ex.blocks[0] as { runs: Array<{ x: string; href?: string }> }; |
| 47 | expect(p.runs[0]!.href).toBeUndefined(); |
| 48 | expect(p.runs[1]!.href).toBe("https://ok.test/"); |
| 49 | expect(JSON.stringify(ex)).not.toContain("onerror"); |
| 50 | expect(JSON.stringify(ex)).not.toContain("<b>"); |
| 51 | }); |
| 52 | it("rejects non-extractions and pages with no usable content", () => { |
| 53 | expect(validateExtraction(null)).toBeNull(); |
| 54 | expect(validateExtraction("x")).toBeNull(); |
| 55 | expect(validateExtraction(base([], {}))).toBeNull(); |
| 56 | expect(validateExtraction(base([P("x")], { url: "javascript:alert(1)" }))).toBeNull(); |
| 57 | expect(validateExtraction({ ...base([P("x")]), blocks: "nope" })).toBeNull(); |
| 58 | }); |
| 59 | it("enforces size limits", () => { |
| 60 | const many = Array.from({ length: LIMITS.blocks + 500 }, (_, i) => P(`p${i}`)); |
| 61 | expect(validateExtraction(base(many))!.blocks.length).toBeLessThanOrEqual(LIMITS.blocks); |
| 62 | const huge = validateExtraction(base([P("x".repeat(LIMITS.runChars * 3))]))!; |
| 63 | expect((huge.blocks[0] as { runs: Array<{ x: string }> }).runs[0]!.x.length).toBe(LIMITS.runChars); |
| 64 | expect(validateExtraction(base([P("x")], { title: "t".repeat(5000) }))!.title.length).toBeLessThanOrEqual(LIMITS.title); |
| 65 | }); |
| 66 | it("falls back to the host when the title is empty, and to 'en' for a bogus lang", () => { |
| 67 | const ex = validateExtraction(base([P("x")], { title: " ", lang: "<script>" }))!; |
| 68 | expect(ex.title).toBe("example.com"); |
| 69 | expect(ex.lang).toBe("en"); |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | describe("buildReaderHtml — the page is written from scratch and is script-free", () => { |
| 74 | const hostile = validateExtraction( |
| 75 | base( |
| 76 | [ |
| 77 | P(`</p><script>alert("xss")</script><img src=x onerror=alert(1)>`), |
| 78 | { t: "h", level: 2, runs: [{ x: `"><svg onload=alert(1)>` }] }, |
| 79 | { t: "pre", text: "<script>alert(1)</script>" }, |
| 80 | { t: "img", src: `https://img.test/a.png" onerror="alert(1)`, alt: `"><script>x</script>` }, |
| 81 | { t: "p", runs: [{ x: "x", href: `https://ok.test/?a="b"&c=<d>` }] }, |
| 82 | ], |
| 83 | { title: `</title><script>alert("t")</script>`, byline: `<b>by</b>`, siteName: "<i>s</i>" }, |
| 84 | ), |
| 85 | )!; |
| 86 | const html = buildReaderHtml(hostile, { ai: { state: "pending", provider: "Anthropic", model: "claude-x" } }); |
| 87 | |
| 88 | it("contains no executable markup from the page", () => { |
| 89 | expect(html).not.toMatch(/<script/i); |
| 90 | expect(html).not.toMatch(/<svg/i); |
| 91 | // No real TAG may carry an event-handler attribute (the same words appear as escaped, inert text). |
| 92 | expect(html).not.toMatch(/<[a-z][^>]*\son[a-z]+\s*=/i); |
| 93 | expect(html).not.toContain('onerror="alert'); |
| 94 | }); |
| 95 | it("escapes text, attributes and the title", () => { |
| 96 | expect(html).toContain("<script>alert("xss")</script>"); |
| 97 | expect(html).toContain("<title></title><script>"); |
| 98 | expect(html).toContain("<b>by</b>"); |
| 99 | }); |
| 100 | it("sends a strict CSP and no referrer", () => { |
| 101 | expect(html).toMatch(/Content-Security-Policy" content="default-src 'none'/); |
| 102 | expect(html).not.toMatch(/script-src/); |
| 103 | expect(html).toContain('name="referrer" content="no-referrer"'); |
| 104 | expect(html).toContain("form-action 'none'"); |
| 105 | }); |
| 106 | it("shows the pending skeleton with the provider, and the other states as plain notes", () => { |
| 107 | expect(html).toContain('class="tldr pending"'); |
| 108 | expect(html).toContain("Summarizing with Anthropic · claude-x"); |
| 109 | const none = buildReaderHtml(hostile, { ai: { state: "none" } }); |
| 110 | expect(none).toContain('class="tldr none"'); |
| 111 | expect(none).toContain("No AI summary"); |
| 112 | const declined = buildReaderHtml(hostile, { ai: { state: "declined" } }); |
| 113 | expect(declined).toContain("nothing from this page was sent"); |
| 114 | expect(buildReaderHtml(hostile, { ai: { state: "error", message: "rate limit" } })).toContain("rate limit"); |
| 115 | }); |
| 116 | it("notes when the page isn't an article", () => { |
| 117 | const t = validateExtraction(base([P("text")], { mode: "text" }))!; |
| 118 | expect(buildReaderHtml(t, { ai: { state: "none" } })).toContain("isn't an article"); |
| 119 | }); |
| 120 | it("reading time and original link are present", () => { |
| 121 | const ex = validateExtraction(base([P("word ".repeat(660))]))!; |
| 122 | const h = buildReaderHtml(ex, { ai: { state: "none" } }); |
| 123 | expect(h).toContain("3 min read"); |
| 124 | expect(h).toContain('href="https://example.com/post"'); |
| 125 | }); |
| 126 | }); |
| 127 | |
| 128 | describe("buildReaderHtml — the finished state (what Back/Forward must show)", () => { |
| 129 | const ex = validateExtraction(base([P("Body text")]))!; |
| 130 | const html = buildReaderHtml(ex, { ai: { state: "done", tldr: "The <b>summary</b>.", points: ["one & two", "<img src=x onerror=alert(1)>"], foot: "Summarized by m" } }); |
| 131 | it("renders the TL;DR, bullet points and footer in the page itself", () => { |
| 132 | expect(html).toContain('class="tldr done"'); |
| 133 | expect(html).toContain('<div id="tldr-body">The <b>summary</b>.</div>'); |
| 134 | expect(html).toContain("<li>one & two</li>"); |
| 135 | expect(html).toContain('<div id="tldr-foot" class="foot">Summarized by m</div>'); |
| 136 | }); |
| 137 | it("shows no skeleton and escapes everything the model wrote", () => { |
| 138 | expect(html).not.toContain('class="sk"'); |
| 139 | expect(html).not.toMatch(/<[a-z][^>]*\son[a-z]+\s*=/i); |
| 140 | expect(html).not.toMatch(/<script/i); |
| 141 | }); |
| 142 | it("matches what the live update script writes (same structure, so a reload looks identical)", () => { |
| 143 | expect(html).toMatch(/id="tldr-points"><li>/); |
| 144 | expect(html).toContain('id="tldr"'); |
| 145 | }); |
| 146 | }); |
| 147 | |
| 148 | describe("readerDataUrl / tldrUpdateScript", () => { |
| 149 | it("round-trips the HTML through a base64 data URL", () => { |
| 150 | const html = "<p>héllo ✓</p>"; |
| 151 | const url = readerDataUrl(html); |
| 152 | expect(url.startsWith("data:text/html;charset=utf-8;base64,")).toBe(true); |
| 153 | expect(Buffer.from(url.split(",")[1]!, "base64").toString("utf8")).toBe(html); |
| 154 | }); |
| 155 | it("the update script embeds values as string literals and writes only textContent", () => { |
| 156 | const evil = `"); alert(1); ("</script><img onerror=x>`; |
| 157 | const js = tldrUpdateScript({ state: "done", tldr: evil, points: [evil], foot: evil }); |
| 158 | expect(js).not.toContain("innerHTML"); |
| 159 | expect(js).toContain("textContent"); |
| 160 | // Executing the generated code against a tiny fake DOM proves the payload stays inert data. |
| 161 | const els: Record<string, { className: string; textContent: string; children: Array<{ textContent: string }>; appendChild(c: { textContent: string }): void }> = {}; |
| 162 | const mk = () => ({ className: "", textContent: "", children: [] as Array<{ textContent: string }>, appendChild(c: { textContent: string }) { this.children.push(c); } }); |
| 163 | for (const id of ["tldr", "tldr-body", "tldr-points", "tldr-foot"]) els[id] = mk(); |
| 164 | let alerted = false; |
| 165 | const doc = { getElementById: (id: string) => els[id], createElement: () => ({ textContent: "" }) }; |
| 166 | new Function("document", "alert", `return ${js}`)(doc, () => (alerted = true)); |
| 167 | expect(alerted).toBe(false); |
| 168 | expect(els["tldr-body"]!.textContent).toBe(evil); |
| 169 | expect(els["tldr-points"]!.children[0]!.textContent).toBe(evil); |
| 170 | expect(els["tldr"]!.className).toBe("tldr done"); |
| 171 | }); |
| 172 | }); |
| 173 | |
| 174 | describe("blocksToText / footFor / escapeHtml", () => { |
| 175 | it("renders plain text for the summarizer", () => { |
| 176 | const ex = validateExtraction(base([{ t: "h", level: 2, runs: [{ x: "Head" }] }, P("Para"), { t: "ol", items: [[{ x: "one" }], [{ x: "two" }]] }, { t: "img", src: "https://i.test/a.png", alt: "a cat" }])) as Extracted; |
| 177 | expect(blocksToText(ex.blocks)).toBe("## Head\n\nPara\n\n1. one\n2. two\n\n[image: a cat]"); |
| 178 | }); |
| 179 | it("footFor shows provider, model, time, tokens and cost only when known", () => { |
| 180 | const f = footFor({ provider: "anthropic", model: "claude-haiku-5-5", ms: 2631, usage: { inputTokens: 12517, outputTokens: 404 }, costUsd: 0.0015, truncated: true }); |
| 181 | expect(f).toContain("claude-haiku-5-5"); |
| 182 | expect(f).toContain("2.6 s"); |
| 183 | expect(f).toContain("12,517 in / 404 out"); |
| 184 | expect(f).toContain("≈$0.0015"); |
| 185 | expect(f).toContain("only the first part"); |
| 186 | expect(footFor({ provider: "openai", model: "m", ms: 1000, usage: { inputTokens: 1, outputTokens: 1 }, costUsd: null, truncated: false })).not.toContain("$"); |
| 187 | }); |
| 188 | it("escapeHtml covers the five characters", () => { |
| 189 | expect(escapeHtml(`<a href="x">&'</a>`)).toBe("<a href="x">&'</a>"); |
| 190 | }); |
| 191 | }); |