Fix open redirect vulnerability in login_and_redirect endpoint#19
Merged
Conversation
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).
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 open redirect vulnerability by validating and sanitizing the redirect URL using urlparse().
Changes
urlparse()before usenetlocorscheme(rejecting absolute URLs to external sites)pathandquerycomponentsredirect()instead of the original user inputWhy
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.