Skip to content

Commit 5c06a0a

Browse files
committed
fix: handle nil body in CEL eval
CEL evaluation marshaled the event body then unmarshaled into a map. When body was nil (or marshaled to JSON null), json.Unmarshal failed with "cannot unmarshal null into map[string]interface{}". This caused cel: placeholders to be replaced with an empty string, breaking pac-only expressions in contexts without a webhook payload. Treat nil/null body as an empty object for CEL evaluation and add a unit test to cover pac-only expressions with a nil body. Signed-off-by: Chmouel Boudjnah <[email protected]> Assisted-by: GPT-5.2 (via Cursor)
1 parent 7ea8473 commit 5c06a0a

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

pkg/cel/cel.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cel
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67

@@ -38,14 +39,29 @@ func evaluate(expr string, env *cel.Env, data map[string]any) (ref.Val, error) {
3839
// / pacParams, it will output a Cel value or an error if selectedjm.
3940
func Value(query string, body any, headers, pacParams map[string]string, changedFiles map[string]any) (ref.Val, error) {
4041
// Marshal/Unmarshal the body to a map[string]any so we can access it from the CEL
41-
nbody, err := json.Marshal(body)
42-
if err != nil {
43-
return nil, err
44-
}
4542
var jsonMap map[string]any
46-
err = json.Unmarshal(nbody, &jsonMap)
47-
if err != nil {
48-
return nil, err
43+
switch b := body.(type) {
44+
case nil:
45+
jsonMap = map[string]any{}
46+
case map[string]any:
47+
jsonMap = b
48+
default:
49+
nbody, err := json.Marshal(body)
50+
if err != nil {
51+
return nil, err
52+
}
53+
trimmed := bytes.TrimSpace(nbody)
54+
if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("null")) {
55+
jsonMap = map[string]any{}
56+
} else {
57+
err = json.Unmarshal(nbody, &jsonMap)
58+
if err != nil {
59+
return nil, err
60+
}
61+
if jsonMap == nil {
62+
jsonMap = map[string]any{}
63+
}
64+
}
4965
}
5066

5167
mapStrDyn := types.NewMapType(types.StringType, types.DynType)

pkg/cel/cel_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func TestValue(t *testing.T) {
3939
assert.NilError(t, err)
4040
assert.Equal(t, ref.Val(val), val)
4141

42+
// Test pac-only query with nil body
43+
val, err = Value("pac.param", nil, headers, pacParams, changedFiles)
44+
assert.NilError(t, err)
45+
assert.Equal(t, val.Value(), "value")
46+
4247
// Test an invalid query
4348
_, err = Value("invalid query", body, headers, pacParams, changedFiles)
4449
assert.ErrorContains(t, err, "failed to parse expression")

0 commit comments

Comments
 (0)