Skip to content

feat(cli): serve bundles as websites on their own origins (#2457) - #2590

Merged
thiras merged 2 commits into
mainfrom
feat/gateway-sites
Oct 24, 2026
Merged

thiras merged 2 commits into
mainfrom
feat/gateway-sites

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Closes #2457.

What

decdn gateway serves every bundle as a website at http://[<ns>.]<hash32>.localhost:<port>/, with no browser extension.

  • Routing on a site origin: / serves index.html, or a file list with relative links. /<path> serves that entry, and /<dir>/ serves <dir>/index.html. /<dir> redirects to <dir>/. HTML, CSS and JS are served as themselves. Every response carries default-src 'self' 'unsafe-inline' 'unsafe-eval' data: blob:; connect-src 'self' http://<gateway>, so a page loads only from its own origin and can still reach /_delegate and /_capability on the gateway origin. Inline code is allowed because it ships in the same bundle as the page.
  • Spending without a token (site::may_spend): allowed for same-origin, for none (a typed URL or bookmark), and for top-level navigation from anywhere, at any path. Any other cross-origin request gets 403. A request with no Sec-Fetch-Site comes from outside a browser and needs the token. Payment uses the capability that covers the site host, then falls back to *.
  • Link rewriting (rewrite): decdn: URIs in HTML URL attributes, style attributes, <style> and CSS become /_decdn/<hash>… as the page streams. HTML goes through lol_html and CSS through a byte scanner that holds back a 69-byte tail between chunks, so a URI split across chunks is still caught. Served pages name no host or port.
  • /_decdn/ on a site origin: a subresource request gets the bytes, paid by this site. A navigation to a bundle redirects to that bundle's own site origin. A navigation to a single blob downloads it as an attachment.
  • Namespace label: <ns> goes left of the hash. That way a site's registrable domain is <hash32>.localhost, and no page can register a scope that covers other bundles.
  • Bare origin: unchanged with the token. A request without the token for /<hash>/[<path>] now redirects to the bundle's site at no cost, so http://127.0.0.1:8484/<hex>/ opens a site in any browser.

Dependencies

  • data-encoding, for base32. It was already in the lockfile through iroh.
  • lol_html 3, BSD-3-Clause. It pulls in cssparser, selectors and encoding_rs. cargo deny licenses and bans pass, and the crate-edges check passes.

Testing

  • cargo nextest run -p decdn-cli: 942 passed. That includes new unit tests for base32 and Host parsing, the Sec-Fetch decision, the CSP, and the HTML/CSS rewrites at every chunk size. Gateway-level tests cover index rewriting, directories, /_decdn/ subresources, redirects and downloads, cross-site refusal, the 404 for the API on a site origin, refusal of bad hosts, and that the bare origin still serves text.
  • clippy, cargo doc with warnings denied, the no-inline-test check, and crate edges all pass.
  • cargo deny advisories fail only on RUSTSEC-2026-0384 (tokio 1.54.0) and RUSTSEC-2026-0385 (ctutils), and both are already on main.
  • Not done: I haven't tried a real browser against a published bundle on testnet. I also haven't checked whether Safari resolves *.localhost (an acceptance item in the issue).

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings October 23, 2026 14:52
@github-code-quality

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

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

main f8f4b1a feat/gateway-sites c748f37 +/-
77% 77% 0%
View coverage of the 5 most impacted files.
File main f8f4b1a feat/gateway-sites c748f37 +/-
crates/node/src/dht/publish.rs 80% 78% -2%
crates/client/s...undle/engine.rs 65% 65% 0%
crates/cli/src/...ands/gateway.rs 69% 73% +4%
crates/cli/src/...eway/rewrite.rs 0% 100% +100%
crates/cli/src/...gateway/site.rs 0% 100% +100%

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


Updated October 23, 2026 15:15 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.

🔵 Needs a closer look

It introduces new per-bundle browser origins with a security-sensitive model (CSP, Sec-Fetch spend gating, DNS-rebinding host checks, streaming rewrite of untrusted content) that warrants final human review.

1 open issue
What changed in this PR

This PR extends decdn gateway so every content-addressed bundle is served as an isolated website at http://[<ns>.]<hash32>.localhost:<port>/, with no browser extension. Each bundle gets its own browser origin (via *.localhost loopback resolution + base32 host labels), so pages are isolated from one another and from the gateway. It adds a Content-Security-Policy, a Sec-Fetch-*-based spend gate (so pages pay without a token but cross-origin requests cannot), and streaming rewriting of decdn: URIs in HTML/CSS to root-relative /_decdn/<hash>… paths. The bare origin is largely unchanged, except a tokenless bundle request now redirects to the bundle's site.

Changes:

  • New site module: base32 Site host round-trip, may_spend Sec-Fetch gate, CSP, and site content-typing.
  • New rewrite module: streaming lol_html HTML rewriter and a chunk-safe CSS byte scanner (69-byte holdback) that rewrite decdn: URIs.
  • Gateway routing: Present enum, site routing + /_decdn/ handling, to_site redirects, entry_link, and a refactored Streaming body that passes bytes through the optional rewrite.
File Description
crates/​cli/​src/​commands/​gateway.rs Site routing, Present/Streaming, to_site/entry_link/redirect, rewrite-aware streaming, tokenless redirect.
crates/​cli/​src/​commands/​gateway/​site.rs New: Site host parse/encode, may_spend, CSP, content_type.
crates/​cli/​src/​commands/​gateway/​rewrite.rs New: streaming HTML/CSS decdn: URI rewriting.
crates/​cli/​src/​commands/​gateway/​site/​tests.rs New: host round-trip, Sec-Fetch decision, CSP, content-type tests.
crates/​cli/​src/​commands/​gateway/​rewrite/​tests.rs New: URI rewrite + chunk-boundary tests.
crates/​cli/​src/​commands/​gateway/​tests.rs New gateway-level site/redirect/download/cross-site tests.
crates/​cli/​Cargo.toml, Cargo.toml, Cargo.lock Add data-encoding and lol_html 3 dependencies.
CHANGELOG.md Document the site-serving feature.

I verified the trickier logic: base32 label length/trailing-bit rejection, the CSS 69-byte holdback (URI_HEAD-1) correctly never misses a cross-chunk URI and stays linear, the prev-byte tracking across drains, the streaming Streaming state machine (tail emission on clean EOF, abort on funding error), Content-Length suppression when a rewrite is active, loopback-only --listen enforcement, and the Sec-Fetch spend gate against the tests. These all hold up. The only issue found is a minor documentation inconsistency on the Rewrite enum variants.

Review effort: Balanced

alpergundogdu and others added 2 commits October 24, 2026 18:05
`decdn gateway` serves every bundle at
`http://[<ns>.]<hash32>.localhost:<port>/`: `index.html` at the root,
entries by path, a directory's `index.html`, HTML/CSS/JS as themselves
under a CSP that confines the page to its origin.

No extension is needed. `Sec-Fetch-*` lets the site's own requests,
typed URLs and top-level navigation spend; other cross-origin requests
get 403, and a request with no `Sec-Fetch-Site` needs the token. A site
pays with the capability covering its host, falling back to `*`.

`decdn:` URIs in HTML attributes, `<style>` and CSS are rewritten as
they stream (lol_html) to root-relative `/_decdn/<hash>...`, so served
pages name no gateway host or port. `/_decdn/` serves a page's
subresources and redirects navigation to the other bundle's site. A
tokenless bundle request on the gateway origin redirects to its site.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@thiras
thiras force-pushed the feat/gateway-sites branch from c748f37 to 6021ca8 Compare October 24, 2026 15:07
@thiras
thiras merged commit 4fff77e into main Oct 24, 2026
8 checks passed
@thiras
thiras deleted the feat/gateway-sites branch October 24, 2026 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gateway: serve bundles as websites, one browser origin per bundle, no extension needed

3 participants