Files
coder/coderd/workspaceapps/proxy_test.go
T
Danielle MaywoodandClaude Opus 5.5 02567e9194 feat: serve agent previews on strict preview hosts (#32439)
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>
2026-12-03 10:42:54 +00:00

382 lines
13 KiB
Go

package workspaceapps_test
// App tests can be found in the apptest package.
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/agent/agenttest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbfake"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/coderd/jwtutils"
"github.com/coder/coder/v2/coderd/workspaceapps"
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisionersdk/proto"
"github.com/coder/coder/v2/testutil"
)
type fakeSignedTokenProvider struct {
token *workspaceapps.SignedToken
// issue writes the error page Issue answers with.
issue func(rw http.ResponseWriter)
fromRequestCalls int
issueCalls int
}
func (s *fakeSignedTokenProvider) FromRequest(_ *http.Request) (*workspaceapps.SignedToken, bool) {
s.fromRequestCalls++
return s.token, s.token != nil
}
func (s *fakeSignedTokenProvider) Issue(_ context.Context, rw http.ResponseWriter, _ *http.Request, _ workspaceapps.IssueTokenRequest) (*workspaceapps.SignedToken, string, bool) {
s.issueCalls++
if s.issue != nil {
s.issue(rw)
}
return nil, "", false
}
func TestHandleSubdomain(t *testing.T) {
t.Parallel()
t.Run("IgnoresUntrustedForwardedHost", func(t *testing.T) {
t.Parallel()
hostnamePattern := "*--apps.test.coder.com"
hostnameRegex, err := appurl.CompileHostnamePattern(hostnamePattern)
require.NoError(t, err)
dashboardURL, err := url.Parse("https://dashboard.test.coder.com")
require.NoError(t, err)
provider := &fakeSignedTokenProvider{}
srv := workspaceapps.NewServer(workspaceapps.ServerOptions{
Logger: testutil.Logger(t),
DashboardURL: dashboardURL,
AccessURL: dashboardURL,
Hostname: hostnamePattern,
HostnameRegex: hostnameRegex,
RealIPConfig: &httpmw.RealIPConfig{
TrustedOrigins: []*net.IPNet{{
IP: net.ParseIP("10.0.0.1"),
Mask: net.CIDRMask(32, 32),
}},
},
SignedTokenProvider: provider,
})
forgedHost := appurl.ApplicationURL{
AppSlugOrPort: "app",
WorkspaceName: "workspace",
Username: "victim",
}.String() + "--apps.test.coder.com"
nextCalled := false
next := http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
nextCalled = true
})
// Given: a request with a forged X-Forwarded-Host set to a valid
// app hostname, and an immediate peer outside the trusted proxy
// config.
req := httptest.NewRequest(http.MethodGet, "https://dashboard.test.coder.com/", nil)
req.Header.Set(httpapi.XForwardedHostHeader, forgedHost)
req.RemoteAddr = "17.18.19.20:1234"
// When: HandleSubdomain runs.
srv.HandleSubdomain()(next).ServeHTTP(httptest.NewRecorder(), req)
// Then: it ignores untrusted X-Forwarded-Host, so the received
// dashboard host is used, the request falls through to the next
// handler, and the signed app token provider is never called.
require.True(t, nextCalled)
require.Zero(t, provider.fromRequestCalls)
require.Zero(t, provider.issueCalls)
})
// After consuming a smuggled API key, the handler redirects to strip the key.
// A path that smuggles a separate host (e.g. "//evil.com") must redirect back
// to the current origin, not off-site. The full path-sanitization matrix lives
// in Test_originLocalURL.
t.Run("APIKeySmugglingStaysOnOrigin", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
hostnamePattern := "*--apps.test.coder.com"
hostnameRegex, err := appurl.CompileHostnamePattern(hostnamePattern)
require.NoError(t, err)
dashboardURL, err := url.Parse("https://dashboard.test.coder.com")
require.NoError(t, err)
// StaticKey lets us mint a smuggled key the handler can decrypt.
// A256GCMKW needs a 32-byte key.
keycache := jwtutils.StaticKey{ID: "test", Key: generateSecret(t, 32)}
payload := workspaceapps.EncryptedAPIKeyPayload{APIKey: "fake-api-key"}
payload.Fill(time.Now())
encryptedAPIKey, err := jwtutils.Encrypt(ctx, keycache, payload)
require.NoError(t, err)
srv := workspaceapps.NewServer(workspaceapps.ServerOptions{
Logger: testutil.Logger(t),
DashboardURL: dashboardURL,
AccessURL: dashboardURL,
Hostname: hostnamePattern,
HostnameRegex: hostnameRegex,
RealIPConfig: &httpmw.RealIPConfig{
TrustedOrigins: []*net.IPNet{{
IP: net.ParseIP("10.0.0.1"),
Mask: net.CIDRMask(32, 32),
}},
},
SignedTokenProvider: &fakeSignedTokenProvider{},
APIKeyEncryptionKeycache: keycache,
})
host := appurl.ApplicationURL{
AppSlugOrPort: "app",
WorkspaceName: "workspace",
Username: "user",
}.String() + "--apps.test.coder.com"
// Set r.URL.Path directly: the slash-collapsing middleware writes to chi's
// RoutePath, not r.URL.Path, so the raw "//evil.com" survives to the handler.
// "x=1" rides along to confirm unrelated params are preserved.
req := httptest.NewRequest(http.MethodGet, "https://"+host+"/", nil)
req.Host = host
req.RemoteAddr = "10.0.0.1:1234"
req.URL.Path = "//evil.com/phish"
req.URL.RawQuery = url.Values{
workspaceapps.SubdomainProxyAPIKeyParam: {encryptedAPIKey},
"x": {"1"},
}.Encode()
rec := httptest.NewRecorder()
nextCalled := false
next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
nextCalled = true
})
srv.HandleSubdomain()(next).ServeHTTP(rec, req)
res := rec.Result()
defer res.Body.Close()
// The key is consumed and a redirect is issued instead of proxying.
require.Equal(t, http.StatusSeeOther, res.StatusCode)
require.False(t, nextCalled)
loc := res.Header.Get("Location")
require.NotEmpty(t, loc)
require.False(t, strings.HasPrefix(loc, "//"), "redirect %q must stay same-origin", loc)
parsed, err := url.Parse(loc)
require.NoError(t, err)
require.Empty(t, parsed.Scheme, "redirect %q must not carry a scheme", loc)
require.Empty(t, parsed.Host, "redirect %q must not carry a host", loc)
// The smuggled key is stripped and unrelated query params survive.
require.Empty(t, parsed.Query().Get(workspaceapps.SubdomainProxyAPIKeyParam))
require.Equal(t, "1", parsed.Query().Get("x"))
})
}
// Through the real server, an app whose slug is "preview" keeps its hostnames
// with the experiment off or on, and its requests are not taken for a
// preview's, while a request from a preview host is still refused.
func TestHandleSubdomainAppNamedPreview(t *testing.T) {
t.Parallel()
for _, enabled := range []bool{false, true} {
t.Run(fmt.Sprintf("Experiment=%t", enabled), func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
upstream := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
_, _ = rw.Write([]byte("app ok"))
}))
t.Cleanup(upstream.Close)
dv := coderdtest.DeploymentValues(t)
dv.HTTPCookies.EnableHostPrefix = true
if enabled {
dv.Experiments = []string{string(codersdk.ExperimentOpenPreview)}
}
client, _, api := coderdtest.NewWithAPI(t, &coderdtest.Options{
AppHostname: "*.test.coder.com",
DeploymentValues: dv,
})
first := coderdtest.CreateFirstUser(t, client)
me, err := client.User(ctx, codersdk.Me)
require.NoError(t, err)
r := dbfake.WorkspaceBuild(t, api.Database, database.WorkspaceTable{
Name: "site",
OwnerID: me.ID,
OrganizationID: first.OrganizationID,
}).WithAgent(func(agents []*proto.Agent) []*proto.Agent {
agents[0].Apps = []*proto.App{{Slug: "preview", Url: upstream.URL, Subdomain: true}}
return agents
}).Do()
_ = agenttest.New(t, client.URL, r.AgentToken)
coderdtest.NewWorkspaceAgentWaiter(t, client, r.Workspace.ID).Wait()
appHost := "preview--dev--site--" + me.Username + ".test.coder.com"
agentlessAppHost := "preview--site--" + me.Username + ".test.coder.com"
previewHost := "preview--0123abcd--dev--site--" + me.Username + ".test.coder.com"
pathApp := "/@" + me.Username + "/site.dev/apps/preview/"
noRedirects := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
}}
for _, tc := range []struct {
name string
host string
path string
origin string
wantStatus int
}{
{name: "App", host: appHost, path: "/", wantStatus: http.StatusOK},
{name: "AgentlessApp", host: agentlessAppHost, path: "/", wantStatus: http.StatusOK},
{name: "AppFromApp", host: agentlessAppHost, path: "/", origin: appHost, wantStatus: http.StatusOK},
{name: "AppFromPreview", host: appHost, path: "/", origin: previewHost, wantStatus: http.StatusForbidden},
{name: "PathAppFromApp", path: pathApp, origin: appHost, wantStatus: http.StatusOK},
{name: "PathAppFromPreview", path: pathApp, origin: previewHost, wantStatus: http.StatusForbidden},
} {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, client.URL.String()+tc.path, nil)
require.NoError(t, err)
if tc.host != "" {
req.Host = tc.host
}
req.Header.Set(codersdk.SessionTokenHeader, client.SessionToken())
if tc.origin != "" {
req.Header.Set(httpmw.OriginHeader, "http://"+tc.origin)
}
res, err := noRedirects.Do(req)
require.NoError(t, err)
body, err := io.ReadAll(res.Body)
_ = res.Body.Close()
require.NoError(t, err)
require.Equal(t, tc.wantStatus, res.StatusCode, "%s: %s", tc.name, body)
if tc.wantStatus == http.StatusOK {
require.Equal(t, "app ok", string(body), tc.name)
}
}
})
}
}
// A preview host's signed-out page asks the dashboard that frames it to sign
// it in, and the preview CSP allows that inline script.
func TestPreviewSignedOutPage(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
dv := coderdtest.DeploymentValues(t)
dv.Experiments = []string{string(codersdk.ExperimentOpenPreview)}
dv.HTTPCookies.EnableHostPrefix = true
client := coderdtest.New(t, &coderdtest.Options{
AppHostname: "*.test.coder.com",
DeploymentValues: dv,
})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, client.URL.String()+"/", nil)
require.NoError(t, err)
req.Host = "preview--8080--dev--site--alice.test.coder.com"
req.Header.Set("Sec-Fetch-Dest", "iframe")
res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
body, err := io.ReadAll(res.Body)
_ = res.Body.Close()
require.NoError(t, err)
require.Equal(t, http.StatusUnauthorized, res.StatusCode, string(body))
csp := res.Header.Values("Content-Security-Policy")
require.Len(t, csp, 1)
require.Contains(t, csp[0], "'unsafe-inline'")
dashboardOrigin := strings.TrimSuffix(client.URL.String(), "/")
require.Contains(t, string(body), `<script>window.top.postMessage("coder-preview-sign-in-needed", "`+dashboardOrigin+`")</script>`)
}
// Without the experiment, or without --host-prefix-cookie, coderd builds no
// preview hosting, so a preview host gets the same response as any invalid app
// URL, without the preview policy. Only the missing cookie setting warns,
// because only it blocks an enabled experiment.
func TestPreviewHostsWithoutHosting(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
experiment bool
hostPrefix bool
wantWarn int
}{
{name: "ExperimentOff", hostPrefix: true},
{name: "NoHostPrefixCookie", experiment: true, wantWarn: 1},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
sink := testutil.NewFakeSink(t)
logger := sink.Logger(slog.LevelWarn)
dv := coderdtest.DeploymentValues(t)
dv.HTTPCookies.EnableHostPrefix = tc.hostPrefix
if tc.experiment {
dv.Experiments = []string{string(codersdk.ExperimentOpenPreview)}
}
client := coderdtest.New(t, &coderdtest.Options{
AppHostname: "*.test.coder.com",
DeploymentValues: dv,
Logger: &logger,
})
require.Len(t, sink.Entries(func(e slog.SinkEntry) bool {
return e.Level == slog.LevelWarn && e.Message == "workspace previews are unavailable"
}), tc.wantWarn)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, client.URL.String()+"/", nil)
require.NoError(t, err)
req.Host = "preview--8080--dev--site--alice.test.coder.com"
req.Header.Set("Sec-Fetch-Dest", "iframe")
res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
body, err := io.ReadAll(res.Body)
_ = res.Body.Close()
require.NoError(t, err)
require.Equal(t, http.StatusBadRequest, res.StatusCode, string(body))
require.Contains(t, string(body), "Invalid Application URL")
require.NotContains(t, string(body), "preview")
require.Empty(t, res.Header.Values("Content-Security-Policy"))
})
}
}
type fakeAgentProvider struct {
workspaceapps.AgentProvider
upstream *url.URL
}
func (p *fakeAgentProvider) ReverseProxy(targetURL, _ *url.URL, _ uuid.UUID, _ appurl.ApplicationURL, _ string) *httputil.ReverseProxy {
target := *targetURL
target.Host = p.upstream.Host
return httputil.NewSingleHostReverseProxy(&target)
}