Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix to delete cookie when AppSubURL is non-empty #30375

Merged
merged 20 commits into from
Apr 14, 2024
Merged
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions modules/web/middleware/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,26 @@ func SetSiteCookie(resp http.ResponseWriter, name, value string, maxAge int) {
Name: name,
Value: url.QueryEscape(value),
MaxAge: maxAge,
Path: setting.SessionConfig.CookiePath,
Path: "", // Filled in below.
Domain: setting.SessionConfig.Domain,
Secure: setting.SessionConfig.Secure,
HttpOnly: true,
SameSite: setting.SessionConfig.SameSite,
}
resp.Header().Add("Set-Cookie", cookie.String())
if maxAge < 0 {
// There was a bug in "setting.SessionConfig.CookiePath" code, the old default value of it was empty "".
// So we have to delete the cookie on path="" again, because some old code leaves cookies on path="".
cookie.Path = strings.TrimSuffix(setting.SessionConfig.CookiePath, "/")
// The code was updated, but it behaves differently depending on the
// value of AppSubURL. When AppSubURL is non-empty, the cookie with a
// trailing slash must be deleted.
withoutTrailingSlash := strings.TrimSuffix(setting.SessionConfig.CookiePath, "/")
withTrailingSlash := withoutTrailingSlash + "/"
for _, path := range []string{withoutTrailingSlash, withTrailingSlash} {
cookie.Path = path
resp.Header().Add("Set-Cookie", cookie.String())
}
} else {
cookie.Path = setting.SessionConfig.CookiePath
resp.Header().Add("Set-Cookie", cookie.String())
}
}