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
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
policy.ts · 5.9 KB · 170 lines
| 1 | /** |
| 2 | * The policy engine. |
| 3 | * |
| 4 | * x402's whole value is that payment happens at machine speed. A wallet modal per |
| 5 | * $0.001 request destroys that. But auto-approval is also the entire attack surface: |
| 6 | * a hostile page can fire ten thousand 402 subresources in a loop, and because |
| 7 | * EIP-3009 is signature-based, our exposure is exactly what we agree to sign. |
| 8 | * |
| 9 | * So the signing policy IS the security boundary. Budgets are scoped by the pair |
| 10 | * (top-level origin the user is actually looking at) x (origin being paid), because |
| 11 | * a global cap alone lets one hostile tab spend the whole day's allowance, and a |
| 12 | * per-destination cap alone lets a hostile page fan out across a thousand domains |
| 13 | * it controls. We also cap payment *rate*, not just amount: draining $20 in 200 |
| 14 | * separate $0.10 payments is still a drain. |
| 15 | */ |
| 16 | |
| 17 | import { formatUsd } from "./assets.js"; |
| 18 | import type { Quote } from "./types.js"; |
| 19 | |
| 20 | export interface PolicyLimits { |
| 21 | /** Anything at or below this is paid without asking. */ |
| 22 | autoApproveUnderUsdMicro: bigint; |
| 23 | /** Hard ceiling per (topOrigin, destOrigin) pair per window. */ |
| 24 | perPairPerWindowUsdMicro: bigint; |
| 25 | /** Hard ceiling across everything per window. Last line of defence. */ |
| 26 | globalPerWindowUsdMicro: bigint; |
| 27 | windowMs: number; |
| 28 | /** Max payments per (topOrigin, destOrigin) per window, regardless of size. */ |
| 29 | maxPaymentsPerPairPerWindow: number; |
| 30 | /** Clamp on server-chosen maxTimeoutSeconds. Bounds how long a signed |
| 31 | * authorization stays settleable, so a server can't bank it and settle later. */ |
| 32 | maxAuthorizationLifetimeSeconds: number; |
| 33 | /** Networks we're willing to sign on. */ |
| 34 | allowedNetworks: string[]; |
| 35 | /** Schemes we implement. */ |
| 36 | allowedSchemes: string[]; |
| 37 | } |
| 38 | |
| 39 | export const DEFAULT_LIMITS: PolicyLimits = { |
| 40 | autoApproveUnderUsdMicro: 50_000n, // $0.05 |
| 41 | perPairPerWindowUsdMicro: 5_000_000n, // $5.00 |
| 42 | globalPerWindowUsdMicro: 20_000_000n, // $20.00 |
| 43 | windowMs: 24 * 60 * 60 * 1000, |
| 44 | maxPaymentsPerPairPerWindow: 500, |
| 45 | maxAuthorizationLifetimeSeconds: 120, |
| 46 | allowedNetworks: ["eip155:8453", "eip155:84532"], |
| 47 | allowedSchemes: ["exact"], |
| 48 | }; |
| 49 | |
| 50 | export type Verdict = |
| 51 | | { action: "allow"; reason: string } |
| 52 | | { action: "prompt"; reason: string } |
| 53 | | { action: "deny"; reason: string }; |
| 54 | |
| 55 | interface Counter { |
| 56 | spentUsdMicro: bigint; |
| 57 | count: number; |
| 58 | windowStart: number; |
| 59 | } |
| 60 | |
| 61 | export interface PolicyContext { |
| 62 | /** The page the user is actually looking at. Empty string for agent/CLI traffic. */ |
| 63 | topOrigin: string; |
| 64 | /** The origin we'd be paying. */ |
| 65 | destOrigin: string; |
| 66 | } |
| 67 | |
| 68 | export class PolicyEngine { |
| 69 | private pairs = new Map<string, Counter>(); |
| 70 | private global: Counter = { spentUsdMicro: 0n, count: 0, windowStart: 0 }; |
| 71 | /** Origins the user has explicitly blessed, with a standing allowance. */ |
| 72 | private grants = new Map<string, bigint>(); |
| 73 | |
| 74 | constructor( |
| 75 | public limits: PolicyLimits = DEFAULT_LIMITS, |
| 76 | private now: () => number = Date.now, |
| 77 | ) {} |
| 78 | |
| 79 | private roll(c: Counter): Counter { |
| 80 | const t = this.now(); |
| 81 | if (t - c.windowStart >= this.limits.windowMs) { |
| 82 | c.spentUsdMicro = 0n; |
| 83 | c.count = 0; |
| 84 | c.windowStart = t; |
| 85 | } |
| 86 | return c; |
| 87 | } |
| 88 | |
| 89 | private pairCounter(ctx: PolicyContext): Counter { |
| 90 | const k = `${ctx.topOrigin}>${ctx.destOrigin}`; |
| 91 | let c = this.pairs.get(k); |
| 92 | if (!c) { |
| 93 | c = { spentUsdMicro: 0n, count: 0, windowStart: this.now() }; |
| 94 | this.pairs.set(k, c); |
| 95 | } |
| 96 | return this.roll(c); |
| 97 | } |
| 98 | |
| 99 | /** User explicitly authorises an origin up to `usdMicro` for this session. */ |
| 100 | grant(destOrigin: string, usdMicro: bigint): void { |
| 101 | this.grants.set(destOrigin, (this.grants.get(destOrigin) ?? 0n) + usdMicro); |
| 102 | } |
| 103 | |
| 104 | evaluate(quote: Quote, ctx: PolicyContext): Verdict { |
| 105 | const { requirements: r } = quote; |
| 106 | const L = this.limits; |
| 107 | |
| 108 | if (!L.allowedSchemes.includes(r.scheme)) { |
| 109 | return { action: "deny", reason: `unsupported scheme "${r.scheme}"` }; |
| 110 | } |
| 111 | if (!L.allowedNetworks.includes(r.network)) { |
| 112 | return { action: "deny", reason: `network "${r.network}" not allowed` }; |
| 113 | } |
| 114 | if (quote.priceUsdMicro <= 0n) { |
| 115 | return { action: "deny", reason: "non-positive price" }; |
| 116 | } |
| 117 | |
| 118 | const pair = this.pairCounter(ctx); |
| 119 | const global = this.roll(this.global); |
| 120 | |
| 121 | if (pair.count >= L.maxPaymentsPerPairPerWindow) { |
| 122 | return { |
| 123 | action: "deny", |
| 124 | reason: `payment rate cap hit for ${ctx.destOrigin} (${L.maxPaymentsPerPairPerWindow}/window)`, |
| 125 | }; |
| 126 | } |
| 127 | if (global.spentUsdMicro + quote.priceUsdMicro > L.globalPerWindowUsdMicro) { |
| 128 | return { |
| 129 | action: "deny", |
| 130 | reason: `global budget exhausted (${formatUsd(global.spentUsdMicro)} of ${formatUsd(L.globalPerWindowUsdMicro)})`, |
| 131 | }; |
| 132 | } |
| 133 | if (pair.spentUsdMicro + quote.priceUsdMicro > L.perPairPerWindowUsdMicro) { |
| 134 | return { |
| 135 | action: "prompt", |
| 136 | reason: `${ctx.destOrigin} would exceed its ${formatUsd(L.perPairPerWindowUsdMicro)} budget`, |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | const granted = this.grants.get(ctx.destOrigin) ?? 0n; |
| 141 | if (granted >= quote.priceUsdMicro) { |
| 142 | return { action: "allow", reason: `within standing grant for ${ctx.destOrigin}` }; |
| 143 | } |
| 144 | if (quote.priceUsdMicro <= L.autoApproveUnderUsdMicro) { |
| 145 | return { action: "allow", reason: `${formatUsd(quote.priceUsdMicro)} under auto-approve threshold` }; |
| 146 | } |
| 147 | return { |
| 148 | action: "prompt", |
| 149 | reason: `${formatUsd(quote.priceUsdMicro)} exceeds auto-approve threshold`, |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | /** Call only after a payment is actually signed and sent. */ |
| 154 | record(quote: Quote, ctx: PolicyContext): void { |
| 155 | const pair = this.pairCounter(ctx); |
| 156 | const global = this.roll(this.global); |
| 157 | pair.spentUsdMicro += quote.priceUsdMicro; |
| 158 | pair.count += 1; |
| 159 | global.spentUsdMicro += quote.priceUsdMicro; |
| 160 | global.count += 1; |
| 161 | const g = this.grants.get(ctx.destOrigin); |
| 162 | if (g !== undefined) { |
| 163 | this.grants.set(ctx.destOrigin, g > quote.priceUsdMicro ? g - quote.priceUsdMicro : 0n); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | spentUsdMicro(): bigint { |
| 168 | return this.roll(this.global).spentUsdMicro; |
| 169 | } |
| 170 | } |