Skip to content

Fix open redirect vulnerability in login_and_redirect endpoint#17

Draft
semgrep-code-studentsca023-rgb[bot] wants to merge 1 commit into
masterfrom
semgrep-autofix/1773740942
Draft

Fix open redirect vulnerability in login_and_redirect endpoint#17
semgrep-code-studentsca023-rgb[bot] wants to merge 1 commit into
masterfrom
semgrep-autofix/1773740942

Conversation

@semgrep-code-studentsca023-rgb

Copy link
Copy Markdown

Fix open redirect vulnerability by replacing user-controlled URL with url_for().

Changes

  • Removed the user-controlled url parameter from the login_and_redirect endpoint
  • Replaced redirect(url) with redirect(url_for("home.home")) to redirect to a known safe route
  • Removed the unused is_safe_url helper function and urlparse import

Why

The original code accepted a URL from user input via request.args.get("url") and passed it directly to redirect(). This allowed attackers to craft malicious links that would redirect users to arbitrary external sites after authentication, enabling phishing attacks.

By using url_for() to generate the redirect URL, we ensure the redirect always goes to a known, internal route, completely eliminating the open redirect attack vector.

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 replacing user-controlled URL with url_for().

## Changes
- Removed the user-controlled `url` parameter from the `login_and_redirect` endpoint
- Replaced `redirect(url)` with `redirect(url_for("home.home"))` to redirect to a known safe route
- Removed the unused `is_safe_url` helper function and `urlparse` import

## Why
The original code accepted a URL from user input via `request.args.get("url")` and passed it directly to `redirect()`. This allowed attackers to craft malicious links that would redirect users to arbitrary external sites after authentication, enabling phishing attacks.

By using `url_for()` to generate the redirect URL, we ensure the redirect always goes to a known, internal route, completely eliminating the open redirect attack vector.

## 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).
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