Skip to content

feat(cli): decdn gateway --public-url serves anyone from its own pool - #2608

Merged
thiras merged 1 commit into
mainfrom
feat/gateway-public
Oct 24, 2026
Merged

thiras merged 1 commit into
mainfrom
feat/gateway-public

Conversation

@thiras

@thiras thiras commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

What

decdn gateway --public-url <origin> turns the gateway into a hosted public HTTP gateway (#2593): it serves a domain to anyone, behind a proxy that terminates TLS, and pays for every request from its own pool.

  • Hosts (gateway/site.rs, SiteDomain/Route): <origin>/<hash>[/<path>] on the bare host (a blob streams, a bundle or entry redirects to its site over the public scheme), every bundle as a site at <scheme>://[<ns>.]<hash32>.<host>/ (the gateway: serve bundles as websites, one browser origin per bundle, no extension needed #2457 origins on the real domain), and a namespace host <ns>.<host> that answers 404 until it has a pointer (next PR). Every other Host is 403; the listen address's loopback names still reach the bare host for health checks.
  • No token, no capabilities. The API paths answer 404, Sec-Fetch-* decides nothing (the operator pays, within the allowance; curl sends none).
  • Operator payer (worker.rs, fetch.rs): Payment::{Held, Pool}; the worker builds one Payer::pool with its host (keystore unlocked at startup, so a wrong password fails before the gateway listens) and every pool pull shares it; a pull that ends with PoolReplaced retires it so the next pull's ledgers know only the new pool. Pool open and low-water top-up happen inside the existing fetch path, so there is no keeper. The buyer store lives in <data-dir>/store so the pool and lanes survive a restart.
  • Allowance (gateway/allowance.rs): --allowance-bytes-per-day (default 1 GiB) bounds the bytes the gateway delivers per UTC day, over every request, metered in memory on delivered bytes (entries, manifests, index pages). A request past it answers 402 before any pull; an entry whose manifest size does not fit what is left is refused before its pull too; a stream of unknown size is cut (body error, never a quiet short file) once the bound is reached, so delivery is bounded by the allowance plus the frames in flight. 402 and the 503 for a pool that cannot pay both carry Retry-After, Link: <decdn:…>; rel="alternate" and a JSON body, so a paying client continues on its own. The namespace hint (?ns=, the <ns>. site label) buys no bucket of its own: it is the client's to set (ADR 002), so a per-namespace allowance only makes sense for a namespace served through its owner-signed pointer, which is the next PR along with --allowance <ns>=<bytes>.
  • Spend accounting is tight against a hostile client. Every byte a pull lands is charged as it lands, so a blob that turns out not to be a manifest costs what it cost. Public pulls run a 2 MiB read-ahead (PUBLIC_READ_AHEAD_BYTES, via a new BlobFetch.read_ahead), and a response that does not end whole (a client that drops it, a cut, a fault) is charged the pull's maximum paid-but-undelivered bound (DROP_PENALTY_BYTES = read-ahead + pipe + one frame), so a connect-and-drop loop spends no more than it is charged. A body with a declared length that hyper stops polling once that length is out counts as whole (Streaming::drop), and that declared length is safe to trust because an entry pull is capped at it (BlobFetch.size_cap → the fetch's max_blob_bytes): a manifest that understates an entry cannot make the gateway pay for the rest, and an entry declared empty is answered empty with no pull at all (a zero size never lifts the cap: held_cap).
  • Review round (Copilot, nine comments, all addressed): one shared RunFunding per pool payer so concurrent pulls never race a topUp; leave_carried only for sequential payers; a Job.admit re-check once a pull has its slot; the listing admitted by its length; the manifest read stops at the allowance; a PullGuard armed before the worker has the job (a request dropped while waiting for its first byte is charged); the resolved keystore pinned before the store dir replaces the data dir (the e2e now runs without --keystore); HEAD resolving paths through the same URI parsing as GET; per-gateway test logs.
  • Upstream failures answer a bare 502 on a public gateway, with the detail (node ids, addresses, chain errors) in the log; loopback keeps showing its own user the detail.
  • /_decdn/ without Sec-Fetch-* is a navigation on a public site (redirect to the other bundle's own origin, or a download for a blob), so a browser that sends no Sec-Fetch-Mode can never have another bundle's page run on this origin. Loopback was already covered by may_spend refusing header-less requests.
  • Isolation caveat, documented: hash sites are separate origins, but share cookies and other site-scoped state unless the public host is on the Public Suffix List (as github.io is). The docs say to register it.
  • HEAD is answered from the cached bundle manifest (Content-Length, ETag = entry hash) at no pull of the entry; a raw blob's HEAD has no length. No Accept-Ranges yet (Range is the next PR; advertising it now would make huggingface_hub resume with a Range and restart on the 200).

Loopback mode is unchanged: same token, capabilities, Sec-Fetch-* rules, 405 on HEAD, and its store still dies with the process.

Rebased onto main after #2590 and #2607 merged. Part of #2593; the next PRs add Range, metrics, the namespace pointer and the HF_ENDPOINT resolve shim.

Reviewer notes

Read site.rs first (SiteDomain::route is the whole host policy), then worker.rs (Payment, PayerId, pool_payer, rebuild_pool_payer), then gateway.rs (mode, paid → allowance_refusal/serve/pull_failed, head_site). fetch.rs only lifts the keystore-load closure into Payer::keys so unlock can run it at startup; fetch_on calls the same code.

Design calls worth a look: scheme comes from --public-url, not X-Forwarded-Proto; the allowance is operator-set, in memory and one bucket (two automated security passes on the pushed commits flagged: a client-chosen ?ns= bucket defeats the cap, a finished-after-exhaustion response is unbounded, HEAD skipped the check, manifest bytes went uncharged when the blob was no manifest, a dropped response left the read-ahead uncharged, a manifest-declared size was trusted for "delivered whole", a zero declared size lifted the cap, a header-less /_decdn/ request was served as site content, and the shared registrable domain; all are fixed or documented here, each with a test); a --capability is allowed in public mode (the gateway then delegates for that pool, and cannot top it up).

Verification

Always:

  • pre-commit run on commit (all hooks passed; cargo deny findings are the known warn-only advisories)
  • CARGO_BUILD_WARNINGS=deny cargo clippy --workspace --all-targets --all-features: clean
  • cargo nextest run -p decdn-cli -p decdn-common -p decdn-e2e: 1507 passed (decdn-client untouched)
  • PR title uses an allowed type and the subject does not start with an uppercase letter

Only if touched:

  • anvil e2e: cargo nextest run -p decdn-e2e --features anvil-e2e -E 'binary(gateway_public)' passes (pool opened by the gateway, site entry byte-exact, HEAD from the manifest, bare blob, four concurrent pulls with zero decdn_serve_stream_voucher_rejected_total, 402 before any pull once the entry no longer fits the allowance, 403 for an unknown host)
  • User-visible change: CHANGELOG.md entry added by hand, one per PR

🤖 Generated with Claude Code

@thiras thiras added kind:feature New capability or a change to current behavior area:cli decdn binary: get, gateway, pool, publish, origin, setup labels Oct 24, 2026
@thiras thiras self-assigned this Oct 24, 2026
thiras added a commit that referenced this pull request Oct 24, 2026
@thiras
thiras force-pushed the feat/gateway-public branch 3 times, most recently from 0d2efe9 to aaa717d Compare October 24, 2026 14:58
@thiras
thiras force-pushed the feat/gateway-sites branch from c748f37 to 6021ca8 Compare October 24, 2026 15:07
Base automatically changed from feat/gateway-sites to main October 24, 2026 15:07
@thiras
thiras force-pushed the feat/gateway-public branch from aaa717d to 3453643 Compare October 24, 2026 15:18
@thiras
thiras marked this pull request as ready for review October 24, 2026 15:18
Copilot AI balanced review requested due to automatic review settings October 24, 2026 15:18
@github-code-quality

github-code-quality Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

main 4fff77e feat/gateway-public 7ac2af5 +/-
77% 77% 0%
View coverage of the 7 most impacted files.
File main 4fff77e feat/gateway-public 7ac2af5 +/-
crates/cli/src/...gateway/site.rs 100% 97% -3%
crates/node/src/dht/publish.rs 80% 78% -2%
crates/client/src/lib.rs 89% 89% 0%
crates/cli/src/...mmands/fetch.rs 50% 50% 0%
crates/cli/src/...ands/gateway.rs 73% 74% +1%
crates/cli/src/...teway/worker.rs 80% 82% +2%
crates/cli/src/...ay/allowance.rs 0% 100% +100%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated October 24, 2026 16:46 UTC

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Allowance enforcement and concurrent pool-funding defects can permit unintended operator spending or disrupt service.

9 open issues
What changed in this PR

Adds a hosted public mode to the existing CLI gateway, letting ordinary HTTP clients retrieve deCDN content using the operator’s payment pool while retaining loopback mode.

Changes:

  • Adds public-domain routing, per-bundle sites, and metadata-only HEAD responses.
  • Introduces shared pool payment, persistent buyer storage, and daily allowance accounting.
  • Adds gateway regression tests, an anvil-backed integration test, and deployment documentation.
File Description
Dockerfile Clarifies port publishing for public gateways in comments.
docker/​README.decdn.md Documents container deployment and operator funding.
crates/​e2e/​tests/​gateway_public.rs Adds a funded public-gateway integration journey.
crates/​e2e/​Cargo.toml Adds BLAKE3 for integration-test hashing.
crates/​common/​src/​cli/​mod.rs Updates gateway command help.
crates/​common/​src/​cli/​gateway/​tests.rs Tests public URL and allowance arguments.
crates/​common/​src/​cli/​gateway.rs Defines public-mode CLI options.
crates/​cli/​src/​commands/​gateway/​worker/​tests.rs Tests shared payers, replacement, and startup failures.
crates/​cli/​src/​commands/​gateway/​worker.rs Adds pool-backed jobs and payer retirement.
crates/​cli/​src/​commands/​gateway/​tests.rs Covers public routing, HEAD, refusals, and accounting.
crates/​cli/​src/​commands/​gateway/​site/​tests.rs Tests public hosts, schemes, ports, and content policy.
crates/​cli/​src/​commands/​gateway/​site.rs Generalizes site routing to public domains.
crates/​cli/​src/​commands/​gateway/​allowance/​tests.rs Tests allowance charging and daily reset.
crates/​cli/​src/​commands/​gateway/​allowance.rs Implements in-memory daily byte accounting.
crates/​cli/​src/​commands/​gateway.rs Integrates public serving, allowance checks, and HEAD handling.
crates/​cli/​src/​commands/​fetch/​tests.rs Tests declared-size cap behavior.
crates/​cli/​src/​commands/​fetch.rs Adds shared pool payers, early unlocking, and pull limits.
crates/​cli/​README.md Introduces public gateway usage.
CHANGELOG.md Records the public gateway feature.
Cargo.lock Records the integration-test dependency.

Review effort: Balanced

Comment thread crates/cli/src/commands/fetch.rs Resolved
Comment thread crates/cli/src/commands/fetch.rs Resolved
Comment thread crates/cli/src/commands/gateway.rs Resolved
Comment thread crates/cli/src/commands/gateway.rs Resolved
Comment thread crates/cli/src/commands/gateway.rs Resolved
Comment thread crates/cli/src/commands/gateway.rs Resolved
Comment thread crates/cli/src/commands/gateway.rs Resolved
Comment thread crates/cli/src/commands/gateway.rs Resolved
A public gateway serves a domain to anyone, behind a proxy that terminates
TLS: `<origin>/<hash>[/<path>]`, every bundle as a site at
`[<ns>.]<hash32>.<host>`, and a namespace host (`<ns>.<host>`) that answers
404 until it has a pointer. There is no token and no capability: every
request pays from the keystore's own pool, through one payer the pull worker
builds with its host and every pool pull shares (one set of funding facts,
so no two pulls send a `topUp` at once, and no pull clears a replacement a
sibling still runs on), and a daily allowance
(`--allowance-bytes-per-day`) bounds the bytes the gateway delivers. A
request past it, or an entry whose size does not fit what is left, answers
402 before any pull; a stream of unknown size is cut when the allowance runs
out; a pool that cannot pay answers 503. Each carries the canonical `decdn:`
URI in a `Link` header and a JSON body so a paying client continues on its
own. The namespace hint buys no bucket of its own: it is the client's to set.
Every byte a pull lands is charged, a manifest that does not parse included,
and a manifest read stops where the allowance does; a generated listing is
admitted by its length; a pull is admitted again once it has its slot, and
a request dropped at any point, its first byte still awaited included, is
charged the pull's read-ahead bound;
an entry's pull is capped at the size its manifest declares, so a manifest
cannot make the gateway pay for more than it says, and an entry declared
empty is answered empty with no pull; a public pull runs a small read-ahead, and a response that does not end
whole (a client that drops it, a cut, a fault) is charged what the pull may
have paid past the frames it delivered, so a client that connects and drops
spends no more than it is charged. `HEAD` is answered from the bundle
manifest at no pull of the entry. An upstream failure answers a bare 502
with the detail in the log. The buyer store lives under the data dir, so the
pool and its lanes survive a restart. A `/_decdn/` request with no
`Sec-Fetch-Mode` is a navigation: another bundle's pages never run on a
site's origin. Sites are separate origins; they are separate sites only when
the public host is on the Public Suffix List.
Part of #2593.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@thiras
thiras force-pushed the feat/gateway-public branch from 3453643 to 7ac2af5 Compare October 24, 2026 16:37
@thiras
thiras merged commit 636061c into main Oct 24, 2026
36 checks passed
@thiras
thiras deleted the feat/gateway-public branch October 24, 2026 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cli decdn binary: get, gateway, pool, publish, origin, setup kind:feature New capability or a change to current behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants