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

Skip to content

Commit 34f3fcc

Browse files
committed
Issue #12541: Be lenient with quotes around Realm field of HTTP Basic Authentation in urllib2.
G: changed Misc/NEWS
1 parent 539f239 commit 34f3fcc

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

Lib/test/test_urllib2.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,21 @@ def test_basic_auth(self, quote_char='"'):
12181218
def test_basic_auth_with_single_quoted_realm(self):
12191219
self.test_basic_auth(quote_char="'")
12201220

1221+
def test_basic_auth_with_unquoted_realm(self):
1222+
opener = OpenerDirector()
1223+
password_manager = MockPasswordManager()
1224+
auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
1225+
realm = "ACME Widget Store"
1226+
http_handler = MockHTTPHandler(
1227+
401, 'WWW-Authenticate: Basic realm=%s\r\n\r\n' % realm)
1228+
opener.add_handler(auth_handler)
1229+
opener.add_handler(http_handler)
1230+
self._test_basic_auth(opener, auth_handler, "Authorization",
1231+
realm, http_handler, password_manager,
1232+
"http://acme.example.com/protected",
1233+
"http://acme.example.com/protected",
1234+
)
1235+
12211236
def test_proxy_basic_auth(self):
12221237
opener = OpenerDirector()
12231238
ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))

Lib/urllib/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ class AbstractBasicAuthHandler:
794794
# allow for double- and single-quoted realm values
795795
# (single quotes are a violation of the RFC, but appear in the wild)
796796
rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'
797-
'realm=(["\'])(.*?)\\2', re.I)
797+
'realm=(["\']?)([^"\']*)\\2', re.I)
798798

799799
# XXX could pre-emptively send auth info already accepted (RFC 2617,
800800
# end of section 2, and section 1.2 immediately after "credentials"

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ Core and Builtins
6363
Library
6464
-------
6565

66+
- Issue #12541: Be lenient with quotes around Realm field of HTTP Basic
67+
Authentation in urllib2.
68+
6669
- Issue #14662: Prevent shutil failures on OS X when destination does not
6770
support chflag operations. Patch by Hynek Schlawack.
6871

0 commit comments

Comments
 (0)