forked from omab/django-social-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged sanitize redirect url checker. Closes omabgh-38
- Loading branch information
Showing
2 changed files
with
34 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,43 @@ | ||
import urlparse | ||
|
||
|
||
def sanitize_redirect(host, redirect_to): | ||
""" | ||
Given the hostname and an untrusted URL to redirect to, | ||
this method tests it to make sure it isn't garbage/harmful | ||
and returns it, else returns None. | ||
See http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/views.py#L36 | ||
>>> print sanitize_redirect('myapp.com', None) | ||
None | ||
>>> print sanitize_redirect('myapp.com', '') | ||
None | ||
>>> print sanitize_redirect('myapp.com', {}) | ||
None | ||
>>> print sanitize_redirect('myapp.com', 'http://notmyapp.com/path/') | ||
None | ||
>>> print sanitize_redirect('myapp.com', 'http://myapp.com/path/') | ||
http://myapp.com/path/ | ||
>>> print sanitize_redirect('myapp.com', '/path/') | ||
/path/ | ||
""" | ||
# Quick sanity check. | ||
if not redirect_to: | ||
return None | ||
netloc = urlparse.urlparse(redirect_to)[1] | ||
# Heavier security check -- don't allow redirection to a different host. | ||
|
||
# Heavier security check, don't allow redirection to a different host. | ||
try: | ||
netloc = urlparse.urlparse(redirect_to)[1] | ||
except TypeError: # not valid redirect_to value | ||
return None | ||
|
||
if netloc and netloc != host: | ||
return None | ||
|
||
return redirect_to | ||
|
||
|
||
if __name__ == '__main__': | ||
import doctest | ||
doctest.testmod() |
This file contains 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