Fix SQL injection vulnerability in auth.py using parameterized queries#10
Merged
Merged
Conversation
Fix SQL injection vulnerability in the login endpoint by replacing string formatting with parameterized queries. Also fix an open redirect vulnerability in the same file. ## Changes - Replace string formatting (`%`) with parameterized query using `?` placeholders in the `/login` endpoint - Add `is_safe_url()` helper function to validate redirect URLs - Add URL validation before redirect in `/login_and_redirect` endpoint to prevent open redirect attacks ## Why The original code used Python string formatting to construct SQL queries with user-supplied input, which allows attackers to inject malicious SQL and potentially steal or modify database contents. Parameterized queries ensure user input is properly escaped and treated as data, not executable SQL. The open redirect vulnerability was also fixed because it allowed attackers to redirect users to malicious external sites after a failed login attempt. ## Semgrep Finding Details Detected user input used to manually construct a SQL string. This is usually bad practice because manual construction could accidentally result in a SQL injection. An attacker could use a SQL injection to steal or modify contents of the database. Instead, use a parameterized query which is available by default in most database engines. Alternatively, consider using the Django object-relational mappers (ORM) instead of raw SQL queries. @267212124 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/studentsca023_personal_org/findings/722169014) from the detection rule [python.django.security.injection.tainted-sql-string.tainted-sql-string](https://semgrep.dev/r/python.django.security.injection.tainted-sql-string.tainted-sql-string).
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 in the login endpoint by replacing string formatting with parameterized queries. Also fix an open redirect vulnerability in the same file.
Changes
%) with parameterized query using?placeholders in the/loginendpointis_safe_url()helper function to validate redirect URLs/login_and_redirectendpoint to prevent open redirect attacksWhy
The original code used Python string formatting to construct SQL queries with user-supplied input, which allows attackers to inject malicious SQL and potentially steal or modify database contents. Parameterized queries ensure user input is properly escaped and treated as data, not executable SQL.
The open redirect vulnerability was also fixed because it allowed attackers to redirect users to malicious external sites after a failed login attempt.
Semgrep Finding Details
Detected user input used to manually construct a SQL string. This is usually bad practice because manual construction could accidentally result in a SQL injection. An attacker could use a SQL injection to steal or modify contents of the database. Instead, use a parameterized query which is available by default in most database engines. Alternatively, consider using the Django object-relational mappers (ORM) instead of raw SQL queries.
@267212124 requested Semgrep Assistant generate this pull request to fix a finding from the detection rule python.django.security.injection.tainted-sql-string.tainted-sql-string.