Skip to content

Commit 6952938

Browse files
fix: Add array index overflow protection (fixes #19) (#70)
Prevent integer overflow when converting CEL's 0-based array indexing to PostgreSQL's 1-based indexing. Changes: - Add overflow detection for math.MaxInt64 in visitCallListIndex() - Add validation for negative array indices - Return clear error messages for both cases - Add comprehensive test coverage This fixes CWE-190 (Integer Overflow) with severity Critical. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 8eed263 commit 6952938

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

cel2sql.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"log/slog"
9+
"math"
910
"regexp"
1011
"strconv"
1112
"strings"
@@ -1027,7 +1028,14 @@ func (con *converter) visitCallListIndex(expr *exprpb.Expr) error {
10271028
index := args[1]
10281029
// PostgreSQL arrays are 1-indexed, CEL is 0-indexed, so add 1
10291030
if constExpr := index.GetConstExpr(); constExpr != nil {
1030-
con.str.WriteString(strconv.FormatInt(constExpr.GetInt64Value()+1, 10))
1031+
idx := constExpr.GetInt64Value()
1032+
if idx == math.MaxInt64 {
1033+
return errors.New("array index overflow: cannot convert math.MaxInt64 to 1-based indexing")
1034+
}
1035+
if idx < 0 {
1036+
return fmt.Errorf("invalid negative array index: %d", idx)
1037+
}
1038+
con.str.WriteString(strconv.FormatInt(idx+1, 10))
10311039
} else {
10321040
if err := con.visit(index); err != nil {
10331041
return err

cel2sql_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ func TestConvert(t *testing.T) {
222222
want: "string_list[1] = 'a'", // PostgreSQL arrays are 1-indexed
223223
wantErr: false,
224224
},
225+
{
226+
name: "array_index_overflow",
227+
args: args{source: `string_list[9223372036854775807]`}, // math.MaxInt64
228+
want: "",
229+
wantErr: true,
230+
},
231+
{
232+
name: "array_index_negative",
233+
args: args{source: `string_list[-1]`},
234+
want: "",
235+
wantErr: true,
236+
},
225237
{
226238
name: "map",
227239
args: args{source: `{"one": 1, "two": 2, "three": 3}["one"] == 1`},

0 commit comments

Comments
 (0)