Skip to content

chore: CORs option for yarn dev server #7630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add cors
  • Loading branch information
Emyrk committed May 22, 2023
commit e2cdf3dc4a25d07167a409a63a0159c7089d4dd5
19 changes: 18 additions & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,24 @@ func New(options *Options) *API {
derpHandler := derphttp.Handler(api.DERPServer)
derpHandler, api.derpCloseFunc = tailnet.WithWebsocketSupport(api.DERPServer, derpHandler)

// By default, the app will never set any cors headers. If the AllowAllCors is provided, then
// we should add the appropriate cors headers.
cors := func(next http.Handler) http.Handler {
return next
}
if options.DeploymentValues.Dangerous.AllowAllCors {
cors = func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Access-Control-Allow-Origin", "*")
rw.Header().Set("Access-Control-Allow-Headers", "*")
rw.Header().Set("Access-Control-Allow-Methods", "*")
next.ServeHTTP(rw, r)
})
}
}

r.Use(
cors,
httpmw.Recover(api.Logger),
tracing.StatusWriterMiddleware,
tracing.Middleware(api.TracerProvider),
Expand Down Expand Up @@ -817,7 +834,7 @@ func New(options *Options) *API {
// This is the only route we add before all the middleware.
// We want to time the latency of the request, so any middleware will
// interfere with that timing.
rootRouter.Get("/latency-check", LatencyCheck(options.DeploymentValues.Dangerous.AllowAllCors.Value(), api.AccessURL))
rootRouter.Get("/latency-check", cors(LatencyCheck(options.DeploymentValues.Dangerous.AllowAllCors.Value(), api.AccessURL)).ServeHTTP)
rootRouter.Mount("/", r)
api.RootHandler = rootRouter

Expand Down
5 changes: 3 additions & 2 deletions coderd/httpmw/csp.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ func CSPHeaders(websocketHosts func() []string) func(next http.Handler) http.Han
if len(extraConnect) > 0 {
for _, extraHost := range extraConnect {
if extraHost == "*" {
// '*' means all, which means we only need the prefixes.
extraHost = ""
// '*' means all
cspSrcs.Append(cspDirectiveConnectSrc, "*")
continue
}
cspSrcs.Append(cspDirectiveConnectSrc, fmt.Sprintf("wss://%[1]s ws://%[1]s", extraHost))
// We also require this to make http/https requests to the workspace proxy for latency checking.
Expand Down