summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSenthil Kumaran <senthil@uthcode.com>2012-01-14 11:12:28 (GMT)
committerSenthil Kumaran <senthil@uthcode.com>2012-01-14 11:12:28 (GMT)
commit4479577388e737aba228ce8fbb7583add78ce80d (patch)
treef1cb34e2f5f88e21ac172fb575717ec98535ee3e
parent6c3dd7ea42c20d8e71da79c81b6c0738311d7f2e (diff)
parentc5c5a14577f1188125630dcbf31c5c2cd258b541 (diff)
downloadcpython-4479577388e737aba228ce8fbb7583add78ce80d.zip
cpython-4479577388e737aba228ce8fbb7583add78ce80d.tar.gz
cpython-4479577388e737aba228ce8fbb7583add78ce80d.tar.bz2
merge from 3.2 - Fix Issue #13642: Unquote before b64encoding user:password during Basic Authentication.
-rw-r--r--Lib/test/test_urllib.py33
-rw-r--r--Lib/urllib/request.py2
-rw-r--r--Misc/NEWS3
3 files changed, 36 insertions, 2 deletions
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index d929a5e..4cb4682 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -12,6 +12,8 @@ import os
import sys
import tempfile
+from base64 import b64encode
+
def hexescape(char):
"""Escape char as RFC 2396 specifies"""
hex_repr = hex(ord(char))[2:].upper()
@@ -42,8 +44,8 @@ class FakeHTTPMixin(object):
class FakeSocket(io.BytesIO):
io_refs = 1
- def sendall(self, str):
- pass
+ def sendall(self, data):
+ FakeHTTPConnection.buf = data
def makefile(self, *args, **kwds):
self.io_refs += 1
@@ -65,8 +67,13 @@ class FakeHTTPMixin(object):
io.BytesIO.close(self)
class FakeHTTPConnection(http.client.HTTPConnection):
+
+ # buffer to store data for verification in urlopen tests.
+ buf = None
+
def connect(self):
self.sock = FakeSocket(fakedata)
+
self._connection_class = http.client.HTTPConnection
http.client.HTTPConnection = FakeHTTPConnection
@@ -268,6 +275,25 @@ Content-Type: text/html; charset=iso-8859-1
finally:
self.unfakehttp()
+ def test_userpass_inurl_w_spaces(self):
+ self.fakehttp(b"HTTP/1.0 200 OK\r\n\r\nHello!")
+ try:
+ userpass = "a b:c d"
+ url = "http://{}@python.org/".format(userpass)
+ fakehttp_wrapper = http.client.HTTPConnection
+ authorization = ("Authorization: Basic %s\r\n" %
+ b64encode(userpass.encode("ASCII")).decode("ASCII"))
+ fp = urlopen(url)
+ # The authorization header must be in place
+ self.assertIn(authorization, fakehttp_wrapper.buf.decode("UTF-8"))
+ self.assertEqual(fp.readline(), b"Hello!")
+ self.assertEqual(fp.readline(), b"")
+ # the spaces are quoted in URL so no match
+ self.assertNotEqual(fp.geturl(), url)
+ self.assertEqual(fp.getcode(), 200)
+ finally:
+ self.unfakehttp()
+
class urlretrieve_FileTests(unittest.TestCase):
"""Test urllib.urlretrieve() on local files"""
@@ -1111,6 +1137,9 @@ class Utility_Tests(unittest.TestCase):
self.assertEqual(('user', 'a\fb'),urllib.parse.splitpasswd('user:a\fb'))
self.assertEqual(('user', 'a\vb'),urllib.parse.splitpasswd('user:a\vb'))
self.assertEqual(('user', 'a:b'),urllib.parse.splitpasswd('user:a:b'))
+ self.assertEqual(('user', 'a b'),urllib.parse.splitpasswd('user:a b'))
+ self.assertEqual(('user 2', 'ab'),urllib.parse.splitpasswd('user 2:ab'))
+ self.assertEqual(('user+1', 'a+b'),urllib.parse.splitpasswd('user+1:a+b'))
def test_thishost(self):
"""Test the urllib.request.thishost utility function returns a tuple"""
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index 2f74759..c9f2c1d 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -1693,11 +1693,13 @@ class URLopener:
if not host: raise IOError('http error', 'no host given')
if proxy_passwd:
+ proxy_passwd = unquote(proxy_passwd)
proxy_auth = base64.b64encode(proxy_passwd.encode()).decode('ascii')
else:
proxy_auth = None
if user_passwd:
+ user_passwd = unquote(user_passwd)
auth = base64.b64encode(user_passwd.encode()).decode('ascii')
else:
auth = None
diff --git a/Misc/NEWS b/Misc/NEWS
index c5ced44..c7ed8b8 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -442,6 +442,9 @@ Core and Builtins
Library
-------
+- Issue #13642: Unquote before b64encoding user:password during Basic
+ Authentication. Patch contributed by Joonas Kuorilehto.
+
- Issue #13726: Fix the ambiguous -S flag in regrtest. It is -o/--slow for slow
tests.