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>
42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/textproto"
|
|
"strings"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// StripCoderCookies removes the session token from the cookie header provided.
|
|
func StripCoderCookies(header string) string {
|
|
header = textproto.TrimString(header)
|
|
cookies := []string{}
|
|
|
|
var part string
|
|
for len(header) > 0 { // continue since we have rest
|
|
part, header, _ = strings.Cut(header, ";")
|
|
part = textproto.TrimString(part)
|
|
if part == "" {
|
|
continue
|
|
}
|
|
name, _, _ := strings.Cut(part, "=")
|
|
if name == codersdk.SessionTokenCookie ||
|
|
name == codersdk.OAuth2StateCookie ||
|
|
name == codersdk.OAuth2RedirectCookie ||
|
|
name == codersdk.PathAppSessionTokenCookie ||
|
|
// This uses a prefix check because the subdomain cookie is unique
|
|
// per workspace proxy and is based on a hash of the workspace proxy
|
|
// subdomain hostname. See the workspaceapps package for more
|
|
// details.
|
|
strings.HasPrefix(name, codersdk.SubdomainAppSessionTokenCookie) ||
|
|
// Preview hosts use the __Host- name over HTTPS and the plain
|
|
// name over HTTP, so strip both.
|
|
strings.TrimPrefix(name, "__Host-") == codersdk.PreviewSessionTokenCookie ||
|
|
name == codersdk.SignedAppTokenCookie {
|
|
continue
|
|
}
|
|
cookies = append(cookies, part)
|
|
}
|
|
return strings.Join(cookies, "; ")
|
|
}
|