2023-05-22 20:02:39 +02:00
|
|
|
package httpmw
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2023-06-07 11:08:14 -08:00
|
|
|
"net/url"
|
|
|
|
|
"regexp"
|
2025-07-19 22:05:15 +02:00
|
|
|
"strings"
|
2023-05-22 20:02:39 +02:00
|
|
|
|
|
|
|
|
"github.com/go-chi/cors"
|
2023-06-07 11:08:14 -08:00
|
|
|
|
2024-01-17 10:41:42 -06:00
|
|
|
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
|
2023-05-22 20:02:39 +02:00
|
|
|
)
|
|
|
|
|
|
2023-06-21 13:41:27 -08:00
|
|
|
const (
|
|
|
|
|
// Server headers.
|
|
|
|
|
AccessControlAllowOriginHeader = "Access-Control-Allow-Origin"
|
|
|
|
|
AccessControlAllowCredentialsHeader = "Access-Control-Allow-Credentials"
|
|
|
|
|
AccessControlAllowMethodsHeader = "Access-Control-Allow-Methods"
|
|
|
|
|
AccessControlAllowHeadersHeader = "Access-Control-Allow-Headers"
|
|
|
|
|
VaryHeader = "Vary"
|
|
|
|
|
|
|
|
|
|
// Client headers.
|
|
|
|
|
OriginHeader = "Origin"
|
|
|
|
|
AccessControlRequestMethodsHeader = "Access-Control-Request-Methods"
|
|
|
|
|
AccessControlRequestHeadersHeader = "Access-Control-Request-Headers"
|
|
|
|
|
)
|
|
|
|
|
|
2023-05-22 20:02:39 +02:00
|
|
|
//nolint:revive
|
|
|
|
|
func Cors(allowAll bool, origins ...string) func(next http.Handler) http.Handler {
|
|
|
|
|
if len(origins) == 0 {
|
|
|
|
|
// The default behavior is '*', so putting the empty string defaults to
|
2025-07-19 22:05:15 +02:00
|
|
|
// the secure behavior of blocking CORS requests.
|
2023-05-22 20:02:39 +02:00
|
|
|
origins = []string{""}
|
|
|
|
|
}
|
|
|
|
|
if allowAll {
|
|
|
|
|
origins = []string{"*"}
|
|
|
|
|
}
|
2025-07-19 22:05:15 +02:00
|
|
|
|
|
|
|
|
// Standard CORS for most endpoints
|
|
|
|
|
standardCors := cors.Handler(cors.Options{
|
2023-05-22 20:02:39 +02:00
|
|
|
AllowedOrigins: origins,
|
|
|
|
|
// We only need GET for latency requests
|
|
|
|
|
AllowedMethods: []string{http.MethodOptions, http.MethodGet},
|
|
|
|
|
AllowedHeaders: []string{"Accept", "Content-Type", "X-LATENCY-CHECK", "X-CSRF-TOKEN"},
|
|
|
|
|
// Do not send any cookies
|
|
|
|
|
AllowCredentials: false,
|
|
|
|
|
})
|
2025-07-19 22:05:15 +02:00
|
|
|
|
|
|
|
|
// Permissive CORS for OAuth2 and MCP endpoints
|
|
|
|
|
permissiveCors := cors.Handler(cors.Options{
|
|
|
|
|
AllowedOrigins: []string{"*"},
|
|
|
|
|
AllowedMethods: []string{
|
|
|
|
|
http.MethodGet,
|
|
|
|
|
http.MethodPost,
|
|
|
|
|
http.MethodDelete,
|
|
|
|
|
http.MethodOptions,
|
|
|
|
|
},
|
|
|
|
|
AllowedHeaders: []string{
|
|
|
|
|
"Content-Type",
|
|
|
|
|
"Accept",
|
|
|
|
|
"Authorization",
|
|
|
|
|
"x-api-key",
|
|
|
|
|
"Mcp-Session-Id",
|
|
|
|
|
"MCP-Protocol-Version",
|
|
|
|
|
"Last-Event-ID",
|
|
|
|
|
},
|
|
|
|
|
ExposedHeaders: []string{
|
|
|
|
|
"Content-Type",
|
|
|
|
|
"Authorization",
|
|
|
|
|
"x-api-key",
|
|
|
|
|
"Mcp-Session-Id",
|
|
|
|
|
"MCP-Protocol-Version",
|
|
|
|
|
},
|
|
|
|
|
MaxAge: 86400, // 24 hours in seconds
|
|
|
|
|
AllowCredentials: false,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// Use permissive CORS for OAuth2, MCP, and well-known endpoints
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/oauth2/") ||
|
|
|
|
|
strings.HasPrefix(r.URL.Path, "/api/experimental/mcp/") ||
|
|
|
|
|
strings.HasPrefix(r.URL.Path, "/.well-known/oauth-") {
|
|
|
|
|
permissiveCors(next).ServeHTTP(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use standard CORS for all other endpoints
|
|
|
|
|
standardCors(next).ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-05-22 20:02:39 +02:00
|
|
|
}
|
2023-06-07 11:08:14 -08:00
|
|
|
|
2024-01-17 10:41:42 -06:00
|
|
|
func WorkspaceAppCors(regex *regexp.Regexp, app appurl.ApplicationURL) func(next http.Handler) http.Handler {
|
2023-06-07 11:08:14 -08:00
|
|
|
return cors.Handler(cors.Options{
|
2025-03-26 01:56:39 -05:00
|
|
|
AllowOriginFunc: func(_ *http.Request, rawOrigin string) bool {
|
2023-06-07 11:08:14 -08:00
|
|
|
origin, err := url.Parse(rawOrigin)
|
|
|
|
|
if rawOrigin == "" || origin.Host == "" || err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-01-17 10:41:42 -06:00
|
|
|
subdomain, ok := appurl.ExecuteHostnamePattern(regex, origin.Host)
|
2023-06-07 11:08:14 -08:00
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-01-17 10:41:42 -06:00
|
|
|
originApp, err := appurl.ParseSubdomainAppURL(subdomain)
|
2023-06-07 11:08:14 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return ok && originApp.Username == app.Username
|
|
|
|
|
},
|
|
|
|
|
AllowedMethods: []string{
|
|
|
|
|
http.MethodHead,
|
|
|
|
|
http.MethodGet,
|
|
|
|
|
http.MethodPost,
|
|
|
|
|
http.MethodPut,
|
|
|
|
|
http.MethodPatch,
|
|
|
|
|
http.MethodDelete,
|
|
|
|
|
},
|
|
|
|
|
AllowedHeaders: []string{"*"},
|
|
|
|
|
AllowCredentials: true,
|
|
|
|
|
})
|
|
|
|
|
}
|