Skip to content

Plain comparison only works with primitives #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions filter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
}
conditions = append(conditions, innerResult)
default:
if !isScalar(value) {
return "", nil, fmt.Errorf("invalid comparison value (must be a primitive): %v", value)
}
conditions = append(conditions, fmt.Sprintf("(%s = $%d)", c.columnName(key), paramIndex))
paramIndex++
values = append(values, value)
Expand Down
16 changes: 16 additions & 0 deletions filter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,22 @@ func TestConverter_Convert(t *testing.T) {
nil,
fmt.Errorf("invalid value for $not operator (must be object): John"),
},
{
"sql injection",
nil,
`{"\"bla = 1 --": 1}`,
``,
nil,
fmt.Errorf("invalid column name: \"bla = 1 --"),
},
{
"compare with array",
nil,
`{"items": [200, 300]}`,
``,
nil,
fmt.Errorf("invalid comparison value (must be a primitive): [200 300]"),
},
}

for _, tt := range tests {
Expand Down
6 changes: 6 additions & 0 deletions integration/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ func TestIntegration_BasicOperators(t *testing.T) {
[]int{3, 4, 5, 6, 7, 8, 9, 10},
nil,
},
{
"$gt with jsonb column",
`{"guild_id": { "$gt": 40 }}`,
[]int{7, 8, 9, 10},
nil,
},
}

for _, tt := range tests {
Expand Down
Loading