-
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
Release - Fix path traversal bug for sqlite, new clickhouse options, and support for OpenAI-like URLs #329
base: release
Are you sure you want to change the base?
Conversation
…e, readonly and debug advanced options Signed-off-by: Slach <[email protected]>
add additional options for clickhouse support
Add OpenAI Compatible Provider
…n instead of string concat
…search to build url using url.URL, further url.QueryEscapes
fix for sqlite path traversal
@@ -72,7 +72,7 @@ | |||
query += fmt.Sprintf("\nSETTINGS %s", strings.Join(settingsClauses, ", ")) | |||
} | |||
|
|||
err = conn.Exec(context.Background(), query) | |||
_, err = conn.ExecContext(context.Background(), query) |
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 7 days ago
To fix the problem, we need to ensure that user input is safely embedded into the SQL query. This can be achieved by using parameterized queries or prepared statements. In this case, we will use parameterized queries to avoid SQL injection vulnerabilities.
- Modify the
AddStorageUnit
function to use parameterized queries for theschema
andstorageUnit
parameters. - Use
sql.Named
to safely include theschema
andstorageUnit
parameters in the query.
-
Copy modified lines R53-R54 -
Copy modified line R75
@@ -52,4 +52,4 @@ | ||
// Build the CREATE TABLE query | ||
query := fmt.Sprintf("CREATE TABLE %s.%s (\n\t%s\n) ENGINE = %s", | ||
schema, storageUnit, strings.Join(columns, ",\n\t"), engineSettings.engine) | ||
query := fmt.Sprintf("CREATE TABLE ?.? (\n\t%s\n) ENGINE = %s", | ||
strings.Join(columns, ",\n\t"), engineSettings.engine) | ||
|
||
@@ -74,3 +74,3 @@ | ||
|
||
_, err = conn.ExecContext(context.Background(), query) | ||
_, err = conn.ExecContext(context.Background(), query, sql.Named("schema", schema), sql.Named("storageUnit", storageUnit)) | ||
if err != nil { |
@@ -100,6 +100,6 @@ | |||
query := fmt.Sprintf("INSERT INTO %s.%s (%s) VALUES (%s)", | |||
schema, storageUnit, strings.Join(columns, ", "), strings.Join(placeholders, ", ")) | |||
|
|||
err = conn.Exec(context.Background(), query, args...) | |||
_, err = conn.ExecContext(context.Background(), query, args...) |
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 7 days ago
To fix the problem, we need to use parameterized queries or prepared statements to safely embed user-controlled data into the SQL query. This approach prevents SQL injection by ensuring that user input is treated as data rather than executable code.
In the AddRow
function, we should modify the query construction to use placeholders for the schema
and storageUnit
parameters and pass them as arguments to the ExecContext
function. This will ensure that these parameters are safely embedded into the query.
-
Copy modified lines R100-R101 -
Copy modified line R103
@@ -99,6 +99,6 @@ | ||
|
||
query := fmt.Sprintf("INSERT INTO %s.%s (%s) VALUES (%s)", | ||
schema, storageUnit, strings.Join(columns, ", "), strings.Join(placeholders, ", ")) | ||
query := fmt.Sprintf("INSERT INTO ?.? (%s) VALUES (%s)", | ||
strings.Join(columns, ", "), strings.Join(placeholders, ", ")) | ||
|
||
_, err = conn.ExecContext(context.Background(), query, args...) | ||
_, err = conn.ExecContext(context.Background(), query, append([]interface{}{schema, storageUnit}, args...)...) | ||
return err == nil, err |
@@ -66,7 +66,7 @@ | |||
WHERE database = '%s' | |||
`, schema) | |||
|
|||
rows, err := conn.Query(context.Background(), query) | |||
rows, err := conn.QueryContext(context.Background(), query) |
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 7 days ago
To fix the problem, we need to use parameterized queries instead of string concatenation to safely include the user-provided schema
parameter in the SQL query. This can be achieved by using the QueryContext
method with placeholders for parameters.
- Modify the
GetStorageUnits
function incore/src/plugins/clickhouse/clickhouse.go
to use a parameterized query. - Replace the
fmt.Sprintf
usage with a query string that includes a placeholder for theschema
parameter. - Pass the
schema
parameter as an argument to theQueryContext
method.
-
Copy modified line R59 -
Copy modified lines R66-R67 -
Copy modified line R69
@@ -58,3 +58,3 @@ | ||
|
||
query := fmt.Sprintf(` | ||
query := ` | ||
SELECT | ||
@@ -65,6 +65,6 @@ | ||
FROM system.tables | ||
WHERE database = '%s' | ||
`, schema) | ||
WHERE database = ? | ||
` | ||
|
||
rows, err := conn.QueryContext(context.Background(), query) | ||
rows, err := conn.QueryContext(context.Background(), query, schema) | ||
if err != nil { |
@@ -112,7 +112,7 @@ | |||
ORDER BY position | |||
`, schema, tableName) | |||
|
|||
rows, err := conn.Query(context.Background(), query) | |||
rows, err := conn.QueryContext(context.Background(), query) |
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 7 days ago
To fix the problem, we need to use parameterized queries instead of string concatenation to construct the SQL query. This will ensure that user input is properly escaped and cannot be used to inject malicious SQL code.
- Replace the
fmt.Sprintf
usage with parameterized queries using?
placeholders. - Modify the
getTableSchema
function to accept theschema
andtableName
parameters as query arguments. - Update the
conn.QueryContext
call to pass the parameters separately.
-
Copy modified line R106 -
Copy modified line R111 -
Copy modified line R113 -
Copy modified line R115
@@ -105,3 +105,3 @@ | ||
func getTableSchema(conn *sql.DB, schema string, tableName string) ([]engine.Record, error) { | ||
query := fmt.Sprintf(` | ||
query := ` | ||
SELECT | ||
@@ -110,7 +110,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 { |
@@ -68,7 +73,7 @@ | |||
strings.Join(whereClauses, " AND ")) | |||
|
|||
// Execute the query | |||
err = conn.Exec(context.Background(), query, args...) | |||
_, err = conn.ExecContext(context.Background(), query, args...) |
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
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 7 days ago
To fix the problem, we need to ensure that user-provided data is safely embedded into the SQL query. This can be achieved by using parameterized queries or prepared statements. In this case, we will modify the code to use parameterized queries for the schema
and storageUnit
parameters.
- Modify the
DeleteRow
function incore/src/plugins/clickhouse/delete.go
to use parameterized queries for theschema
andstorageUnit
parameters. - Update the query construction to include placeholders for the
schema
andstorageUnit
parameters. - Pass the
schema
andstorageUnit
parameters as arguments to theExecContext
function.
-
Copy modified lines R68-R70 -
Copy modified line R73
@@ -67,10 +67,8 @@ | ||
// Construct the DELETE query | ||
query := fmt.Sprintf(` | ||
ALTER TABLE %s.%s | ||
DELETE WHERE %s`, | ||
schema, | ||
storageUnit, | ||
strings.Join(whereClauses, " AND ")) | ||
query := ` | ||
ALTER TABLE ?.? | ||
DELETE WHERE ` + strings.Join(whereClauses, " AND ") | ||
|
||
// Execute the query | ||
args = append([]interface{}{schema, storageUnit}, args...) | ||
_, err = conn.ExecContext(context.Background(), query, args...) |
@@ -28,13 +28,16 @@ | |||
} | |||
defer conn.Close() | |||
|
|||
rows, err := conn.Query(context.Background(), query, params) | |||
rows, err := conn.QueryContext(context.Background(), query, params) |
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
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 7 days ago
To fix the problem, we need to use parameterized queries instead of directly embedding user-controlled values into the SQL query string. This can be achieved by using placeholders (?
) in the query string and passing the actual values as arguments to the QueryContext
method.
- Modify the
GetRows
function to use parameterized queries. - Update the
executeQuery
function to accept additional parameters for the query placeholders. - Ensure that the
RawExecute
function also uses parameterized queries if applicable.
-
Copy modified lines R11-R12 -
Copy modified line R14 -
Copy modified line R28
@@ -10,9 +10,6 @@ | ||
func (p *ClickHousePlugin) GetRows(config *engine.PluginConfig, schema string, storageUnit string, where string, pageSize int, pageOffset int) (*engine.GetRowsResult, error) { | ||
query := fmt.Sprintf("SELECT * FROM %s.%s", schema, storageUnit) | ||
if where != "" { | ||
query += " WHERE " + where | ||
} | ||
query += fmt.Sprintf(" LIMIT %d OFFSET %d", pageSize, pageOffset) | ||
query := "SELECT * FROM ?.? WHERE ? LIMIT ? OFFSET ?" | ||
params := []interface{}{schema, storageUnit, where, pageSize, pageOffset} | ||
|
||
return p.executeQuery(config, query) | ||
return p.executeQuery(config, query, params...) | ||
} | ||
@@ -30,3 +27,3 @@ | ||
|
||
rows, err := conn.QueryContext(context.Background(), query, params) | ||
rows, err := conn.QueryContext(context.Background(), query, params...) | ||
if err != nil { |
@@ -99,7 +104,7 @@ | |||
strings.Join(setClauses, ", "), | |||
strings.Join(whereClauses, " AND ")) | |||
|
|||
err = conn.Exec(context.Background(), query, args...) | |||
_, err = conn.ExecContext(context.Background(), query, args...) |
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
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 7 days ago
To fix the problem, we need to ensure that the schema
parameter is safely embedded into the SQL query. This can be achieved by using parameterized queries or prepared statements, which prevent SQL injection by treating user input as data rather than executable code.
In this specific case, we can use the sql.Named
function to safely include the schema
parameter in the query. This approach ensures that the schema
value is properly escaped and treated as a parameter rather than part of the SQL command.
-
Copy modified lines R99-R104
@@ -98,9 +98,8 @@ | ||
|
||
query := fmt.Sprintf(` | ||
ALTER TABLE %s.%s | ||
UPDATE %s | ||
WHERE %s`, | ||
schema, storageUnit, | ||
strings.Join(setClauses, ", "), | ||
strings.Join(whereClauses, " AND ")) | ||
query := ` | ||
ALTER TABLE ?.? | ||
UPDATE ? | ||
WHERE ?` | ||
|
||
args = append([]interface{}{schema, storageUnit, strings.Join(setClauses, ", "), strings.Join(whereClauses, " AND ")}, args...) | ||
|
@@ -18,7 +18,7 @@ | |||
WHERE database = '%s' AND table = '%s'`, | |||
schema, table) | |||
|
|||
rows, err := conn.Query(context.Background(), query) | |||
rows, err := conn.QueryContext(context.Background(), query) |
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
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 11 days ago
To fix the problem, we need to use parameterized queries instead of string concatenation to construct the SQL query. This will ensure that user input is properly escaped and cannot be used to inject malicious SQL code.
- Replace the
fmt.Sprintf
call with a parameterized query using placeholders. - Use the
QueryContext
method with the appropriate arguments to pass the user input as parameters to the query.
-
Copy modified line R13 -
Copy modified line R18 -
Copy modified line R20
@@ -12,3 +12,3 @@ | ||
func getColumnTypes(conn *sql.DB, schema string, table string) (map[string]string, error) { | ||
query := fmt.Sprintf(` | ||
query := ` | ||
SELECT | ||
@@ -17,6 +17,5 @@ | ||
FROM system.columns | ||
WHERE database = '%s' AND table = '%s'`, | ||
schema, table) | ||
WHERE database = ? AND table = ?` | ||
|
||
rows, err := conn.QueryContext(context.Background(), query) | ||
rows, err := conn.QueryContext(context.Background(), query, schema, table) | ||
if err != nil { |
… out of the params
Update how databases connect to avoid parameter injection
Add Alias logic
Ensure ids are unique for columns with same name
Replace monaco editor with code mirror
This reverts commit 46a7956.
✨ Improvements
Added new clickhouse options for HTTP
Support for OpenAI-like URLs
🐛 Bug Fixes
Path traversal issue for sqlite
Parameter injection issue for MySQL
Thank you to everyone who contributed to this release! 🚀
Your feedback and support are invaluable.