Skip to content

Commit

Permalink
Sanitize cookie names.
Browse files Browse the repository at this point in the history
  • Loading branch information
timoreimann authored and traefiker committed Oct 5, 2017
1 parent c94e5f3 commit 8a67434
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
28 changes: 27 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 8a67434

Please sign in to comment.