mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-13 20:18:45 +00:00
fix(policy/expressions): do not Contains missing keys
Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
@@ -46,6 +46,10 @@ func (h HTTPHeaders) Find(key ref.Val) (ref.Val, bool) {
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, ok := h.Header[string(k)]; !ok {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
return types.String(strings.Join(h.Header.Values(string(k)), ",")), true
|
return types.String(strings.Join(h.Header.Values(string(k)), ",")), true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
52
lib/policy/expressions/http_headers_test.go
Normal file
52
lib/policy/expressions/http_headers_test.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package expressions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/cel-go/common/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHTTPHeaders(t *testing.T) {
|
||||||
|
headers := HTTPHeaders{
|
||||||
|
Header: http.Header{
|
||||||
|
"Content-Type": {"application/json"},
|
||||||
|
"Cf-Worker": {"true"},
|
||||||
|
"User-Agent": {"Go-http-client/2"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("contains-existing-header", func(t *testing.T) {
|
||||||
|
resp := headers.Contains(types.String("User-Agent"))
|
||||||
|
if !bool(resp.(types.Bool)) {
|
||||||
|
t.Fatal("headers does not contain User-Agent")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("not-contains-missing-header", func(t *testing.T) {
|
||||||
|
resp := headers.Contains(types.String("Xxx-Random-Header"))
|
||||||
|
if bool(resp.(types.Bool)) {
|
||||||
|
t.Fatal("headers does not contain User-Agent")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("get-existing-header", func(t *testing.T) {
|
||||||
|
val := headers.Get(types.String("User-Agent"))
|
||||||
|
switch val.(type) {
|
||||||
|
case types.String:
|
||||||
|
// ok
|
||||||
|
default:
|
||||||
|
t.Fatalf("result was wrong type %T", val)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("not-get-missing-header", func(t *testing.T) {
|
||||||
|
val := headers.Get(types.String("Xxx-Random-Header"))
|
||||||
|
switch val.(type) {
|
||||||
|
case *types.Err:
|
||||||
|
// ok
|
||||||
|
default:
|
||||||
|
t.Fatalf("result was wrong type %T", val)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -49,6 +49,10 @@ func (u URLValues) Find(key ref.Val) (ref.Val, bool) {
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, ok := u.Values[string(k)]; !ok {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
return types.String(strings.Join(u.Values[string(k)], ",")), true
|
return types.String(strings.Join(u.Values[string(k)], ",")), true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
50
lib/policy/expressions/url_values_test.go
Normal file
50
lib/policy/expressions/url_values_test.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package expressions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/cel-go/common/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestURLValues(t *testing.T) {
|
||||||
|
headers := URLValues{
|
||||||
|
Values: url.Values{
|
||||||
|
"format": {"json"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("contains-existing-key", func(t *testing.T) {
|
||||||
|
resp := headers.Contains(types.String("format"))
|
||||||
|
if !bool(resp.(types.Bool)) {
|
||||||
|
t.Fatal("headers does not contain User-Agent")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("not-contains-missing-key", func(t *testing.T) {
|
||||||
|
resp := headers.Contains(types.String("not-there"))
|
||||||
|
if bool(resp.(types.Bool)) {
|
||||||
|
t.Fatal("headers does not contain User-Agent")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("get-existing-key", func(t *testing.T) {
|
||||||
|
val := headers.Get(types.String("format"))
|
||||||
|
switch val.(type) {
|
||||||
|
case types.String:
|
||||||
|
// ok
|
||||||
|
default:
|
||||||
|
t.Fatalf("result was wrong type %T", val)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("not-get-missing-key", func(t *testing.T) {
|
||||||
|
val := headers.Get(types.String("not-there"))
|
||||||
|
switch val.(type) {
|
||||||
|
case *types.Err:
|
||||||
|
// ok
|
||||||
|
default:
|
||||||
|
t.Fatalf("result was wrong type %T", val)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user