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

0 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 site icons (favicons) in tabs, address bar suggestions and the bookmarks panel 7249579 15 hours, 56 minutes ago History
api.mjs · 6.1 KB · 86 lines
1// The automation API: transport guards, vault login, real input, tabs, screenshots, x402 policy.
2import http from "node:http";
3
4export default async function (ctx) {
5 const { call: T, check, base, sleep } = ctx;
6 const port = new URL(base).port;
7 const url = ctx.controlUrl;
8 const token = ctx.controlToken;
9
10 // ---- transport guards ----
11 const raw = (headers, body = '{"method":"status"}') => fetch(url, { method: "POST", headers, body }).then((r) => r.status);
12 check((await raw({})) === 401, "no token → 401");
13 check((await raw({ authorization: "Bearer wrong" })) === 401, "wrong token → 401");
14 check((await raw({ authorization: "Bearer " + token, origin: "https://evil.test" })) === 403, "request carrying an Origin header → 403 (a web page can't drive it)");
15 const u = new URL(url);
16 const hostCode = await new Promise((res) => {
17 const r = http.request({ host: u.hostname, port: u.port, path: "/rpc", method: "POST", headers: { host: `evil.test:${u.port}`, authorization: "Bearer " + token } }, (x) => { x.resume(); res(x.statusCode); });
18 r.end('{"method":"status"}');
19 });
20 check(hostCode === 403, "non-loopback Host header → 403 (DNS-rebinding guard)");
21 check((await raw({ authorization: "Bearer " + token })) === 200, "valid token → 200");
22
23 // ---- vault login ----
24 await T("page.goto", { url: base + "/login" });
25 await T("login").then(() => check(false, "login while locked should fail"), (e) => check(e.code === "locked", "login while the vault is locked is refused", e.code));
26 await T("vault.unlock", { master: "wrong master" }).then(() => check(false, "wrong master accepted"), (e) => check(e.code === "wrong-password", "wrong master password rejected"));
27 const st = await T("vault.unlock", { master: ctx.vaultMaster });
28 check(st.unlocked && st.count === 1, "unlock → one saved login");
29 const r = await T("login");
30 check(r.username === "demo" && !JSON.stringify(r).includes("demo-pass"), "login() returns the username only, never the password");
31 check(/Welcome, demo/.test((await T("page.text", { selector: "#who" })).text), "signed in with the vault");
32 check(new URL((await T("tabs.list"))[0].url).pathname === "/account", "after the login redirect the tab URL is /account");
33
34 // origin binding: a different origin (same machine, other host name) must not get the credential
35 await T("page.goto", { url: `http://localhost:${port}/login` });
36 await T("login").then(() => check(false, "cross-origin fill should fail"), (e) => check(e.code === "not-found" || e.code === "bad-origin", "localhost ≠ 127.0.0.1: the credential is refused", e.code));
37
38 // ---- real, trusted input ----
39 await T("page.goto", { url: base + "/login" });
40 await T("page.type", { selector: "input[name=username]", text: "demo" });
41 await T("page.type", { selector: "input[name=password]", text: "nope" });
42 await T("page.press", { key: "Enter" });
43 await T("page.waitFor", { text: "Wrong username or password", timeoutMs: 5000 });
44 check(true, "typed a wrong password + Enter → rejected by the server");
45 await T("page.goto", { url: base + "/login" });
46 await T("page.type", { selector: "input[name=username]", text: "demo" });
47 await T("page.type", { selector: "input[name=password]", text: "demo-pass" });
48 await T("page.click", { selector: "button[type=submit]" });
49 await T("page.waitFor", { url: "/account", text: "Welcome, demo", timeoutMs: 5000 });
50 check(true, "typed credentials + a real click → signed in at /account");
51
52 // ---- tabs ----
53 await T("page.goto", { url: base + "/login" });
54 const before = (await T("tabs.list")).length;
55 await T("page.click", { selector: "a[target=_blank]" });
56 await sleep(1500);
57 const tabs = await T("tabs.list");
58 check(tabs.length === before + 1 && tabs.some((t) => t.url.endsWith("/free")), `target=_blank opens a tab (${before} → ${tabs.length})`);
59 // The keyboard goes to the new page (space scrolls or pauses it), not the address bar.
60 const focused = await (await ctx.chrome()).ev(`(() => { const a = document.activeElement; return a ? (a.tagName === "WEBVIEW" ? (a.classList.contains("hidden") ? "a hidden page" : "the page") : a.id || a.tagName) : "nothing"; })()`);
61 check(focused === "the page", "a link that opens a new tab gives that tab's page the keyboard", focused);
62 // Site icons: the tab shows the site's icon (a data: URL the browser fetched once per site, never loaded by the window itself).
63 const tabIcons = await (await ctx.chrome()).ev(`[...document.querySelectorAll("#tabs .tab .th img.fav")].map((i) => i.src.slice(0, 22))`);
64 check(tabIcons.length >= 1 && tabIcons.every((s) => s.startsWith("data:image/")), "tabs show the site's icon", JSON.stringify(tabIcons));
65 const iconHits = (await (await fetch(base + "/o/_icon-hits")).json()).hits;
66 check(iconHits === 2, "and each site's icon is fetched once, however many of its pages are opened (127.0.0.1 and localhost here)", `${iconHits} fetch(es)`);
67 await T("tabs.close", { tab: tabs.find((t) => t.url.endsWith("/free")).id });
68 check((await T("tabs.list")).length === before, "tabs.close works");
69
70 const shot = await T("page.screenshot");
71 check(shot.png.length > 2000 && shot.width > 300, `screenshot ${shot.width}×${shot.height}`);
72
73 // ---- x402 approval policy ----
74 check((await T("x402.approval")).mode === "ask", "default payment approval is 'ask'");
75 const e0 = (await T("x402.events", { since: 0 })).next;
76 await T("x402.approval", { mode: "auto", maxUsd: 1 });
77 await T("page.goto", { url: base + "/premium" }); // $0.50: above the auto-approve threshold, so it would prompt
78 const kinds = (await T("x402.events", { since: e0 })).events.map((e) => e.kind);
79 check(kinds.includes("auto-decision") && kinds.includes("paid"), "auto mode under the cap: approved, paid, and logged", kinds.join(" → "));
80 await T("x402.approval", { mode: "deny" });
81 const e1 = (await T("x402.events", { since: 0 })).next;
82 await T("page.goto", { url: base + "/premium" });
83 const ev2 = (await T("x402.events", { since: e1 })).events;
84 check(ev2.some((e) => e.kind === "auto-decision" && e.approved === false) && !ev2.some((e) => e.kind === "paid"), "deny mode refuses the same page; no payment");
85 await T("x402.approval", { mode: "ask", maxUsd: 0 });
86}