Skip to content

Fix SQL injection vulnerability in auth.py using parameterized queries#10

Merged
studentsca023-rgb merged 1 commit into
masterfrom
semgrep-autofix/1773733945
Mar 17, 2026
Merged

Fix SQL injection vulnerability in auth.py using parameterized queries#10
studentsca023-rgb merged 1 commit into
masterfrom
semgrep-autofix/1773733945

Conversation

@semgrep-code-studentsca023-rgb

Copy link
Copy Markdown

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 from the detection rule python.django.security.injection.tainted-sql-string.tainted-sql-string.


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

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).
@studentsca023-rgb studentsca023-rgb marked this pull request as ready for review March 17, 2026 07:55
@studentsca023-rgb studentsca023-rgb merged commit a0cb306 into master Mar 17, 2026
2 of 3 checks passed
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.

1 participant