2023-04-06 04:41:55 +10:00
package workspaceapps
import (
"context"
"fmt"
"net"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strconv"
"strings"
"sync"
2024-10-25 17:14:35 +01:00
"time"
2023-04-06 04:41:55 +10:00
"github.com/go-chi/chi/v5"
2024-10-25 17:14:35 +01:00
"github.com/go-jose/go-jose/v4/jwt"
2023-04-06 04:41:55 +10:00
"github.com/google/uuid"
"go.opentelemetry.io/otel/trace"
2026-01-08 10:29:41 +04:00
"cdr.dev/slog/v3"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/agent/agentssh"
2024-10-25 17:14:35 +01:00
"github.com/coder/coder/v2/coderd/cryptokeys"
2023-09-01 19:50:12 +03:00
"github.com/coder/coder/v2/coderd/database/dbtime"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
2024-10-25 17:14:35 +01:00
"github.com/coder/coder/v2/coderd/jwtutils"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/tracing"
"github.com/coder/coder/v2/coderd/util/slice"
2024-01-17 10:41:42 -06:00
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
2025-07-30 13:42:39 -07:00
"github.com/coder/coder/v2/coderd/workspaceapps/cors"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/codersdk"
2024-03-26 12:44:31 -05:00
"github.com/coder/coder/v2/codersdk/workspacesdk"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/site"
2024-12-19 00:51:30 +04:00
"github.com/coder/websocket"
2023-04-06 04:41:55 +10:00
)
const (
// This needs to be a super unique query parameter because we don't want to
// conflict with query parameters that users may use.
//nolint:gosec
SubdomainProxyAPIKeyParam = "coder_application_connect_api_key_35e783"
// appLogoutHostname is the hostname to use for the logout redirect. When
// the dashboard logs out, it will redirect to this subdomain of the app
// hostname, and the server will remove the cookie and redirect to the main
// login page.
// It is important that this URL can never match a valid app hostname.
//
2025-03-26 01:56:39 -05:00
// Deprecated: we no longer use this, but we still redirect from it to the
2023-04-06 04:41:55 +10:00
// main login page.
appLogoutHostname = "coder-logout"
)
// nonCanonicalHeaders is a map from "canonical" headers to the actual header we
// should send to the app in the workspace. Some headers (such as the websocket
// upgrade headers from RFC 6455) are not canonical according to the HTTP/1
// spec. Golang has said that they will not add custom cases for these headers,
// so we need to do it ourselves.
//
// Some apps our customers use are sensitive to the case of these headers.
//
// https://github.com/golang/go/issues/18495
var nonCanonicalHeaders = map [ string ] string {
"Sec-Websocket-Accept" : "Sec-WebSocket-Accept" ,
"Sec-Websocket-Extensions" : "Sec-WebSocket-Extensions" ,
"Sec-Websocket-Key" : "Sec-WebSocket-Key" ,
"Sec-Websocket-Protocol" : "Sec-WebSocket-Protocol" ,
"Sec-Websocket-Version" : "Sec-WebSocket-Version" ,
}
2023-07-12 17:37:31 -05:00
type AgentProvider interface {
// ReverseProxy returns an httputil.ReverseProxy for proxying HTTP requests
// to the specified agent.
2024-04-26 11:52:53 -04:00
ReverseProxy ( targetURL , dashboardURL * url . URL , agentID uuid . UUID , app appurl . ApplicationURL , wildcardHost string ) * httputil . ReverseProxy
2023-07-12 17:37:31 -05:00
// AgentConn returns a new connection to the specified agent.
2025-08-20 10:00:44 +01:00
AgentConn ( ctx context . Context , agentID uuid . UUID ) ( _ workspacesdk . AgentConn , release func (), _ error )
2023-07-12 17:37:31 -05:00
2023-11-13 17:14:12 -06:00
ServeHTTPDebug ( w http . ResponseWriter , r * http . Request )
2023-07-12 17:37:31 -05:00
Close () error
}
2025-09-25 00:30:02 +10:00
type ServerOptions struct {
2023-04-06 04:41:55 +10:00
Logger slog . Logger
// DashboardURL should be a url to the coderd dashboard. This can be the
// same as the AccessURL if the Server is embedded.
DashboardURL * url . URL
AccessURL * url . URL
// Hostname should be the wildcard hostname to use for workspace
// applications INCLUDING the asterisk, (optional) suffix and leading dot.
// It will use the same scheme and port number as the access URL.
// E.g. "*.apps.coder.com" or "*-apps.coder.com".
Hostname string
// HostnameRegex contains the regex version of Hostname as generated by
2024-01-17 10:41:42 -06:00
// appurl.CompileHostnamePattern(). It MUST be set if Hostname is set.
2023-04-17 14:57:21 -05:00
HostnameRegex * regexp . Regexp
RealIPConfig * httpmw . RealIPConfig
2023-04-06 04:41:55 +10:00
2024-10-25 17:14:35 +01:00
SignedTokenProvider SignedTokenProvider
APIKeyEncryptionKeycache cryptokeys . EncryptionKeycache
2023-04-06 04:41:55 +10:00
2023-04-17 14:57:21 -05:00
// DisablePathApps disables path-based apps. This is a security feature as path
// based apps share the same cookie as the dashboard, and are susceptible to XSS
// by a malicious workspace app.
//
// Subdomain apps are safer with their cookies scoped to the subdomain, and XSS
// calls to the dashboard are not possible due to CORs.
2025-04-08 14:15:14 -05:00
DisablePathApps bool
2025-09-25 00:30:02 +10:00
CookiesConfig codersdk . HTTPCookieConfig
2023-04-17 14:57:21 -05:00
2023-08-16 15:22:00 +03:00
AgentProvider AgentProvider
StatsCollector * StatsCollector
2026-06-03 10:46:07 +01:00
WSWatcher * httpapi . WSWatcher
2025-09-25 00:30:02 +10:00
}
// Server serves workspace apps endpoints, including:
// - Path-based apps
// - Subdomain app middleware
// - Workspace reconnecting-pty (aka. web terminal)
type Server struct {
ServerOptions
cookies AppCookies
2023-07-12 17:37:31 -05:00
2023-04-06 04:41:55 +10:00
websocketWaitMutex sync . Mutex
websocketWaitGroup sync . WaitGroup
}
2025-09-25 00:30:02 +10:00
func NewServer ( options ServerOptions ) * Server {
return & Server {
ServerOptions : options ,
cookies : NewAppCookies ( options . Hostname ),
}
}
2023-04-06 04:41:55 +10:00
// Close waits for all reconnecting-pty WebSocket connections to drain before
// returning.
func ( s * Server ) Close () error {
s . websocketWaitMutex . Lock ()
s . websocketWaitGroup . Wait ()
s . websocketWaitMutex . Unlock ()
2023-08-16 15:22:00 +03:00
if s . StatsCollector != nil {
_ = s . StatsCollector . Close ()
}
2023-07-12 17:37:31 -05:00
// The caller must close the SignedTokenProvider and the AgentProvider (if
// necessary).
2023-04-06 04:41:55 +10:00
return nil
}
func ( s * Server ) Attach ( r chi . Router ) {
servePathApps := func ( r chi . Router ) {
r . HandleFunc ( "/*" , s . workspaceAppsProxyPath )
}
// %40 is the encoded character of the @ symbol. VS Code Web does
// not handle character encoding properly, so it's safe to assume
// other applications might not as well.
r . Route ( "/%40{user}/{workspace_and_agent}/apps/{workspaceapp}" , servePathApps )
r . Route ( "/@{user}/{workspace_and_agent}/apps/{workspaceapp}" , servePathApps )
r . Get ( "/api/v2/workspaceagents/{workspaceagent}/pty" , s . workspaceAgentPTY )
}
2026-06-25 11:17:23 -05:00
// originLocalURL returns p as a relative URL rooted at the current origin,
// safe to use as a redirect Location. p (typically r.URL.Path) is
// attacker-controlled and already percent-decoded, so a leading "//" or "/\"
// run would otherwise be parsed by http.Redirect or a browser as a
// scheme-relative URL pointing at another host, turning a redirect back to the
// same path into an open redirect. Collapsing the leading slash and backslash
// run to a single "/" keeps the result same-origin, and url.URL.String()
// percent-encodes any control characters in the path.
func originLocalURL ( p string ) * url . URL {
return & url . URL { Path : "/" + strings . TrimLeft ( p , `/\` )}
}
2023-04-17 14:57:21 -05:00
// handleAPIKeySmuggling is called by the proxy path and subdomain handlers to
// process any "smuggled" API keys in the query parameters.
//
// If a smuggled key is found, it is decrypted and the cookie is set, and the
// user is redirected to strip the query parameter.
func ( s * Server ) handleAPIKeySmuggling ( rw http . ResponseWriter , r * http . Request , accessMethod AccessMethod ) bool {
ctx := r . Context ()
encryptedAPIKey := r . URL . Query (). Get ( SubdomainProxyAPIKeyParam )
if encryptedAPIKey == "" {
return true
}
// API key smuggling is not permitted for path apps on the primary access
// URL. The user is already covered by their full session token.
if accessMethod == AccessMethodPath && s . AccessURL . Host == s . DashboardURL . Host {
site . RenderStaticErrorPage ( rw , r , site . ErrorPageData {
Status : http . StatusBadRequest ,
Title : "Bad Request" ,
Description : "Could not decrypt API key. Workspace app API key smuggling is not permitted on the primary access URL. Please remove the query parameter and try again." ,
2025-12-08 14:16:00 -09:00
// No retry is included because the user needs to remove the query
2023-04-17 14:57:21 -05:00
// parameter before they try again.
2025-12-08 14:16:00 -09:00
Actions : [] site . Action {
{
URL : s . DashboardURL . String (),
Text : "Back to site" ,
},
},
2023-04-17 14:57:21 -05:00
})
return false
}
// Exchange the encoded API key for a real one.
2024-10-25 17:14:35 +01:00
var payload EncryptedAPIKeyPayload
err := jwtutils . Decrypt ( ctx , s . APIKeyEncryptionKeycache , encryptedAPIKey , & payload , jwtutils . WithDecryptExpected ( jwt . Expected {
Time : time . Now (),
}))
2023-04-17 14:57:21 -05:00
if err != nil {
s . Logger . Debug ( ctx , "could not decrypt smuggled workspace app API key" , slog . Error ( err ))
site . RenderStaticErrorPage ( rw , r , site . ErrorPageData {
Status : http . StatusBadRequest ,
Title : "Bad Request" ,
Description : "Could not decrypt API key. Please remove the query parameter and try again." ,
2025-12-08 14:16:00 -09:00
// No retry is included because the user needs to remove the query
2023-04-17 14:57:21 -05:00
// parameter before they try again.
2025-12-08 14:16:00 -09:00
Actions : [] site . Action {
{
URL : s . DashboardURL . String (),
Text : "Back to site" ,
},
},
2023-04-17 14:57:21 -05:00
})
return false
}
// Set the cookie. For subdomain apps, we set the cookie on the whole
// wildcard so users don't need to re-auth for every subdomain app they
// access. For path apps (only on proxies, see above) we just set it on the
// current domain.
domain := "" // use the current domain
if accessMethod == AccessMethodSubdomain {
hostSplit := strings . SplitN ( s . Hostname , "." , 2 )
if len ( hostSplit ) != 2 {
// This should be impossible as we verify the app hostname on
// startup, but we'll check anyways.
s . Logger . Error ( r . Context (), "could not split invalid app hostname" , slog . F ( "hostname" , s . Hostname ))
site . RenderStaticErrorPage ( rw , r , site . ErrorPageData {
2025-12-08 14:16:00 -09:00
Status : http . StatusInternalServerError ,
Title : "Internal Server Error" ,
Description : "The app is configured with an invalid app wildcard hostname. Please contact an administrator." ,
Actions : [] site . Action {
{
URL : s . DashboardURL . String (),
Text : "Back to site" ,
},
},
2023-04-17 14:57:21 -05:00
})
return false
}
// Set the cookie for all subdomains of s.Hostname.
domain = "." + hostSplit [ 1 ]
}
// We don't set an expiration because the key in the database already has an
// expiration, and expired tokens don't affect the user experience (they get
// auto-redirected to re-smuggle the API key).
2023-08-28 18:34:52 -07:00
//
// We use different cookie names for path apps and for subdomain apps to
// avoid both being set and sent to the server at the same time and the
// server using the wrong value.
2025-09-25 00:30:02 +10:00
http . SetCookie ( rw , s . CookiesConfig . Apply ( & http . Cookie {
Name : s . cookies . CookieNameForAccessMethod ( accessMethod ),
2024-10-25 17:14:35 +01:00
Value : payload . APIKey ,
2023-04-17 14:57:21 -05:00
Domain : domain ,
Path : "/" ,
MaxAge : 0 ,
HttpOnly : true ,
2025-04-08 14:15:14 -05:00
}))
2023-04-17 14:57:21 -05:00
2026-06-25 11:17:23 -05:00
// Strip the smuggled API key query parameter and redirect back to the same
// path. r.URL.Path is attacker-controlled and can smuggle a separate host
// (e.g. "//evil.com"); originLocalURL keeps the redirect on the current
// origin.
redirectURL := originLocalURL ( r . URL . Path )
2023-04-17 14:57:21 -05:00
q := r . URL . Query ()
q . Del ( SubdomainProxyAPIKeyParam )
2026-06-25 11:17:23 -05:00
redirectURL . RawQuery = q . Encode ()
2023-04-17 14:57:21 -05:00
2026-06-25 11:17:23 -05:00
http . Redirect ( rw , r , redirectURL . String (), http . StatusSeeOther )
2023-04-17 14:57:21 -05:00
return false
}
2023-04-06 04:41:55 +10:00
// workspaceAppsProxyPath proxies requests to a workspace application
// through a relative URL path.
func ( s * Server ) workspaceAppsProxyPath ( rw http . ResponseWriter , r * http . Request ) {
2023-04-17 14:57:21 -05:00
if s . DisablePathApps {
2023-04-06 04:41:55 +10:00
site . RenderStaticErrorPage ( rw , r , site . ErrorPageData {
2025-12-08 14:16:00 -09:00
Status : http . StatusForbidden ,
Title : "Forbidden" ,
Description : "Path-based applications are disabled on this Coder deployment by the administrator." ,
Actions : [] site . Action {
{
URL : s . DashboardURL . String (),
Text : "Back to site" ,
},
},
2023-04-06 04:41:55 +10:00
})
return
}
// We don't support @me in path apps since it requires the database to
// lookup the username from token. We used to redirect by doing this lookup.
if chi . URLParam ( r , "user" ) == codersdk . Me {
site . RenderStaticErrorPage ( rw , r , site . ErrorPageData {
2025-12-08 14:16:00 -09:00
Status : http . StatusNotFound ,
Title : "Application Not Found" ,
Description : "Applications must be accessed with the full username, not @me." ,
Actions : [] site . Action {
{
URL : s . DashboardURL . String (),
Text : "Back to site" ,
},
},
2023-04-06 04:41:55 +10:00
})
return
}
2023-04-17 14:57:21 -05:00
if ! s . handleAPIKeySmuggling ( rw , r , AccessMethodPath ) {
return
}
2023-04-06 04:41:55 +10:00
// Determine the real path that was hit. The * URL parameter in Chi will not
// include the leading slash if it was present, so we need to add it back.
chiPath := chi . URLParam ( r , "*" )
basePath := strings . TrimSuffix ( r . URL . Path , chiPath )
if strings . HasSuffix ( basePath , "/" ) {
chiPath = "/" + chiPath
}
// ResolveRequest will only return a new signed token if the actor has the RBAC
// permissions to connect to a workspace.
2023-04-17 14:57:21 -05:00
token , ok := ResolveRequest ( rw , r , ResolveRequestOptions {
Logger : s . Logger ,
2025-09-25 00:30:02 +10:00
Cookies : s . cookies ,
CookieCfg : s . CookiesConfig ,
2023-04-17 14:57:21 -05:00
SignedTokenProvider : s . SignedTokenProvider ,
DashboardURL : s . DashboardURL ,
PathAppBaseURL : s . AccessURL ,
AppHostname : s . Hostname ,
AppRequest : Request {
AccessMethod : AccessMethodPath ,
BasePath : basePath ,
2023-10-11 07:02:39 +11:00
Prefix : "" , // Prefix doesn't exist for path apps
2023-04-17 14:57:21 -05:00
UsernameOrID : chi . URLParam ( r , "user" ),
WorkspaceAndAgent : chi . URLParam ( r , "workspace_and_agent" ),
// We don't support port proxying on paths. The ResolveRequest method
// won't allow port proxying on path-based apps if the app is a number.
AppSlugOrPort : chi . URLParam ( r , "workspaceapp" ),
},
AppPath : chiPath ,
AppQuery : r . URL . RawQuery ,
2023-04-06 04:41:55 +10:00
})
if ! ok {
return
}
2024-04-26 11:52:53 -04:00
s . proxyWorkspaceApp ( rw , r , * token , chiPath , appurl . ApplicationURL {})
2023-04-06 04:41:55 +10:00
}
2025-07-30 13:42:39 -07:00
// determineCORSBehavior examines the given token and conditionally applies
// CORS middleware if the token specifies that behavior.
func ( s * Server ) determineCORSBehavior ( token * SignedToken , app appurl . ApplicationURL ) func ( http . Handler ) http . Handler {
return func ( next http . Handler ) http . Handler {
// Create the CORS middleware handler upfront.
corsHandler := httpmw . WorkspaceAppCors ( s . HostnameRegex , app )( next )
return http . HandlerFunc ( func ( rw http . ResponseWriter , r * http . Request ) {
var behavior codersdk . CORSBehavior
if token != nil {
behavior = token . CORSBehavior
}
// Add behavior to context regardless of which handler we use,
// since we will use this later on to determine if we should strip
// CORS headers in the response.
r = r . WithContext ( cors . WithBehavior ( r . Context (), behavior ))
switch behavior {
case codersdk . CORSBehaviorPassthru :
// Bypass the CORS middleware.
next . ServeHTTP ( rw , r )
return
default :
// Apply the CORS middleware.
corsHandler . ServeHTTP ( rw , r )
}
})
}
}
2023-04-17 14:57:21 -05:00
// HandleSubdomain handles subdomain-based application proxy requests (aka.
2023-04-06 04:41:55 +10:00
// DevURLs in Coder V1).
//
// There are a lot of paths here:
// 1. If api.Hostname is not set then we pass on.
// 2. If we can't read the request hostname then we return a 400.
// 3. If the request hostname matches api.AccessURL then we pass on.
// 5. We split the subdomain into the subdomain and the "rest". If there are no
// periods in the hostname then we pass on.
2024-01-17 10:41:42 -06:00
// 5. We parse the subdomain into a appurl.ApplicationURL struct. If we
2023-04-06 04:41:55 +10:00
// encounter an error:
// a. If the "rest" does not match api.Hostname then we pass on;
// b. Otherwise, we return a 400.
// 6. Finally, we verify that the "rest" matches api.Hostname, else we
// return a 404.
//
// Rationales for each of the above steps:
// 1. We pass on if api.Hostname is not set to avoid returning any errors if
// `--app-hostname` is not configured.
// 2. Every request should have a valid Host header anyways.
// 3. We pass on if the request hostname matches api.AccessURL so we can
// support having the access URL be at the same level as the application
// base hostname.
// 4. We pass on if there are no periods in the hostname as application URLs
// must be a subdomain of a hostname, which implies there must be at least
// one period.
// 5. a. If the request subdomain is not a valid application URL, and the
// "rest" does not match api.Hostname, then it is very unlikely that
// the request was intended for this handler. We pass on.
// b. If the request subdomain is not a valid application URL, but the
// "rest" matches api.Hostname, then we return a 400 because the
// request is probably a typo or something.
// 6. We finally verify that the "rest" matches api.Hostname for security
// purposes regarding re-authentication and application proxy session
// tokens.
2023-04-17 14:57:21 -05:00
func ( s * Server ) HandleSubdomain ( middlewares ... func ( http . Handler ) http . Handler ) func ( http . Handler ) http . Handler {
2023-04-06 04:41:55 +10:00
return func ( next http . Handler ) http . Handler {
return http . HandlerFunc ( func ( rw http . ResponseWriter , r * http . Request ) {
ctx := r . Context ()
// Step 1: Pass on if subdomain-based application proxying is not
// configured.
if s . Hostname == "" || s . HostnameRegex == nil {
next . ServeHTTP ( rw , r )
return
}
// Step 2: Get the request Host.
2026-06-11 10:55:00 -07:00
host := httpmw . EffectiveHost ( s . RealIPConfig , r )
2023-04-06 04:41:55 +10:00
if host == "" {
if r . URL . Path == "/derp" {
// The /derp endpoint is used by wireguard clients to tunnel
// through coderd. For some reason these requests don't set
// a Host header properly sometimes in tests (no idea how),
// which causes this path to get hit.
next . ServeHTTP ( rw , r )
return
}
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
Message : "Could not determine request Host." ,
})
return
}
// Steps 3-6: Parse application from subdomain.
app , ok := s . parseHostname ( rw , r , next , host )
if ! ok {
return
}
2025-07-30 13:42:39 -07:00
if ! s . handleAPIKeySmuggling ( rw , r , AccessMethodSubdomain ) {
return
}
2023-06-07 11:08:14 -08:00
2025-07-30 13:42:39 -07:00
// Generate a signed token for the request.
token , ok := ResolveRequest ( rw , r , ResolveRequestOptions {
Logger : s . Logger ,
2025-09-25 00:30:02 +10:00
Cookies : s . cookies ,
CookieCfg : s . CookiesConfig ,
2025-07-30 13:42:39 -07:00
SignedTokenProvider : s . SignedTokenProvider ,
DashboardURL : s . DashboardURL ,
PathAppBaseURL : s . AccessURL ,
AppHostname : s . Hostname ,
AppRequest : Request {
AccessMethod : AccessMethodSubdomain ,
BasePath : "/" ,
Prefix : app . Prefix ,
UsernameOrID : app . Username ,
WorkspaceNameOrID : app . WorkspaceName ,
AgentNameOrID : app . AgentName ,
AppSlugOrPort : app . AppSlugOrPort ,
},
AppPath : r . URL . Path ,
AppQuery : r . URL . RawQuery ,
})
if ! ok {
return
}
// Proxy the request (possibly with the CORS middleware).
mws := chi . Middlewares ( append ( middlewares , s . determineCORSBehavior ( token , app )))
mws . Handler ( http . HandlerFunc ( func ( rw http . ResponseWriter , r * http . Request ) {
2024-04-26 11:52:53 -04:00
s . proxyWorkspaceApp ( rw , r , * token , r . URL . Path , app )
2023-04-06 04:41:55 +10:00
})). ServeHTTP ( rw , r . WithContext ( ctx ))
})
}
}
// parseHostname will return if a given request is attempting to access a
// workspace app via a subdomain. If it is, the hostname of the request is parsed
2024-01-17 10:41:42 -06:00
// into an appurl.ApplicationURL and true is returned. If the request is not
2023-04-06 04:41:55 +10:00
// accessing a workspace app, then the next handler is called and false is
// returned.
2024-01-17 10:41:42 -06:00
func ( s * Server ) parseHostname ( rw http . ResponseWriter , r * http . Request , next http . Handler , host string ) ( appurl . ApplicationURL , bool ) {
2023-04-06 04:41:55 +10:00
// Check if the hostname matches either of the access URLs. If it does, the
// user was definitely trying to connect to the dashboard/API or a
// path-based app.
2024-01-17 10:41:42 -06:00
if appurl . HostnamesMatch ( s . DashboardURL . Hostname (), host ) || appurl . HostnamesMatch ( s . AccessURL . Hostname (), host ) {
2023-04-06 04:41:55 +10:00
next . ServeHTTP ( rw , r )
2024-01-17 10:41:42 -06:00
return appurl . ApplicationURL {}, false
2023-04-06 04:41:55 +10:00
}
// If there are no periods in the hostname, then it can't be a valid
// application URL.
if ! strings . Contains ( host , "." ) {
next . ServeHTTP ( rw , r )
2024-01-17 10:41:42 -06:00
return appurl . ApplicationURL {}, false
2023-04-06 04:41:55 +10:00
}
// Split the subdomain so we can parse the application details and verify it
// matches the configured app hostname later.
2024-01-17 10:41:42 -06:00
subdomain , ok := appurl . ExecuteHostnamePattern ( s . HostnameRegex , host )
2023-04-06 04:41:55 +10:00
if ! ok {
// Doesn't match the regex, so it's not a valid application URL.
next . ServeHTTP ( rw , r )
2024-01-17 10:41:42 -06:00
return appurl . ApplicationURL {}, false
2023-04-06 04:41:55 +10:00
}
// Check if the request is part of the deprecated logout flow. If so, we
// just redirect to the main access URL.
if subdomain == appLogoutHostname {
2023-04-17 14:57:21 -05:00
http . Redirect ( rw , r , s . AccessURL . String (), http . StatusSeeOther )
2024-01-17 10:41:42 -06:00
return appurl . ApplicationURL {}, false
2023-04-06 04:41:55 +10:00
}
// Parse the application URL from the subdomain.
2024-01-17 10:41:42 -06:00
app , err := appurl . ParseSubdomainAppURL ( subdomain )
2023-04-06 04:41:55 +10:00
if err != nil {
site . RenderStaticErrorPage ( rw , r , site . ErrorPageData {
2025-12-08 14:16:00 -09:00
Status : http . StatusBadRequest ,
Title : "Invalid Application URL" ,
Description : fmt . Sprintf ( "Could not parse subdomain application URL %q: %s" , subdomain , err . Error ()),
Actions : [] site . Action {
{
URL : s . DashboardURL . String (),
Text : "Back to site" ,
},
},
2023-04-06 04:41:55 +10:00
})
2024-01-17 10:41:42 -06:00
return appurl . ApplicationURL {}, false
2023-04-06 04:41:55 +10:00
}
return app , true
}
2024-04-26 11:52:53 -04:00
func ( s * Server ) proxyWorkspaceApp ( rw http . ResponseWriter , r * http . Request , appToken SignedToken , path string , app appurl . ApplicationURL ) {
2023-04-06 04:41:55 +10:00
ctx := r . Context ()
// Filter IP headers from untrusted origins.
httpmw . FilterUntrustedOriginHeaders ( s . RealIPConfig , r )
// Ensure proper IP headers get sent to the forwarded application.
err := httpmw . EnsureXForwardedForHeader ( r )
if err != nil {
httpapi . InternalServerError ( rw , err )
return
}
appURL , err := url . Parse ( appToken . AppURL )
if err != nil {
site . RenderStaticErrorPage ( rw , r , site . ErrorPageData {
2025-12-08 14:16:00 -09:00
Status : http . StatusBadRequest ,
Title : "Bad Request" ,
Description : fmt . Sprintf ( "Application has an invalid URL %q: %s" , appToken . AppURL , err . Error ()),
Actions : [] site . Action {
{
Text : "Retry" ,
},
{
URL : s . DashboardURL . String (),
Text : "Back to site" ,
},
},
2023-04-06 04:41:55 +10:00
})
return
}
// Verify that the port is allowed. See the docs above
// `codersdk.MinimumListeningPort` for more details.
port := appURL . Port ()
if port != "" {
portInt , err := strconv . Atoi ( port )
if err != nil {
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
Message : fmt . Sprintf ( "App URL %q has an invalid port %q." , appToken . AppURL , port ),
Detail : err . Error (),
})
return
}
2024-03-26 12:44:31 -05:00
if portInt < workspacesdk . AgentMinimumListeningPort {
2023-04-06 04:41:55 +10:00
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
2024-03-26 12:44:31 -05:00
Message : fmt . Sprintf ( "Application port %d is not permitted. Coder reserves ports less than %d for internal use." ,
portInt , workspacesdk . AgentMinimumListeningPort ,
),
2023-04-06 04:41:55 +10:00
})
return
}
}
// Ensure path and query parameter correctness.
if path == "" {
// Web applications typically request paths relative to the
// root URL. This allows for routing behind a proxy or subpath.
// See https://github.com/coder/code-server/issues/241 for examples.
2026-06-25 11:17:23 -05:00
//
// r.URL.Path is attacker-controlled, so sanitize it before redirecting
// to avoid an off-origin "//host" Location (see originLocalURL).
redirectURL := originLocalURL ( r . URL . Path )
if ! strings . HasSuffix ( redirectURL . Path , "/" ) {
redirectURL . Path += "/"
}
http . Redirect ( rw , r , redirectURL . String (), http . StatusTemporaryRedirect )
2023-04-06 04:41:55 +10:00
return
}
if path == "/" && r . URL . RawQuery == "" && appURL . RawQuery != "" {
// If the application defines a default set of query parameters,
// we should always respect them. The reverse proxy will merge
// query parameters for server-side requests, but sometimes
// client-side applications require the query parameters to render
// properly. With code-server, this is the "folder" param.
2026-06-25 11:17:23 -05:00
//
// r.URL.Path is attacker-controlled, so build the Location from a
// sanitized same-origin path instead of r.URL directly (see
// originLocalURL).
redirectURL := originLocalURL ( r . URL . Path )
redirectURL . RawQuery = appURL . RawQuery
http . Redirect ( rw , r , redirectURL . String (), http . StatusTemporaryRedirect )
2023-04-06 04:41:55 +10:00
return
}
r . URL . Path = path
appURL . RawQuery = ""
2024-04-26 11:52:53 -04:00
_ , protocol , isPort := app . PortInfo ()
if isPort {
appURL . Scheme = protocol
}
2023-04-06 04:41:55 +10:00
2024-04-26 11:52:53 -04:00
proxy := s . AgentProvider . ReverseProxy ( appURL , s . DashboardURL , appToken . AgentID , app , s . Hostname )
2023-04-06 04:41:55 +10:00
2023-06-21 13:41:27 -08:00
proxy . ModifyResponse = func ( r * http . Response ) error {
2025-07-30 13:42:39 -07:00
// If passthru behavior is set, disable our CORS header stripping.
if cors . HasBehavior ( r . Request . Context (), codersdk . CORSBehaviorPassthru ) {
return nil
}
2023-06-21 13:41:27 -08:00
r . Header . Del ( httpmw . AccessControlAllowOriginHeader )
r . Header . Del ( httpmw . AccessControlAllowCredentialsHeader )
r . Header . Del ( httpmw . AccessControlAllowMethodsHeader )
r . Header . Del ( httpmw . AccessControlAllowHeadersHeader )
varies := r . Header . Values ( httpmw . VaryHeader )
r . Header . Del ( httpmw . VaryHeader )
forbiddenVary := [] string {
httpmw . OriginHeader ,
httpmw . AccessControlRequestMethodsHeader ,
httpmw . AccessControlRequestHeadersHeader ,
}
for _ , value := range varies {
if ! slice . ContainsCompare ( forbiddenVary , value , strings . EqualFold ) {
r . Header . Add ( httpmw . VaryHeader , value )
}
}
return nil
}
2023-04-06 04:41:55 +10:00
// This strips the session token from a workspace app request.
2024-06-03 12:29:50 -05:00
cookieHeaders := r . Header . Values ( "Cookie" )
2023-04-06 04:41:55 +10:00
r . Header . Del ( "Cookie" )
for _ , cookieHeader := range cookieHeaders {
r . Header . Add ( "Cookie" , httpapi . StripCoderCookies ( cookieHeader ))
}
// Convert canonicalized headers to their non-canonicalized counterparts.
// See the comment on `nonCanonicalHeaders` for more information on why this
// is necessary.
for k , v := range r . Header {
if n , ok := nonCanonicalHeaders [ k ]; ok {
r . Header . Del ( k )
r . Header [ n ] = v
}
}
// end span so we don't get long lived trace data
2026-08-27 13:12:41 -07:00
tracing . EndHTTPSpan ( r , http . StatusOK , trace . SpanFromContext ( ctx ), "coderd" )
2023-04-06 04:41:55 +10:00
2023-08-16 15:22:00 +03:00
report := newStatsReportFromSignedToken ( appToken )
defer func () {
// We must use defer here because ServeHTTP may panic.
2023-09-01 19:50:12 +03:00
report . SessionEndedAt = dbtime . Now ()
2023-08-16 15:22:00 +03:00
s . collectStats ( report )
}()
2023-04-06 04:41:55 +10:00
proxy . ServeHTTP ( rw , r )
}
// workspaceAgentPTY spawns a PTY and pipes it over a WebSocket.
// This is used for the web terminal.
//
// @Summary Open PTY to workspace agent
// @ID open-pty-to-workspace-agent
// @Security CoderSessionToken
// @Tags Agents
// @Param workspaceagent path string true "Workspace agent ID" format(uuid)
// @Success 101
2026-05-05 02:54:03 +05:00
// @Router /api/v2/workspaceagents/{workspaceagent}/pty [get]
2023-04-06 04:41:55 +10:00
func ( s * Server ) workspaceAgentPTY ( rw http . ResponseWriter , r * http . Request ) {
2024-10-24 15:12:35 -04:00
ctx , cancel := context . WithCancel ( r . Context ())
defer cancel ()
2023-04-06 04:41:55 +10:00
s . websocketWaitMutex . Lock ()
s . websocketWaitGroup . Add ( 1 )
s . websocketWaitMutex . Unlock ()
defer s . websocketWaitGroup . Done ()
2023-04-17 14:57:21 -05:00
appToken , ok := ResolveRequest ( rw , r , ResolveRequestOptions {
Logger : s . Logger ,
2025-09-25 00:30:02 +10:00
Cookies : s . cookies ,
CookieCfg : s . CookiesConfig ,
2023-04-17 14:57:21 -05:00
SignedTokenProvider : s . SignedTokenProvider ,
DashboardURL : s . DashboardURL ,
PathAppBaseURL : s . AccessURL ,
AppHostname : s . Hostname ,
AppRequest : Request {
AccessMethod : AccessMethodTerminal ,
BasePath : r . URL . Path ,
AgentNameOrID : chi . URLParam ( r , "workspaceagent" ),
},
2023-04-20 16:59:45 -07:00
AppPath : "" ,
2023-04-17 14:57:21 -05:00
AppQuery : "" ,
2023-04-06 04:41:55 +10:00
})
if ! ok {
return
}
2023-04-27 13:59:01 +04:00
log := s . Logger . With ( slog . F ( "agent_id" , appToken . AgentID ))
log . Debug ( ctx , "resolved PTY request" )
2023-04-06 04:41:55 +10:00
values := r . URL . Query ()
parser := httpapi . NewQueryParamParser ()
2024-02-20 15:58:43 -08:00
reconnect := parser . RequiredNotEmpty ( "reconnect" ). UUID ( values , uuid . New (), "reconnect" )
2023-04-06 04:41:55 +10:00
height := parser . UInt ( values , 80 , "height" )
width := parser . UInt ( values , 80 , "width" )
2025-02-26 09:03:27 +00:00
container := parser . String ( values , "" , "container" )
containerUser := parser . String ( values , "" , "container_user" )
2025-03-20 13:45:31 +00:00
backendType := parser . String ( values , "" , "backend_type" )
2023-04-06 04:41:55 +10:00
if len ( parser . Errors ) > 0 {
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
Message : "Invalid query parameters." ,
Validations : parser . Errors ,
})
return
}
conn , err := websocket . Accept ( rw , r , & websocket . AcceptOptions {
CompressionMode : websocket . CompressionDisabled ,
2023-04-28 16:04:52 -05:00
// Always allow websockets from the primary dashboard URL.
// Terminals are opened there and connect to the proxy.
OriginPatterns : [] string {
s . DashboardURL . Host ,
s . AccessURL . Host ,
},
2023-04-06 04:41:55 +10:00
})
if err != nil {
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
Message : "Failed to accept websocket." ,
Detail : err . Error (),
})
return
}
ctx , wsNetConn := WebsocketNetConn ( ctx , conn , websocket . MessageBinary )
defer wsNetConn . Close () // Also closes conn.
2026-06-03 10:46:07 +01:00
ctx = s . WSWatcher . Watch ( ctx , s . Logger , conn )
2023-07-12 17:37:31 -05:00
agentConn , release , err := s . AgentProvider . AgentConn ( ctx , appToken . AgentID )
2023-04-06 04:41:55 +10:00
if err != nil {
2023-04-27 13:59:01 +04:00
log . Debug ( ctx , "dial workspace agent" , slog . Error ( err ))
2023-04-06 04:41:55 +10:00
_ = conn . Close ( websocket . StatusInternalError , httpapi . WebsocketCloseSprintf ( "dial workspace agent: %s" , err ))
return
}
defer release ()
2023-04-27 13:59:01 +04:00
log . Debug ( ctx , "dialed workspace agent" )
2025-03-26 01:56:39 -05:00
// #nosec G115 - Safe conversion for terminal height/width which are expected to be within uint16 range (0-65535)
2025-02-26 09:03:27 +00:00
ptNetConn , err := agentConn . ReconnectingPTY ( ctx , reconnect , uint16 ( height ), uint16 ( width ), r . URL . Query (). Get ( "command" ), func ( arp * workspacesdk . AgentReconnectingPTYInit ) {
arp . Container = container
arp . ContainerUser = containerUser
2025-03-20 13:45:31 +00:00
arp . BackendType = backendType
2025-02-26 09:03:27 +00:00
})
2023-04-06 04:41:55 +10:00
if err != nil {
2023-04-27 13:59:01 +04:00
log . Debug ( ctx , "dial reconnecting pty server in workspace agent" , slog . Error ( err ))
2023-04-06 04:41:55 +10:00
_ = conn . Close ( websocket . StatusInternalError , httpapi . WebsocketCloseSprintf ( "dial: %s" , err ))
return
}
defer ptNetConn . Close ()
2023-04-27 13:59:01 +04:00
log . Debug ( ctx , "obtained PTY" )
2023-08-16 15:22:00 +03:00
report := newStatsReportFromSignedToken ( * appToken )
s . collectStats ( report )
defer func () {
2023-09-01 19:50:12 +03:00
report . SessionEndedAt = dbtime . Now ()
2023-08-16 15:22:00 +03:00
s . collectStats ( report )
}()
2023-04-06 19:39:22 +03:00
agentssh . Bicopy ( ctx , wsNetConn , ptNetConn )
2023-04-27 13:59:01 +04:00
log . Debug ( ctx , "pty Bicopy finished" )
2023-04-06 04:41:55 +10:00
}
2023-08-16 15:22:00 +03:00
func ( s * Server ) collectStats ( stats StatsReport ) {
if s . StatsCollector != nil {
s . StatsCollector . Collect ( stats )
}
}
2023-04-06 04:41:55 +10:00
// wsNetConn wraps net.Conn created by websocket.NetConn(). Cancel func
// is called if a read or write error is encountered.
type wsNetConn struct {
cancel context . CancelFunc
net . Conn
}
func ( c * wsNetConn ) Read ( b [] byte ) ( n int , err error ) {
n , err = c . Conn . Read ( b )
if err != nil {
c . cancel ()
}
return n , err
}
func ( c * wsNetConn ) Write ( b [] byte ) ( n int , err error ) {
n , err = c . Conn . Write ( b )
if err != nil {
c . cancel ()
}
return n , err
}
func ( c * wsNetConn ) Close () error {
defer c . cancel ()
return c . Conn . Close ()
}
// WebsocketNetConn wraps websocket.NetConn and returns a context that
// is tied to the parent context and the lifetime of the conn. Any error
// during read or write will cancel the context, but not close the
// conn. Close should be called to release context resources.
func WebsocketNetConn ( ctx context . Context , conn * websocket . Conn , msgType websocket . MessageType ) ( context . Context , net . Conn ) {
ctx , cancel := context . WithCancel ( ctx )
nc := websocket . NetConn ( ctx , conn , msgType )
return ctx , & wsNetConn {
cancel : cancel ,
Conn : nc ,
}
}