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
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
plugins.test.ts · 15.7 KB · 270 lines
| 1 | import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; |
| 2 | import http from "node:http"; |
| 3 | import { tmpdir } from "node:os"; |
| 4 | import { join } from "node:path"; |
| 5 | import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 6 | import { gitBlobId, parsePluginSource, parseTagRefs, siteMatches, styleProblem, validateManifest } from "../src/plugins.ts"; |
| 7 | import { PluginStore, describePowers, parseListing, type Fetched } from "../src/plugin-service.ts"; |
| 8 | // @ts-expect-error: a plain .mjs test helper, shared with the demo server |
| 9 | import { fakeGitstable } from "../demo/fake-gitstable.mjs"; |
| 10 | |
| 11 | const good = { v: 1, id: "x-whole-window", name: "X uses the whole window", kind: "style-rule", version: "2026.11v1", sites: ["x.com", "twitter.com"], powers: ["style"], style: "x-whole-window.css", min_api: 1, tested_on: ["2026.11v1"], license: "MIT" }; |
| 12 | |
| 13 | describe("plugin manifest (dbbasic-rule.json)", () => { |
| 14 | it("accepts a style rule", () => { |
| 15 | const r = validateManifest(good, 1); |
| 16 | expect(r.ok && r.manifest.sites).toEqual(["x.com", "twitter.com"]); |
| 17 | }); |
| 18 | it("refuses what this browser can't honour, saying why", () => { |
| 19 | const err = (m: object, api = 1) => { const r = validateManifest({ ...good, ...m }, api); return r.ok ? "" : r.error; }; |
| 20 | expect(err({ v: 2 })).toMatch(/format/); |
| 21 | expect(err({ kind: "userscript" })).toMatch(/installs style rules and view rules, not "userscript"/); |
| 22 | expect(err({ powers: ["style", "page-script"] })).toMatch(/page-script/); |
| 23 | expect(err({ sites: [] })).toMatch(/sites/); |
| 24 | expect(err({ sites: ["https://x.com/"] })).toMatch(/not a site name/); |
| 25 | expect(err({ sites: ["*"] })).toMatch(/not a site name/); |
| 26 | expect(err({ style: "../../evil.css" })).toMatch(/\.css file/); |
| 27 | expect(err({ style: "a.js" })).toMatch(/\.css file/); |
| 28 | expect(err({ id: "Bad Id" })).toMatch(/id/); |
| 29 | expect(err({ min_api: 2 })).toMatch(/plugin level 2/); |
| 30 | expect(err({ min_api: 2 }, 2)).toBe(""); |
| 31 | }); |
| 32 | }); |
| 33 | |
| 34 | describe("style rules stay look-only", () => { |
| 35 | it("lets ordinary CSS through", () => { |
| 36 | expect(styleProblem('[data-testid="primaryColumn"] { max-width: none !important; } @media (min-width: 900px) { a { color: red } }')).toBeNull(); |
| 37 | expect(styleProblem("a { background: linear-gradient(red, blue); content: 'x' }")).toBeNull(); |
| 38 | }); |
| 39 | it("refuses anything that could fetch or hide a fetch", () => { |
| 40 | for (const css of [ |
| 41 | "a { background: url(https://evil.test/x) }", |
| 42 | "a { background: URL ( 'x' ) }", |
| 43 | "@import 'https://evil.test/a.css';", |
| 44 | "a { background-image: image-set('a.png' 1x) }", |
| 45 | "@font-face { font-family: x; src: local(x) }", |
| 46 | "a { background: \\75rl(x) }", |
| 47 | "a { -moz-binding: x }", |
| 48 | ]) expect(styleProblem(css), css).not.toBeNull(); |
| 49 | }); |
| 50 | it("sees through comments", () => { |
| 51 | expect(styleProblem("/* harmless */ a { background: url(x) }")).not.toBeNull(); |
| 52 | expect(styleProblem("/* url(x) is only mentioned */ a { color: red }")).toBeNull(); |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | describe("view rules in dbbasic-rule.json", () => { |
| 57 | const vr = (over: Record<string, unknown> = {}) => ({ v: 1, id: "cards", name: "Cards", kind: "view-rule", version: "2026.11v1", sites: ["x.com"], powers: ["read-posts"], view: "cards.json", button: { label: "Cards", icon: "cards" }, min_api: 2, ...over }); |
| 58 | it("accepts a view rule with its button", () => { |
| 59 | const r = validateManifest(vr(), 2); |
| 60 | expect(r.ok && r.manifest.kind === "view-rule" && r.manifest.button?.icon === "cards" && r.manifest.view === "cards.json").toBe(true); |
| 61 | }); |
| 62 | it("refuses a view rule asking for more, a button with its own picture, and a style rule asking to read", () => { |
| 63 | expect(validateManifest(vr({ powers: ["read-posts", "style"] }), 2)).toMatchObject({ ok: false, error: expect.stringMatching(/"read-posts" and nothing else/) }); |
| 64 | expect(validateManifest(vr({ button: { label: "Cards", icon: "https://evil.test/i.svg" } }), 2)).toMatchObject({ ok: false, error: expect.stringMatching(/icon must be one of cards, grid, list, columns/) }); |
| 65 | expect(validateManifest(vr({ button: { label: "x".repeat(30), icon: "cards" } }), 2).ok).toBe(false); |
| 66 | expect(validateManifest(vr({ view: "../cards.json" }), 2).ok).toBe(false); |
| 67 | expect(validateManifest({ v: 1, id: "w", name: "W", kind: "style-rule", version: "2026.11v1", sites: ["x.com"], powers: ["style", "read-posts"], style: "w.css" }, 2)).toMatchObject({ ok: false, error: expect.stringMatching(/style rule may only ask for "style"/) }); |
| 68 | }); |
| 69 | }); |
| 70 | |
| 71 | describe("sites, sources and tags", () => { |
| 72 | it("names the sites in plain words", () => { |
| 73 | expect(describePowers(["x.com"])).toMatch(/^It changes how x\.com looks\./); |
| 74 | expect(describePowers(["x.com", "twitter.com"])).toMatch(/^It changes how x\.com and twitter\.com look\./); |
| 75 | expect(describePowers(["a.com", "b.com", "c.com", "d.com", "e.com"])).toMatch(/a\.com, b\.com, c\.com and 2 more look\./); |
| 76 | }); |
| 77 | it("x.com covers its subdomains; *.example.com only those", () => { |
| 78 | expect(siteMatches("x.com", ["x.com"])).toBe(true); |
| 79 | expect(siteMatches("mobile.x.com", ["x.com"])).toBe(true); |
| 80 | expect(siteMatches("notx.com", ["x.com"])).toBe(false); |
| 81 | expect(siteMatches("example.com", ["*.example.com"])).toBe(false); |
| 82 | expect(siteMatches("a.example.com", ["*.example.com"])).toBe(true); |
| 83 | }); |
| 84 | it("reads owner/repo, a version, or the gitstable address, and refuses other sites", () => { |
| 85 | const o = "https://gitstable.com"; |
| 86 | expect(parsePluginSource("askrobots/x-whole-window", o)).toEqual({ repository: "askrobots/x-whole-window", tag: null }); |
| 87 | expect(parsePluginSource("askrobots/x-whole-window@2026.11v1", o)).toEqual({ repository: "askrobots/x-whole-window", tag: "2026.11v1" }); |
| 88 | expect(parsePluginSource("https://gitstable.com/repositories/askrobots/x-whole-window/", o)).toEqual({ repository: "askrobots/x-whole-window", tag: null }); |
| 89 | expect(parsePluginSource("https://gitstable.com/git/askrobots/x-whole-window.git", o)).toEqual({ repository: "askrobots/x-whole-window", tag: null }); |
| 90 | expect(parsePluginSource("gitstable.com/askrobots/x-whole-window", o)).toEqual({ repository: "askrobots/x-whole-window", tag: null }); |
| 91 | expect(parsePluginSource("https://evil.test/askrobots/x-whole-window", o)).toEqual({ error: "plugins install from gitstable.com only" }); |
| 92 | expect("error" in parsePluginSource("just-a-name", o)).toBe(true); |
| 93 | }); |
| 94 | it("takes an annotated tag's commit from its peeled line", () => { |
| 95 | const a = "a".repeat(40), b = "b".repeat(40), c = "c".repeat(40); |
| 96 | const refs = parseTagRefs(`003f${a} refs/tags/2026.11v1\n0042${b} refs/tags/2026.11v1^{}\n003f${c} refs/tags/2026.10v1\n`); |
| 97 | expect(refs.get("2026.11v1")).toBe(b); |
| 98 | expect(refs.get("2026.10v1")).toBe(c); |
| 99 | }); |
| 100 | it("computes git's blob id", () => { |
| 101 | expect(gitBlobId(new TextEncoder().encode("hello\n"))).toBe("ce013625030ba8dba906f756967f9e9ca394464a"); // git hash-object |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | describe("installing from gitstable (the stand-in)", () => { |
| 106 | let server: http.Server; |
| 107 | let origin = ""; |
| 108 | let dir = ""; |
| 109 | const fetchVia = async (url: string): Promise<Fetched> => { |
| 110 | const r = await fetch(url); |
| 111 | return { status: r.status, header: (n) => r.headers.get(n), bytes: new Uint8Array(await r.arrayBuffer()) }; |
| 112 | }; |
| 113 | const store = () => new PluginStore({ fetch: fetchVia, dir, origin, apiLevel: 1, browserVersion: "2026.11v1", now: () => new Date() }); |
| 114 | beforeAll(async () => { |
| 115 | server = http.createServer((req, res) => { if (!fakeGitstable(req, res)) res.writeHead(404).end(); }); |
| 116 | await new Promise<void>((r) => server.listen(0, "127.0.0.1", r)); |
| 117 | origin = `http://127.0.0.1:${(server.address() as { port: number }).port}`; |
| 118 | dir = mkdtempSync(join(tmpdir(), "dbb-plugins-")); |
| 119 | }); |
| 120 | afterAll(() => { server.close(); rmSync(dir, { recursive: true, force: true }); }); |
| 121 | |
| 122 | it("previews the newest release, installs exactly that, and applies it to its sites only", async () => { |
| 123 | const s = store(); |
| 124 | s.load(); |
| 125 | const p = await s.preview("demo/wide"); |
| 126 | expect(p.tag).toBe("2026.11v1"); |
| 127 | expect(p.commit).toMatch(/^[0-9a-f]{40}$/); |
| 128 | expect(p.says).toMatch(/can't run code, read the page or send anything/); |
| 129 | expect(p.testedHere).toBe(true); |
| 130 | const inst = s.install(p.token); |
| 131 | expect(inst.problem).toBe(""); |
| 132 | expect(inst.source.commit).toBe(p.commit); |
| 133 | expect(s.stylesFor("127.0.0.1").map((x) => x.id)).toEqual(["wide"]); |
| 134 | expect(s.stylesFor("localhost")).toEqual([]); |
| 135 | expect(() => s.install(p.token)).toThrow(/expired/); // a preview is used once |
| 136 | const again = await s.preview("demo/wide"); |
| 137 | expect(again.relation).toBe("same"); // looking up what is installed offers no update |
| 138 | expect((await s.preview("wide")).repository).toBe("demo/wide"); // its name alone finds the installed plugin |
| 139 | expect((await s.preview("wide@2026.10v1")).tag).toBe("2026.10v1"); |
| 140 | expect(again.installedProblem).toBe(false); |
| 141 | expect(statSync(join(dir, "installed.json")).mode & 0o777).toBe(0o600); |
| 142 | }); |
| 143 | |
| 144 | it("installs an older release when asked, as an update of the same plugin", async () => { |
| 145 | const s = store(); |
| 146 | s.load(); |
| 147 | const p = await s.preview("demo/wide@2026.10v1"); |
| 148 | expect(p.replaces).toBe("2026.11v1"); |
| 149 | expect(p.relation).toBe("older"); |
| 150 | s.install(p.token); |
| 151 | expect(s.list().find((x) => x.id === "wide")?.version).toBe("2026.10v1"); |
| 152 | }); |
| 153 | |
| 154 | it("refuses CSS that could send, escapes, more powers, a lying server, a mislabelled release, and a newer interface", async () => { |
| 155 | const s = store(); |
| 156 | s.load(); |
| 157 | await expect(s.preview("demo/sneaky")).rejects.toThrow(/url/); |
| 158 | await expect(s.preview("demo/escaped")).rejects.toThrow(/backslash/); |
| 159 | await expect(s.preview("demo/scripty")).rejects.toThrow(/page-script/); |
| 160 | await expect(s.preview("demo/tampered")).rejects.toThrow(/doesn't match its git blob id/); |
| 161 | await expect(s.preview("demo/mislabelled")).rejects.toThrow(/tagged 2026.11v1/); |
| 162 | await expect(s.preview("demo/future")).rejects.toThrow(/plugin level 99/); |
| 163 | await expect(s.preview("demo/nothing-here")).rejects.toThrow(/not found/); |
| 164 | await expect(s.preview("demo/wide@2027.01v1")).rejects.toThrow(/no release 2027.01v1/); |
| 165 | }); |
| 166 | |
| 167 | it("switches off a plugin whose files changed on this computer", async () => { |
| 168 | const s = store(); |
| 169 | s.load(); |
| 170 | s.install((await s.preview("demo/wide")).token); |
| 171 | writeFileSync(join(dir, "wide", "wide.css"), "#col { color: red }\n"); |
| 172 | const again = store(); |
| 173 | again.load(); |
| 174 | expect(again.list().find((x) => x.id === "wide")?.problem).toMatch(/changed on this computer/); |
| 175 | expect(again.stylesFor("127.0.0.1")).toEqual([]); |
| 176 | }); |
| 177 | |
| 178 | it("reads gitstable's plugin list, keeping only style rules whose pages are on gitstable", async () => { |
| 179 | const s = store(); |
| 180 | expect(await s.refreshListing()).toBe(true); |
| 181 | const wide = s.listed().find((x) => x.id === "wide"); |
| 182 | expect(wide?.repository).toBe("demo/wide"); |
| 183 | expect(wide?.url).toBe(origin + "/store/wide/"); |
| 184 | expect(await s.refreshListing()).toBe(false); // fetched in the last five minutes: not again |
| 185 | const c = "c".repeat(40); |
| 186 | const items = [ |
| 187 | { kind: "style-rule", id: "a", name: "A", repository: "o/a", tag: "2026.11v1", commit: c, url: "https://evil.test/a/", repository_url: origin + "/repositories/o/a/" }, |
| 188 | { kind: "Django module", id: "b", name: "B", repository: "o/b", tag: "2026.11v1", commit: c }, |
| 189 | { kind: "style-rule", id: "c", name: "C", repository: "not a repo", tag: "2026.11v1", commit: c }, |
| 190 | ]; |
| 191 | const got = parseListing({ v: 1, items }, origin); |
| 192 | expect(got.map((x) => x.id)).toEqual(["a"]); |
| 193 | expect(got[0]?.url).toBe(""); // a page off gitstable is never trusted as the plugin's page |
| 194 | expect(parseListing({ v: 2, items }, origin)).toEqual([]); |
| 195 | }); |
| 196 | |
| 197 | it("on a plugin's own page, fetches a list older than a minute again; elsewhere on gitstable, every five minutes", async () => { |
| 198 | let t = Date.parse("2026-11-23T12:00:00Z"); |
| 199 | const s = new PluginStore({ fetch: fetchVia, dir, origin, apiLevel: 1, browserVersion: "2026.11v1", now: () => new Date(t) }); |
| 200 | const hits = async () => (await (await fetch(origin + "/o/_plugin-list-hits")).json()).hits as number; |
| 201 | await s.refreshListingOn(origin + "/"); |
| 202 | const h0 = await hits(); |
| 203 | t += 30_000; |
| 204 | await s.refreshListingOn(origin + "/store/wide/"); |
| 205 | expect(await hits()).toBe(h0); // half a minute old: still used |
| 206 | t += 40_000; |
| 207 | await s.refreshListingOn(origin + "/docs/"); |
| 208 | expect(await hits()).toBe(h0); // another gitstable page: the five minutes hold |
| 209 | await s.refreshListingOn(origin + "/repositories/demo/wide"); |
| 210 | expect(await hits()).toBe(h0 + 1); // the plugin's own page, list over a minute old: fetched again |
| 211 | }); |
| 212 | |
| 213 | it("takes gitstable's ?install= link only for listed plugins, on gitstable itself", async () => { |
| 214 | const s = store(); |
| 215 | await s.refreshListing(true); |
| 216 | expect(s.installLinkSource(origin + "/store/wide/?install=demo/wide@2026.11v1")).toBe("demo/wide@2026.11v1"); |
| 217 | expect(s.installLinkSource(origin + "/repositories/demo/wide/?install=demo/wide")).toBe("demo/wide"); |
| 218 | expect(s.installLinkSource(origin + "/store/wide/?install=demo/sneaky@2026.11v1")).toBeNull(); // a real repository, not listed |
| 219 | expect(s.installLinkSource("https://evil.test/store/wide/?install=demo/wide")).toBeNull(); // only on gitstable |
| 220 | expect(s.installLinkSource(origin + "/store/wide/?install=https://evil.test/demo/wide")).toBeNull(); |
| 221 | expect(s.installLinkSource(origin + "/store/wide/")).toBeNull(); |
| 222 | }); |
| 223 | |
| 224 | it("finds a plugin by its name alone in gitstable's list", async () => { |
| 225 | const s = store(); |
| 226 | s.load(); |
| 227 | for (const p of s.list()) s.remove(p.id); |
| 228 | expect((await s.preview("wide")).repository).toBe("demo/wide"); |
| 229 | await expect(s.preview("nothing-called-this")).rejects.toThrow(/no installed or listed plugin is called nothing-called-this/); |
| 230 | }); |
| 231 | |
| 232 | it("installs a view rule: a status-bar button for its sites, the posts read with its selectors", async () => { |
| 233 | const s2 = new PluginStore({ fetch: fetchVia, dir, origin, apiLevel: 2, browserVersion: "2026.11v1", now: () => new Date() }); |
| 234 | s2.load(); |
| 235 | const p = await s2.preview("demo/cards"); |
| 236 | expect(p.says).toMatch(/adds a Cards button to the status bar on 127\.0\.0\.1: it reads the posts there and shows them as cards/); |
| 237 | expect(p.says).toMatch(/can't run code, change the page or send anything/); |
| 238 | s2.install(p.token); |
| 239 | const v = s2.viewsFor("127.0.0.1"); |
| 240 | expect(v.map((x) => [x.id, x.button.label, x.button.icon, x.rule.post])).toEqual([["cards", "Cards", "cards", "article"]]); |
| 241 | expect(s2.viewsFor("localhost")).toEqual([]); |
| 242 | expect(s2.stylesFor("127.0.0.1").map((x) => x.id)).not.toContain("cards"); // a view rule adds no style |
| 243 | s2.setEnabled("cards", false); |
| 244 | expect(s2.viewsFor("127.0.0.1")).toEqual([]); |
| 245 | s2.setEnabled("cards", true); |
| 246 | const again = new PluginStore({ fetch: fetchVia, dir, origin, apiLevel: 2, browserVersion: "2026.11v1", now: () => new Date() }); |
| 247 | again.load(); // checked again at every start |
| 248 | expect(again.viewsFor("127.0.0.1").map((x) => x.id)).toEqual(["cards"]); |
| 249 | await expect(s2.preview("demo/cards-sneaky")).rejects.toThrow(/refused: bad field text/); |
| 250 | await expect(s2.preview("demo/cards-elsewhere")).rejects.toThrow(/reads x\.com, which the plugin doesn't name/); |
| 251 | s2.remove("cards"); |
| 252 | expect(s2.viewsFor("127.0.0.1")).toEqual([]); |
| 253 | await expect(store().preview("demo/cards")).rejects.toThrow(/needs plugin level 2; this browser has 1/); |
| 254 | }); |
| 255 | |
| 256 | it("all off, one off, and remove", async () => { |
| 257 | const s = store(); |
| 258 | s.load(); |
| 259 | s.install((await s.preview("demo/wide")).token); |
| 260 | s.setAllOff(true); |
| 261 | expect(s.stylesFor("127.0.0.1")).toEqual([]); |
| 262 | s.setAllOff(false); |
| 263 | s.setEnabled("wide", false); |
| 264 | expect(s.stylesFor("127.0.0.1")).toEqual([]); |
| 265 | const kept = JSON.parse(readFileSync(join(dir, "installed.json"), "utf8")); |
| 266 | expect(kept.plugins[0].enabled).toBe(false); |
| 267 | s.remove("wide"); |
| 268 | expect(s.list()).toEqual([]); |
| 269 | }); |
| 270 | }); |