Repository navigation
feat!: speak the gateway's pending-key and scoped capability API - #15
Conversation
The loopback gateway holds one pending delegate key, registers a capability
without a scope, and pays with the capabilities it holds in registration
order. `delegate()` returns the pending key, `register_capability` takes the
capability alone and returns `{signer, pool, expiry}`, `capabilities()`
entries carry no `origin` or `scope`, and the clients drop `host` and the
`x-decdn-host` header. The fake gateway and the contract suite follow; tests
of the new behaviour are declared only for gateways after release 0.3.1.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A public gateway holds no pool and no daily allowance: it pays with the capabilities registered through the API on its bare host and answers 503 `the gateway cannot pay right now` when none can. The fake drops its allowance, answers the capability API and `/_status` on the public bare host, and serves cached blobs for free; a loopback refusal is always the 402. The public contract suite checks the API on the bare host for gateways after release 0.3.1. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A loopback client needs no token: the clients drop `token`, a loopback address alone selects the loopback context, and no request carries `x-decdn-token`. `decdn.local` (`read_loopback_token`, `default_data_dir`) and the `UNAUTHORIZED` code are removed. The fake gates every loopback route on browser headers (cross-site, a foreign origin, a browser with no Sec-Fetch-Site), counts its own sites as own origins, keeps a site's own spending rule, and redirects only a navigation to a bundle on the bare host. The loopback contract suite drops the token and checks the gate for gateways after release 0.3.1. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…POOL_UNFUNDED `register_capability(capability, scope)` takes a required scope: "any", or a `NamespaceScope(namespaces, signature)` the pool's owner signed, which the SDK passes through. `Registration` and `RegisteredCapability` carry the optional `namespaces` the gateway echoes. A 503 from a public gateway maps to `PAYMENT_REQUIRED`, as a 402 does; the `POOL_UNFUNDED` code is gone. The fake gateway requires `scope`, checks a namespace scope's shape and echoes its namespaces in the 201 and the listing. The loopback contract suite registers with scope "any" and checks a registration without a scope is 422. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oken (#2910) * feat(gateway)!: one pending delegate key, capabilities on both gateway modes The gateway holds at most one pending delegate key: GET /_delegate returns it until a capability claims it. PUT /_capability takes {"capability"} only, for a key the gateway holds, one capability per pool per key. The gateway pays every pull with the first registered capability that no funding stop retired and that sits outside the node expiry margin, and serves a cache hit with nothing that can pay. /_delegate, /_capability and /_status need no token and no Origin; a loopback gateway answers nothing cross-origin. A public gateway answers the same API on its bare host and pays with capabilities first, then from its own pool when it names a keystore. Removes the per-page scope, x-decdn-host selection, per-site delegate bounds, the pending-key TTL and the psl dependency. Co-Authored-By: Claude Opus 5.5 <[email protected]> * feat(gateway)!: pay only with capabilities, no keystore and no allowance A gateway, loopback or public, pays only with the capabilities pool owners register to its in-memory delegate keys: --keystore and --capability are refused, it opens no pool and keeps no buyer store past its run. The daily allowance (--allowance-bytes-per-day, --allowance) is gone; the caps of the capabilities bound what a gateway spends. A public gateway with nothing that can pay answers 503 with the canonical decdn: URI. The counter requests_allowance_exhausted is renamed requests_payment_required, /_status loses its allowance field, and an active pull's payer is {signer, pool}. The e2e journeys fund their gateways through decdn_e2e::pool_owner. Co-Authored-By: Claude Opus 5.5 <[email protected]> * feat(gateway)!: a loopback gateway takes no token; browser headers gate it A loopback gateway writes no token file and reads no x-decdn-token. Every request passes a browser-header check after the Host check: a cross-site page, an Origin that is none of the gateway's own (the bare host and its site origins), and a browser that sends no Sec-Fetch-Site (an Origin, or a Mozilla/ User-Agent) get 403. A client outside a browser passes. On the bare host, a browser's navigation to a bundle or entry redirects to its site; any other request is served there. Site pages may connect to their own origin only. decdn dashboard reads the gateway's /_status with no token. Moves the SDK pins to decdn/sdk-typescript#28 and decdn/sdk-python#15, whose contract suites pass against this gateway. Co-Authored-By: Claude Opus 5.5 <[email protected]> * feat(gateway): a pool owner scopes a capability to its namespaces A capability registered with a scope that its pool's owner signed (EIP-712 CapabilityScope, domain decdn-capability-scope) pays only for requests that one of the scope's namespace pointers serves: the pointed bundle on <ns>.<host>. Scoped matches pay before unscoped capabilities. The listing, /_status and the dashboard show each capability's namespaces. `decdn pool assign --namespace` signs the scope and prints the whole PUT /_capability body. Co-Authored-By: Claude Opus 5.5 <[email protected]> * feat(gateway)!: every capability registration states its scope `PUT /_capability` requires `scope`: "any" for a capability that pays for any content, or a pool owner's signed namespace scope. A body with no scope is refused, so no capability pays for everything by accident. `decdn pool assign --any-namespace` prints the "any" registration. A loopback gateway selects a scoped capability by the namespace the request's decdn: URI names (`?ns=`, or the site label). A top-level GET navigation passes its browser check from any site, so a link on any page opens loopback content. The gateway_pointer journey funds its gateway with a namespace-scoped capability. Moves the SDK pins to decdn/sdk-typescript@1dab70e and decdn/sdk-python@3b502a9, which send the scope and map 503 to PAYMENT_REQUIRED. Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(adr): point ADR 003 and the overview at the gateway's capability scope Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
PR review: #15 (feat!: speak the gateway's pending-key capability API)ContextRead-only review of branch Critical (fix before merge)
Important
Suggestions
Strengths
Not verifiedThe test suite was not run (plan mode). The decdn harness change (one pending key, a pool per flavour, public pre-registration) must land with this PR. |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…irst As the gateway's Delegates::paying does: the earliest usable capability scoped to the namespace (the URI's on loopback, the serving pointer's on public), else the earliest "any" one. A scope may name namespace 0. The browser-header gate refuses before anything pays. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…or an answer outside the contract localhost and every address the gateway counts as loopback (127.0.0.0/8, ::1) open a loopback client. A delegate, capability or pointer answer that is not the JSON the contract names raises DecdnError HTTP. NamespaceScope, Registration and RegisteredCapability keep namespaces as tuples. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ture order The loopback gate lets a top-level GET navigation through from any site, a site spends on a navigation only when it loads a whole page, and a scope signature's length is checked after the key and pool 409s, as the gateway's browser_gate, may_spend and verify_owner do. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Manifest fetches lose namespace scope and can break scoped-only reads, response readers accept invalid field types, and the fake gateway advertises incorrect CORS methods.
3 open issues
What changed in this PR
This PR updates the Python SDK to the gateway’s pending-key and scoped-capability API, replacing token-based loopback access and public daily allowances.
Changes:
- Removes token and host options, adds namespace scopes, and updates capability result types.
- Unifies payment errors and adds handling for malformed JSON success responses.
- Updates the fake gateway, contract tests, documentation, and packaging checks for the new API.
| File | Description |
|---|---|
tests/unit/test_pointer.py |
Funds the public fake used by pointer tests. |
tests/unit/test_loopback.py |
Tests pending keys, scopes, registration, and malformed responses. |
tests/unit/test_local.py |
Removes obsolete token-helper tests. |
tests/unit/test_http.py |
Updates status-to-error mapping assertions. |
tests/unit/test_fake_gateway.py |
Tests capability payments, browser gates, and registration rules. |
tests/unit/test_exports.py |
Checks the new scope exports. |
tests/unit/test_client.py |
Updates client classification, payment, and credential-free request tests. |
tests/support/flavour.py |
Supports scoped registration across both client flavours. |
tests/fake_gateway.py |
Models pending keys, scoped payments, and browser-header gating. |
tests/contract/test_public.py |
Adds release-gated public capability API tests. |
tests/contract/test_loopback.py |
Replaces token tests with release-gated capability and gate tests. |
tests/contract/fixture.py |
Adapts fixtures to shared pending keys and separate pools. |
src/decdn/pointer.py |
Routes pointer responses through the JSON error wrapper. |
src/decdn/loopback.py |
Adds scope types and revises capability requests and results. |
src/decdn/local.py |
Removes local token helpers. |
src/decdn/errors.py |
Removes obsolete authentication and pool-funding error codes. |
src/decdn/client.py |
Removes token and host options and authentication headers. |
src/decdn/_http.py |
Unifies payment status mapping and wraps JSON decoding errors. |
src/decdn/_core.py |
Detects loopback addresses and removes authentication state. |
src/decdn/__init__.py |
Exports capability scope types. |
scripts/check-package.sh |
Replaces the removed module in the wheel import check. |
README.md |
Documents the breaking API changes and gateway compatibility limits. |
CONTRIBUTING.md |
Documents the revised loopback contract fixture. |
Review effort: Balanced
…ly public gateways, no loopback token (#28) The `decdn gateway` changes how it pays and who may use it. This PR updates the SDK, the fake gateway and the contract suite to match. - **Loopback:** no token. Every route passes a browser-header gate. - The gateway holds one pending delegate key. - A capability is registered with a required scope: `"any"` or an owner-signed namespace scope. - A key holds one capability per pool, and capabilities from several pools. - **Public:** no daily allowance. It pays only with capabilities registered through the same API on its bare host, and answers 503 when none can pay. - **Payment order:** among usable capabilities (not spent, not inside the expiry margin), the earliest one scoped to the request's namespace pays. Failing that, the earliest `"any"` one pays. - On loopback, the request's namespace is the one its URI names (`?ns=` or the site's label). - On public, it is the namespace whose pointer serves the request, and only for that pointer's own bundle: site entries, and `/_decdn/` for the site's own hash. `?ns=` and `<ns>.<hash32>` select no scope. The gateway change is on decdn `main`, which pins this branch in `.github/sdk-typescript.sha`. sdk-python makes the same change in [decdn/sdk-python#15](decdn/sdk-python#15), and the two SDKs behave the same. ## Public API changes (breaking) - **Opening a client:** - `OpenOptions.token` and `OpenOptions.host` are removed. - A URL on `localhost` or a loopback address (127.0.0.0/8, `[::1]` in any spelling) opens a loopback client. Any other URL opens a public one, including an IPv4-mapped address, `0.0.0.0` and `decdn.localhost`. - In a page on a site origin, `open()` opens that site, whatever `gatewayUrl` names. - The client sends no `x-decdn-token`, no `x-decdn-host` and no `Origin`. From Node, `fetch` sends `User-Agent: node`, which the gate passes. - **Removed:** - The `@decdn/client/node` subpath, with `readLoopbackToken` and `defaultDataDir`. - The `UNAUTHORIZED` error code: a 401 maps to `HTTP`. - `POOL_UNFUNDED`. - **`client.loopback.delegate()`** returns the gateway's pending key. It is the same address on every call until a capability claims it. The gateway keeps keys in memory, so a restart loses them. - **`client.loopback.registerCapability({ capability, scope })`:** - `scope` is `"any"` or `{ namespaces, signature }`, exported as `CapabilityScope`. - It returns `Registration`, newly exported: `{ signer, pool, expiry, namespaces? }`. - Before sending anything, it refuses with `CONFIG` an empty `namespaces`, an id that is not a u64 of at least 1, or a signature that is not `0x` plus 65 bytes. Otherwise it sends the signature as given, and the gateway checks it. - **`client.loopback.capabilities()`** lists the held capabilities in registration order, as `RegisteredCapability` entries `{ cap, expiry, namespaces?, pool, signer, spent }`. `namespaces` is present only on a namespace-scoped capability. - **`client.loopback` on a non-loopback client** throws `CONFIG`: "the SDK offers the delegate and capability API on a loopback client alone: open one on a localhost or loopback-address URL". - **`PAYMENT_REQUIRED`** is a loopback gateway's plain-text 402, and a public gateway's 503 that names a fallback URI. It carries `retryAfterSecs` and `fallbackUri`. Any other 503 is `HTTP`. - **Bad answers:** a delegate, capability or pointer answer that is not JSON, lacks a field its contract names, or has a field of another type raises `HTTP`: "the gateway's answer to `<path>` is not the JSON its contract names: …". The error carries the body. It no longer surfaces as a raw `SyntaxError` or a silent `undefined`. ## Fake gateway **Loopback gate.** A top-level `GET` (`Sec-Fetch-Mode: navigate` and `Sec-Fetch-Dest: document`) passes first. Otherwise, in this order: 1. `Sec-Fetch-Site: cross-site` gives 403. 2. An `Origin` that is neither the bare host nor a site origin `http://[<ns>.]<hash32>.localhost:<port>` gives 403. 3. No `Sec-Fetch-Site`, on a request with an `Origin` or a `Mozilla/` user agent, gives 403. **Loopback routing.** - A site origin spends only for its own pages, for the user (`same-origin`, `none`, or no `Sec-Fetch-Site`), and for a navigation to a document. - On the bare host, a browser's navigation to a bundle or entry redirects to its site. **API, both modes:** - `/_delegate` keeps a single pending key. - `PUT /_capability`, in the real gateway's check order: 1. 422 for a body that does not parse, an unknown field, or a missing or malformed scope. The scope must be `"any"` or a non-empty list of u64s (0 included) with an even-length hex signature. 2. 422 for a cap of 0, or an expiry inside the 420 s margin. 3. 409 for a key the gateway does not hold, or a second capability from one pool on one key. 4. 422 for a signature that is not 65 bytes. - The registration and the listing echo `namespaces` for a namespace scope. - On a loopback gateway, the API answers no cross-origin `OPTIONS`. On a public gateway, `/_delegate`, `/_capability` and `/_status` answer on the bare host to any origin, with a 204 preflight, and answer 404 on site hosts. **Paying.** - Payment is ranked as described above. - A pool in `drained` retires its capability, and the next one pays. - A raw blob the cache holds whole, and the empty root, need nothing that pays. - When nothing can pay: - Loopback answers 402: `the gateway holds no capability that can pay: register one with PUT /_capability`. - Public answers 503 JSON `{"error": "the gateway cannot pay right now", "retry_after_secs": 60, "uri": ...}`, with `Retry-After: 60` and the `Link` fallback. - `fake.fund()` gives a test a capability that pays. ## Contract suite and released gateways - **Loopback** (`describe.skipIf(!shippedAfter("0.3.1"))`): - The fixture has no `token`. Its `capabilities` field is one `{ capability }` per flavour, each from its own pool, all issued to the pending key. Real-gateway registrations pass `scope: "any"`. - The suite covers: - the pending key; - the 402 when nothing pays; - 422 for a missing or unknown scope, or an extra field; - 409 for an unheld key and for a second capability from one pool; - the listing's exact keys; - verified blob, entry, window and `head` reads; - each gate refusal, and the order the gate checks them; - a top-level navigation from another site passing while a frame's is refused; - the redirect of a navigation to its site. - Registering a namespace scope runs against the fake only, since a real gateway needs a pool-owner signature. - **Public** (`if (shippedAfter("0.3.1"))`): `/_delegate`, `/_capability` and `/_status` (with no `allowance`) on the bare host to any origin, and the API preflight on the bare host only. The fixture registers a capability before the suite, as decdn's harness does. - **Released gateways:** - The floor (v0.3.0) and the latest release (v0.3.1) have the old scoped, token-holding loopback API, so `scripts/decdn-contract.sh --tag` leaves out the loopback journey for any release at or before v0.3.1. - The public journey still runs against both. - Script tests check the script's `released_after` against `shippedAfter`. - The floor stays at 0.3.0 until decdn releases this API. Against 0.3.0 or 0.3.1, a loopback client gets the gateway's 401 as `HTTP`. The README says so. ## Docs The README covers: - the payment order and the expiry margin (one redeem interval plus 120 s, 420 s by default); - that only a pointer selects a scope on a public gateway; - the navigation exception and the site-origin rule; - that a browser sees a gate refusal as `NETWORK`; - the loopback hosts; - a `0.1, 0.2` row in the compatibility table. The docs link the ADR sections `#capabilities`, `#scope` and `#cross-origin-reads`. ## Gates Local, on b9ae4e2: - `pnpm lint` (eslint, prettier, tsc) passes. - `pnpm test:coverage`: 285 passed, 43 skipped, 94.3% statement coverage. One test fails: `publish.test.ts` "stops on a version npm has from another tarball", which fails on macOS because its `tar` has no `--mtime`. CI runs it on Linux. - `pnpm test:browser` did not run locally, because Playwright's Chromium is not installed here. CI's browser job runs it. Real gateway (`scripts/decdn-contract.sh --ref main`): - The loopback journey passes. - The public journey passes except "serves a directory under a site's /_decdn/ as its index.html". That test is unchanged from sdk `main`, and decdn `main`'s harness does not write its `docs/index.html` fixture yet (decdn/decdn#2818). On b9ae4e2, all 17 CI checks pass, including Node 20.19/22/24/26, browser (Chromium), coverage, package and the v0.3.0/v0.3.1 release contracts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Yigit Gurbulak <[email protected]>
… answers A namespaced URI's manifest is read with its ?ns=, so a capability scoped to that namespace pays for it. Delegate, capability and pointer answers whose fields have the wrong type raise HTTP. The fake's preflight advertises GET alone for /_delegate and /_status, as the gateway does. Co-Authored-By: Claude Opus 5.5 <[email protected]>

The
decdn gatewaychanges how it pays and who may use it."any"or an owner-signed namespace scope."any"one pays. On loopback, the request's namespace is the one its URI names. On public, it is the namespace whose pointer serves the request.decdn/decdn's gateway change pins this branch in
.github/sdk-python.sha.Public API changes (breaking)
Client(...)andAsyncClient(...)losetokenandhost. AlocalhostURL, or one on a loopback address (127.0.0.0/8,::1), opens a loopback client; any other URL opens a public one. Requests carry nox-decdn-token,x-decdn-host,OriginorSec-Fetch-*, and use httpx's own user agent, so the loopback gate serves them.decdn.local(read_loopback_token,default_data_dir), theUNAUTHORIZEDerror code (a 401 maps toHTTP) andPOOL_UNFUNDED.client.loopback.delegate()returns the gateway's pending key. It is the same address until a capability claims it.client.loopback.register_capability(capability, scope):scopeis"any"or aNamespaceScope(namespaces, signature)that the pool's owner signed. The SDK sends{"capability", "scope"}and passes the signature through unchanged.client.loopback.capabilities()lists the held capabilities in registration order.Registrationhassigner,pool,expiryandnamespaces.RegisteredCapabilityhassigner,pool,cap,expiry,spentandnamespaces.Registration,RegisteredCapabilityandNamespaceScope,namespacesis atuple[int, ...](Nonefor"any").PAYMENT_REQUIREDnow covers both "nothing can pay" cases: a loopback 402 and a public 503. It carriesretry_after_secsandfallback_uri.HTTP, not a rawKeyErrororJSONDecodeError. That covers a missing field and one of the wrong type, including a u64 that is fractional, negative or too large.?ns=, so a capability scoped to that namespace pays for it. This applies toget,head,resolveandmanifest(uri).Fake gateway
API, both modes:
pend).grant(..., namespaces=),capabilities).GET /_delegate,PUT /_capability,GET /_capabilityandGET /_statuson the bare host; 404 on site and namespace hosts.0xoptional).the scope signature is malformedfor a signature that isn't 65 bytes. As on the real gateway, this comes after both 409 checks.GET, PUTfor/_capability,GETfor/_delegateand/_status, andPUTfor/_pointer.Payment:
unfunded_poolsretires the paying capability, and the next one pays.cachedblobs are served when nothing can pay.unfundedhashes are refused without retiring anything.Loopback gate: it runs on every route before anything pays. A top-level
GETnavigation (Sec-Fetch-Mode: navigate,Sec-Fetch-Dest: document) passes from any site. Anything else is checked in this order:Sec-Fetch-Site: cross-site→ 403.Originthat is neither the bare host nor one of its sites → 403.Sec-Fetch-Site, on a request with anOriginor aMozilla/user agent → 403.A site spends only on its own pages (
same-origin), on the user's own requests (none), and on a top-level navigation. A navigation counts only when it loads a whole page (Sec-Fetch-Dest: document); a missingSec-Fetch-Modecounts as a navigation. Anything else → 403.When nothing can pay → 402
the gateway holds no capability that can pay: register one with PUT /_capability.Public: no gate. When nothing can pay → 503 JSON
{"error": "the gateway cannot pay right now", "retry_after_secs": 60, "uri": ...}, withRetry-After: 60and theLinkfallback. CORS*on everything, and the API paths answer a preflight with 204.Contract suite and released gateways
token.capabilitiesis one{"capability": "dcap1:..."}per flavour, each from its own pool, all issued to the gateway's one pending key. The suite registers each with"any".Every test of the new behaviour sits inside
if shipped_after("0.3.1"):, the mechanism CONTRIBUTING.md describes (decdn 0.3.1 is the latest release):scripts/decdn-contract.sh --tagsetsDECDN_CONTRACT_RELEASE, so a run against 0.3.1 or older never collects those tests. The fake and any--refrun them.Namespace scopes against the real gateway: the suite tests only that a scope-less registration gets 422. Echoing a namespace scope is tested against the fake only, because a real gateway needs a pool-owner signature.
Floors:
gateway-floorstays at 0.3.0 because no released gateway has the new API yet. Until one does, the floor covers public clients only; against 0.3.0 or 0.3.1, a loopback client gets the gateway's 401 asHTTP. The README says so, and the floor rises to the release that ships this API before the SDK's next release.harness-flooris still empty.Gates
ruff check,ruff format --check,mypyandpytest tests/unit tests/contract(385 passed) pass. The contract suite withDECDN_CONTRACT_RELEASE=0.3.1passes (30 tests).tests/scriptsfails on macOS (nosha256sum, gpg key generation, notomllibin the systempython3), as it does onmain; CI runs it on Linux.🤖 Generated with Claude Code