Skip to content
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ require (
github.com/jinzhu/now v1.1.5 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/klauspost/compress v1.19.1 // indirect
github.com/klauspost/compress v1.20.2 // indirect
github.com/ktr0731/go-ansisgr v0.1.0 // indirect
github.com/ktr0731/go-fuzzyfinder v0.9.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ github.com/keybase/go-keychain v0.0.1/go.mod h1:PdEILRW3i9D8JcdM+FmY6RwkHGnhHxXw
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk=
github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/klauspost/compress v1.20.2 h1:v5tiZvm1KGLyg5d5oXdXe3lsSPrbEUiKoXGia//I7Qo=
github.com/klauspost/compress v1.20.2/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
19 changes: 13 additions & 6 deletions pkg/api/server/cel2sql/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,28 @@ import (
"github.com/google/cel-go/cel"
)

// Convert takes CEL expressions and attempt to convert them into Postgres SQL
// filters.
func Convert(env *cel.Env, filters string) (string, error) {
// Convert takes CEL expressions and attempts to convert them into a
// parameterized Postgres SQL WHERE fragment. It returns the SQL string
// (containing GORM-style ? placeholders), a slice of bind-parameter values,
// and any error. Callers must pass both to GORM:
//
// db.Where(sql, args...)
//
// This ensures that user-controlled string values are never interpolated
// directly into the SQL string, preventing SQL injection.
func Convert(env *cel.Env, filters string) (string, []any, error) {
ast, issues := env.Compile(filters)
if issues != nil && issues.Err() != nil {
return "", fmt.Errorf("error compiling CEL filters: %w", issues.Err())
return "", nil, fmt.Errorf("error compiling CEL filters: %w", issues.Err())
}

if outputType := ast.OutputType(); !outputType.IsAssignableType(cel.BoolType) {
return "", fmt.Errorf("expected boolean expression, but got %s", outputType.String())
return "", nil, fmt.Errorf("expected boolean expression, but got %s", outputType.String())
}

interpreter, err := newInterpreter(ast)
if err != nil {
return "", fmt.Errorf("error creating cel2sql interpreter: %w", err)
return "", nil, fmt.Errorf("error creating cel2sql interpreter: %w", err)
}

return interpreter.interpret()
Expand Down
Loading
Loading