fix: traefik doesnt know cookies helpers

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

35
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)
if err != nil {
// Best-effort fallback for malformed Cookie headers.
for _, part := range strings.Split(headerValue, ";") { for _, part := range strings.Split(headerValue, ";") {
part = strings.TrimSpace(part) part = strings.TrimSpace(part)
if part == "" { if part == "" {
continue continue
} }
name, value, hasValue := strings.Cut(part, "=") name, _, _ := strings.Cut(part, "=")
name = strings.TrimSpace(name) name = strings.TrimSpace(name)
if strings.HasPrefix(name, p.userSessionCookieName) { if !strings.HasPrefix(name, p.userSessionCookieName) {
continue remainingPairs = append(remainingPairs, part)
}
if hasValue {
remaining = append(remaining, &http.Cookie{
Name: name,
Value: strings.TrimSpace(value),
})
}
}
continue
}
for _, cookie := range parsedCookies {
if !strings.HasPrefix(cookie.Name, p.userSessionCookieName) {
remaining = append(remaining, cookie)
} }
} }
} }
if len(remainingPairs) == 0 {
req.Header.Del("Cookie") req.Header.Del("Cookie")
if len(remaining) == 0 {
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 {