Skip to content

fix open redirect vulnerability in OAuth callback handler#265

Draft
semgrep-code-rootflo[bot] wants to merge 1 commit intodevelopfrom
semgrep-autofix/1775116779
Draft

fix open redirect vulnerability in OAuth callback handler#265
semgrep-code-rootflo[bot] wants to merge 1 commit intodevelopfrom
semgrep-autofix/1775116779

Conversation

@semgrep-code-rootflo
Copy link
Copy Markdown

Add URL validation to prevent open redirect attacks in the OAuth callback flow.

Changes

  • Added validate_redirect_url() function to user_utils.py that validates redirect URLs by:
    • Ensuring only http/https schemes are allowed
    • Requiring a valid host (netloc)
    • Blocking protocol-relative URL bypasses (e.g., //evil.com)
    • Blocking backslash-based bypasses (e.g., /\evil.com)
    • Blocking URLs with embedded credentials (e.g., https://attacker@victim.com)
    • Supporting optional domain allowlist validation
  • Updated auth_plugin_controller.py to validate success_url and failure_url before using them in RedirectResponse
  • Invalid URLs now fall back to about:blank instead of redirecting to potentially malicious domains

Why

The client_redirect_success_url and client_redirect_failure_url values from configuration were being used directly in RedirectResponse without validation. An attacker who could control these configuration values could redirect users to malicious domains, enabling phishing attacks or credential theft. The new validation ensures all redirect URLs are safe before use.

Semgrep Finding Details

The application builds a URL using user-controlled input which can lead to an open redirect vulnerability. An attacker can manipulate the URL and redirect users to an arbitrary domain. Open redirect vulnerabilities can lead to issues such as Cross-site scripting (XSS) or redirecting to a malicious domain for activities such as phishing to capture users' credentials. To prevent this vulnerability perform strict input validation of the domain against an allowlist of approved domains. Notify a user in your application that they are leaving the website. Display a domain where they are redirected to the user. A user can then either accept or deny the redirect to an untrusted site.

@18578539 requested Semgrep Assistant generate this pull request to fix a finding from the detection rule python.fastapi.web.tainted-redirect-fastapi.tainted-redirect-fastapi.


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

Add URL validation to prevent open redirect attacks in the OAuth callback flow.

## Changes
- Added `validate_redirect_url()` function to `user_utils.py` that validates redirect URLs by:
  - Ensuring only `http`/`https` schemes are allowed
  - Requiring a valid host (netloc)
  - Blocking protocol-relative URL bypasses (e.g., `//evil.com`)
  - Blocking backslash-based bypasses (e.g., `/\evil.com`)
  - Blocking URLs with embedded credentials (e.g., `https://attacker@victim.com`)
  - Supporting optional domain allowlist validation
- Updated `auth_plugin_controller.py` to validate `success_url` and `failure_url` before using them in `RedirectResponse`
- Invalid URLs now fall back to `about:blank` instead of redirecting to potentially malicious domains

## Why
The `client_redirect_success_url` and `client_redirect_failure_url` values from configuration were being used directly in `RedirectResponse` without validation. An attacker who could control these configuration values could redirect users to malicious domains, enabling phishing attacks or credential theft. The new validation ensures all redirect URLs are safe before use.

## Semgrep Finding Details
The application builds a URL using user-controlled input which can lead to an open redirect vulnerability. An attacker can manipulate the URL and redirect users to an arbitrary domain. Open redirect vulnerabilities can lead to issues such as Cross-site scripting (XSS) or redirecting to a malicious domain for activities such as phishing to capture users' credentials. To prevent this vulnerability perform strict input validation of the domain against an allowlist of approved domains. Notify a user in your application that they are leaving the website. Display a domain where they are redirected to the user. A user can then either accept or deny the redirect to an untrusted site.

@18578539 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/rootflo_ai/findings/683091413) from the detection rule [python.fastapi.web.tainted-redirect-fastapi.tainted-redirect-fastapi](https://semgrep.dev/r/python.fastapi.web.tainted-redirect-fastapi.tainted-redirect-fastapi).
)
return RedirectResponse(url=f'{failure_url}?{params}')
except Exception as e:
except Exception:
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