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
api.mjs · 6.4 KB · 88 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 const about = (await T("status")).browser;
23 check(/^\d{4}\.(0[1-9]|1[0-2])v[1-9]\d*(-[0-9A-Za-z.]+)?$/.test(about?.version) && about.pluginApi >= 1, "status gives the browser's version as people see it (year.month v count) and the plugin interface level", JSON.stringify(about));
24
25 // ---- vault login ----
26 await T("page.goto", { url: base + "/login" });
27 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));
28 await T("vault.unlock", { master: "wrong master" }).then(() => check(false, "wrong master accepted"), (e) => check(e.code === "wrong-password", "wrong master password rejected"));
29 const st = await T("vault.unlock", { master: ctx.vaultMaster });
30 check(st.unlocked && st.count === 1, "unlock → one saved login");
31 const r = await T("login");
32 check(r.username === "demo" && !JSON.stringify(r).includes("demo-pass"), "login() returns the username only, never the password");
33 check(/Welcome, demo/.test((await T("page.text", { selector: "#who" })).text), "signed in with the vault");
34 check(new URL((await T("tabs.list"))[0].url).pathname === "/account", "after the login redirect the tab URL is /account");
35
36 // origin binding: a different origin (same machine, other host name) must not get the credential
37 await T("page.goto", { url: `http://localhost:${port}/login` });
38 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));
39
40 // ---- real, trusted input ----
41 await T("page.goto", { url: base + "/login" });
42 await T("page.type", { selector: "input[name=username]", text: "demo" });
43 await T("page.type", { selector: "input[name=password]", text: "nope" });
44 await T("page.press", { key: "Enter" });
45 await T("page.waitFor", { text: "Wrong username or password", timeoutMs: 5000 });
46 check(true, "typed a wrong password + Enter → rejected by the server");
47 await T("page.goto", { url: base + "/login" });
48 await T("page.type", { selector: "input[name=username]", text: "demo" });
49 await T("page.type", { selector: "input[name=password]", text: "demo-pass" });
50 await T("page.click", { selector: "button[type=submit]" });
51 await T("page.waitFor", { url: "/account", text: "Welcome, demo", timeoutMs: 5000 });
52 check(true, "typed credentials + a real click → signed in at /account");
53
54 // ---- tabs ----
55 await T("page.goto", { url: base + "/login" });
56 const before = (await T("tabs.list")).length;
57 await T("page.click", { selector: "a[target=_blank]" });
58 await sleep(1500);
59 const tabs = await T("tabs.list");
60 check(tabs.length === before + 1 && tabs.some((t) => t.url.endsWith("/free")), `target=_blank opens a tab (${before} → ${tabs.length})`);
61 // The keyboard goes to the new page (space scrolls or pauses it), not the address bar.
62 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"; })()`);
63 check(focused === "the page", "a link that opens a new tab gives that tab's page the keyboard", focused);
64 // Site icons: the tab shows the site's icon (a data: URL the browser fetched once per site, never loaded by the window itself).
65 const tabIcons = await (await ctx.chrome()).ev(`[...document.querySelectorAll("#tabs .tab .th img.fav")].map((i) => i.src.slice(0, 22))`);
66 check(tabIcons.length >= 1 && tabIcons.every((s) => s.startsWith("data:image/")), "tabs show the site's icon", JSON.stringify(tabIcons));
67 const iconHits = (await (await fetch(base + "/o/_icon-hits")).json()).hits;
68 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)`);
69 await T("tabs.close", { tab: tabs.find((t) => t.url.endsWith("/free")).id });
70 check((await T("tabs.list")).length === before, "tabs.close works");
71
72 const shot = await T("page.screenshot");
73 check(shot.png.length > 2000 && shot.width > 300, `screenshot ${shot.width}×${shot.height}`);
74
75 // ---- x402 approval policy ----
76 check((await T("x402.approval")).mode === "ask", "default payment approval is 'ask'");
77 const e0 = (await T("x402.events", { since: 0 })).next;
78 await T("x402.approval", { mode: "auto", maxUsd: 1 });
79 await T("page.goto", { url: base + "/premium" }); // $0.50: above the auto-approve threshold, so it would prompt
80 const kinds = (await T("x402.events", { since: e0 })).events.map((e) => e.kind);
81 check(kinds.includes("auto-decision") && kinds.includes("paid"), "auto mode under the cap: approved, paid, and logged", kinds.join(" → "));
82 await T("x402.approval", { mode: "deny" });
83 const e1 = (await T("x402.events", { since: 0 })).next;
84 await T("page.goto", { url: base + "/premium" });
85 const ev2 = (await T("x402.events", { since: e1 })).events;
86 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");
87 await T("x402.approval", { mode: "ask", maxUsd: 0 });
88}