Skip to content

Commit

Permalink
Case insensitive header matching and safe lookup
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Nov 13, 2024
1 parent d5f0184 commit 312e779
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 5 additions & 2 deletions pkg/wasm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"

"github.com/kuadrant/policy-machinery/machinery"
"github.com/samber/lo"
Expand Down Expand Up @@ -179,9 +180,11 @@ func predicateFromMethod(method gatewayapiv1.HTTPMethod) string {
func predicateFromHeader(headerMatch gatewayapiv1.HTTPHeaderMatch) string {
// As for gateway api v1, the only operation type with core support is Exact match.
// https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.HTTPHeaderMatch
return fmt.Sprintf("request.headers['%s'] == '%s'", headerMatch.Name, headerMatch.Value)
return fmt.Sprintf("request.headers.exists(h, h.lowerAscii() == '%s' && request.headers[h] == '%s')",
strings.ToLower(string(headerMatch.Name)), headerMatch.Value)
}

func predicateFromQueryParam(queryParam gatewayapiv1.HTTPQueryParamMatch) string {
return fmt.Sprintf("queryMap(request.query)['%s'] == '%s'", queryParam.Name, queryParam.Value)
return fmt.Sprintf("'%s' in queryMap(request.query) ? queryMap(request.query)['%s'] == '%s' : false",
queryParam.Name, queryParam.Name, queryParam.Value)
}
8 changes: 4 additions & 4 deletions pkg/wasm/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func TestPredicatesFromHTTPRouteMatch(t *testing.T) {
headerMatch := gatewayapiv1.HeaderMatchExact
header := gatewayapiv1.HTTPHeaderMatch{
Type: &headerMatch,
Name: "x-auth",
Name: "X-Auth",
Value: "kuadrant",
}

Expand All @@ -282,8 +282,8 @@ func TestPredicatesFromHTTPRouteMatch(t *testing.T) {

assert.Equal(t, predicates[0], "request.method == 'TRACE'")
assert.Equal(t, predicates[1], "request.url_path.startsWith('/admin')")
assert.Equal(t, predicates[2], "request.headers['x-auth'] == 'kuadrant'")
assert.Equal(t, predicates[3], "queryMap(request.query)['foo'] == 'bar'")
assert.Equal(t, predicates[4], "queryMap(request.query)['kua'] == 'drant'")
assert.Equal(t, predicates[2], "request.headers.exists(h, h.lowerAscii() == 'x-auth' && request.headers[h] == 'kuadrant')")
assert.Equal(t, predicates[3], "'foo' in queryMap(request.query) ? queryMap(request.query)['foo'] == 'bar' : false")
assert.Equal(t, predicates[4], "'kua' in queryMap(request.query) ? queryMap(request.query)['kua'] == 'drant' : false")
assert.Equal(t, len(predicates), 5)
}

0 comments on commit 312e779

Please sign in to comment.