Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3e4ba61

Browse files
committed
Hard & simplify CORORS handling logic
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent94cf49e commit3e4ba61

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

‎coderd/httpmw/cors.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/go-chi/cors"
99

1010
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
11-
ws_cors"github.com/coder/coder/v2/coderd/workspaceapps/cors"
1211
)
1312

1413
const (
@@ -48,11 +47,6 @@ func Cors(allowAll bool, origins ...string) func(next http.Handler) http.Handler
4847
funcWorkspaceAppCors(regex*regexp.Regexp,app appurl.ApplicationURL)func(next http.Handler) http.Handler {
4948
returncors.Handler(cors.Options{
5049
AllowOriginFunc:func(r*http.Request,rawOriginstring)bool {
51-
// If passthru behavior is set, disable our simplified CORS handling.
52-
ifws_cors.HasBehavior(r.Context(),ws_cors.AppCORSBehaviorPassthru) {
53-
returntrue
54-
}
55-
5650
origin,err:=url.Parse(rawOrigin)
5751
ifrawOrigin==""||origin.Host==""||err!=nil {
5852
returnfalse

‎coderd/workspaceapps/proxy.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,24 +424,42 @@ func (s *Server) HandleSubdomain(middlewares ...func(http.Handler) http.Handler)
424424
return
425425
}
426426

427-
//Use thepassed in app middlewares andCORS middleware with the token
428-
mws:=chi.Middlewares(append(middlewares,s.injectCORSBehavior(token),httpmw.WorkspaceAppCors(s.HostnameRegex,app)))
427+
//Proxy therequest (possibly with theCORS middleware).
428+
mws:=chi.Middlewares(append(middlewares,s.determineCORSBehavior(token,app)))
429429
mws.Handler(http.HandlerFunc(func(rw http.ResponseWriter,r*http.Request) {
430430
s.proxyWorkspaceApp(rw,r,*token,r.URL.Path,app)
431431
})).ServeHTTP(rw,r.WithContext(ctx))
432432
})
433433
}
434434
}
435435

436-
func (s*Server)injectCORSBehavior(token*SignedToken)func(http.Handler) http.Handler {
436+
// determineCORSBehavior examines the given token and conditionally applies
437+
// CORS middleware if the token specifies that behavior.
438+
func (s*Server)determineCORSBehavior(token*SignedToken,app appurl.ApplicationURL)func(http.Handler) http.Handler {
437439
returnfunc(next http.Handler) http.Handler {
440+
// Create the CORS middleware handler upfront.
441+
corsHandler:=httpmw.WorkspaceAppCors(s.HostnameRegex,app)(next)
442+
438443
returnhttp.HandlerFunc(func(rw http.ResponseWriter,r*http.Request) {
439444
varbehavior cors.AppCORSBehavior
440445
iftoken!=nil {
441446
behavior=token.CORSBehavior
442447
}
443448

444-
next.ServeHTTP(rw,r.WithContext(cors.WithBehavior(r.Context(),behavior)))
449+
// Add behavior to context regardless of which handler we use,
450+
// since we will use this later on to determine if we should strip
451+
// CORS headers in the response.
452+
r=r.WithContext(cors.WithBehavior(r.Context(),behavior))
453+
454+
switchbehavior {
455+
casecors.AppCORSBehaviorPassthru:
456+
// Bypass the CORS middleware.
457+
next.ServeHTTP(rw,r)
458+
return
459+
default:
460+
// Apply the CORS middleware.
461+
corsHandler.ServeHTTP(rw,r)
462+
}
445463
})
446464
}
447465
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp