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.ts · 8.2 KB · 200 lines
| 1 | /** |
| 2 | * How the browser presents itself to web servers — kept Electron-free so it is unit-tested. |
| 3 | * |
| 4 | * Two jobs: |
| 5 | * |
| 6 | * 1. `brandedUserAgent`: a Chromium-compatible User-Agent that says honestly who we are. |
| 7 | * Electron's default UA carries an `Electron/x.y.z` token (which many WAFs treat as |
| 8 | * "embedded automation, not a browser") and an app token showing ELECTRON's version |
| 9 | * rather than ours. We drop both and append our own product token with a link to |
| 10 | * dbbasic.com — the same convention Edge ("Edg/…"), Opera ("OPR/…") and Vivaldi use. |
| 11 | * We do NOT claim to be Firefox or Chrome: the engine is Chromium, and a UA that |
| 12 | * contradicts the client hints, the JS APIs and the TLS fingerprint is worse for |
| 13 | * compatibility than an honest Chromium-based UA, besides being untrue. |
| 14 | * |
| 15 | * 2. `chromiumRequestHeaders`: our transport (src/transport.ts) builds requests with |
| 16 | * net.request, which does not add what Chromium's own loader would: client hints |
| 17 | * (`sec-ch-ua*`) and correct fetch metadata (`sec-fetch-*`). A server that compares the |
| 18 | * HTTP headers with what the page's own JS reports (`navigator.userAgentData`) sees a |
| 19 | * contradiction — the classic "this is not a real browser" tell — and may refuse the |
| 20 | * page. This restores what a real Chromium navigation sends. |
| 21 | */ |
| 22 | |
| 23 | export interface Product { |
| 24 | name: string; |
| 25 | version: string; |
| 26 | url: string; |
| 27 | } |
| 28 | |
| 29 | /** `Chrome/130.0.6723.191` → 130, or null. */ |
| 30 | export function chromeMajor(ua: string): number | null { |
| 31 | const m = /\bChrome\/(\d+)\./.exec(ua); |
| 32 | return m ? Number(m[1]) : null; |
| 33 | } |
| 34 | |
| 35 | const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 36 | |
| 37 | /** |
| 38 | * Electron's UA minus its `Electron/x` token and its app-name token, plus |
| 39 | * `<name>/<version> (+<url>)` at the end. |
| 40 | */ |
| 41 | export function brandedUserAgent(baseUA: string, product: Product): string { |
| 42 | const cleaned = baseUA |
| 43 | .replace(/\s+Electron\/\S+/g, "") |
| 44 | .replace(new RegExp(`\\s+${escapeRe(product.name)}\\/\\S+`, "g"), "") |
| 45 | .replace(new RegExp(`\\s*\\(\\+${escapeRe(product.url)}\\)`, "g"), "") // our own earlier link |
| 46 | .trim(); |
| 47 | return `${cleaned} ${product.name}/${product.version} (+${product.url})`; |
| 48 | } |
| 49 | |
| 50 | /** What the engine itself reports in `navigator.userAgentData` — the source of truth for client hints. */ |
| 51 | export interface Hints { |
| 52 | brands: Array<{ brand: string; version: string }>; |
| 53 | platform: string; |
| 54 | mobile: boolean; |
| 55 | } |
| 56 | |
| 57 | const sfv = (s: string) => `"${s.replace(/["\\]/g, "\\$&")}"`; |
| 58 | |
| 59 | export function secChUa(h: Hints): string { |
| 60 | return h.brands.map((b) => `${sfv(b.brand)};v=${sfv(b.version)}`).join(", "); |
| 61 | } |
| 62 | |
| 63 | /** Chrome only sends client hints / fetch metadata to potentially trustworthy origins. */ |
| 64 | export function isTrustworthy(url: URL): boolean { |
| 65 | if (url.protocol === "https:") return true; |
| 66 | return url.protocol === "http:" && (url.hostname === "localhost" || /^127(\.\d+){3}$/.test(url.hostname) || url.hostname === "[::1]"); |
| 67 | } |
| 68 | |
| 69 | const registrableish = (h: string) => h.split(".").slice(-2).join("."); |
| 70 | |
| 71 | function siteRelation(target: URL, initiator: URL | null): "none" | "same-origin" | "same-site" | "cross-site" { |
| 72 | if (!initiator) return "none"; |
| 73 | if (initiator.origin === target.origin) return "same-origin"; |
| 74 | // Good-enough "same site" (eTLD+1 is approximated by the last two labels; the worst case is |
| 75 | // calling a same-site request cross-site on multi-part public suffixes like .co.uk). |
| 76 | if (initiator.protocol === target.protocol && registrableish(initiator.hostname) === registrableish(target.hostname)) return "same-site"; |
| 77 | return "cross-site"; |
| 78 | } |
| 79 | |
| 80 | function parseUrl(v: string | null): URL | null { |
| 81 | if (!v) return null; |
| 82 | try { |
| 83 | return new URL(v); |
| 84 | } catch { |
| 85 | return null; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * The `Referer` header a browser sends, per the page's referrer policy. Chromium gives the |
| 91 | * handler the referrer as a Request PROPERTY and expects the network layer to turn it into |
| 92 | * a header; through our own transport that never happens, so we do it. Default policy is |
| 93 | * `strict-origin-when-cross-origin`, like every current browser. |
| 94 | */ |
| 95 | export function refererFor(target: URL, referrer: URL, policy: string): string | null { |
| 96 | const sameOrigin = referrer.origin === target.origin; |
| 97 | const downgrade = referrer.protocol === "https:" && target.protocol === "http:"; |
| 98 | const clean = new URL(referrer.href); |
| 99 | clean.username = ""; |
| 100 | clean.password = ""; |
| 101 | clean.hash = ""; |
| 102 | const full = clean.href; |
| 103 | const origin = `${referrer.origin}/`; |
| 104 | switch (policy) { |
| 105 | case "no-referrer": |
| 106 | return null; |
| 107 | case "origin": |
| 108 | return origin; |
| 109 | case "unsafe-url": |
| 110 | return full; |
| 111 | case "same-origin": |
| 112 | return sameOrigin ? full : null; |
| 113 | case "strict-origin": |
| 114 | return downgrade ? null : origin; |
| 115 | case "origin-when-cross-origin": |
| 116 | return sameOrigin ? full : origin; |
| 117 | case "no-referrer-when-downgrade": |
| 118 | return downgrade ? null : full; |
| 119 | default: // "" or strict-origin-when-cross-origin |
| 120 | return sameOrigin ? full : downgrade ? null : origin; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** What the Request object knows that its headers don't. */ |
| 125 | export interface RequestContext { |
| 126 | referrer?: string; |
| 127 | referrerPolicy?: string; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * The headers a real Chromium would send for this request, merged over what the renderer |
| 132 | * gave us. Returns a NEW Headers; the input is not modified. |
| 133 | * |
| 134 | * A top-level/frame navigation is recognised the way servers recognise it: an HTML-first |
| 135 | * `Accept` plus `Upgrade-Insecure-Requests: 1`. Everything else is a subresource, where |
| 136 | * the destination can only be guessed from `Accept` — scripts and XHR/fetch are |
| 137 | * indistinguishable here and are reported as `empty`, which is still far closer than the |
| 138 | * unconditional `no-cors`/`empty`/`none` the loader would otherwise send. |
| 139 | */ |
| 140 | export function chromiumRequestHeaders(url: URL, input: Headers, hints: Hints | null, ctx: RequestContext = {}): Headers { |
| 141 | const h = new Headers(input); |
| 142 | const refUrl = parseUrl(ctx.referrer ?? null); |
| 143 | const hasWebReferrer = !!refUrl && (refUrl.protocol === "https:" || refUrl.protocol === "http:"); |
| 144 | if (hasWebReferrer && !h.has("referer")) { |
| 145 | const r = refererFor(url, refUrl, ctx.referrerPolicy ?? ""); |
| 146 | if (r) h.set("referer", r); |
| 147 | } |
| 148 | if (!isTrustworthy(url)) return h; |
| 149 | |
| 150 | // client hints (the three low-entropy ones Chrome sends on every request) |
| 151 | if (hints) { |
| 152 | if (!h.has("sec-ch-ua")) h.set("sec-ch-ua", secChUa(hints)); |
| 153 | if (!h.has("sec-ch-ua-mobile")) h.set("sec-ch-ua-mobile", hints.mobile ? "?1" : "?0"); |
| 154 | if (!h.has("sec-ch-ua-platform")) h.set("sec-ch-ua-platform", sfv(hints.platform)); |
| 155 | } |
| 156 | |
| 157 | // fetch metadata |
| 158 | const accept = h.get("accept") ?? ""; |
| 159 | const navigation = /\btext\/html\b/.test(accept) && h.get("upgrade-insecure-requests") === "1"; |
| 160 | const initiator = parseUrl(h.get("origin")) ?? (hasWebReferrer ? refUrl : null) ?? parseUrl(h.get("referer")); |
| 161 | const site = siteRelation(url, initiator); |
| 162 | |
| 163 | if (navigation) { |
| 164 | h.set("sec-fetch-mode", "navigate"); |
| 165 | h.set("sec-fetch-dest", "document"); |
| 166 | h.set("sec-fetch-site", site); |
| 167 | h.set("sec-fetch-user", "?1"); // user-activated navigation (typed or clicked) |
| 168 | } else { |
| 169 | h.delete("sec-fetch-user"); |
| 170 | h.set("sec-fetch-mode", h.has("origin") ? "cors" : "no-cors"); |
| 171 | h.set("sec-fetch-dest", /^image\//.test(accept) ? "image" : /^text\/css\b/.test(accept) ? "style" : "empty"); |
| 172 | h.set("sec-fetch-site", site === "none" ? "same-origin" : site); |
| 173 | } |
| 174 | return h; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Whether a request should carry cookies. The Request the page made says ("omit", "same-origin" or "include"); "same-origin" |
| 179 | * means cookies only when the target is the page's own origin. Without that information, include (the old behaviour). |
| 180 | */ |
| 181 | export function requestCredentials(input: { url: string; credentials?: string; referrer?: string }, headers: Headers): "include" | "omit" { |
| 182 | const mode = input.credentials; |
| 183 | if (mode === "omit") return "omit"; |
| 184 | if (mode !== "same-origin") return "include"; |
| 185 | let target: URL; |
| 186 | try { |
| 187 | target = new URL(input.url); |
| 188 | } catch { |
| 189 | return "include"; |
| 190 | } |
| 191 | const from = (v: string | null | undefined): string | null => { |
| 192 | try { |
| 193 | return v && v !== "about:client" ? new URL(v).origin : null; |
| 194 | } catch { |
| 195 | return null; |
| 196 | } |
| 197 | }; |
| 198 | const initiator = from(headers.get("origin")) ?? from(input.referrer) ?? from(headers.get("referer")); |
| 199 | return initiator && initiator !== target.origin ? "omit" : "include"; |
| 200 | } |