Sanitize the URL before redirecting (#1260)
* URL Quote the URL before redirecting * Use safe url instead of unsafe one * Fix query params * fix build * Whitelist all reserved characters from rfc3986 * Add tests for redirect url sanitizing * Remove check for resulting URL on header injection test The thing the tests are testing for can be implemented in other ways that don't redirect to 100% the same address, but they'll all have to match the remaining parts of the test to succeed.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from mimetypes import guess_type
|
||||
from os import path
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
try:
|
||||
from ujson import dumps as json_dumps
|
||||
@@ -360,8 +361,11 @@ def redirect(to, headers=None, status=302,
|
||||
"""
|
||||
headers = headers or {}
|
||||
|
||||
# URL Quote the URL before redirecting
|
||||
safe_to = quote_plus(to, safe=":/#?&=@[]!$&'()*+,;")
|
||||
|
||||
# According to RFC 7231, a relative URI is now permitted.
|
||||
headers['Location'] = to
|
||||
headers['Location'] = safe_to
|
||||
|
||||
return HTTPResponse(
|
||||
status=status,
|
||||
|
||||
Reference in New Issue
Block a user