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

9 downloads

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
provider.mjs · 14.7 KB · 186 lines
1// A network layer the caller supplies: the browser makes no request of its own; the caller answers each one.
2export default async function (ctx) {
3 const { call: T, check, base, sleep } = ctx;
4 const UA = "ExampleCaller/1.0 (+https://caller.example/about)";
5 const hits = async () => (await (await fetch(base + "/hits")).json()).hits;
6 const rejects = (p) => p.then(() => null, (e) => e);
7
8 // ---- identity comes from the caller, and only from the caller ----
9 check((await rejects(T("net.provider", { enabled: true })))?.code === "bad-params", "turning it on without a userAgent is refused (identity is the caller's to give)");
10 check((await rejects(T("net.provider", { enabled: true, userAgent: "dbbasic-browser/1 (+https://dbbasic.com)" })))?.code === "bad-params", "…and the desktop browser's own identity is refused");
11 check((await T("net.provider")).enabled === false, "it is off by default");
12
13 // ---- the caller: answers by fetching for real, unless told otherwise ----
14 let stop = false;
15 const seen = [];
16 const rules = []; // [(path) => bool, async (req) => void]
17 const answerByFetching = async (r) => {
18 const res = await fetch(r.url, { method: r.method, headers: r.headers, redirect: "manual", ...(r.bodyBase64 ? { body: Buffer.from(r.bodyBase64, "base64") } : {}) });
19 const body = Buffer.from(await res.arrayBuffer());
20 await T("net.answer", { id: r.id, status: res.status, headers: Object.fromEntries(res.headers), bodyBase64: body.toString("base64") });
21 };
22 const handle = async (r) => {
23 try {
24 const path = new URL(r.url).pathname;
25 const rule = rules.find(([m]) => m(path));
26 if (rule) await rule[1](r);
27 else await answerByFetching(r);
28 } catch (e) {
29 if (!/no-request|no request/.test(String(e.message))) console.error("caller error:", e.message);
30 }
31 };
32 const loop = (async () => {
33 let after = 0;
34 while (!stop) {
35 let reqs = [];
36 try { reqs = (await T("net.pending", { afterId: after, waitMs: 400 })).requests; } catch { await sleep(200); }
37 for (const r of reqs) { after = Math.max(after, r.id); seen.push(r); void handle(r); }
38 }
39 })();
40
41 const st = await T("net.provider", { enabled: true, userAgent: UA, answerTimeoutMs: 3000 });
42 check(st.enabled === true && st.userAgent === UA && st.answerTimeoutMs === 3000, "turned on with the caller's identity and its own bound", JSON.stringify(st));
43 const settle = (url, opts = {}) => T("page.goto", { url, settle: { frameTimeoutMs: 1500, ...opts } }).then((r) => r.settle);
44
45 // ---- an ordinary load, answered by the caller ----
46 let v = await settle(base + "/free");
47 check(v.settled && v.outcome === "ok" && v.status === 200, "a page loads from the caller's answers", `${v.rule} in ${v.waitedMs} ms`);
48 const doc = seen.find((r) => new URL(r.url).pathname === "/free" && r.navigate);
49 check(!!doc && doc.headers["user-agent"] === UA, "the request was handed over as a navigation, carrying the caller's identity", doc && doc.headers["user-agent"]);
50 check((await T("page.eval", { js: "navigator.userAgent" })).value === UA, "…and the page itself sees the caller's identity, not the desktop browser's");
51 v = await settle(base + "/headers");
52 const sent = JSON.parse((await T("page.text")).text);
53 check(sent["user-agent"] === UA && !/dbbasic/i.test(sent["user-agent"]), "the server saw the caller's identity, with nothing of the desktop browser's", sent["user-agent"]);
54
55 // ---- every hop of a redirect is the caller's decision ----
56 seen.length = 0;
57 v = await settle(base + "/redir/chain");
58 const hops = seen.filter((r) => r.navigate).map((r) => new URL(r.url).pathname);
59 check(v.settled && v.status === 200 && v.final.url === base + "/free" && v.redirects.length === 2, "a redirect chain: final address, its status (200) and both hops reported", v.redirects.map((r) => r.status).join(","));
60 check(hops.length === 3 && hops[0] === "/redir/chain" && hops[2] === "/free", "…and each hop was a separate request to the caller", hops.join(" → "));
61
62 // ---- a request that follows a page says which page ----
63 seen.length = 0;
64 v = await settle(base + "/late");
65 const sub = seen.find((r) => !r.navigate);
66 check(!!sub && sub.follows.startsWith(base + "/late"), "a page's own request says which page it follows", sub && `${new URL(sub.url).pathname} follows ${sub.follows.replace(base, "")}`);
67
68 // ---- a page that has loaded and then moves itself: the request for where it goes says which page it came from ----
69 seen.length = 0;
70 v = await settle(base + "/moves-js", { quietMs: 1500 });
71 let mv = seen.find((r) => r.navigate && new URL(r.url).pathname === "/redir/chain");
72 check(!!mv && mv.follows === base + "/moves-js", "page A committed, then A's script moves to B: the request for B follows A's address", mv && `follows="${mv.follows.replace(base, "")}"`);
73 check(v.moves.length === 1, "…and it is reported as a move the page made, not a new visit", `${v.moves.length} move(s)`);
74 seen.length = 0;
75 v = await settle(base + "/moves-meta", { quietMs: 1500 });
76 mv = seen.find((r) => r.navigate && new URL(r.url).pathname === "/free");
77 check(!!mv && mv.follows === base + "/moves-meta", "the same for a <meta refresh>", mv && `follows="${mv.follows.replace(base, "")}"`);
78 seen.length = 0;
79 await settle(base + "/free");
80 check(seen.find((r) => r.navigate)?.follows === "", "a first visit (a goto) follows nothing", JSON.stringify(seen.find((r) => r.navigate)?.follows));
81
82 // ---- a refusal ends the load with the caller's reason ----
83 rules.push([(p) => p === "/refuse", (r) => T("net.fail", { id: r.id, kind: "blocked", detail: "the site's rules disallow this" })]);
84 let h0 = await hits();
85 v = await settle(base + "/refuse");
86 check(v.resources?.mainDocument === "failed" && v.resources.failedByKind.blocked === 1, "…and the counts record the caller's refusal by reason", JSON.stringify(v.resources));
87 check(v.outcome === "failed" && v.failure?.kind === "blocked" && v.failure?.source === "caller", "a refusal: failed, kind 'blocked', source 'caller'", JSON.stringify(v.failure));
88 check(v.failure?.description === "the site's rules disallow this", "…with the caller's own words", v.failure?.description);
89 check((await hits()) === h0, "…and the refused request never reached the server (the browser makes none of its own)");
90 check((await rejects(T("net.fail", { id: 999999, kind: "blocked" })))?.code === "no-request", "refusing an unknown request is 'no-request'");
91 check((await rejects(T("net.answer", { id: 999999, status: 200 })))?.code === "no-request", "answering an unknown request is 'no-request'");
92
93 // ---- the caller that never answers: its own bound, its own reason ----
94 rules.push([(p) => p === "/silent", () => {}]);
95 const t0 = Date.now();
96 v = await settle(base + "/silent", { timeoutMs: 30000 });
97 check(v.outcome === "failed" && v.failure?.kind === "caller-silent" && v.failure?.source === "observed", "no answer: failed with its own reason 'caller-silent' (not 'timed-out')", JSON.stringify(v.failure));
98 check(Date.now() - t0 >= 2800 && Date.now() - t0 < 9000 && v.waitedOnCallerMs >= 2000, "…after the caller's bound (3 s), and the wait is reported apart", `${Date.now() - t0} ms, ${v.waitedOnCallerMs} ms on the caller`);
99
100 // ---- waiting on the caller never trips the settle deadline ----
101 await T("net.provider", { answerTimeoutMs: 15000 });
102 rules.push([(p) => p === "/deliberate", async (r) => { await sleep(2500); await answerByFetching({ ...r, url: base + "/free" }); }]);
103 v = await settle(base + "/deliberate", { timeoutMs: 1000 });
104 check(v.settled === true && v.outcome === "ok" && v.rule !== "deadline", "waiting on the caller (2.5 s) does not trip a 1 s deadline", `${v.rule}, waited ${v.waitedMs} ms, ${v.waitedOnCallerMs} ms on the caller`);
105 check(v.waitedOnCallerMs >= 2000, "…and says how long it waited on the caller", `${v.waitedOnCallerMs} ms`);
106
107 // ---- a document the caller already fetched ----
108 seen.length = 0;
109 const html = '<!doctype html><title>Prefetched</title><body style="font:18px system-ui;padding:40px"><h1>Already fetched</h1><a id="l" href="next">next</a> <img src="pic.png" width="8" height="8">';
110 v = (await T("page.load", { url: base + "/prefetched/dir/page", status: 200, headers: { "content-type": "text/html; charset=utf-8" }, bodyText: html, redirects: [{ status: 301, to: base + "/prefetched/dir/page" }, { status: 302, to: base + "/prefetched/dir/page" }], settle: { frameTimeoutMs: 1500 } })).settle;
111 check(v.settled && v.status === 200 && v.final.url === base + "/prefetched/dir/page", "an already-fetched document loads at its own address with its status", `${v.status} ${v.final.url.replace(base, "")}`);
112 check((await T("page.eval", { js: "document.querySelector('#l').href" })).value === base + "/prefetched/dir/next", "…relative links resolve against that address");
113 check(v.redirects.length === 2 && v.redirects[0].status === 301, "…and the facts list the redirects that led to it, though the browser never saw them", v.redirects.map((r) => r.status).join(","));
114 check(!seen.some((r) => r.navigate && new URL(r.url).pathname === "/prefetched/dir/page"), "…without asking the caller for the document again");
115 check(seen.some((r) => new URL(r.url).pathname === "/prefetched/dir/pic.png"), "…while the page's own subresources still go to the caller", seen.map((r) => new URL(r.url).pathname).join(","));
116 check((await rejects(T("page.load", { url: base + "/x", status: 7, bodyText: "x" })))?.code === "bad-params", "a bad document is refused up front");
117
118 // ---- nothing a page leaves behind reaches the next page ----
119 await settle(base + "/free");
120 await T("page.eval", { js: "document.cookie = 'left=behind; path=/'; localStorage.setItem('left', 'behind'); sessionStorage.setItem('left', 'behind'); 1" });
121 seen.length = 0;
122 await settle(base + "/headers");
123 const next = JSON.parse((await T("page.text")).text);
124 const handed = seen.find((r) => r.navigate && new URL(r.url).pathname === "/headers");
125 check(!handed || !("cookie" in handed.headers), "a cookie a page set itself is not handed to the caller with the next request", handed && JSON.stringify(handed.headers.cookie));
126 const left = (await T("page.eval", { js: "JSON.stringify([document.cookie, localStorage.getItem('left'), sessionStorage.getItem('left')])" })).value;
127 check(left === JSON.stringify(["", null, null]), "…and the next page finds no cookie, localStorage or sessionStorage from the last one", left);
128
129 // ---- cookies: none by default; optionally kept for ONE visit ----
130 check((await T("net.provider")).cookies === "off", "cookies are off by default (none stored, none sent)");
131 v = await settle("" + base + "/cookie-loop");
132 check(v.outcome === "failed" && v.failure?.kind === "redirect-loop", "a site that sets a cookie and redirects back loops without the jar", `${v.outcome} ${v.failure?.kind}`);
133 check((await rejects(T("net.provider", { cookies: "forever" })))?.code === "bad-params", "an unknown cookie mode is refused");
134 await T("net.provider", { cookies: "visit" });
135 seen.length = 0;
136 v = await settle(base + "/cookie-loop");
137 check(v.settled && v.outcome === "ok" && v.redirects.length === 1 && /cookie came back/i.test((await T("page.text")).text), "with the per-visit jar the cookie comes back and the page loads", `${v.outcome}, ${v.redirects.length} redirect(s)`);
138 await settle(base + "/headers");
139 check(JSON.parse((await T("page.text")).text).cookie === undefined, "…and the next visit starts with no cookie (the jar lives for one visit)");
140 await T("net.provider", { cookies: "off" });
141
142 // ---- a page that never stops moving itself ends as a loop ----
143 v = await settle(base + "/self-loop", { timeoutMs: 30000 });
144 check(v.outcome === "failed" && v.failure?.kind === "redirect-loop" && v.failure?.source === "observed", "a page that keeps moving itself ends as a redirect loop, after a limited number of moves", `${v.outcome} ${v.failure?.kind} after ${v.moves?.length} moves`);
145
146 // ---- per-visit limits ----
147 check((await T("net.provider")).limits.requests === 0, "no limits by default");
148 check((await rejects(T("net.provider", { limits: { requests: -1 } })))?.code === "bad-params", "a bad limit is refused");
149 // a page that makes about ten requests of its own (the hostile test page; its targets are refused by the caller below)
150 rules.push([(p) => p === "/img" || p === "/fetch" || p === "/xhr" || p === "/beacon" || p === "/sse" || p === "/prefetch", (r) => T("net.fail", { id: r.id, kind: "blocked", detail: "test" })]);
151 await T("net.provider", { limits: { requests: 3 } });
152 seen.length = 0;
153 v = await settle(base + "/escape");
154 check(seen.length <= 3, "past the request limit the browser asks the caller for nothing more", `${seen.length} handed over`);
155 check(v.resources.failedByKind.other >= 1 && v.resources.failed >= 1, "…and the verdict counts those refusals as failures, by reason", JSON.stringify(v.resources));
156 await T("net.provider", { limits: { requests: 0 } });
157 seen.length = 0;
158 await settle(base + "/escape");
159 check(seen.length >= 6, "with the limit off again a visit makes all its requests", `${seen.length} handed over`);
160
161 // ---- permissions, pop-ups and downloads are refused in this mode ----
162 const tabsBefore = (await T("tabs.list")).length;
163 await settle(base + "/asks");
164 await sleep(500);
165 const perm = (await T("tabs.list")).find((t) => t.active).title;
166 check(perm === "perm:denied", "a permission prompt is refused", perm);
167 check((await T("tabs.list")).length === tabsBefore, "a pop-up opens no tab");
168 v = await settle(base + "/download");
169 check(v.outcome === "not-a-page" && v.download === "hello-report.txt", "a download is still reported as one, and nothing is written", `${v.outcome} (${v.download})`);
170
171 // ---- things the browser would do on its own are switched off, visibly ----
172 check((await rejects(T("login", {})))?.code === "caller-network-on", "login is refused while the caller network is on");
173 check((await rejects(T("reader.open", {})))?.code === "caller-network-on", "the reader (an AI call) is refused while it is on");
174 check((await T("net.policy")).blockWebSockets === true, "WebSockets are reported as refused while it is on, whatever net.policy says");
175 const c = await ctx.chrome();
176 check((await c.ev(`document.getElementById('cn').classList.contains('show')`)) === true, "a visible 'Caller network ON' badge is shown");
177
178 // ---- turning it off puts everything back ----
179 stop = true;
180 await T("net.provider", { enabled: false });
181 await loop;
182 check((await T("net.provider")).enabled === false, "turned off");
183 check((await c.ev(`document.getElementById('cn').classList.contains('show')`)) === false, "…and the badge goes away");
184 await T("page.goto", { url: base + "/free" });
185 check(/dbbasic-browser\/\S+ \(\+https:\/\/dbbasic\.com\)$/.test((await T("page.eval", { js: "navigator.userAgent" })).value), "the desktop browser's own identity is restored, and it fetches for itself again");
186}