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

Sanitize cookie names. #2216

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 27 additions & 1 deletion server/server.go
Expand Up @@ -15,6 +15,7 @@ import (
"reflect"
"regexp"
"sort"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -826,7 +827,7 @@ func (server *Server) loadConfig(configurations types.Configurations, globalConf
}

stickySession := config.Backends[frontend.Backend].LoadBalancer.Sticky
cookieName := "_TRAEFIK_BACKEND_" + frontend.Backend
cookieName := getCookieName(frontend.Backend)
var sticky *roundrobin.StickySession

if stickySession {
Expand Down Expand Up @@ -1208,3 +1209,28 @@ func (server *Server) buildRetryMiddleware(handler http.Handler, globalConfig co

return middlewares.NewRetry(retryAttempts, handler, retryListeners)
}

// getCookieName returns a cookie name from the given backend, sanitizing
// characters that do not satisfy the requirements of RFC 2616.
func getCookieName(backend string) string {
const cookiePrefix = "_TRAEFIK_BACKEND_"
sanitizer := func(r rune) rune {
switch r {
case '!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '^', '`', '|', '~':
return r
}

switch {
case r >= 'a' && r <= 'z':
fallthrough
case r >= 'A' && r <= 'Z':
fallthrough
case r >= '0' && r <= '9':
return r
default:
return '_'
}
}

return cookiePrefix + strings.Map(sanitizer, backend)
}
7 changes: 7 additions & 0 deletions server/server_test.go
Expand Up @@ -659,6 +659,13 @@ func TestServerResponseEmptyBackend(t *testing.T) {
}
}

func TestGetCookieName(t *testing.T) {
want := "_TRAEFIK_BACKEND__my_BACKEND-v1.0~rc1"
if got := getCookieName("/my/BACKEND-v1.0~rc1"); got != want {
t.Errorf("got sticky cookie name %q, want %q", got, want)
}
}

func buildDynamicConfig(dynamicConfigBuilders ...func(*types.Configuration)) *types.Configuration {
config := &types.Configuration{
Frontends: make(map[string]*types.Frontend),
Expand Down