Skip to content

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
semgrep-autofix/1773963101
Draft

fix: Use parameterized query to prevent SQL injection in tainted-sql-string.py#3773
semgrep-code-dev-returntocorp[bot] wants to merge 1 commit intodevelopfrom
semgrep-autofix/1773963101

Conversation

@semgrep-code-dev-returntocorp
Copy link
Copy Markdown

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 from the detection rule python.flask.db.generic-sql-flask.generic-sql-flask.


⚠️ Review carefully before merging. This PR was generated by AI and may cause breaking changes or introduce new vulnerabilities.

…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants