Skip to content

Commit f62bc77

Browse files
Plain comparison only works with primitives (#15)
Comparing with an array will result in an error. I tried to see if I could implement comparison with an array, but that doesn't seem possible as you need to know the array type in postgres, which we don't.
1 parent b4f78e2 commit f62bc77

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

filter/converter.go

+3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
191191
}
192192
conditions = append(conditions, innerResult)
193193
default:
194+
if !isScalar(value) {
195+
return "", nil, fmt.Errorf("invalid comparison value (must be a primitive): %v", value)
196+
}
194197
conditions = append(conditions, fmt.Sprintf("(%s = $%d)", c.columnName(key), paramIndex))
195198
paramIndex++
196199
values = append(values, value)

filter/converter_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,22 @@ func TestConverter_Convert(t *testing.T) {
248248
nil,
249249
fmt.Errorf("invalid value for $not operator (must be object): John"),
250250
},
251+
{
252+
"sql injection",
253+
nil,
254+
`{"\"bla = 1 --": 1}`,
255+
``,
256+
nil,
257+
fmt.Errorf("invalid column name: \"bla = 1 --"),
258+
},
259+
{
260+
"compare with array",
261+
nil,
262+
`{"items": [200, 300]}`,
263+
``,
264+
nil,
265+
fmt.Errorf("invalid comparison value (must be a primitive): [200 300]"),
266+
},
251267
}
252268

253269
for _, tt := range tests {

integration/postgres_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ func TestIntegration_BasicOperators(t *testing.T) {
321321
[]int{3, 4, 5, 6, 7, 8, 9, 10},
322322
nil,
323323
},
324+
{
325+
"$gt with jsonb column",
326+
`{"guild_id": { "$gt": 40 }}`,
327+
[]int{7, 8, 9, 10},
328+
nil,
329+
},
324330
}
325331

326332
for _, tt := range tests {

0 commit comments

Comments
 (0)