diff options
| author | Matthew Holt <mholt@users.noreply.github.com> | 2022-06-08 16:42:24 -0600 | 
|---|---|---|
| committer | Matthew Holt <mholt@users.noreply.github.com> | 2022-06-08 16:42:24 -0600 | 
| commit | 1498132ea3c4f01d3be41812dbd02364cb77d263 (patch) | |
| tree | f37d4ec6ed66c5479c6fcf00b14d62b216d7d93a /modules/caddyhttp | |
| parent | 7f9b1f43c9f8c7d0f4ca9271090cae92103232c7 (diff) | |
caddyhttp: Log error from CEL evaluation (fix #4832)
Diffstat (limited to 'modules/caddyhttp')
| -rw-r--r-- | modules/caddyhttp/celmatcher.go | 13 | ||||
| -rw-r--r-- | modules/caddyhttp/celmatcher_test.go | 2 | 
2 files changed, 12 insertions, 3 deletions
| diff --git a/modules/caddyhttp/celmatcher.go b/modules/caddyhttp/celmatcher.go index 0306f39..ee640fd 100644 --- a/modules/caddyhttp/celmatcher.go +++ b/modules/caddyhttp/celmatcher.go @@ -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"  ) @@ -62,6 +63,8 @@ type MatchExpression struct {  	expandedExpr string  	prg          cel.Program  	ta           ref.TypeAdapter + +	log *zap.Logger  }  // CaddyModule returns the Caddy module information. @@ -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) @@ -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  	} diff --git a/modules/caddyhttp/celmatcher_test.go b/modules/caddyhttp/celmatcher_test.go index a78eb5a..d71fc42 100644 --- a/modules/caddyhttp/celmatcher_test.go +++ b/modules/caddyhttp/celmatcher_test.go @@ -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)  			}  		}) | 
