fix: traefik doesnt know cookies helpers

This commit is contained in:
Laurence
2026-03-03 19:03:08 +00:00
parent 2902340f7b
commit d81af67db3

47
main.go
View File

@@ -440,54 +440,35 @@ func (p *Badger) stripSessionParam(req *http.Request) {
} }
// stripSessionCookies removes session cookies from the request before forwarding to the backend. // stripSessionCookies removes session cookies from the request before forwarding to the backend.
// Cookie request headers only contain name=value pairs (Set-Cookie attributes like Path/Domain // It processes raw Cookie header pairs so non-target cookies are preserved as-is.
// are response-only), so we filter parsed request cookies and rebuild the Cookie header.
func (p *Badger) stripSessionCookies(req *http.Request) { func (p *Badger) stripSessionCookies(req *http.Request) {
cookieHeaders := req.Header.Values("Cookie") cookieHeaders := req.Header.Values("Cookie")
if len(cookieHeaders) == 0 { if len(cookieHeaders) == 0 {
return return
} }
var remaining []*http.Cookie var remainingPairs []string
for _, headerValue := range cookieHeaders { for _, headerValue := range cookieHeaders {
parsedCookies, err := http.ParseCookie(headerValue) for _, part := range strings.Split(headerValue, ";") {
if err != nil { part = strings.TrimSpace(part)
// Best-effort fallback for malformed Cookie headers. if part == "" {
for _, part := range strings.Split(headerValue, ";") { continue
part = strings.TrimSpace(part)
if part == "" {
continue
}
name, value, hasValue := strings.Cut(part, "=")
name = strings.TrimSpace(name)
if strings.HasPrefix(name, p.userSessionCookieName) {
continue
}
if hasValue {
remaining = append(remaining, &http.Cookie{
Name: name,
Value: strings.TrimSpace(value),
})
}
} }
continue name, _, _ := strings.Cut(part, "=")
} name = strings.TrimSpace(name)
if !strings.HasPrefix(name, p.userSessionCookieName) {
for _, cookie := range parsedCookies { remainingPairs = append(remainingPairs, part)
if !strings.HasPrefix(cookie.Name, p.userSessionCookieName) {
remaining = append(remaining, cookie)
} }
} }
} }
req.Header.Del("Cookie") if len(remainingPairs) == 0 {
if len(remaining) == 0 { req.Header.Del("Cookie")
return return
} }
for _, cookie := range remaining { // Keep a single canonical Cookie header while preserving surviving name=value pairs.
req.AddCookie(cookie) req.Header.Set("Cookie", strings.Join(remainingPairs, "; "))
}
} }
func (p *Badger) isTrustedIP(remoteAddr string) bool { func (p *Badger) isTrustedIP(remoteAddr string) bool {