Skip to content

Commit

Permalink
caddyhttp: Log error from CEL evaluation (fix #4832)
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Jun 8, 2022
1 parent 7f9b1f4 commit 1498132
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions modules/caddyhttp/celmatcher.go
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/google/cel-go/common/types/traits"
"github.com/google/cel-go/ext"
"github.com/google/cel-go/interpreter/functions"
"go.uber.org/zap"
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -62,6 +63,8 @@ type MatchExpression struct {
expandedExpr string
prg cel.Program
ta ref.TypeAdapter

log *zap.Logger
}

// CaddyModule returns the Caddy module information.
Expand All @@ -83,7 +86,9 @@ func (m *MatchExpression) UnmarshalJSON(data []byte) error {
}

// Provision sets ups m.
func (m *MatchExpression) Provision(_ caddy.Context) error {
func (m *MatchExpression) Provision(ctx caddy.Context) error {
m.log = ctx.Logger(m)

// replace placeholders with a function call - this is just some
// light (and possibly naïve) syntactic sugar
m.expandedExpr = placeholderRegexp.ReplaceAllString(m.Expr, placeholderExpansion)
Expand Down Expand Up @@ -137,9 +142,13 @@ func (m *MatchExpression) Provision(_ caddy.Context) error {

// Match returns true if r matches m.
func (m MatchExpression) Match(r *http.Request) bool {
out, _, _ := m.prg.Eval(map[string]interface{}{
out, _, err := m.prg.Eval(map[string]interface{}{
"request": celHTTPRequest{r},
})
if err != nil {
m.log.Error("evaluating expression", zap.Error(err))
return false
}
if outBool, ok := out.Value().(bool); ok {
return outBool
}
Expand Down
2 changes: 1 addition & 1 deletion modules/caddyhttp/celmatcher_test.go
Expand Up @@ -116,7 +116,7 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
}

if tt.expression.Match(req) != tt.wantResult {
t.Errorf("MatchExpression.Match() expected to return '%t', for expression : '%s'", tt.wantResult, tt.expression)
t.Errorf("MatchExpression.Match() expected to return '%t', for expression : '%s'", tt.wantResult, tt.expression.Expr)
}

})
Expand Down

0 comments on commit 1498132

Please sign in to comment.