Skip to content
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

Fix filtering column comparisons #599

Merged
merged 1 commit into from Nov 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions encryptor/searchable_query_filter.go
Expand Up @@ -152,7 +152,7 @@ func (filter *SearchableQueryFilter) filterComparisonExprs(statement sqlparser.S
// ...and find all eligible comparison expressions in them.
var exprs []*sqlparser.ComparisonExpr
for _, whereExpr := range whereExprs {
comparisonExprs, err := filter.getEqualComparisonExprs(whereExpr, defaultTable, aliasedTables)
comparisonExprs, err := filter.getColumnEqualComparisonExprs(whereExpr, defaultTable, aliasedTables)
if err != nil {
logrus.WithError(err).Debugln("Failed to extract comparison expressions")
return nil
Expand Down Expand Up @@ -234,12 +234,15 @@ func isSupportedSQLVal(val *sqlparser.SQLVal) bool {
return false
}

// getEqualComparisonExprs return only <ColName> = <VALUE> or <ColName> != <VALUE> or <ColName> <=> <VALUE> expressions
func (filter *SearchableQueryFilter) getEqualComparisonExprs(stmt sqlparser.SQLNode, defaultTable *AliasedTableName, aliasedTables AliasToTableMap) ([]*sqlparser.ComparisonExpr, error) {
// getColumnEqualComparisonExprs return only <ColName> = <VALUE> or <ColName> != <VALUE> or <ColName> <=> <VALUE> expressions
func (filter *SearchableQueryFilter) getColumnEqualComparisonExprs(stmt sqlparser.SQLNode, defaultTable *AliasedTableName, aliasedTables AliasToTableMap) ([]*sqlparser.ComparisonExpr, error) {
var exprs []*sqlparser.ComparisonExpr
err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
if comparisonExpr, ok := node.(*sqlparser.ComparisonExpr); ok {
lColumn := comparisonExpr.Left.(*sqlparser.ColName)
lColumn, ok := comparisonExpr.Left.(*sqlparser.ColName)
if !ok {
return true, nil
}

lColumnSetting := filter.getColumnSetting(lColumn, defaultTable, aliasedTables)
if lColumnSetting == nil {
Expand Down
32 changes: 32 additions & 0 deletions encryptor/searchable_query_filter_test.go
Expand Up @@ -71,3 +71,35 @@ schemas:
t.Fatalf("Expect not nil aliasedTable, matched with config")
}
}

func Test_getColumnEqualComparisonExprs_NotColumnComparisonQueries(t *testing.T) {
searchableQueryFilter := SearchableQueryFilter{}

queries := []string{
`select * from mytable where substring(name, 1, 33) = '\x7F08FFD5012B0A7659EABE5758009178A2713749B1200C0BFD505B02D4FA26B08F';`,
`select * from mytable where encode('é', 'hex') = 'c3a9'`,
}

parser := sqlparser.New(sqlparser.ModeStrict)

for _, query := range queries {
statement, err := parser.Parse(query)
if err != nil {
t.Fatal(err)
}

whereStatements, err := getWhereStatements(statement)
if err != nil {
t.Fatalf("expected no error on parsing valid WHERE clause query - %s", err.Error())
}

compExprs, err := searchableQueryFilter.getColumnEqualComparisonExprs(whereStatements[0], nil, nil)
if err != nil {
t.Fatal(err)
}

if compExprs != nil {
t.Fatalf("expected nil compExprs")
}
}
}