Skip to content

Commit b18c04e

Browse files
jtranwxiaoguangkdumontnu
authored
fix: Fix to delete cookie when AppSubURL is non-empty (#30375)
Cookies may exist on "/subpath" and "/subpath/" for some legacy reasons (eg: changed CookiePath behavior in code). The legacy cookie should be removed correctly. --------- Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: Kyle D <[email protected]>
1 parent c77e814 commit b18c04e

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

modules/session/store.go

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package session
66
import (
77
"net/http"
88

9+
"code.gitea.io/gitea/modules/setting"
10+
"code.gitea.io/gitea/modules/web/middleware"
11+
912
"gitea.com/go-chi/session"
1013
)
1114

@@ -18,6 +21,10 @@ type Store interface {
1821

1922
// RegenerateSession regenerates the underlying session and returns the new store
2023
func RegenerateSession(resp http.ResponseWriter, req *http.Request) (Store, error) {
24+
// Ensure that a cookie with a trailing slash does not take precedence over
25+
// the cookie written by the middleware.
26+
middleware.DeleteLegacySiteCookie(resp, setting.SessionConfig.CookieName)
27+
2128
s, err := session.RegenerateSession(resp, req)
2229
return s, err
2330
}

modules/web/middleware/cookie.go

+27-5
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,32 @@ func SetSiteCookie(resp http.ResponseWriter, name, value string, maxAge int) {
4545
SameSite: setting.SessionConfig.SameSite,
4646
}
4747
resp.Header().Add("Set-Cookie", cookie.String())
48-
if maxAge < 0 {
49-
// There was a bug in "setting.SessionConfig.CookiePath" code, the old default value of it was empty "".
50-
// So we have to delete the cookie on path="" again, because some old code leaves cookies on path="".
51-
cookie.Path = strings.TrimSuffix(setting.SessionConfig.CookiePath, "/")
52-
resp.Header().Add("Set-Cookie", cookie.String())
48+
// Previous versions would use a cookie path with a trailing /.
49+
// These are more specific than cookies without a trailing /, so
50+
// we need to delete these if they exist.
51+
DeleteLegacySiteCookie(resp, name)
52+
}
53+
54+
// DeleteLegacySiteCookie deletes the cookie with the given name at the cookie
55+
// path with a trailing /, which would unintentionally override the cookie.
56+
func DeleteLegacySiteCookie(resp http.ResponseWriter, name string) {
57+
if setting.SessionConfig.CookiePath == "" || strings.HasSuffix(setting.SessionConfig.CookiePath, "/") {
58+
// If the cookie path ends with /, no legacy cookies will take
59+
// precedence, so do nothing. The exception is that cookies with no
60+
// path could override other cookies, but it's complicated and we don't
61+
// currently handle that.
62+
return
5363
}
64+
65+
cookie := &http.Cookie{
66+
Name: name,
67+
Value: "",
68+
MaxAge: -1,
69+
Path: setting.SessionConfig.CookiePath + "/",
70+
Domain: setting.SessionConfig.Domain,
71+
Secure: setting.SessionConfig.Secure,
72+
HttpOnly: true,
73+
SameSite: setting.SessionConfig.SameSite,
74+
}
75+
resp.Header().Add("Set-Cookie", cookie.String())
5476
}

services/auth/source/oauth2/store.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010

1111
"code.gitea.io/gitea/modules/log"
12+
session_module "code.gitea.io/gitea/modules/session"
1213

1314
chiSession "gitea.com/go-chi/session"
1415
"github.com/gorilla/sessions"
@@ -65,7 +66,7 @@ func (st *SessionsStore) Save(r *http.Request, w http.ResponseWriter, session *s
6566
chiStore := chiSession.GetSession(r)
6667

6768
if session.IsNew {
68-
_, _ = chiSession.RegenerateSession(w, r)
69+
_, _ = session_module.RegenerateSession(w, r)
6970
session.IsNew = false
7071
}
7172

0 commit comments

Comments
 (0)