Skip to content

Commit

Permalink
feature: URL prefix case-sensitive (#1637)
Browse files Browse the repository at this point in the history
* bug fix: URL prefix case-sensitive

* default case sensitive

* eliminate repeated judgments
  • Loading branch information
GLYASAI committed Apr 25, 2021
1 parent 78a8f92 commit b3b875c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 9 additions & 2 deletions pkg/filter/stream/jwtauthn/matcher.go
Expand Up @@ -34,6 +34,7 @@ type baseMatcher struct {
}

func newBaseMatcher(rule *jwtauthnv3.RequirementRule) *baseMatcher {
// default case sensitive
caseSensitive := true
if rule.Match.CaseSensitive != nil {
caseSensitive = rule.Match.CaseSensitive.Value
Expand Down Expand Up @@ -69,7 +70,13 @@ func newPrefixMatcher(rule *jwtauthnv3.RequirementRule) Matcher {
}

func (p *prefixMatcher) Matches(headers api.HeaderMap, requestPath string) bool {
if p.baseMatcher.matchRoutes(headers, requestPath) && strings.HasPrefix(requestPath, p.prefix) {
path, prefix := requestPath, p.prefix
if !p.baseMatcher.caseSensitive {
path, prefix = strings.ToLower(requestPath), strings.ToLower(p.prefix)
}
prefixMatch := strings.HasPrefix(path, prefix)

if p.baseMatcher.matchRoutes(headers, requestPath) && prefixMatch {
if log.DefaultLogger.GetLogLevel() >= log.DEBUG {
log.DefaultLogger.Debugf("Prefix requirement '%s' matched.", p.prefix)
}
Expand Down Expand Up @@ -99,7 +106,7 @@ func (p *pathMatcher) Matches(headers api.HeaderMap, requestPath string) bool {

pathMatch := p.path == requestPath
if !p.caseSensitive {
pathMatch = strings.ToLower(p.path) == strings.ToLower(requestPath)
pathMatch = strings.EqualFold(strings.ToLower(p.path), strings.ToLower(requestPath))
}
if p.baseMatcher.matchRoutes(headers, requestPath) && pathMatch {
if log.DefaultLogger.GetLogLevel() >= log.DEBUG {
Expand Down
16 changes: 15 additions & 1 deletion pkg/filter/stream/jwtauthn/matcher_test.go
Expand Up @@ -6,7 +6,6 @@ import (
routev3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
jwtauthnv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/jwt_authn/v3"
"github.com/golang/protobuf/ptypes/wrappers"
//matcherv3 "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
"github.com/stretchr/testify/assert"
)

Expand All @@ -27,6 +26,21 @@ func TestMatchPrefix(t *testing.T) {
assert.False(t, matcher.Matches(nil, "/no"))
}

func TestMatchPrefixCaseInsensitive(t *testing.T) {
rule := &jwtauthnv3.RequirementRule{
Match: &routev3.RouteMatch{
PathSpecifier: &routev3.RouteMatch_Prefix{
Prefix: "/match",
},
CaseSensitive: &wrappers.BoolValue{Value: false},
},
}

matcher := NewMatcher(rule)
assert.True(t, matcher.Matches(nil, "/matching"))
assert.True(t, matcher.Matches(nil, "/MATCH"))
}

func TestMatchPath(t *testing.T) {
rule := &jwtauthnv3.RequirementRule{
Match: &routev3.RouteMatch{
Expand Down

0 comments on commit b3b875c

Please sign in to comment.