fix: URL redirection from authenticate_user #251
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 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