# Hosts

A built playgrounds directory holds no base and no host's syntax. You say where it is served
and for which host, and the `playgrounds` command renders the response headers it needs
(docs/plan.md D68). Why headers at all: the sandbox frames must carry a policy of their own
(`frame/<policy>/`, one each), and the page's policy must not replace it.

```sh
playgrounds headers dist/playgrounds --base /playgrounds/ --for cloudflare
playgrounds verify dist --base /playgrounds/ --host cloudflare
```

`headers --for` is `cloudflare`, `netlify`, `nginx` or `caddy`; `verify --host` also takes
`none`, a host that sends no headers (below). `verify` reads the site as the host would serve
it and fails on a frame with no policy or two, a page policy that lacks `frame-src 'self'` or
`connect-src 'self'`, a missing blob, and a rule the host would refuse (too many, too long, or
the reserved header name). The Astro integration and `checks.playgrounds-site` run it for you.

Two options change what the frames' headers say:

- `--frame-ancestors '<sources>'` sets who may embed a frame (default `'self'`).
- `--x-frame-options keep|detach`: `keep` (default) leaves the site's own
  `X-Frame-Options` on the frames, which refuses a frame embedded from another origin;
  `detach` removes it there, so `frame-ancestors` decides.

## Cloudflare and Netlify

Both read a `_headers` file in the published directory: `headers/_headers`, or the file the
integration appends to the site's. The frames' rules come after the site's `/*` rule, remove
the site's policy with `!` and set their own. Cloudflare Workers static assets refuse a file
over 25 MiB; the build splits larger ones (protocol §1).

On Cloudflare, **one console error per page** appears under a strict policy: Cloudflare's bot
detection injects an inline script, which `script-src 'self'` refuses. Nothing breaks. To
remove it, turn off JavaScript detections: Security, Settings, then the edit icon for
"JS detections" (not possible with Bot Fight Mode). A nonce in a `<meta>` policy does not
help; Cloudflare reads only the header.

## nginx

`headers/nginx.conf` has one `location` per frame policy; include it in the `server` block that
serves the directory. With `precompress = true` it serves `<file>.br` (ngx_brotli).

## Caddy

`headers/Caddyfile` is a snippet for the site block. Caddy cannot defer a deletion, so with
`--x-frame-options detach` the frames' `-X-Frame-Options` runs before the site's own header:
set the site's `X-Frame-Options` outside the frame paths.

## Hosts without headers (GitHub Pages)

A host that cannot send response headers (GitHub Pages, an object bucket, a file server) uses
`host: "none"` (Astro integration; also `playgrounds verify --host none`, and `host = "none"`
for `lib.checkSite` and `playgrounds.host`). Nothing is written to `_headers`, and `verify`
does not look for headers. The sandbox still holds; a few protections are lost.

What `host: "none"` checks instead, besides the directory itself (index, manifests, blobs,
frames):

- **Each page that holds a playground carries its policy as a `<meta
  http-equiv="Content-Security-Policy">`** that is the first element of its `<head>` (a
  `<meta charset>` may come before it). A meta policy governs only what follows it. It allows
  `frame-src 'self'` and `connect-src 'self'` like a header policy. It may not carry
  `frame-ancestors`, `report-uri`, `report-to` or `sandbox`: a meta policy ignores them, so
  `verify` fails a page that lists one.
- **Each frame document has its own meta policy** as the first element of its `<head>`, with
  `default-src 'none'` and no network source. This is checked for every host.

What holds with no headers:

- The frames keep their own meta policy (`documentFor` in `packages/frame/build.ts`) and the
  iframe's `sandbox` attribute keeps them on an opaque origin with no network. A `blob:` worker under the meta policy reaches no network in Chromium, Firefox and
  WebKit (docs/plan.md D6 and O3; `tests/sandbox.spec.ts` runs with no headers).

What is lost:

- `frame-ancestors` and `sandbox` sent as headers. Another site could embed a frame page and
  run code in it under your origin, without the `sandbox` attribute. That code has no network
  (the meta policy) and can reach only what the origin's storage holds (`browserStorage()` in
  `packages/core/storage.ts` uses IndexedDB). The frame takes messages only from its parent
  window (`event.source`, `packages/frame/frame.ts`), so only the page that embeds it can talk
  to it: the code that runs is the embedder's own.
- The page's policy is a meta tag: no `frame-ancestors`, so your pages can be framed by
  others, and no `report-to`.
- Caching falls to the host's default (GitHub Pages: 10 minutes), where the headers
  would mark the content-addressed files `immutable`. The files are named by hash, so a stale
  copy is never wrong, only fetched again.

On GitHub Pages deployed from a branch, put an empty `.nojekyll` file in the published
directory, or Jekyll drops the files and directories that start with `_` (Astro's `_astro/`).
A deploy from a workflow does not need it. The directory's own files are served as they are:
`.wasm` as `application/wasm`.

```ts
// astro.config.ts
integrations: [playgrounds({ dir: process.env.PLAYGROUNDS, host: "none" })],
```

For a site on Astro with `security.csp`, the policy is already a meta element at the start of
the head; add `frame-src 'self'` and `connect-src 'self'` to its directives.

## A remote behind the proxy

A site that runs engines on a remote (`docs/remote.md`) proxies one path to it. The rules
for every host: upgrade the WebSocket, **drop `Cookie`**, set `X-Forwarded-For` to the client's
address as the proxy saw it (never what the client sent), do not buffer, and time out reads only
after more than the remote's 25 s ping. List the proxy in the remote's `--trusted-proxy`.

nginx:

```nginx
location /remote/ {
  proxy_pass http://127.0.0.1:8081/;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
  proxy_set_header Host $host;
  proxy_set_header Cookie "";
  proxy_set_header X-Forwarded-For $remote_addr;
  proxy_buffering off;
  proxy_read_timeout 120s;
  proxy_send_timeout 120s;
}
```

Caddy, which upgrades WebSockets by itself and sets `X-Forwarded-For` from the connection
(add `trusted_proxies` to the site's `servers` only when another proxy is in front of Caddy):

```caddyfile
handle_path /remote/* {
  reverse_proxy 127.0.0.1:8081 {
    header_up -Cookie
    flush_interval -1
    transport http {
      read_timeout 120s
    }
  }
}
```

Cloudflare and Netlify static hosting cannot run a remote: put it on a machine of your own
and give the page its `wss://` URL.

## Deploying

A visitor must never see an `index.json` that names a file the host does not have yet. Copy in
two steps:

1. Copy everything content-addressed: `blobs/`, `frame/`, `packages/` and `element/`. These
   names never change meaning, so a copy can run beside the live site.
2. Then copy `index.json`, `headers.json` and `headers/`, and the pages that use them.

The same order serves a rollback.
