Open
Conversation
| err := GetQueue().EnqueueOperation(func() error { | ||
| var err error | ||
| rows, err = db.Query(query, args...) | ||
| stmt, err := db.Prepare(query) |
Check failure
Code scanning / CodeQL
Database query built from user-controlled sources
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the problem, we need to ensure that user-provided data is safely embedded into SQL queries using placeholder parameters or prepared statements. Specifically, we should avoid directly concatenating user-provided keys into the query string. Instead, we can use a switch-case or if-else structure to handle different keys and construct the query string safely.
- Modify the
QueryCoursefunction indatabase/course.goto use a switch-case structure to handle different keys and construct the query string safely. - Ensure that the
QueuedQueryfunction indatabase/queue.gouses the constructed query string with placeholder parameters.
Suggested changeset
1
database/course.go
Outside changed files
| @@ -226,2 +226,4 @@ | ||
| var err error | ||
| var query string | ||
| var args []interface{} | ||
|
|
||
| @@ -229,9 +231,13 @@ | ||
| case "title": | ||
| rows, err = QueuedQuery("SELECT term_crn FROM courses WHERE title LIKE ?", "%"+values[0]+"%") | ||
| query = "SELECT term_crn FROM courses WHERE title LIKE ?" | ||
| args = append(args, "%"+values[0]+"%") | ||
| case "subject-number": | ||
| rows, err = QueuedQuery("SELECT term_crn FROM courses WHERE subject_code = ? AND course_number LIKE ?", values[0], "%"+values[1]+"%") | ||
| query = "SELECT term_crn FROM courses WHERE subject_code = ? AND course_number LIKE ?" | ||
| args = append(args, values[0], "%"+values[1]+"%") | ||
| default: | ||
| rows, err = QueuedQuery("SELECT term_crn FROM courses WHERE "+key+" = ?", values[0]) | ||
| query = "SELECT term_crn FROM courses WHERE " + key + " = ?" | ||
| args = append(args, values[0]) | ||
| } | ||
|
|
||
| rows, err = QueuedQuery(query, args...) | ||
| if err != nil { |
Copilot is powered by AI and may make mistakes. Always verify output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.