99 "net/url"
1010 "strings"
1111
12+ "code.gitea.io/gitea/modules/session"
1213 "code.gitea.io/gitea/modules/setting"
1314)
1415
@@ -48,12 +49,12 @@ func SetSiteCookie(resp http.ResponseWriter, name, value string, maxAge int) {
4849 // Previous versions would use a cookie path with a trailing /.
4950 // These are more specific than cookies without a trailing /, so
5051 // we need to delete these if they exist.
51- DeleteLegacySiteCookie (resp , name )
52+ deleteLegacySiteCookie (resp , name )
5253}
5354
54- // DeleteLegacySiteCookie deletes the cookie with the given name at the cookie
55+ // deleteLegacySiteCookie deletes the cookie with the given name at the cookie
5556// path with a trailing /, which would unintentionally override the cookie.
56- func DeleteLegacySiteCookie (resp http.ResponseWriter , name string ) {
57+ func deleteLegacySiteCookie (resp http.ResponseWriter , name string ) {
5758 if setting .SessionConfig .CookiePath == "" || strings .HasSuffix (setting .SessionConfig .CookiePath , "/" ) {
5859 // If the cookie path ends with /, no legacy cookies will take
5960 // precedence, so do nothing. The exception is that cookies with no
@@ -74,3 +75,11 @@ func DeleteLegacySiteCookie(resp http.ResponseWriter, name string) {
7475 }
7576 resp .Header ().Add ("Set-Cookie" , cookie .String ())
7677}
78+
79+ func init () {
80+ session .BeforeRegenerateSession = append (session .BeforeRegenerateSession , func (resp http.ResponseWriter , _ * http.Request ) {
81+ // Ensure that a cookie with a trailing slash does not take precedence over
82+ // the cookie written by the middleware.
83+ deleteLegacySiteCookie (resp , setting .SessionConfig .CookieName )
84+ })
85+ }
0 commit comments