fix: Use parameterized query to prevent SQL injection in tainted-sql-string.py#3773
Draft
semgrep-code-dev-returntocorp[bot] wants to merge 1 commit intodevelopfrom
Draft
fix: Use parameterized query to prevent SQL injection in tainted-sql-string.py#3773semgrep-code-dev-returntocorp[bot] wants to merge 1 commit intodevelopfrom
semgrep-code-dev-returntocorp[bot] wants to merge 1 commit intodevelopfrom
Conversation
…string.py
Fix SQL injection vulnerability by replacing string concatenation with parameterized query in the `insert_person_path` function.
## Changes
- Replaced vulnerable string concatenation `"INSERT INTO person (name) VALUES ('" + name + "')"` with a parameterized query using `text()` and named parameter binding
- Updated the test annotation from `ruleid:` to `ok:` since the code is now safe
## Why
The original code concatenated user input (`name` from the URL path) directly into the SQL query string, making it vulnerable to SQL injection attacks. By using parameterized queries with `text()` and passing the value via `engine.execute(stmt, name=name)`, the database driver safely escapes the input, preventing malicious SQL from being executed.
## Semgrep Finding Details
Untrusted input might be used to build a database query, which can lead to a SQL injection vulnerability. An attacker can execute malicious SQL statements and gain unauthorized access to sensitive data, modify, delete data, or execute arbitrary system commands. The driver API has the ability to bind parameters to the query in a safe way. Make sure not to dynamically create SQL queries from user-influenced inputs. If you cannot avoid this, either escape the data properly or create an allowlist to check the value.
@adam@semgrep.com requested Semgrep Assistant generate this pull request to fix [a finding](https://dev2.semgrep.dev/orgs/returntocorp/findings/499659) from the detection rule [python.flask.db.generic-sql-flask.generic-sql-flask](https://semgrep.dev/r/python.flask.db.generic-sql-flask.generic-sql-flask).
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.
Fix SQL injection vulnerability by replacing string concatenation with parameterized query in the
insert_person_pathfunction.Changes
"INSERT INTO person (name) VALUES ('" + name + "')"with a parameterized query usingtext()and named parameter bindingruleid:took:since the code is now safeWhy
The original code concatenated user input (
namefrom the URL path) directly into the SQL query string, making it vulnerable to SQL injection attacks. By using parameterized queries withtext()and passing the value viaengine.execute(stmt, name=name), the database driver safely escapes the input, preventing malicious SQL from being executed.Semgrep Finding Details
Untrusted input might be used to build a database query, which can lead to a SQL injection vulnerability. An attacker can execute malicious SQL statements and gain unauthorized access to sensitive data, modify, delete data, or execute arbitrary system commands. The driver API has the ability to bind parameters to the query in a safe way. Make sure not to dynamically create SQL queries from user-influenced inputs. If you cannot avoid this, either escape the data properly or create an allowlist to check the value.
@adam@semgrep.com requested Semgrep Assistant generate this pull request to fix a finding from the detection rule python.flask.db.generic-sql-flask.generic-sql-flask.