@nibling/sandbox #
The page every sandbox origin serves (VISION D3, D27). The app on nibling.dev frames it from <id>.nibling.run in a hidden iframe. The page boots a kernel on its own origin and serves it to the app over a MessagePort. The kernel, its Service Worker, its storage, and the guest pages it serves stay on the sandbox's origin, so guest code cannot reach the app's OAuth session.
@nibling/sandbox/client is the app's side. connectSandbox({ origin }) creates the iframe, handshakes, and resolves with a SandboxKernel, the part of @nibling/kernel's Kernel the app drives. A local Kernel satisfies the same type.
Handshake #
- The coordinator creates the iframe with
allow="cross-origin-isolated". Its address carries the kernel's boot options (sandboxUrl), so the kernel boots while the page loads:?npm-gateway=<origin>once per npm gateway, in order (connectSandbox({ origin, npm: { gateways } })), and?guest=<prefix>to serve each guest server on its own origin (connectSandbox({ origin, guest }), VISION D28), with?guest-fallback=<prefix>for when another tab servesguest's (guestFallback, see Guest origins). The page drops a value that is not an http(s) origin, and aguestor fallback that is not one label prefix (<slug>-<hash>ordraft-<hash>, at most 56 characters). Anyone can open the page with any address, but only an accepted coordinator drives the kernel, and it picks the address. - On the iframe's
load, it posts{ t: "connect" }to the iframe's window with a transferred MessagePort, targeting the sandbox's origin. - The page accepts the first such message whose
event.originis one of its app origins and whoseevent.sourceis its parent. It ignores every other message. - The page answers
acceptedon the port at once, thenreadyonce the kernel boots, naming the guest prefix the kernel uses ({ t: "ready", guest }), orbootFailedwith the error.
After ready, the port carries the messages in src/protocol.ts. Requests correlate by id, processes by a proc handle the coordinator picks, and subscriptions by a sub number per listener. Stdio crosses as chunk messages whose buffers are transferred. A process started with pty takes keys as ptyWrite chunks and sizes as ptyResize, and its output arrives as stdout. Each HostFs method is a request named by op. Its error keeps the Node code, and readFile bytes cross transferred.
Anything running on the sandbox's origin can reach the page's port, so the client treats what arrives as untrusted. It drops a listening URL that is not on the sandbox's origin, and a guest write that is malformed or names a path outside the prefixes its listener asked for. putFile rejects an answer that is not a generation, and each fs call rejects an answer of the wrong shape for its method. A process drops output that is not bytes, a second end or exit, and an exit whose code is not a whole number or whose signal is not a signal name. An error keeps its code only when it looks like a Node code.
Guest origins #
With guest, the kernel serves the guest server on a port at <guest>--<port> in place of the sandbox's own first label: https://astro-minimal-k3f7a2qhw7xnb4e2--4321.nibling.run beside https://k3f7a2qhw7xnb4e2.nibling.run. The client takes a listening URL only on that port's guest origin.
A guest origin names the sandbox and the port, not the tab. Two tabs on one project boot two kernels on one sandbox origin, and in one browser their guest frames share a storage partition, so on one guest origin they would share its Service Worker and one kernel would serve the other's previews. So with guestFallback as well, the page elects (electGuest()): it asks for the nib-guests Web Lock on the sandbox's origin, without waiting. The page that gets it serves guest's origins and holds the lock until it goes. Any other page of the sandbox serves guestFallback's, which the app makes from a random name per connection (spareGuestLabel()), so those guests work but their storage is not kept. A page keeps the prefix it got for its kernel's life, even after the holder goes, because its previews already show those origins. The next page of the sandbox to boot gets the lock. ready names the prefix, and the client believes it only if it is one of the two it offered. Without guestFallback, the page takes no lock and serves guest's origins.
Until a guest origin's own Service Worker runs, its requests reach this package's server, which answers with guestResponse() from @nibling/kernel/server: the loader for a navigation, the guest worker's script at /~/sw.js, and errors for the rest. The loader asks its parent for a bridge. The app, which framed it, checks that the request comes from its own iframe on the origin it framed, creates a MessageChannel, and calls bridgeGuest(port, end) on the SandboxKernel. That sends the end to the page as { t: "guest", port, bridge }, and the page hands it to the kernel. The app posts the other end to the loader.
Configuration #
NIB_APP_ORIGINS names the coordinators, comma-separated, at build time and when the dev server starts. They are the only parents the page accepts, and the page's Content-Security-Policy: frame-ancestors. The default is http://127.0.0.1:5173, the app's dev server.
Development #
pnpm --filter @nibling/sandbox dev
This serves the page on port 5174. Chromium and Firefox resolve every *.localhost name to loopback, so http://<id>.localhost:5174 is a separate origin per sandbox, and http://<guest>--<port>.localhost:5174 one per guest server, which the dev server answers as the Worker does. The app's dev script starts this server too (see packages/app/README.md).
Deployment #
pnpm run deploy deploys the nibling-sandbox Worker with the built page as static assets. The Worker classifies the host by its first label (src/hosts.ts). It redirects the apex nibling.run to https://nibling.dev, serves the page on a sandbox's label, answers a guest's label (one holding --<port>) with guestResponse(), and answers anything else with 404. The build writes the guest worker's script and the app origins to /__nib-guest/, where the Worker reads them from its assets. Build with NIB_APP_ORIGINS=https://nibling.dev.
Cloudflare custom domains take no wildcard, so the Worker serves through the routes *.nibling.run/* and nibling.run/*. Wrangler does not create DNS records for routes, so scripts/dns.ts declares the two proxied records the nibling.run zone needs, and the deploy pipeline syncs them:
| Type | Name | Content | Proxy |
|---|---|---|---|
AAAA |
* |
100:: |
Proxied |
AAAA |
nibling.run |
100:: |
Proxied |
100:: is a discard address. Requests never reach it, because the routes answer first.