A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

9 downloads

dbbasic-browser/apps/browser/test/compat.test.ts
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
compat.test.ts · 11.1 KB · 179 lines
1import { describe, expect, it } from "vitest";
2import { brandedUserAgent, chromeMajor, chromiumRequestHeaders, isTrustworthy, refererFor, requestCredentials, secChUa, type Hints } from "../src/compat.js";
3
4const 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";
6const PRODUCT = { name: "dbbasic-browser", version: "2026.11v1", url: "https://dbbasic.com" };
7const HINTS: Hints = { brands: [{ brand: "Not?A_Brand", version: "99" }, { brand: "Chromium", version: "130" }], platform: "macOS", mobile: false };
8
9describe("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/2026.11v1 (+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
29describe("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
45describe("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
110describe("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
130describe("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
147describe("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('Electron says "same-origin" for everything, so the headers decide: a load with an Origin header carries cookies only to its own origin', () => {
154 const cors = (site: string) => h({ origin: "https://page.example", "sec-fetch-mode": "cors", "sec-fetch-site": site });
155 expect(requestCredentials({ url: "https://cdn.example/x.js", credentials: "same-origin" }, cors("cross-site"))).toBe("omit");
156 expect(requestCredentials({ url: "https://static.page.example/x.js", credentials: "same-origin" }, cors("same-site"))).toBe("omit"); // <script crossorigin> from a subdomain
157 expect(requestCredentials({ url: "https://page.example/api", credentials: "same-origin" }, cors("same-origin"))).toBe("include");
158 });
159 it("navigations and no-cors loads to another site carry cookies (a link, a frame, a redirect after a form post, a plain image)", () => {
160 const nav = h({ origin: "https://cloud.example", "sec-fetch-mode": "navigate", "sec-fetch-site": "cross-site" });
161 expect(requestCredentials({ url: "https://shop.example/callback?code=1", credentials: "same-origin", referrer: "https://cloud.example/" }, nav)).toBe("include");
162 const img = h({ referer: "https://page.example/a", "sec-fetch-mode": "no-cors", "sec-fetch-site": "cross-site" });
163 expect(requestCredentials({ url: "https://cdn.example/a.png", credentials: "same-origin", referrer: "https://page.example/a" }, img)).toBe("include");
164 });
165 it("without fetch metadata (http): navigations and loads without an Origin header carry cookies; CORS ones only to their own origin", () => {
166 const navHeaders = h({ accept: "text/html,application/xhtml+xml", "upgrade-insecure-requests": "1", referer: "http://a.example/" });
167 expect(requestCredentials({ url: "http://b.example/", credentials: "same-origin", referrer: "http://a.example/" }, navHeaders)).toBe("include");
168 expect(requestCredentials({ url: "http://cdn.example/x.js", credentials: "same-origin", referrer: "http://a.example/" }, h({ referer: "http://a.example/" }))).toBe("include");
169 expect(requestCredentials({ url: "http://cdn.example/x.js", credentials: "same-origin" }, h({ origin: "http://a.example" }))).toBe("omit");
170 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
171 expect(requestCredentials({ url: "http://a.example/api", credentials: "same-origin" }, h({ origin: "http://a.example" }))).toBe("include");
172 });
173 it('"include", or no information, carries them, as before', () => {
174 expect(requestCredentials({ url: "https://api.example/x", credentials: "include" }, h({ origin: "https://page.example" }))).toBe("include");
175 expect(requestCredentials({ url: "https://api.example/x" }, h({ origin: "https://page.example" }))).toBe("include");
176 expect(requestCredentials({ url: "https://api.example/x", credentials: "same-origin", referrer: "about:client" }, h())).toBe("include"); // no way to tell whose page this is
177 expect(requestCredentials({ url: "not a url", credentials: "same-origin" }, h())).toBe("include");
178 });
179});