Agent-written pages are untrusted, because a prompt-injected agent can
write any HTML. The security review (coder/security-assessments#437)
showed that serving them like ordinary apps lets them reach the user's
apps in every workspace. Previews therefore get their own strict host
mode instead of the app path: a sandboxing CSP, framing only by the
dashboard, no shared app cookie, and no top-level loads.
Preview hosts sit under the existing wildcard app hostname, so operators
need no new DNS or certificates. The experiment stays out of the safe
experiments, so `--experiments='*'` does not enable it, until we have
more experience running previews. That is a rollout choice, not a known
safety problem.
<details>
<summary>Implementation details</summary>
Adds the `open-preview` experiment, which is hidden from
`--experiments='*'` and must be named explicitly, and serves agent
previews on preview hosts under the existing wildcard app hostname in a
strict mode. A security review (coder/security-assessments#437) showed
that agent-written pages served like ordinary apps can reach the user's
apps in every workspace, so preview hosts are locked down:
- `preview--<id>--<agent>--<workspace>--<owner>` serves a file preview
from the agent API (port 4), and `preview--<port>--…` serves a port.
Both use the owner sharing level and ignore port shares. The port
forwarding display app only hides the dashboard's ports UI, so it does
not gate port previews, just as it does not gate port URLs. Connection
logs record file previews as `workspace_app` and port previews as
`port_forwarding`, under the `preview--` slug.
- `coderd/x/previewhost` decides how previews are hosted. Coderd and
workspace proxies build one `Hosting` at startup with
`previewhost.Strict`, and every preview host request goes through
`servePreview`. A later preview domain on another site would add a
second constructor. `Hosting.Sandbox`, `Hosting.URL` and
`PreviewID.Slug` are used by the sign-in (#32522) and tool (#32440) PRs.
`API.previewsUnavailable` and `previewhost.ErrHostPrefixCookieOff` have
no reader here beyond the startup log; the sign-in (#32522) and tool
(#32440) PRs read them.
- `Hosting.ApplyPolicy` adds the CSP (`sandbox`, `default-src 'self'`,
`form-action 'self'`, `frame-ancestors` the dashboard) and drops `Link`
headers, `Clear-Site-Data` and cookies with a `Domain` attribute. A
response writer applies it to every response coderd writes on a preview
host, including its own sign-in, error and 403 pages, at `WriteHeader`,
because Go's `ReverseProxy` clears response headers after a 1xx. The
writer drops 1xx responses other than 101. `ReverseProxy` writes a
WebSocket 101 to the hijacked connection instead, so the proxy also
applies the policy to every proxied response in `ModifyResponse`.
- Requests whose `Origin` is another preview host of the same server get
a 403, before CORS is applied.
- `DBTokenProvider.Issue` refuses previews unless the experiment is on,
`--host-prefix-cookie` is enabled, the request is a subdomain request,
and the caller owns the workspace. The host-prefix requirement keeps a
preview's script from replacing the user's Coder session cookie; without
it, coderd still starts and logs a warning.
- A preview without a session gets a 401 page that posts a
`coder-preview-sign-in-needed` message to the dashboard, which then
signs the user in once (see #32522).
- A top-level open of a preview host gets a 403 page that links to the
dashboard, so a preview never loads outside the dashboard's wrapper.
Browsers send no fetch metadata to a plain-HTTP host other than
localhost, so there the page says previews need an HTTPS access URL.
Service workers are refused.
</details>
**Stack** (review bottom-up). Replaces the closed#31282 stack. Security
review: coder/security-assessments#437.
1. #32438 Agent file previews
2. #32439 Strict preview hosting and experiment, this PR
3. #32522 Preview sign-in
4. #32440 open_preview chat tool
5. #32441 Agents UI
6. #32442 Docs
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
There is currently an issue with subdomain workspace apps on workspace
proxies, where if you have a workspace proxy wildcard nested beneath the
primary wildcard, cookies from the primary may be sent to the server
before cookies from the proxy specifically.
Currently:
1. Use a subdomain app via the primary proxy `*.coder.corp.com`
a. Client sends no cookies
a. Server does token smuggling flow
a. Server sets a cookie `coder_subdomain_app_session_token` on
`*.coder.corp.com`
a. Server redirects client to reload the page
a. Request should succeed as usual
1. Wait until the primary proxy's session token cookie has expired in
the database (or make it invalid yourself)
1. Use a subdomain app via a separate proxy `*.sydney.coder.corp.com`
a. Client sends `coder_subdomain_app_session_token` cookie from
`*.coder.corp.com`
a. Server validates supplied cookie, it fails because it's expired
a. Server does token smuggling flow
a. Server sets a cookie `coder_subdomain_app_session_token` on
`*.sydney.coder.corp.com`
a. Server redirects client to reload page
a. Client sends BOTH cookies.
a. The server will only process the first cookie it receives, so if the
expired cookie for the primary proxy is sent first the request will end
up in a permanent loop on step b.
The fix is to append `_{hash(wildcard_access_url)}` to the subdomain
cookies as we cannot control browser behavior further. This avoids the
conflict as each proxy will only read it's specific cookie.
* chore: add /v2 to import module path
go mod requires semantic versioning with versions greater than 1.x
This was a mechanical update by running:
```
go install github.com/marwan-at-work/mod/cmd/mod@latest
mod upgrade
```
Migrate generated files to import /v2
* Fix gen