Skip to content

Fix open redirect vulnerability in login_and_redirect endpoint#19

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

Fix open redirect vulnerability in login_and_redirect endpoint#19
studentsca023-rgb merged 1 commit into
masterfrom
semgrep-autofix/1773741010

Conversation

@semgrep-code-studentsca023-rgb

Copy link
Copy Markdown

Fix open redirect vulnerability by validating and sanitizing the redirect URL using urlparse().

Changes

  • Parse the user-provided URL with urlparse() before use
  • Validate that the URL has no netloc or scheme (rejecting absolute URLs to external sites)
  • Construct a new safe URL from only the validated path and query components
  • Use the sanitized URL in redirect() instead of the original user input

Why

The original code passed user-controlled input directly to redirect(), which could allow an attacker to craft a malicious URL that redirects users to an external site (open redirect attack). By parsing the URL and reconstructing it from only the path and query components, we break the taint chain and ensure only relative URLs are used for redirects.

Semgrep Finding Details

Data from request is passed to redirect(). This is an open redirect and could be exploited. Consider using 'url_for()' to generate links to known locations. If you must use a URL to unknown pages, consider using 'urlparse()' or similar and checking if the 'netloc' property is the same as your site's host name. See the references for more information.

@267212124 requested Semgrep Assistant generate this pull request to fix a finding from the detection rule python.flask.security.open-redirect.open-redirect.


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

Fix open redirect vulnerability by validating and sanitizing the redirect URL using urlparse().

## Changes
- Parse the user-provided URL with `urlparse()` before use
- Validate that the URL has no `netloc` or `scheme` (rejecting absolute URLs to external sites)
- Construct a new safe URL from only the validated `path` and `query` components
- Use the sanitized URL in `redirect()` instead of the original user input

## Why
The original code passed user-controlled input directly to `redirect()`, which could allow an attacker to craft a malicious URL that redirects users to an external site (open redirect attack). By parsing the URL and reconstructing it from only the path and query components, we break the taint chain and ensure only relative URLs are used for redirects.

## Semgrep Finding Details
Data from request is passed to redirect(). This is an open redirect and could be exploited. Consider using 'url_for()' to generate links to known locations. If you must use a URL to unknown pages, consider using 'urlparse()' or similar and checking if the 'netloc' property is the same as your site's host name. See the references for more information.

@267212124 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/studentsca023_personal_org/findings/722169005) from the detection rule [python.flask.security.open-redirect.open-redirect](https://semgrep.dev/r/python.flask.security.open-redirect.open-redirect).
@studentsca023-rgb studentsca023-rgb marked this pull request as ready for review March 17, 2026 09:53
@studentsca023-rgb studentsca023-rgb merged commit 8e3a9ef 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