-
Notifications
You must be signed in to change notification settings - Fork 65
fix: URL redirection from authenticate_user #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
) | ||
next_url = request.args.get('next', '') | ||
next_url = next_url.replace('\\', '') | ||
if not urlparse(next_url).netloc and not urlparse(next_url).scheme: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
urlparse
was renamed to urllib.parse
in Python 3, it should be updated to that and also imported
https://docs.python.org/3/library/urllib.parse.html#module-urllib.parse
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (1)
api/authentication.py:50
- [nitpick] Consider parsing next_url once into a variable (e.g., parsed_url) instead of calling urlparse twice for improved readability and minor performance benefits.
if not urlparse(next_url).netloc and not urlparse(next_url).scheme:
Original messageI don't believe there's an issue here. For reference, here's the original: redirect_uri = "{login}?next={here}".format(
login=url_for("oidc_auth.login"),
here=quote_plus(request.url),
)
Am I missing something? I was indeed missing something in my original comment above. I was directed to this PR thinking it was calling out an existing vulnerability, but it appears its intention is to fix a bug, and is defending the approach used. Apologies for that. That being said, I do think this has issues in that it will only redirect correctly if the If I understand correctly, as these changes stand, if the
|
fix the problem to validate the user-provided URL before using it in the redirection. We can use the
urlparse
function from the Python standard library to parse the URL and check that it does not include an explicit host name. This ensures that only relative paths within the application's domain are allowed for redirection. modify theauthenticate_user
method in theAuthenticationHelpers
class to include this validation. Specifically, we will replace the current redirection logic with a check that ensures thenext
parameter in the URL is a relative path.Directly incorporating user input into a URL redirect request without validating the input can facilitate phishing attacks. In these attacks, unsuspecting users can be redirected to a malicious site that looks very similar to the real site they intend to visit, but which is controlled by the attacker.
POC
The following vulnerable shows an HTTP request parameter being used directly in a URL redirect without validating the input, which facilitates phishing attacks:
If you know the set of valid redirect targets, you can maintain a list of them on the server and check that the user input is in that list:
Often this is not possible, so an alternative is to check that the target URL does not specify an explicit host name. For example, you can use the
urlparse
function from the Python standard library to parse the URL and check that the netloc attribute is empty.For Django application, you can use the function
url_has_allowed_host_and_scheme
to check that a URL is safe to redirect to, as shown in the following code:References
XSS Unvalidated Redirects and Forwards Cheat Sheet
urllib.parse
CWE-601