-
Notifications
You must be signed in to change notification settings - Fork 72
fix: database query built from user-controlled sources #290
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @odaysec, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical security vulnerability by implementing stringent input validation for database table names that are constructed from user-controlled input. By introducing a regular expression-based validation function, the changes ensure that only safely formatted table names are used in SQL queries, thereby preventing SQL injection attacks and significantly enhancing the application's security posture. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Welcome @odaysec! It looks like this is your first PR to karmada-io/dashboard 🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request aims to fix a SQL injection vulnerability by validating user-provided table names. The introduction of isValidTableName and its use in queryMetricNames is a good step in the right direction. However, the implementation has a couple of issues that need addressing.
First, the regular expression in isValidTableName is compiled on every function call, which is inefficient. I've left a specific comment with a suggestion to compile it once at the package level for better performance.
More critically, the fix is incomplete. The function queryMetricDetailsByName in the same file also constructs SQL queries from the user-provided sanitizedPodName but does not use the new validation function. This leaves a significant SQL injection vulnerability open. Furthermore, the use of ? as placeholders for table names in queryMetricDetailsByName is incorrect and will likely cause runtime errors. Table names cannot be parameterized this way and must be safely interpolated into the query string after thorough validation.
To fully address the vulnerability, it is crucial to apply isValidTableName validation within queryMetricDetailsByName and correct the SQL query construction there.
| func isValidTableName(name string) bool { | ||
| // Only allow table names that start with a letter or underscore, then letters, numbers, underscores, up to 64 chars | ||
| // Adjust length as per your table name limits (e.g., SQLite, MySQL usually 64) | ||
| validTableName := regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]{0,63}$`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
regexp.MustCompile is a relatively expensive operation. Compiling the regular expression on every call to isValidTableName is inefficient. It's a best practice in Go to compile regular expressions once at the package level (e.g., in a var block at the top of the file) and reuse the compiled object. This will improve performance significantly if the function is called frequently.
For example:
var (
validTableName = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]{0,63}$`)
)
// ... in isValidTableName ...
return validTableName.MatchString(name)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense. @odaysec what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
Signed-off-by: Jörmungandrk <[email protected]>
| func isValidTableName(name string) bool { | ||
| // Only allow table names that start with a letter or underscore, then letters, numbers, underscores, up to 64 chars | ||
| // Adjust length as per your table name limits (e.g., SQLite, MySQL usually 64) | ||
| validTableName := regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]{0,63}$`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense. @odaysec what do you think?
| if !isValidTableName(sanitizedPodName) { | ||
| log.Printf("Invalid table name: %v", sanitizedPodName) | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid table name"}) | ||
| return | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, this is a typical SQL injection issue, like an attacker could exploit this by providing a malicious table name (e.g., podName; DROP TABLE users; --), which could result in data deletion...
|
@odaysec Thank you for doing this. Just out of curiosity, how do you find this? Are you evaluating the security of this project or something? |
|
cc @warjiang |
|
@odaysec thanks for your kindly remind for the potential security problems, the changes in the PR would make the |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: poratoes The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/kind bug
/kind feature
The safest approach is to only allow table names that match a trusted, known set of pod names, or at minimum validate that the input conforms precisely to the naming rules of expected tables (e.g., by a regular expression matching only valid table names consisting of alphanumeric characters and underscores, starting with a letter, and of reasonable length). This should be done before inserting the value as an identifier into the SQL string.
To implement this in the current file (
cmd/metrics-scraper/app/routes/metrics/handlerqueries.go), we need to:isValidTableName) to check thatsanitizedPodNamematches a safe pattern (like^[a-zA-Z_][a-zA-Z0-9_]{0,63}$).regexppackage for pattern matching.queryMetricNamesbefore using the table name.