Sanitize cookie names.
This commit is contained in:
parent
c94e5f3589
commit
8a67434380
2 changed files with 34 additions and 1 deletions
|
@ -15,6 +15,7 @@ import (
|
|||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
|
|
Loading…
Reference in a new issue