@@ -473,13 +473,33 @@ func (ctx *Context) JSON(status int, content interface{}) {
473
473
}
474
474
}
475
475
476
+ func removeSessionCookieHeader (w http.ResponseWriter ) {
477
+ cookies := w .Header ()["Set-Cookie" ]
478
+ w .Header ().Del ("Set-Cookie" )
479
+ for _ , cookie := range cookies {
480
+ if strings .HasPrefix (cookie , setting .SessionConfig .CookieName + "=" ) {
481
+ continue
482
+ }
483
+ w .Header ().Add ("Set-Cookie" , cookie )
484
+ }
485
+ }
486
+
476
487
// Redirect redirects the request
477
488
func (ctx * Context ) Redirect (location string , status ... int ) {
478
489
code := http .StatusSeeOther
479
490
if len (status ) == 1 {
480
491
code = status [0 ]
481
492
}
482
493
494
+ if strings .Contains (location , "://" ) || strings .HasPrefix (location , "//" ) {
495
+ // Some browsers (Safari) have buggy behavior for Cookie + Cache + External Redirection, eg: /my-path => https://other/path
496
+ // 1. the first request to "/my-path" contains cookie
497
+ // 2. some time later, the request to "/my-path" doesn't contain cookie (caused by Prevent web tracking)
498
+ // 3. Gitea's Sessioner doesn't see the session cookie, so it generates a new session id, and returns it to browser
499
+ // 4. then the browser accepts the empty session, then the user is logged out
500
+ // So in this case, we should remove the session cookie from the response header
501
+ removeSessionCookieHeader (ctx .Resp )
502
+ }
483
503
http .Redirect (ctx .Resp , ctx .Req , location , code )
484
504
}
485
505
0 commit comments