Files
coder/coderd/workspaceapps/preview.go
T

96 lines
3.7 KiB
Go

package workspaceapps
import (
"html/template"
"net/http"
"net/url"
"github.com/go-chi/chi/v5"
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
"github.com/coder/coder/v2/coderd/x/previewhost"
"github.com/coder/coder/v2/site"
)
// servePreview serves a request to a preview host. previewhost checks the
// request and wraps the writer, so every response coderd writes carries the
// preview policy, and proxyWorkspaceApp applies the same policy to proxied
// responses, WebSocket upgrades included.
func (s *Server) servePreview(rw http.ResponseWriter, r *http.Request, host string, middlewares []func(http.Handler) http.Handler) {
rw, app, refusal := s.PreviewHosts.Prepare(rw, r, host)
if refusal != nil {
writePreviewError(rw, r, s.DashboardURL, refusal.Status, refusal.Description)
return
}
// The smuggled API key sets a cookie on the whole app wildcard, which a
// preview must never do.
if r.URL.Query().Has(appurl.SubdomainProxyAPIKeyParam) {
writePreviewError(rw, r, s.DashboardURL, http.StatusBadRequest, "Previews sign in only from the dashboard.")
return
}
token, ok := ResolveRequest(rw, r, ResolveRequestOptions{
Logger: s.Logger,
Cookies: s.cookies,
CookieCfg: s.CookiesConfig,
SignedTokenProvider: s.SignedTokenProvider,
DashboardURL: s.DashboardURL,
PathAppBaseURL: s.AccessURL,
AppHostname: s.Hostname,
AppRequest: Request{
AccessMethod: AccessMethodSubdomain,
BasePath: "/",
UsernameOrID: app.Username,
WorkspaceNameOrID: app.WorkspaceName,
AgentNameOrID: app.AgentName,
AppSlugOrPort: app.AppSlugOrPort,
},
AppPath: r.URL.Path,
AppQuery: r.URL.RawQuery,
})
if !ok {
return
}
// Previews never get the app's CORS handling, so ModifyResponse strips
// any CORS headers the preview server sends.
chi.Chain(middlewares...).HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
s.proxyWorkspaceApp(rw, r, *token, r.URL.Path, app)
}).ServeHTTP(rw, r)
}
func writePreviewError(rw http.ResponseWriter, r *http.Request, dashboardURL *url.URL, status int, description string) {
site.RenderStaticErrorPage(rw, r, site.ErrorPageData{
Status: status,
Title: http.StatusText(status),
Description: description,
Actions: []site.Action{{URL: dashboardURL.String(), Text: "Back to site"}},
})
}
// previewSignInNeeded is what a preview host's signed-out page posts to the
// dashboard that frames it. The dashboard accepts it only from the preview
// frame's origin and window, and then signs the preview in.
const previewSignInNeeded = "coder-preview-sign-in-needed"
// writePreviewSignedOut answers a preview request that has no session.
func writePreviewSignedOut(rw http.ResponseWriter, r *http.Request, dashboardURL *url.URL) {
// JSEscapeString escapes quotes, <, > and &, so its strings are safe in a
// quoted script string.
message := `"` + template.JSEscapeString(previewSignInNeeded) + `"`
dashboardOrigin := `"` + template.JSEscapeString(dashboardURL.Scheme+"://"+dashboardURL.Host) + `"`
site.RenderStaticErrorPage(rw, r, site.ErrorPageData{
Status: http.StatusUnauthorized,
Title: http.StatusText(http.StatusUnauthorized),
Description: "<p>Reopen this preview from Coder to sign in again.</p>" +
"<script>window.top.postMessage(" + message + ", " + dashboardOrigin + ")</script>",
RenderDescriptionMarkdown: true,
Actions: []site.Action{{URL: dashboardURL.String(), Text: "Back to site"}},
})
}
func writeFromOtherPreview(rw http.ResponseWriter, r *http.Request, dashboardURL *url.URL) {
writePreviewError(rw, r, dashboardURL, previewhost.FromOtherPreviewRefusal.Status, previewhost.FromOtherPreviewRefusal.Description)
}