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
provider.test.ts · 17.7 KB · 345 lines
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { CallerNetwork, ProviderError, buildResponse, clampAnswerTimeout, firstParty, parseSetCookie, type CookieJar, type ParsedCookie } from "../src/provider.js"; |
| 3 | import type { NetEvent } from "../src/transport.js"; |
| 4 | |
| 5 | const make = () => { |
| 6 | const events: NetEvent[] = []; |
| 7 | let n = 0; |
| 8 | const net = new CallerNetwork((e) => events.push(e), () => ++n); |
| 9 | net.enabled = true; |
| 10 | net.userAgent = "ExampleCaller/1.0 (+https://caller.example/about)"; |
| 11 | return { net, events }; |
| 12 | }; |
| 13 | const req = (url = "https://a.test/x", init: RequestInit = {}) => new Request(url, init); |
| 14 | |
| 15 | afterEach(() => vi.useRealTimers()); |
| 16 | |
| 17 | describe("buildResponse validates what the caller sends", () => { |
| 18 | it("builds a response from text and from base64", async () => { |
| 19 | const t = buildResponse("https://a.test/", { status: 200, headers: { "content-type": "text/html" }, bodyText: "hi" }); |
| 20 | expect(t.bytes).toBe(2); |
| 21 | expect(await t.response.text()).toBe("hi"); |
| 22 | expect(await buildResponse("https://a.test/", { status: 200, bodyBase64: Buffer.from("héllo").toString("base64") }).response.text()).toBe("héllo"); |
| 23 | }); |
| 24 | it.each([[{ status: 99 }], [{ status: 700 }], [{ status: 200.5 }], [{ status: "200" }], [{ status: 200, bodyText: "a", bodyBase64: "YQ==" }], [{ status: 200, bodyBase64: "not base64!" }], [{ status: 200, headers: [] }], [{ status: 200, headers: { a: 1 } }], [null]])("rejects %j", (a) => { |
| 25 | expect(() => buildResponse("https://a.test/", a as never)).toThrow(ProviderError); |
| 26 | }); |
| 27 | it("drops headers that describe transport or set state (the body given is the body to show)", () => { |
| 28 | const { response } = buildResponse("https://a.test/", { status: 200, headers: { "content-encoding": "gzip", "content-length": "999", "set-cookie": "a=b", "x-keep": "1" }, bodyText: "x" }); |
| 29 | expect([...response.headers.keys()]).toEqual(["x-keep"]); |
| 30 | }); |
| 31 | it("makes a redirect's Location absolute and gives it no body", () => { |
| 32 | const { response, bytes } = buildResponse("https://a.test/dir/x", { status: 302, headers: { location: "../y" } }); |
| 33 | expect(response.headers.get("location")).toBe("https://a.test/y"); |
| 34 | expect(response.body).toBeNull(); |
| 35 | expect(bytes).toBe(0); |
| 36 | }); |
| 37 | it("a 304 or 204 has no body", () => { |
| 38 | expect(buildResponse("https://a.test/", { status: 204 }).response.body).toBeNull(); |
| 39 | }); |
| 40 | }); |
| 41 | |
| 42 | describe("clampAnswerTimeout", () => { |
| 43 | it("defaults, and clamps to 1 s … 2 min", () => { |
| 44 | expect(clampAnswerTimeout(undefined)).toBe(20_000); |
| 45 | expect(clampAnswerTimeout("abc")).toBe(20_000); |
| 46 | expect(clampAnswerTimeout(5)).toBe(1_000); |
| 47 | expect(clampAnswerTimeout(9e9)).toBe(120_000); |
| 48 | expect(clampAnswerTimeout(15_000)).toBe(15_000); |
| 49 | }); |
| 50 | }); |
| 51 | |
| 52 | describe("a request is parked for the caller, then answered", () => { |
| 53 | it("hands over the request, with the caller's identity and which page it follows", async () => { |
| 54 | const { net, events } = make(); |
| 55 | const p = net.fetch(req("https://a.test/next", { headers: { referer: "https://a.test/prev" } })); |
| 56 | const [r] = await net.pending(0, 0); |
| 57 | expect(r).toMatchObject({ id: 1, url: "https://a.test/next", method: "GET", follows: "https://a.test/prev" }); |
| 58 | expect(r!.headers["user-agent"]).toBe("ExampleCaller/1.0 (+https://caller.example/about)"); |
| 59 | expect(net.isParked("https://a.test/next")).toBe(true); |
| 60 | net.answer(1, { status: 200, headers: { "content-type": "text/html; charset=utf-8" }, bodyText: "<p>ok</p>" }); |
| 61 | const res = await p; |
| 62 | expect(await res.text()).toBe("<p>ok</p>"); |
| 63 | expect(net.isParked("https://a.test/next")).toBe(false); |
| 64 | expect(events.map((e) => e.type)).toEqual(["start", "caller", "headers", "end"]); |
| 65 | expect(events[2]).toMatchObject({ status: 200, contentType: "text/html" }); |
| 66 | }); |
| 67 | it("a request body is handed over as base64", async () => { |
| 68 | const { net } = make(); |
| 69 | void net.fetch(req("https://a.test/post", { method: "POST", body: "a=1" })).catch(() => {}); |
| 70 | await new Promise((r) => setTimeout(r, 5)); |
| 71 | const [r] = await net.pending(0, 0); |
| 72 | expect(Buffer.from(r!.bodyBase64!, "base64").toString()).toBe("a=1"); |
| 73 | }); |
| 74 | it("a redirect answer goes back as a redirect (and is announced as one)", async () => { |
| 75 | const { net, events } = make(); |
| 76 | const p = net.fetch(req("https://a.test/old")); |
| 77 | await net.pending(0, 0); |
| 78 | net.answer(1, { status: 301, headers: { location: "/new" } }); |
| 79 | expect((await p).headers.get("location")).toBe("https://a.test/new"); |
| 80 | expect(events.at(-1)).toMatchObject({ type: "redirect", status: 301, location: "https://a.test/new" }); |
| 81 | }); |
| 82 | it("a bad answer is an error to the caller and leaves the request waiting", async () => { |
| 83 | const { net } = make(); |
| 84 | void net.fetch(req()).catch(() => {}); |
| 85 | await net.pending(0, 0); |
| 86 | expect(() => net.answer(1, { status: 42 })).toThrow(ProviderError); |
| 87 | expect(net.isParked("https://a.test/x")).toBe(true); |
| 88 | }); |
| 89 | it("answering twice, or an unknown request, is 'no-request'", async () => { |
| 90 | const { net } = make(); |
| 91 | const p = net.fetch(req()); |
| 92 | await net.pending(0, 0); |
| 93 | net.answer(1, { status: 200 }); |
| 94 | await p; |
| 95 | expect(() => net.answer(1, { status: 200 })).toThrowError(expect.objectContaining({ code: "no-request" })); |
| 96 | expect(() => net.fail(77, "blocked")).toThrowError(expect.objectContaining({ code: "no-request" })); |
| 97 | }); |
| 98 | it("pending(afterId) only returns newer requests, and a long poll wakes when one arrives", async () => { |
| 99 | const { net } = make(); |
| 100 | void net.fetch(req("https://a.test/1")).catch(() => {}); |
| 101 | await new Promise((r) => setTimeout(r, 5)); |
| 102 | expect((await net.pending(0, 0)).length).toBe(1); |
| 103 | const waiting = net.pending(1, 5000); |
| 104 | void net.fetch(req("https://a.test/2")).catch(() => {}); |
| 105 | expect((await waiting).map((r) => r.url)).toEqual(["https://a.test/2"]); |
| 106 | expect(await net.pending(2, 30)).toEqual([]); |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | describe("limits on what a page can make the caller hold", () => { |
| 111 | it("at most 64 requests wait at once; the next fails at once, with a reason", async () => { |
| 112 | const { net, events } = make(); |
| 113 | for (let i = 0; i < 64; i++) void net.fetch(req(`https://a.test/${i}`)).catch(() => {}); |
| 114 | await new Promise((r) => setTimeout(r, 20)); |
| 115 | expect(net.parkedCount()).toBe(64); |
| 116 | await expect(net.fetch(req("https://a.test/one-too-many"))).rejects.toThrow(/too many requests/); |
| 117 | expect(events.at(-1)).toMatchObject({ type: "error", failureKind: "other", failureSource: "observed" }); |
| 118 | net.failAll("other", "cleanup"); |
| 119 | }); |
| 120 | it("pending hands over at most 50 at a time, oldest first", async () => { |
| 121 | const { net } = make(); |
| 122 | for (let i = 0; i < 60; i++) void net.fetch(req(`https://a.test/${i}`)).catch(() => {}); |
| 123 | await new Promise((r) => setTimeout(r, 20)); |
| 124 | const first = await net.pending(0, 0); |
| 125 | expect(first.length).toBe(50); |
| 126 | expect(first[0]!.id).toBe(1); |
| 127 | expect((await net.pending(first.at(-1)!.id, 0)).length).toBe(10); |
| 128 | net.failAll("other", "cleanup"); |
| 129 | }); |
| 130 | it("a redirect may only go to an http(s) address", () => { |
| 131 | expect(() => buildResponse("https://a.test/", { status: 302, headers: { location: "file:///etc/passwd" } })).toThrow(ProviderError); |
| 132 | expect(() => buildResponse("https://a.test/", { status: 302, headers: { location: "javascript:alert(1)" } })).toThrow(ProviderError); |
| 133 | expect(() => buildResponse("https://a.test/", { status: 302, headers: { location: "https://b.test/x" } })).not.toThrow(); |
| 134 | }); |
| 135 | }); |
| 136 | |
| 137 | describe("per-visit limits", () => { |
| 138 | const ask = async (net: CallerNetwork, url: string) => { |
| 139 | const p = net.fetch(req(url)).catch((e) => e); |
| 140 | await new Promise((r) => setTimeout(r, 5)); |
| 141 | return p; |
| 142 | }; |
| 143 | it("past the request limit the next request fails at once, with a reason, as observed", async () => { |
| 144 | const { net, events } = make(); |
| 145 | net.limits = { requests: 2, bytes: 0 }; |
| 146 | void net.fetch(req("https://a.test/1")).catch(() => {}); |
| 147 | void net.fetch(req("https://a.test/2")).catch(() => {}); |
| 148 | await new Promise((r) => setTimeout(r, 5)); |
| 149 | const third = await ask(net, "https://a.test/3"); |
| 150 | expect(String(third)).toMatch(/visit limit reached: 2 requests/); |
| 151 | expect(events.at(-1)).toMatchObject({ type: "error", failureKind: "other", failureSource: "observed" }); |
| 152 | net.failAll("other", "cleanup"); |
| 153 | }); |
| 154 | it("past the byte limit the next request fails; the response under way is not cut off", async () => { |
| 155 | const { net } = make(); |
| 156 | net.limits = { requests: 0, bytes: 40 }; |
| 157 | const first = net.fetch(req("https://a.test/1")); |
| 158 | await net.pending(0, 0); |
| 159 | net.answer(1, { status: 200, bodyText: "x".repeat(100) }); // over the limit, but delivered whole |
| 160 | expect((await (await first).text()).length).toBe(100); |
| 161 | expect(String(await ask(net, "https://a.test/2"))).toMatch(/visit limit reached: 40 bytes/); |
| 162 | }); |
| 163 | it("a new visit starts the counts over, and 0 means no limit", async () => { |
| 164 | const { net } = make(); |
| 165 | net.limits = { requests: 1, bytes: 0 }; |
| 166 | void net.fetch(req("https://a.test/1")).catch(() => {}); |
| 167 | await new Promise((r) => setTimeout(r, 5)); |
| 168 | expect(String(await ask(net, "https://a.test/2"))).toMatch(/visit limit/); |
| 169 | net.newVisit(); |
| 170 | void net.fetch(req("https://a.test/3")).catch(() => {}); |
| 171 | await new Promise((r) => setTimeout(r, 5)); |
| 172 | expect(net.parkedCount()).toBe(2); |
| 173 | net.limits = { requests: 0, bytes: 0 }; |
| 174 | void net.fetch(req("https://a.test/4")).catch(() => {}); |
| 175 | await new Promise((r) => setTimeout(r, 5)); |
| 176 | expect(net.parkedCount()).toBe(3); |
| 177 | net.failAll("other", "cleanup"); |
| 178 | }); |
| 179 | }); |
| 180 | |
| 181 | describe("a refusal carries the caller's reason", () => { |
| 182 | it("rejects the request, and announces the kind with source 'caller'", async () => { |
| 183 | const { net, events } = make(); |
| 184 | const p = net.fetch(req()); |
| 185 | await net.pending(0, 0); |
| 186 | net.fail(1, "blocked", "the caller's own rules disallow this"); |
| 187 | await expect(p).rejects.toThrow(/caller:blocked/); |
| 188 | expect(events.at(-1)).toMatchObject({ type: "error", error: "the caller's own rules disallow this", failureKind: "blocked", failureSource: "caller" }); |
| 189 | }); |
| 190 | it("only kinds from the shared list are accepted; 'caller-silent' is the browser's to say", async () => { |
| 191 | const { net } = make(); |
| 192 | void net.fetch(req()).catch(() => {}); |
| 193 | await net.pending(0, 0); |
| 194 | expect(() => net.fail(1, "nonsense")).toThrow(ProviderError); |
| 195 | expect(() => net.fail(1, "caller-silent")).toThrow(ProviderError); |
| 196 | }); |
| 197 | }); |
| 198 | |
| 199 | describe("waiting on the caller has ONE bound", () => { |
| 200 | it("past it the request fails as 'caller-silent', observed — not 'timed-out' (server too slow)", async () => { |
| 201 | vi.useFakeTimers(); |
| 202 | const { net, events } = make(); |
| 203 | net.answerTimeoutMs = 20_000; |
| 204 | const p = net.fetch(req()); |
| 205 | const caught = p.catch((e) => e); |
| 206 | await vi.advanceTimersByTimeAsync(19_999); |
| 207 | expect(net.isParked("https://a.test/x")).toBe(true); |
| 208 | await vi.advanceTimersByTimeAsync(2); |
| 209 | expect(String(await caught)).toMatch(/caller-silent/); |
| 210 | expect(events.at(-1)).toMatchObject({ type: "error", failureKind: "caller-silent", failureSource: "observed" }); |
| 211 | expect(net.isParked("https://a.test/x")).toBe(false); |
| 212 | }); |
| 213 | it("an answer in time clears the bound", async () => { |
| 214 | vi.useFakeTimers(); |
| 215 | const { net } = make(); |
| 216 | const p = net.fetch(req()); |
| 217 | await vi.advanceTimersByTimeAsync(1); |
| 218 | net.answer(1, { status: 200, bodyText: "x" }); |
| 219 | await p; |
| 220 | await vi.advanceTimersByTimeAsync(60_000); |
| 221 | expect(net.parkedCount()).toBe(0); |
| 222 | }); |
| 223 | it("turning the mode off refuses everything waiting", async () => { |
| 224 | const { net } = make(); |
| 225 | const p = net.fetch(req()).catch((e) => e); |
| 226 | await net.pending(0, 0); |
| 227 | net.failAll("other", "turned off"); |
| 228 | expect(String(await p)).toMatch(/turned off/); |
| 229 | }); |
| 230 | it("an aborted request leaves the queue", async () => { |
| 231 | const { net } = make(); |
| 232 | const ac = new AbortController(); |
| 233 | const p = net.fetch(new Request("https://a.test/x", { signal: ac.signal })).catch((e) => e); |
| 234 | await net.pending(0, 0); |
| 235 | ac.abort(); |
| 236 | expect(String(await p)).toMatch(/aborted/i); |
| 237 | expect(net.parkedCount()).toBe(0); |
| 238 | }); |
| 239 | }); |
| 240 | |
| 241 | describe("a document the caller already has", () => { |
| 242 | const doc = { url: "https://a.test/final", status: 200, headers: { "content-type": "text/html" }, bodyText: "<a href=x>x</a>", redirects: [{ status: 301, to: "https://a.test/final" }] }; |
| 243 | it("is served once, as it is, when its address is asked for — without being handed to the caller", async () => { |
| 244 | const { net, events } = make(); |
| 245 | net.registerDocument(doc); |
| 246 | expect(net.hasDocumentFor("https://a.test/final")).toBe(true); |
| 247 | const res = await net.fetch(req("https://a.test/final")); |
| 248 | expect(await res.text()).toBe("<a href=x>x</a>"); |
| 249 | expect(net.parkedCount()).toBe(0); |
| 250 | expect(events.map((e) => e.type)).toEqual(["start", "headers", "end"]); |
| 251 | expect(net.hasDocumentFor("https://a.test/final")).toBe(false); // once |
| 252 | }); |
| 253 | it("a bad document is refused up front", () => { |
| 254 | const { net } = make(); |
| 255 | expect(() => net.registerDocument({ ...doc, status: 1 })).toThrow(ProviderError); |
| 256 | expect(() => net.registerDocument({ ...doc, url: "file:///etc/passwd" })).toThrow(ProviderError); |
| 257 | expect(() => net.registerDocument({ ...doc, redirects: [{ to: 5 }] as never })).toThrow(ProviderError); |
| 258 | }); |
| 259 | it("expires if nobody asks for it", () => { |
| 260 | vi.useFakeTimers(); |
| 261 | const { net } = make(); |
| 262 | net.registerDocument(doc); |
| 263 | vi.advanceTimersByTime(61_000); |
| 264 | expect(net.hasDocumentFor("https://a.test/final")).toBe(false); |
| 265 | }); |
| 266 | }); |
| 267 | |
| 268 | describe("parseSetCookie", () => { |
| 269 | const p = (line: string, url = "https://a.test/dir/page") => parseSetCookie(line, url, 1_000_000); |
| 270 | it("reads name, value and the attributes that matter", () => { |
| 271 | expect(p("k=v; Path=/x; Domain=a.test; Secure; HttpOnly; SameSite=Lax; Max-Age=60")).toEqual({ name: "k", value: "v", domain: ".a.test", path: "/x", secure: true, httpOnly: true, sameSite: "lax", expirationDate: 1_000_060 }); |
| 272 | }); |
| 273 | it("defaults the path to the directory of the request, and has no expiry for a session cookie", () => { |
| 274 | expect(p("k=v")).toMatchObject({ path: "/dir", sameSite: "unspecified" }); |
| 275 | expect(p("k=v")!.expirationDate).toBeUndefined(); |
| 276 | expect(p("k=v", "https://a.test/")).toMatchObject({ path: "/" }); |
| 277 | }); |
| 278 | it("Max-Age wins over Expires", () => { |
| 279 | expect(p("k=v; Expires=Wed, 21 Oct 2099 07:28:00 GMT; Max-Age=5")!.expirationDate).toBe(1_000_005); |
| 280 | expect(p("k=v; Expires=Wed, 21 Oct 2099 07:28:00 GMT")!.expirationDate).toBeGreaterThan(4_000_000_000); |
| 281 | }); |
| 282 | it("rejects what is unusable", () => { |
| 283 | for (const bad of ["", "novalue", "=v", "a b=c", "k=v\u0001", "k=" + "x".repeat(5000)]) expect(p(bad)).toBeNull(); |
| 284 | }); |
| 285 | }); |
| 286 | |
| 287 | describe("firstParty", () => { |
| 288 | it("a navigation is; a request is when the page it follows is on the same site", () => { |
| 289 | expect(firstParty("https://a.test/", "", true)).toBe(true); |
| 290 | expect(firstParty("https://a.test/img", "https://a.test/page", false)).toBe(true); |
| 291 | expect(firstParty("https://cdn.a.test/img", "https://www.a.test/page", false)).toBe(true); |
| 292 | expect(firstParty("https://tracker.example/px", "https://a.test/page", false)).toBe(false); |
| 293 | expect(firstParty("https://a.test/img", "", false)).toBe(false); // no page to compare with: not first-party |
| 294 | }); |
| 295 | }); |
| 296 | |
| 297 | describe("the per-visit cookie jar (off by default)", () => { |
| 298 | const jarOf = (stored: ParsedCookie[], header = "seen=1") => ({ on: () => true, header: async () => header, store: async (_u: string, c: ParsedCookie) => { await new Promise((r) => setTimeout(r, 10)); stored.push(c); } }) as CookieJar; |
| 299 | it("with no jar, no cookie is sent and a Set-Cookie is dropped", async () => { |
| 300 | const { net } = make(); |
| 301 | const p = net.fetch(req("https://a.test/x", { headers: { cookie: "page=sent" } })); |
| 302 | const [r] = await net.pending(0, 0); |
| 303 | expect(r!.headers.cookie).toBeUndefined(); |
| 304 | net.answer(1, { status: 200, headers: { "set-cookie": "a=b" }, bodyText: "x" }); |
| 305 | expect([...(await p).headers.keys()]).not.toContain("set-cookie"); |
| 306 | }); |
| 307 | it("a Set-Cookie is stored BEFORE the page sees the response, and the browser's Cookie header is the only one", async () => { |
| 308 | const { net } = make(); |
| 309 | const stored: ParsedCookie[] = []; |
| 310 | net.jar = jarOf(stored); |
| 311 | const p = net.fetch(req("https://a.test/x", { headers: { cookie: "page=sent", "upgrade-insecure-requests": "1" } })); |
| 312 | const [r] = await net.pending(0, 500); // the jar is asked first, so the request is parked a moment later |
| 313 | expect(r!.headers.cookie).toBe("seen=1"); |
| 314 | net.answer(1, { status: 302, headers: { location: "/x", "set-cookie": ["seen=1; Path=/"] } }); |
| 315 | await p; |
| 316 | expect(stored.map((c) => c.name)).toEqual(["seen"]); // already stored when the response resolved |
| 317 | }); |
| 318 | it("a third-party request gets no cookie, and none is stored from it", async () => { |
| 319 | const { net } = make(); |
| 320 | const stored: ParsedCookie[] = []; |
| 321 | net.jar = jarOf(stored); |
| 322 | const p = net.fetch(req("https://tracker.example/px", { headers: { referer: "https://a.test/page" } })); |
| 323 | const [r] = await net.pending(0, 0); |
| 324 | expect(r!.headers.cookie).toBeUndefined(); |
| 325 | net.answer(1, { status: 200, headers: { "set-cookie": "t=1" }, bodyText: "x" }); |
| 326 | await p; |
| 327 | expect(stored).toEqual([]); |
| 328 | }); |
| 329 | it("at most 50 cookies per visit; a new visit starts the count over", async () => { |
| 330 | const { net } = make(); |
| 331 | const stored: ParsedCookie[] = []; |
| 332 | net.jar = jarOf(stored); |
| 333 | const p = net.fetch(req("https://a.test/x", { headers: { "upgrade-insecure-requests": "1" } })); |
| 334 | await net.pending(0, 500); |
| 335 | net.answer(1, { status: 200, headers: { "set-cookie": Array.from({ length: 60 }, (_, i) => `c${i}=v`) }, bodyText: "x" }); |
| 336 | await p; |
| 337 | expect(stored.length).toBe(50); |
| 338 | net.newVisit(); |
| 339 | const p2 = net.fetch(req("https://a.test/y", { headers: { "upgrade-insecure-requests": "1" } })); |
| 340 | await net.pending(1, 500); |
| 341 | net.answer(2, { status: 200, headers: { "set-cookie": "again=1" }, bodyText: "x" }); |
| 342 | await p2; |
| 343 | expect(stored.length).toBe(51); |
| 344 | }); |
| 345 | }); |