@marcel91200 / caddy Mirror · read-only / commits / 1498132e
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 16:45 UTC

caddyhttp: Log error from CEL evaluation (fix #4832)

Matthew Holt committed Jun 8, 2022 at 16:42 UTC 1498132ea3c4f01d3be41812dbd02364cb77d263
2 files changed +12 -3
modules/caddyhttp/celmatcher.go
+11 -2
@@ -33,6 +33,7 @@ import (
33 "github.com/google/cel-go/common/types/traits"
34 "github.com/google/cel-go/ext"
35 "github.com/google/cel-go/interpreter/functions"
36 + "go.uber.org/zap"
37 exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
38 "google.golang.org/protobuf/proto"
39 )
@@ -62,6 +63,8 @@ type MatchExpression struct {
63 expandedExpr string
64 prg cel.Program
65 ta ref.TypeAdapter
66 +
67 + log *zap.Logger
68 }
69
70 // CaddyModule returns the Caddy module information.
@@ -83,7 +86,9 @@ func (m *MatchExpression) UnmarshalJSON(data []byte) error {
86 }
87
88 // Provision sets ups m.
86 -func (m *MatchExpression) Provision(_ caddy.Context) error {
89 +func (m *MatchExpression) Provision(ctx caddy.Context) error {
90 + m.log = ctx.Logger(m)
91 +
92 // replace placeholders with a function call - this is just some
93 // light (and possibly naïve) syntactic sugar
94 m.expandedExpr = placeholderRegexp.ReplaceAllString(m.Expr, placeholderExpansion)
@@ -137,9 +142,13 @@ func (m *MatchExpression) Provision(_ caddy.Context) error {
142
143 // Match returns true if r matches m.
144 func (m MatchExpression) Match(r *http.Request) bool {
140 - out, _, _ := m.prg.Eval(map[string]interface{}{
145 + out, _, err := m.prg.Eval(map[string]interface{}{
146 "request": celHTTPRequest{r},
147 })
148 + if err != nil {
149 + m.log.Error("evaluating expression", zap.Error(err))
150 + return false
151 + }
152 if outBool, ok := out.Value().(bool); ok {
153 return outBool
154 }
modules/caddyhttp/celmatcher_test.go
+1 -1
@@ -116,7 +116,7 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
116 }
117
118 if tt.expression.Match(req) != tt.wantResult {
119 - t.Errorf("MatchExpression.Match() expected to return '%t', for expression : '%s'", tt.wantResult, tt.expression)
119 + t.Errorf("MatchExpression.Match() expected to return '%t', for expression : '%s'", tt.wantResult, tt.expression.Expr)
120 }
121
122 })