Browse documentation

A WebAssembly build fetches through a device file its host offers

Date: 2026-12-02 Area: apps, runtime, website Refines: ADR 0146's consequence that a transport request records no hop decisions in the capability audit: a destination a transport refuses is audited. Related: ADR 0146, ADR 0235, ADR 0236

Context

The wasm32-wasip1 modules have no sockets, so fetch failed in them without connecting (ADR 0235). ADR 0236 keeps the playground's server as its default engine until, among other things, the page answers fetch itself. The engine already has a seam for that: an embedder's transport receives each request after every check fetch makes and answers through a responder (ADR 0146). What the module lacked was a way to hand a request to whatever runs it.

WASI preview 1 offers files, clocks and randomness, and nothing that carries a request out; a function a host supplies would be an import beyond wasi_snapshot_preview1, which the browser's WASI host and every other preview 1 runtime would then have to provide, and which scripts/check-wasm-modules.js refuses. In the browser, a dedicated worker can make a synchronous XMLHttpRequest, so a host can perform a request inside a WASI call that blocks.

Decision

  • A request goes to the host through a file. A host that can perform HTTP requests preopens the directory /.goccia holding the file fetch (FETCH_DEVICE_PATH). For each request the applications' transport (Goccia.CLI.FetchDevice) opens that file for reading and writing, writes one request, reads the answer to its end and closes it. The host performs the request at the descriptor's first read; each opening is one exchange. The modules keep importing only wasi_snapshot_preview1.
  • The contract is JSON, then the body's bytes (Goccia.FetchDevice.Protocol). The request is one UTF-8 JSON object: version (1), method, url (as the built-in client would send it), headers ([name, value] pairs, each character one byte), redirect (follow, error or manual), timeoutMs (0 for none), maxResponseBytes and net, the run's net rules (one object per capability layer, with allowAll, allow, denyAll and deny, TGocciaCapabilities.NetRulesJSON). The answer is one line of UTF-8 JSON ended by a line feed: ok: true with status, statusText, url (the final URL), redirected, headers and bodyLength, followed by exactly that many bytes; or ok: false with error, the TypeError's message, and deniedScope when the host refused the destination. Unknown fields are skipped, so a host may add some; an answer that breaks the rest fails the fetch with TypeError: fetch device answered malformed data: ….
  • One codec for the hosts. scripts/lib/fetch-device-codec.mjs holds the host side of the contract, which the Node test host and the browser runner both use, so they accept and answer the same requests.
  • A host without the file offers nothing. The applications look for the file for each engine, on a build without sockets only (GOCCIA_NO_SOCKETS), and install the transport where they set an engine's settings (ApplySettings). Without it no transport is installed and fetch fails as before, without connecting.
  • The engine still decides everything before network work (ADR 0146): the URL, the host by name against --allow-net, the method, an aborted signal, the cap on requests in flight. The device sees nothing the engine refused.
  • A host that can check hops and addresses does. A host that follows redirects and resolves names itself checks each hop's host against the request's net rules before it looks the name up, and each address the name resolves to before it connects, as FetchOverNetwork does, connecting only to an allowed address and answering a refusal with the refused destination as deniedScope. The Node test host does (scripts/lib/fetch-device-net.mjs ports the engine's rules).
  • The transport checks what it sees. It holds the final URL a host reports to --allow-net and refuses a name that only the private scope allows, as the request's URL or the final one: private allows a name only provisionally, until its address is known, and no device reports the address it reached. A refusal rejects the fetch with PermissionDenied, as a refused redirect hop does.
  • A refused destination is audited. An answer with a denied scope, the device's or the transport's own, is recorded when it settles as a net.fetch deny of the scope's host at the fetch() call, with the answer's message as its reason (for any transport, ADR 0146's included).
  • The engine's deadlines come first. timeoutMs is the time left before the run's deadline or the request's AbortSignal.timeout(), whichever is sooner, plus 500 ms (FETCH_DEVICE_DEADLINE_SLACK_MS), as a worker's socket timeout runs past them by 2 s. A module has one thread, so while the host performs a request the run waits in that call; once it returns, a deadline that passed ends the run or aborts the signal before the answer is delivered, so a little slack suffices. It is shorter than the worker's because the playground's page terminates its worker 1.5 s past the run's timeout (KILL_GRACE_MS); 500 ms leaves the run 1 s to report its own timeout first, and website/src/__tests__/fetch-device.test.ts checks the constants against each other.
  • The playground's browser runner offers the device when a run allows the network. website/src/lib/wasi-shim.ts preopens /.goccia after the run's own directory (FreePascal's runtime takes the first preopened directory as the current one) and hands each request to createXhrFetchDevice (website/src/lib/fetch-device.ts), which performs it with a synchronous XMLHttpRequest in the worker, with the request's timeout, and checks the response against the ceiling. It refuses a host outside the playground's ALLOWED_HOSTS itself, as the request's URL and as the final one, so that nothing else in the module that writes to the device reaches the page's own origin or another host.
  • Node runs offer it for tests. scripts/run-wasi.js --fetch-device serves the file through node:wasi's imports and performs each request in a child Node process (scripts/lib/wasi-fetch-device-request.mjs), which blocks the WASI call as a host must and pins each connection to the address it allowed. scripts/check-wasm-fetch.js, run by scripts/check-wasm-modules.js in the wasm job, checks the module against a local server.

Considered options

  • An import of our own (goccia.fetch). A direct call with no serialization, but every host, the browser's included, would have to supply it, and the modules would stop being plain WASI programs.
  • Sockets through WASI preview 2 or wasi-http. FreePascal targets preview 1, and a browser offers no socket to back them.
  • Asynchronous fetch in the worker with the module suspended (JSPI or Asyncify). It would let the browser apply redirect: "manual", but JSPI is not in every browser the playground serves and Asyncify rewrites the whole module; a synchronous request needs neither.
  • The browser's fetch from the page instead of the worker. The worker blocks in the WASI call, so the page could answer only through shared memory, which needs cross-origin isolation (ADR 0236 declines it).

Consequences

  • A script's fetch works in the playground's browser engine, for the hosts the server allows, which takes one of the two conditions ADR 0236 set on making it the default.
  • The browser cannot do all the server's client does. It follows redirects itself and shows only the final URL, so redirect: "error" and "manual" fail only once one was followed ("manual" cannot be answered with the redirect itself). In the browser, the request's URL is checked before the request (by the engine and by the page's ALLOWED_HOSTS) and the final URL after it; nothing checks a middle hop, which the browser has requested before anyone sees it, and nothing checks the address any hop resolved to, so a name --allow-net allows that resolves to a private address is reached. The playground allows only public hosts. CORS applies: a server that does not allow the page's origin fails as a network error, and only the response headers it exposes are seen (icanhazdadjoke.com, the playground's example, sends Access-Control-Allow-Origin: * and exposes none beyond the CORS-safelisted ones). It drops request headers it forbids, and checks the body against the ceiling once the whole body has arrived. A response over HTTP/2 or HTTP/3 has no reason phrase, so its statusText is empty where the server's client, which speaks HTTP/1.1, reports one such as OK.
  • A refused destination is now audited for every transport; a transport's allowed hops still are not (ADR 0146).
  • On WebAssembly, --allow-net=private reaches an address literal such as 127.0.0.1 but not a name such as localhost, which a native build reaches.
  • An abort cannot interrupt a request the host is performing: the module's one thread waits in the read until the host answers or its timeoutMs passes, and the run's or the page's kill timer bounds that.
  • The contract has a version; a change that a reader would misread takes a new one.