-
Notifications
You must be signed in to change notification settings - Fork 82
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
Add correct pagination for filters #335
base: main
Are you sure you want to change the base?
Conversation
WHERE database = '%s' | ||
ORDER BY table, position | ||
`, schema) | ||
|
Check failure
Code scanning / CodeQL
Database query built from user-controlled sources High
user-provided value
This query depends on a
user-provided value
This query depends on a user-provided value.
This query depends on a user-provided value.
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we should use parameterized queries or prepared statements to safely embed user input into SQL queries. This approach prevents SQL injection by ensuring that user input is treated as data rather than executable code.
In the provided code, we need to replace the use of fmt.Sprintf
with parameterized queries using the QueryContext
method. This involves modifying the getAllTableSchema
and getTableSchema
functions to use query parameters instead of directly embedding the schema
and tableName
values into the query string.
-
Copy modified line R106 -
Copy modified line R112 -
Copy modified line R114 -
Copy modified line R116 -
Copy modified line R135 -
Copy modified line R140 -
Copy modified line R142 -
Copy modified line R144
@@ -105,3 +105,3 @@ | ||
func getAllTableSchema(conn *sql.DB, schema string) (map[string][]engine.Record, error) { | ||
query := fmt.Sprintf(` | ||
query := ` | ||
SELECT | ||
@@ -111,7 +111,7 @@ | ||
FROM system.columns | ||
WHERE database = '%s' | ||
WHERE database = ? | ||
ORDER BY table, position | ||
`, schema) | ||
` | ||
|
||
rows, err := conn.QueryContext(context.Background(), query) | ||
rows, err := conn.QueryContext(context.Background(), query, schema) | ||
if err != nil { | ||
@@ -134,3 +134,3 @@ | ||
func getTableSchema(conn *sql.DB, schema string, tableName string) ([]engine.Record, error) { | ||
query := fmt.Sprintf(` | ||
query := ` | ||
SELECT | ||
@@ -139,7 +139,7 @@ | ||
FROM system.columns | ||
WHERE database = '%s' AND table = '%s' | ||
WHERE database = ? AND table = ? | ||
ORDER BY position | ||
`, schema, tableName) | ||
` | ||
|
||
rows, err := conn.QueryContext(context.Background(), query) | ||
rows, err := conn.QueryContext(context.Background(), query, schema, tableName) | ||
if err != nil { |
|
||
countQuery := fmt.Sprintf("SELECT COUNT(*) %v", baseQuery) | ||
var totalCount int | ||
err = db.Raw(countQuery).Scan(&totalCount).Error |
Check failure
Code scanning / CodeQL
Database query built from user-controlled sources High
user-provided value
This query depends on a
user-provided value
This query depends on a
user-provided value
This query depends on a
user-provided value
This query depends on a
user-provided value
This query depends on a
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to replace the unsafe construction of SQL queries using fmt.Sprintf
with parameterized queries. This involves using placeholders in the SQL query and passing the user-provided values as separate arguments to the query execution function. This approach ensures that the user-provided values are properly escaped and prevents SQL injection attacks.
In the core/src/plugins/mysql/mysql.go
file, we need to:
- Replace the
fmt.Sprintf
calls with parameterized queries. - Pass the user-provided values as arguments to the
db.Raw
function.
-
Copy modified line R120 -
Copy modified line R123 -
Copy modified line R125 -
Copy modified line R127 -
Copy modified lines R145-R146 -
Copy modified line R148 -
Copy modified line R151 -
Copy modified line R153 -
Copy modified lines R158-R159 -
Copy modified line R161
@@ -119,10 +119,10 @@ | ||
|
||
query := fmt.Sprintf(` | ||
query := ` | ||
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE | ||
FROM INFORMATION_SCHEMA.COLUMNS | ||
WHERE TABLE_SCHEMA = '%v' | ||
WHERE TABLE_SCHEMA = ? | ||
ORDER BY TABLE_NAME, ORDINAL_POSITION | ||
`, schema) | ||
` | ||
|
||
if err := db.Raw(query).Scan(&result).Error; err != nil { | ||
if err := db.Raw(query, schema).Scan(&result).Error; err != nil { | ||
return nil, err | ||
@@ -144,10 +144,11 @@ | ||
|
||
baseQuery := fmt.Sprintf("FROM `%v`.`%s`", schema, storageUnit) | ||
baseQuery := "FROM `?`.`?`" | ||
args := []interface{}{schema, storageUnit} | ||
if len(where) > 0 { | ||
baseQuery = fmt.Sprintf("%v WHERE %v", baseQuery, where) | ||
baseQuery += " WHERE " + where | ||
} | ||
|
||
countQuery := fmt.Sprintf("SELECT COUNT(*) %v", baseQuery) | ||
countQuery := "SELECT COUNT(*) " + baseQuery | ||
var totalCount int | ||
err = db.Raw(countQuery).Scan(&totalCount).Error | ||
err = db.Raw(countQuery, args...).Scan(&totalCount).Error | ||
if err != nil { | ||
@@ -156,5 +157,6 @@ | ||
|
||
query := fmt.Sprintf("SELECT * %v LIMIT ? OFFSET ?", baseQuery) | ||
query := "SELECT * " + baseQuery + " LIMIT ? OFFSET ?" | ||
args = append(args, pageSize, pageOffset) | ||
|
||
result, err := p.executeRawSQL(config, query, pageSize, pageOffset) | ||
result, err := p.executeRawSQL(config, query, args...) | ||
if err != nil { |
|
||
countQuery := fmt.Sprintf("SELECT COUNT(*) %v", baseQuery) | ||
var totalCount int | ||
err = db.Raw(countQuery).Scan(&totalCount).Error |
Check failure
Code scanning / CodeQL
Database query built from user-controlled sources High
user-provided value
This query depends on a
user-provided value
This query depends on a
user-provided value
This query depends on a
user-provided value
This query depends on a
user-provided value
This query depends on a
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to use SQL parameterization instead of string concatenation to construct the SQL queries. This can be achieved by using placeholders (?
) in the SQL query and passing the user-provided values as parameters to the db.Raw
method. This approach ensures that the user-provided values are safely embedded into the query, preventing SQL injection attacks.
- Modify the
GetRows
method incore/src/plugins/postgres/postgres.go
to use SQL parameterization. - Replace the
fmt.Sprintf
calls with parameterized queries. - Pass the user-provided values as parameters to the
db.Raw
method.
-
Copy modified line R171 -
Copy modified line R173 -
Copy modified line R176 -
Copy modified line R178 -
Copy modified line R183 -
Copy modified line R187 -
Copy modified line R190 -
Copy modified line R192
@@ -170,10 +170,10 @@ | ||
|
||
baseQuery := fmt.Sprintf("FROM \"%v\".\"%s\"", schema, storageUnit) | ||
baseQuery := "FROM ?.? " | ||
if len(where) > 0 { | ||
baseQuery = fmt.Sprintf("%v WHERE %v", baseQuery, where) | ||
baseQuery += "WHERE " + where + " " | ||
} | ||
|
||
countQuery := fmt.Sprintf("SELECT COUNT(*) %v", baseQuery) | ||
countQuery := "SELECT COUNT(*) " + baseQuery | ||
var totalCount int | ||
err = db.Raw(countQuery).Scan(&totalCount).Error | ||
err = db.Raw(countQuery, schema, storageUnit).Scan(&totalCount).Error | ||
if err != nil { | ||
@@ -182,3 +182,3 @@ | ||
|
||
query := fmt.Sprintf("SELECT * %v", baseQuery) | ||
query := "SELECT * " + baseQuery | ||
sortKeyRes, err := getPrimaryKeyColumns(db, schema, storageUnit) | ||
@@ -186,8 +186,8 @@ | ||
quotedKeys := common.JoinWithQuotes(sortKeyRes) | ||
query = fmt.Sprintf("%v ORDER BY %v ASC", query, quotedKeys) | ||
query += "ORDER BY " + quotedKeys + " ASC " | ||
} | ||
|
||
query = fmt.Sprintf("%v LIMIT ? OFFSET ?", query) | ||
query += "LIMIT ? OFFSET ?" | ||
|
||
result, err := p.executeRawSQL(config, query, pageSize, pageOffset) | ||
result, err := p.executeRawSQL(config, query, schema, storageUnit, pageSize, pageOffset) | ||
if err != nil { |
Fix: #281
Add total count for filter query to ensure that the pagination is correct.
Demo (shows filters has 3 pages with 15 total count on the filter):