Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6497aa3

Browse files
committed
Issue13696 - Fix 302 Redirection for Relative urls.
1 parent b7ffed8 commit 6497aa3

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

Lib/test/test_urllib2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,19 @@ def test_invalid_redirect(self):
10591059
MockHeaders({"location": valid_url}))
10601060
self.assertEqual(o.req.get_full_url(), valid_url)
10611061

1062+
def test_relative_redirect(self):
1063+
from_url = "http://example.com/a.html"
1064+
relative_url = "/b.html"
1065+
h = urllib.request.HTTPRedirectHandler()
1066+
o = h.parent = MockOpener()
1067+
req = Request(from_url)
1068+
req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
1069+
1070+
valid_url = urllib.parse.urljoin(from_url,relative_url)
1071+
h.http_error_302(req, MockFile(), 302, "That's fine",
1072+
MockHeaders({"location": valid_url}))
1073+
self.assertEqual(o.req.get_full_url(), valid_url)
1074+
10621075
def test_cookie_redirect(self):
10631076
# cookies shouldn't leak into redirected requests
10641077
from http.cookiejar import CookieJar

Lib/urllib/request.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def http_error_302(self, req, fp, code, msg, headers):
552552
# For security reasons we don't allow redirection to anything other
553553
# than http, https or ftp.
554554

555-
if urlparts.scheme not in ('http', 'https', 'ftp'):
555+
if urlparts.scheme not in ('http', 'https', 'ftp', ''):
556556
raise HTTPError(
557557
newurl, code,
558558
"%s - Redirection to url '%s' is not allowed" % (msg, newurl),
@@ -1935,7 +1935,7 @@ def redirect_internal(self, url, fp, errcode, errmsg, headers, data):
19351935
# We are using newer HTTPError with older redirect_internal method
19361936
# This older method will get deprecated in 3.3
19371937

1938-
if urlparts.scheme not in ('http', 'https', 'ftp'):
1938+
if urlparts.scheme not in ('http', 'https', 'ftp', ''):
19391939
raise HTTPError(newurl, errcode,
19401940
errmsg +
19411941
" Redirection to url '%s' is not allowed." % newurl,

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ Core and Builtins
9797
Library
9898
-------
9999

100+
- Issue #13696: Fix the 302 Relative URL Redirection problem.
101+
100102
- Issue #13636: Weak ciphers are now disabled by default in the ssl module
101103
(except when SSLv2 is explicitly asked for).
102104

0 commit comments

Comments
 (0)