@@ -61,21 +61,26 @@ const (
61
61
)
62
62
63
63
// CorsHandler return a http handler who set CORS options if enabled by config
64
- func CorsHandler () func (next http.Handler ) http.Handler {
64
+ func optionsCorsHandler () func (next http.Handler ) http.Handler {
65
65
if setting .CORSConfig .Enabled {
66
66
return cors .Handler (cors.Options {
67
- // Scheme: setting.CORSConfig.Scheme, // FIXME: the cors middleware needs scheme option
68
- AllowedOrigins : setting .CORSConfig .AllowDomain ,
69
- // setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option
70
- AllowedMethods : setting .CORSConfig .Methods ,
71
- AllowCredentials : setting .CORSConfig .AllowCredentials ,
72
- AllowedHeaders : setting .CORSConfig .Headers ,
73
- MaxAge : int (setting .CORSConfig .MaxAge .Seconds ()),
67
+ AllowedOrigins : setting .CORSConfig .AllowDomain ,
68
+ AllowedMethods : setting .CORSConfig .Methods ,
69
+ AllowCredentials : setting .CORSConfig .AllowCredentials ,
70
+ AllowedHeaders : setting .CORSConfig .Headers ,
71
+ MaxAge : int (setting .CORSConfig .MaxAge .Seconds ()),
72
+ OptionsPassthrough : false , // explicitly set it to say "avoid OPTIONS requests being passed to the next handler"
74
73
})
75
74
}
76
75
77
76
return func (next http.Handler ) http.Handler {
78
- return next
77
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
78
+ if r .Method == http .MethodOptions {
79
+ w .WriteHeader (http .StatusBadRequest )
80
+ return // it should explicitly deny OPTIONS requests if CORS is disabled, to avoid the following GET/POST handler to be called by the OPTIONS request
81
+ }
82
+ next .ServeHTTP (w , r )
83
+ })
79
84
}
80
85
}
81
86
@@ -218,7 +223,7 @@ func Routes() *web.Route {
218
223
routes := web .NewRoute ()
219
224
220
225
routes .Head ("/" , misc .DummyOK ) // for health check - doesn't need to be passed through gzip handler
221
- routes .Methods ("GET, HEAD" , "/assets/*" , CorsHandler (), public .FileHandlerFunc ())
226
+ routes .Methods ("GET, HEAD, OPTIONS " , "/assets/*" , optionsCorsHandler (), public .FileHandlerFunc ())
222
227
routes .Methods ("GET, HEAD" , "/avatars/*" , storageHandler (setting .Avatar .Storage , "avatars" , storage .Avatars ))
223
228
routes .Methods ("GET, HEAD" , "/repo-avatars/*" , storageHandler (setting .RepoAvatar .Storage , "repo-avatars" , storage .RepoAvatars ))
224
229
routes .Methods ("GET, HEAD" , "/apple-touch-icon.png" , misc .StaticRedirect ("/assets/img/apple-touch-icon.png" ))
@@ -458,8 +463,8 @@ func registerRoutes(m *web.Route) {
458
463
m .Get ("/change-password" , func (ctx * context.Context ) {
459
464
ctx .Redirect (setting .AppSubURL + "/user/settings/account" )
460
465
})
461
- m .Any ( "/* " , CorsHandler () , public .FileHandlerFunc ())
462
- }, CorsHandler ())
466
+ m .Methods ( "GET, HEAD " , "/*" , public .FileHandlerFunc ())
467
+ }, optionsCorsHandler ())
463
468
464
469
m .Group ("/explore" , func () {
465
470
m .Get ("" , func (ctx * context.Context ) {
@@ -532,14 +537,11 @@ func registerRoutes(m *web.Route) {
532
537
// TODO manage redirection
533
538
m .Post ("/authorize" , web .Bind (forms.AuthorizationForm {}), auth .AuthorizeOAuth )
534
539
}, ignSignInAndCsrf , reqSignIn )
535
- m .Options ("/login/oauth/userinfo" , CorsHandler (), misc .DummyBadRequest )
536
- m .Get ("/login/oauth/userinfo" , ignSignInAndCsrf , auth .InfoOAuth )
537
- m .Options ("/login/oauth/access_token" , CorsHandler (), misc .DummyBadRequest )
538
- m .Post ("/login/oauth/access_token" , CorsHandler (), web .Bind (forms.AccessTokenForm {}), ignSignInAndCsrf , auth .AccessTokenOAuth )
539
- m .Options ("/login/oauth/keys" , CorsHandler (), misc .DummyBadRequest )
540
- m .Get ("/login/oauth/keys" , ignSignInAndCsrf , auth .OIDCKeys )
541
- m .Options ("/login/oauth/introspect" , CorsHandler (), misc .DummyBadRequest )
542
- m .Post ("/login/oauth/introspect" , CorsHandler (), web .Bind (forms.IntrospectTokenForm {}), ignSignInAndCsrf , auth .IntrospectOAuth )
540
+
541
+ m .Methods ("GET, OPTIONS" , "/login/oauth/userinfo" , optionsCorsHandler (), ignSignInAndCsrf , auth .InfoOAuth )
542
+ m .Methods ("POST, OPTIONS" , "/login/oauth/access_token" , optionsCorsHandler (), web .Bind (forms.AccessTokenForm {}), ignSignInAndCsrf , auth .AccessTokenOAuth )
543
+ m .Methods ("GET, OPTIONS" , "/login/oauth/keys" , optionsCorsHandler (), ignSignInAndCsrf , auth .OIDCKeys )
544
+ m .Methods ("POST, OPTIONS" , "/login/oauth/introspect" , optionsCorsHandler (), web .Bind (forms.IntrospectTokenForm {}), ignSignInAndCsrf , auth .IntrospectOAuth )
543
545
544
546
m .Group ("/user/settings" , func () {
545
547
m .Get ("" , user_setting .Profile )
0 commit comments