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
compat.test.ts · 9.5 KB · 166 lines
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { brandedUserAgent, chromeMajor, chromiumRequestHeaders, isTrustworthy, refererFor, requestCredentials, secChUa, type Hints } from "../src/compat.js"; |
| 3 | |
| 4 | const ELECTRON_UA = |
| 5 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) dbbasic-browser/33.4.11 Chrome/130.0.6723.191 Electron/33.4.11 Safari/537.36"; |
| 6 | const PRODUCT = { name: "dbbasic-browser", version: "0.1.0", url: "https://dbbasic.com" }; |
| 7 | const HINTS: Hints = { brands: [{ brand: "Not?A_Brand", version: "99" }, { brand: "Chromium", version: "130" }], platform: "macOS", mobile: false }; |
| 8 | |
| 9 | describe("brandedUserAgent", () => { |
| 10 | const ua = brandedUserAgent(ELECTRON_UA, PRODUCT); |
| 11 | it("drops the Electron token and Electron's version of the app token", () => { |
| 12 | expect(ua).not.toMatch(/Electron/); |
| 13 | expect(ua).not.toContain("dbbasic-browser/33.4.11"); |
| 14 | }); |
| 15 | it("stays Chromium-compatible: Mozilla/Chrome/Safari tokens intact, in order", () => { |
| 16 | expect(ua.startsWith("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.191 Safari/537.36")).toBe(true); |
| 17 | }); |
| 18 | it("ends with our product token and a link back to dbbasic.com", () => { |
| 19 | expect(ua.endsWith("dbbasic-browser/0.1.0 (+https://dbbasic.com)")).toBe(true); |
| 20 | }); |
| 21 | it("does not claim to be Firefox", () => { |
| 22 | expect(ua).not.toMatch(/Firefox|Gecko\/\d/); |
| 23 | }); |
| 24 | it("is idempotent in shape when applied to an already-branded UA", () => { |
| 25 | expect(brandedUserAgent(ua, PRODUCT)).toBe(ua); |
| 26 | }); |
| 27 | }); |
| 28 | |
| 29 | describe("chromeMajor / secChUa / isTrustworthy", () => { |
| 30 | it("parses the Chrome major", () => { |
| 31 | expect(chromeMajor(ELECTRON_UA)).toBe(130); |
| 32 | expect(chromeMajor("curl/8")).toBeNull(); |
| 33 | }); |
| 34 | it("formats sec-ch-ua like Chromium", () => { |
| 35 | expect(secChUa(HINTS)).toBe('"Not?A_Brand";v="99", "Chromium";v="130"'); |
| 36 | }); |
| 37 | it("trusts https and loopback only", () => { |
| 38 | expect(isTrustworthy(new URL("https://example.com"))).toBe(true); |
| 39 | expect(isTrustworthy(new URL("http://127.0.0.1:8899/"))).toBe(true); |
| 40 | expect(isTrustworthy(new URL("http://localhost:3000/"))).toBe(true); |
| 41 | expect(isTrustworthy(new URL("http://example.com/"))).toBe(false); |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | describe("chromiumRequestHeaders", () => { |
| 46 | const nav = () => |
| 47 | new Headers({ |
| 48 | accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,*/*;q=0.8", |
| 49 | "upgrade-insecure-requests": "1", |
| 50 | "sec-fetch-mode": "no-cors", |
| 51 | "sec-fetch-dest": "empty", |
| 52 | "sec-fetch-site": "none", |
| 53 | }); |
| 54 | |
| 55 | it("turns a typed top-level navigation into navigate/document/none/?1 with client hints", () => { |
| 56 | const h = chromiumRequestHeaders(new URL("https://www.ebay.com/"), nav(), HINTS); |
| 57 | expect(h.get("sec-fetch-mode")).toBe("navigate"); |
| 58 | expect(h.get("sec-fetch-dest")).toBe("document"); |
| 59 | expect(h.get("sec-fetch-site")).toBe("none"); |
| 60 | expect(h.get("sec-fetch-user")).toBe("?1"); |
| 61 | expect(h.get("sec-ch-ua")).toBe('"Not?A_Brand";v="99", "Chromium";v="130"'); |
| 62 | expect(h.get("sec-ch-ua-mobile")).toBe("?0"); |
| 63 | expect(h.get("sec-ch-ua-platform")).toBe('"macOS"'); |
| 64 | }); |
| 65 | |
| 66 | it("computes sec-fetch-site from the referrer for link clicks", () => { |
| 67 | const same = nav(); same.set("referer", "https://www.ebay.com/a"); |
| 68 | expect(chromiumRequestHeaders(new URL("https://www.ebay.com/b"), same, HINTS).get("sec-fetch-site")).toBe("same-origin"); |
| 69 | const sub = nav(); sub.set("referer", "https://www.ebay.com/a"); |
| 70 | expect(chromiumRequestHeaders(new URL("https://signin.ebay.com/"), sub, HINTS).get("sec-fetch-site")).toBe("same-site"); |
| 71 | const cross = nav(); cross.set("referer", "https://askrobots.com/"); |
| 72 | expect(chromiumRequestHeaders(new URL("https://www.ebay.com/"), cross, HINTS).get("sec-fetch-site")).toBe("cross-site"); |
| 73 | }); |
| 74 | |
| 75 | it("classifies subresources and never sends sec-fetch-user on them", () => { |
| 76 | const img = new Headers({ accept: "image/avif,image/webp,*/*", referer: "https://www.ebay.com/", "sec-fetch-user": "?1" }); |
| 77 | const h = chromiumRequestHeaders(new URL("https://i.ebayimg.com/x.jpg"), img, HINTS); |
| 78 | expect(h.get("sec-fetch-dest")).toBe("image"); |
| 79 | expect(h.get("sec-fetch-mode")).toBe("no-cors"); |
| 80 | expect(h.get("sec-fetch-site")).toBe("cross-site"); |
| 81 | expect(h.has("sec-fetch-user")).toBe(false); |
| 82 | |
| 83 | const xhr = new Headers({ accept: "*/*", origin: "https://www.ebay.com", referer: "https://www.ebay.com/" }); |
| 84 | const x = chromiumRequestHeaders(new URL("https://www.ebay.com/api"), xhr, HINTS); |
| 85 | expect(x.get("sec-fetch-mode")).toBe("cors"); |
| 86 | expect(x.get("sec-fetch-dest")).toBe("empty"); |
| 87 | expect(x.get("sec-fetch-site")).toBe("same-origin"); |
| 88 | }); |
| 89 | |
| 90 | it("leaves plain-http (untrustworthy) requests alone, like Chrome", () => { |
| 91 | const h = chromiumRequestHeaders(new URL("http://example.com/"), nav(), HINTS); |
| 92 | expect(h.has("sec-ch-ua")).toBe(false); |
| 93 | expect(h.get("sec-fetch-mode")).toBe("no-cors"); // untouched |
| 94 | }); |
| 95 | |
| 96 | it("skips client hints when the engine's brands aren't known, but still fixes fetch metadata", () => { |
| 97 | const h = chromiumRequestHeaders(new URL("https://www.ebay.com/"), nav(), null); |
| 98 | expect(h.has("sec-ch-ua")).toBe(false); |
| 99 | expect(h.get("sec-fetch-mode")).toBe("navigate"); |
| 100 | }); |
| 101 | |
| 102 | it("does not overwrite hints the renderer already supplied, and does not mutate its input", () => { |
| 103 | const input = nav(); input.set("sec-ch-ua", '"Custom";v="1"'); |
| 104 | const h = chromiumRequestHeaders(new URL("https://www.ebay.com/"), input, HINTS); |
| 105 | expect(h.get("sec-ch-ua")).toBe('"Custom";v="1"'); |
| 106 | expect(input.get("sec-fetch-mode")).toBe("no-cors"); |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | describe("refererFor (strict-origin-when-cross-origin by default)", () => { |
| 111 | const ref = new URL("https://www.ebay.com/itm/123?secret=1#frag"); |
| 112 | it("full URL (minus fragment) same-origin, origin only cross-origin, nothing on downgrade", () => { |
| 113 | expect(refererFor(new URL("https://www.ebay.com/other"), ref, "")).toBe("https://www.ebay.com/itm/123?secret=1"); |
| 114 | expect(refererFor(new URL("https://i.ebayimg.com/x.jpg"), ref, "")).toBe("https://www.ebay.com/"); |
| 115 | expect(refererFor(new URL("http://example.com/"), ref, "")).toBeNull(); |
| 116 | }); |
| 117 | it("honours explicit policies", () => { |
| 118 | const t = new URL("https://other.test/"); |
| 119 | expect(refererFor(t, ref, "no-referrer")).toBeNull(); |
| 120 | expect(refererFor(t, ref, "origin")).toBe("https://www.ebay.com/"); |
| 121 | expect(refererFor(t, ref, "same-origin")).toBeNull(); |
| 122 | expect(refererFor(t, ref, "unsafe-url")).toBe("https://www.ebay.com/itm/123?secret=1"); |
| 123 | expect(refererFor(new URL("http://other.test/"), ref, "no-referrer-when-downgrade")).toBeNull(); |
| 124 | }); |
| 125 | it("never leaks credentials in the referrer", () => { |
| 126 | expect(refererFor(new URL("https://a.test/x"), new URL("https://u:p@a.test/y"), "")).toBe("https://a.test/y"); |
| 127 | }); |
| 128 | }); |
| 129 | |
| 130 | describe("chromiumRequestHeaders + referrer", () => { |
| 131 | it("adds Referer from the Request's referrer property and uses it for sec-fetch-site", () => { |
| 132 | const nav = new Headers({ accept: "text/html,*/*", "upgrade-insecure-requests": "1" }); |
| 133 | const h = chromiumRequestHeaders(new URL("https://www.ebay.com/b"), nav, HINTS, { referrer: "https://www.ebay.com/a", referrerPolicy: "" }); |
| 134 | expect(h.get("referer")).toBe("https://www.ebay.com/a"); |
| 135 | expect(h.get("sec-fetch-site")).toBe("same-origin"); |
| 136 | }); |
| 137 | it("ignores about:client / empty referrers (typed navigations send none)", () => { |
| 138 | const nav = new Headers({ accept: "text/html,*/*", "upgrade-insecure-requests": "1" }); |
| 139 | for (const r of ["", "about:client"]) { |
| 140 | const h = chromiumRequestHeaders(new URL("https://www.ebay.com/"), nav, HINTS, { referrer: r }); |
| 141 | expect(h.has("referer")).toBe(false); |
| 142 | expect(h.get("sec-fetch-site")).toBe("none"); |
| 143 | } |
| 144 | }); |
| 145 | }); |
| 146 | |
| 147 | describe("requestCredentials", () => { |
| 148 | const h = (o: Record<string, string> = {}) => new Headers(o); |
| 149 | it('"omit" never carries cookies', () => { |
| 150 | expect(requestCredentials({ url: "https://cdn.example/x.js", credentials: "omit" }, h())).toBe("omit"); |
| 151 | expect(requestCredentials({ url: "https://page.example/api", credentials: "omit" }, h({ origin: "https://page.example" }))).toBe("omit"); |
| 152 | }); |
| 153 | it('"same-origin" carries cookies only to the page\'s own origin (a <script crossorigin> from a CDN carries none)', () => { |
| 154 | expect(requestCredentials({ url: "https://cdn.example/x.js", credentials: "same-origin" }, h({ origin: "https://page.example" }))).toBe("omit"); |
| 155 | expect(requestCredentials({ url: "https://cdn.example/x.js", credentials: "same-origin", referrer: "https://page.example/a" }, h())).toBe("omit"); |
| 156 | expect(requestCredentials({ url: "https://cdn.example/x.js", credentials: "same-origin" }, h({ referer: "https://page.example/a" }))).toBe("omit"); |
| 157 | expect(requestCredentials({ url: "https://page.example/api", credentials: "same-origin" }, h({ origin: "https://page.example" }))).toBe("include"); |
| 158 | expect(requestCredentials({ url: "http://127.0.0.1:2/x", credentials: "same-origin" }, h({ origin: "http://127.0.0.1:1" }))).toBe("omit"); // another port is another origin |
| 159 | }); |
| 160 | it('"include", or no information, carries them, as before', () => { |
| 161 | expect(requestCredentials({ url: "https://api.example/x", credentials: "include" }, h({ origin: "https://page.example" }))).toBe("include"); |
| 162 | expect(requestCredentials({ url: "https://api.example/x" }, h({ origin: "https://page.example" }))).toBe("include"); |
| 163 | expect(requestCredentials({ url: "https://api.example/x", credentials: "same-origin", referrer: "about:client" }, h())).toBe("include"); // no way to tell whose page this is |
| 164 | expect(requestCredentials({ url: "not a url", credentials: "same-origin" }, h())).toBe("include"); |
| 165 | }); |
| 166 | }); |