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 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions modules/web/middleware/cookie.go
Expand Up @@ -38,17 +38,23 @@ 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, "/")
resp.Header().Add("Set-Cookie", cookie.String())
// 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="".
// 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} {
if maxAge < 0 || setting.SessionConfig.CookiePath == path {
cookie.Path = path
resp.Header().Add("Set-Cookie", cookie.String())
}
}
jtran marked this conversation as resolved.
Show resolved Hide resolved
}