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>
216 lines
7.3 KiB
Go
216 lines
7.3 KiB
Go
// Package previewhost decides how a Coder server hosts agent-authored
|
|
// previews: which hostnames serve them and what every response on those
|
|
// hostnames carries.
|
|
package previewhost
|
|
|
|
import (
|
|
"bufio"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
const (
|
|
maxHostnameLabelLength = 63
|
|
strictSandbox = "allow-scripts allow-same-origin allow-forms allow-modals allow-downloads"
|
|
)
|
|
|
|
// ErrHostPrefixCookieOff is why a deployment without --host-prefix-cookie
|
|
// serves no previews in any region. A preview page can set cookies for the
|
|
// dashboard's whole site, and only the __Host- prefix keeps such a cookie
|
|
// from replacing the Coder session cookie.
|
|
var ErrHostPrefixCookieOff = xerrors.New("previews require --host-prefix-cookie")
|
|
|
|
// CheckCookieConfig returns ErrHostPrefixCookieOff unless cfg gives the
|
|
// session cookie the __Host- prefix.
|
|
func CheckCookieConfig(cfg codersdk.HTTPCookieConfig) error {
|
|
if !cfg.EnableHostPrefix {
|
|
return ErrHostPrefixCookieOff
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Hosts are the preview hostnames of one server, coderd or a workspace proxy,
|
|
// and the policy their responses carry.
|
|
type Hosts struct {
|
|
pattern *regexp.Regexp
|
|
// host is the wildcard host with the serving port, such as
|
|
// "*.apps.example.com:8443".
|
|
host string
|
|
scheme string
|
|
// csp is added to every response on a preview host.
|
|
csp string
|
|
// sandbox is the iframe sandbox the dashboard frames a preview with.
|
|
sandbox string
|
|
}
|
|
|
|
// Strict returns the preview hosts of a server that serves strict previews under
|
|
// its app wildcard hostname, such as "*.apps.example.com", at the scheme and
|
|
// port of accessURL. dashboardURL is the only other origin that may frame a
|
|
// preview.
|
|
func Strict(wildcardHostname string, accessURL, dashboardURL *url.URL) (*Hosts, error) {
|
|
pattern, err := appurl.CompileHostnamePattern(wildcardHostname)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("compile preview hostname pattern: %w", err)
|
|
}
|
|
dashboardOrigin := (&url.URL{Scheme: dashboardURL.Scheme, Host: dashboardURL.Host}).String()
|
|
// Every request a page makes must stay on its own host, because requests
|
|
// to the user's other apps carry the user's app cookie. CSP cannot stop a
|
|
// page from navigating its own frame, so the dashboard wraps the frame in
|
|
// a page whose frame-src allows only the preview.
|
|
return &Hosts{
|
|
pattern: pattern,
|
|
host: appurl.SubdomainAppHost(wildcardHostname, accessURL),
|
|
scheme: accessURL.Scheme,
|
|
csp: "sandbox " + strictSandbox +
|
|
"; default-src 'self' 'unsafe-inline' 'unsafe-eval' data: blob:; frame-src 'self'" +
|
|
"; object-src 'none'; form-action 'self'; frame-ancestors 'self' " + dashboardOrigin,
|
|
sandbox: strictSandbox,
|
|
}, nil
|
|
}
|
|
|
|
// Sandbox returns the iframe sandbox the dashboard frames h's previews with.
|
|
func (h *Hosts) Sandbox() string {
|
|
return h.sandbox
|
|
}
|
|
|
|
// Covers reports whether host is a preview host of h. That includes a
|
|
// prefixed preview host, which Parse refuses, so its error page carries the
|
|
// policy too.
|
|
func (h *Hosts) Covers(host string) bool {
|
|
_, _, ok := appurl.ParsePreviewHostname(h.pattern, host)
|
|
return ok
|
|
}
|
|
|
|
// Parse returns the app and target that a preview host of h names.
|
|
func (h *Hosts) Parse(host string) (appurl.ApplicationURL, appurl.PreviewTarget, error) {
|
|
app, target, ok := appurl.ParsePreviewHostname(h.pattern, host)
|
|
if !ok || app.Prefix != "" {
|
|
return appurl.ApplicationURL{}, appurl.PreviewTarget{}, xerrors.Errorf("%q is not a preview hostname", host)
|
|
}
|
|
return app, target, nil
|
|
}
|
|
|
|
// URL returns the URL of app's preview at urlPath, a path and query such as
|
|
// "/docs/?page=2". It errors when the hostname label is longer than the DNS
|
|
// limit of 63 characters.
|
|
func (h *Hosts) URL(app appurl.ApplicationURL, urlPath string) (*url.URL, error) {
|
|
pattern, _, _ := strings.Cut(h.host, ".")
|
|
label := strings.Replace(pattern, "*", app.String(), 1)
|
|
if len(label) > maxHostnameLabelLength {
|
|
return nil, xerrors.Errorf("the hostname label %q is %d characters, over the DNS limit of %d", label, len(label), maxHostnameLabelLength)
|
|
}
|
|
ref, err := url.Parse(urlPath)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("parse URL path: %w", err)
|
|
}
|
|
u := &url.URL{
|
|
Scheme: h.scheme,
|
|
Host: strings.Replace(h.host, "*", app.String(), 1),
|
|
Path: ref.Path,
|
|
RawPath: ref.RawPath,
|
|
RawQuery: ref.RawQuery,
|
|
}
|
|
if u.Path == "" {
|
|
u.Path = "/"
|
|
}
|
|
return u, nil
|
|
}
|
|
|
|
// ApplyPolicy applies h's policy to the header of a response on a preview
|
|
// host. Applying it again changes nothing, so the CSP appears once.
|
|
func (h *Hosts) ApplyPolicy(header http.Header) {
|
|
if !slices.Contains(header.Values("Content-Security-Policy"), h.csp) {
|
|
header.Add("Content-Security-Policy", h.csp)
|
|
}
|
|
header.Del("Link")
|
|
// Clear-Site-Data clears cookies for the whole registrable domain.
|
|
header.Del("Clear-Site-Data")
|
|
// A cookie with a Domain attribute is sent to the user's other apps. This
|
|
// strips the ones the preview's server sets, including HttpOnly cookies
|
|
// that page script cannot create. Page script can still set Domain
|
|
// cookies, which the experiment accepts.
|
|
cookies := header.Values("Set-Cookie")
|
|
header.Del("Set-Cookie")
|
|
for _, line := range cookies {
|
|
if !hasDomainAttribute(line) {
|
|
header.Add("Set-Cookie", line)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Wrap returns rw with h's policy applied to every response written through
|
|
// it. Go's ReverseProxy clears the header map after forwarding each 1xx
|
|
// response, so a header set before proxying can be lost; the policy is
|
|
// applied when the final status is written. A proxied WebSocket upgrade is
|
|
// written to the hijacked connection instead, so the proxy applies the
|
|
// policy to it with ApplyPolicy.
|
|
func (h *Hosts) Wrap(rw http.ResponseWriter) http.ResponseWriter {
|
|
return &policyWriter{ResponseWriter: rw, hosts: h}
|
|
}
|
|
|
|
type policyWriter struct {
|
|
http.ResponseWriter
|
|
hosts *Hosts
|
|
wroteHeader bool
|
|
}
|
|
|
|
func (w *policyWriter) WriteHeader(code int) {
|
|
// A 103 Early Hints response can make the browser preload from other
|
|
// hosts before the final response's policy exists.
|
|
if code >= 100 && code < 200 && code != http.StatusSwitchingProtocols {
|
|
return
|
|
}
|
|
if !w.wroteHeader {
|
|
w.wroteHeader = true
|
|
w.hosts.ApplyPolicy(w.Header())
|
|
}
|
|
w.ResponseWriter.WriteHeader(code)
|
|
}
|
|
|
|
func (w *policyWriter) Write(b []byte) (int, error) {
|
|
if !w.wroteHeader {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
return w.ResponseWriter.Write(b)
|
|
}
|
|
|
|
// Flush applies the policy before a flush can send the header.
|
|
func (w *policyWriter) Flush() {
|
|
if !w.wroteHeader {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
_ = http.NewResponseController(w.ResponseWriter).Flush()
|
|
}
|
|
|
|
// Hijack hands over the connection for WebSocket upgrades. A response
|
|
// written to it bypasses WriteHeader, so its writer applies the policy.
|
|
func (w *policyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
return http.NewResponseController(w.ResponseWriter).Hijack()
|
|
}
|
|
|
|
func (w *policyWriter) Unwrap() http.ResponseWriter {
|
|
return w.ResponseWriter
|
|
}
|
|
|
|
// hasDomainAttribute reports whether a browser could read a Domain attribute
|
|
// in the Set-Cookie line.
|
|
func hasDomainAttribute(line string) bool {
|
|
_, attrs, _ := strings.Cut(line, ";")
|
|
for attr := range strings.SplitSeq(attrs, ";") {
|
|
name, _, _ := strings.Cut(attr, "=")
|
|
if strings.EqualFold(strings.TrimSpace(name), "domain") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|