10000 bpo-39503: CVE-2020-8492: Fix AbstractBasicAuthHandler (GH-18284) (#1… · python/cpython@37fe316 · GitHub
[go: up one dir, main page]

Skip to content

Commit 37fe316

Browse files
authored
bpo-39503: CVE-2020-8492: Fix AbstractBasicAuthHandler (GH-18284) (#19305)
The AbstractBasicAuthHandler class of the urllib.request module uses an inefficient regular expression which can be exploited by an attacker to cause a denial of service. Fix the regex to prevent the catastrophic backtracking. Vulnerability reported by Ben Caller and Matt Schwager. AbstractBasicAuthHandler of urllib.request now parses all WWW-Authenticate HTTP headers and accepts multiple challenges per header: use the realm of the first Basic challenge.
1 parent f91a0b6 commit 37fe316

File tree

4 files changed

+115
-52
lines changed

4 files changed

+115
-52
lines changed

Lib/test/test_urllib2.py

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,40 +1361,64 @@ def test_osx_proxy_bypass(self):
13611361
bypass = {'exclude_simple': True, 'exceptions': []}
13621362
self.assertTrue(_proxy_bypass_macosx_sysconf('test', bypass))
13631363

1364-
def test_basic_auth(self, quote_char='"'):
1365-
opener = OpenerDirector()
1366-
password_manager = MockPasswordManager()
1367-
auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
1368-
realm = "ACME Widget Store"
1369-
http_handler = MockHTTPHandler(
1370-
401, 'WWW-Authenticate: Basic realm=%s%s%s\r\n\r\n' %
1371-
(quote_char, realm, quote_char))
1372-
opener.add_handler(auth_handler)
1373-
opener.add_handler(http_handler)
1374-
self._test_basic_auth(opener, auth_handler, "Authorization",
1375-
realm, http_handler, password_manager,
1376-
"http://acme.example.com/protected",
1377-
"http://acme.example.com/protected",
1378-
)
1379-
1380-
def test_basic_auth_with_single_quoted_realm(self):
1381-
self.test_basic_auth(quote_char="'")
1382-
1383-
def test_basic_auth_with_unquoted_realm(self):
1384-
opener = OpenerDirector()
1385-
password_manager = MockPasswordManager()
1386-
auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
1387-
realm = "ACME Widget Store"
1388-
http_handler = MockHTTPHandler(
1389-
401, 'WWW-Authenticate: Basic realm=%s\r\n\r\n' % realm)
1390-
opener.add_handler(auth_handler)
1391-
opener.add_handler(http_handler)
1392-
with self.assertWarns(UserWarning):
1364+
def check_basic_auth(self, headers, realm):
1365+
with self.subTest(realm=realm, headers=headers):
1366+
opener = OpenerDirector()
1367+
password_manager = MockPasswordManager()
1368+
auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
1369+
body = '\r\n'.join(headers) + '\r\n\r\n'
1370+
http_handler = MockHTTPHandler(401, body)
1371+
opener.add_handler(auth_handler)
1372+
opener.add_handler(http_handler)
13931373
self._test_basic_auth(opener, auth_handler, "Authorization",
1394-
realm, http_handler, password_manager,
1395-
"http://acme.example.com/protected",
1396-
"http://acme.example.com/protected",
1397-
)
1374+
realm, http_handler, password_manager,
1375+
"http://acme.example.com/protected",
1376+
"http://acme.example.com/protected")
1377+
1378+
def test_basic_auth(self):
1379+
realm = "realm2@example.com"
1380+
realm2 = "realm2@example.com"
1381+
basic = 'Basic realm="{}"'.format(realm)
1382+
basic2 = 'Basic realm="{}"'.format(realm2)
1383+
other_no_realm = 'Otherscheme xxx'
1384+
digest = ('Digest realm="{}", '
1385+
'qop="auth, auth-int", '
1386+
'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", '
1387+
'opaque="5ccc069c403ebaf9f0171e9517f40e41"').format(realm2)
1388+
for realm_str in (
1389+
# test "quote" and 'quote'
1390+
'Basic realm="{}"'.format(realm),
1391+
"Basic realm='{}'".format(realm),
1392+
1393+
# charset is ignored
1394+
'Basic realm="{}", charset="UTF-8"'.format(realm),
1395+
1396+
# Multiple challenges per header
1397+
'{}, {}'.format(basic, basic2),
1398+
'{}, {}'.format(basic, other_no_realm),
1399+
'{}, {}'.format(other_no_realm, basic),
1400+
'{}, {}'.format(basic, digest),
1401+
'{}, {}'.format(digest, basic),
1402+
):
1403+
headers = ['WWW-Authenticate: {}'.format(realm_str)]
1404+
self.check_basic_auth(headers, realm)
1405+
1406+
# no quote: expect a warning
1407+
with support.check_warnings(("Basic Auth Realm was unquoted",
1408+
UserWarning)):
1409+
headers = ['WWW-Authenticate: Basic realm={}'.format(realm)]
1410+
self.check_basic_auth(headers, realm)
1411+
1412+
# Multiple headers: one challenge per header.
1413+
# Use the first Basic realm.
1414+
for challenges in (
1415+
[basic, basic2],
1416+
[basic, digest],
1417+
[digest, basic],
1418+
):
1419+
headers = ['WWW-Authenticate: {}'.format(challenge)
1420+
for challenge in challenges]
1421+
self.check_basic_auth(headers, realm)
13981422

13991423
def test_proxy_basic_auth(self):
14001424
opener = OpenerDirector()

Lib/urllib/request.py

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,15 @@ class AbstractBasicAuthHandler:
885885

886886
# allow for double- and single-quoted realm values
887887
# (single quotes are a violation of the RFC, but appear in the wild)
888-
rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'
889-
'realm=(["\']?)([^"\']*)\\2', re.I)
888+
rx = re.compile('(?:^|,)' # start of the string or ','
889+
'[ \t]*' # optional whitespaces
890+
'([^ \t]+)' # scheme like "Basic"
891+
'[ \t]+' # mandatory whitespaces
892+
# realm=xxx
893+
# realm='xxx'
894+
# realm="xxx"
895+
'realm=(["\']?)([^"\']*)\\2',
896+
re.I)
890897

891898
# XXX could pre-emptively send auth info already accepted (RFC 2617,
892899
# end of section 2, and section 1.2 immediately after "credentials"
@@ -898,27 +905,51 @@ def __init__(self, password_mgr=None):
898905
self.passwd = password_mgr
899906
self.add_password = self.passwd.add_password
900907

908+
def _parse_realm(self, header):
909+
# parse WWW-Authenticate header: accept multiple challenges per header
910+
found_challenge = False
911+
for mo in AbstractBasicAuthHandler.rx.finditer(header):
912+
scheme, quote, realm = mo.groups()
913+
if quote not in ['"', "'"]:
914+
warnings.warn("Basic Auth Realm was unquoted",
915+
UserWarning, 3)
916+
917+
yield (scheme, realm)
918+
919+
found_challenge = True
920+
921+
if not found_challenge:
922+
if header:
923+
scheme = header.split()[0]
924+
else:
925+
scheme = ''
926+
yield (scheme, None)
927+
901928
def http_error_auth_reqed(self, authreq, host, req, headers):
902929
# host may be an authority (without userinfo) or a URL with an
903930
# authority
904-
# XXX could be multiple headers
905-
authreq = headers.get(authreq, None)
931+
headers = headers.get_all(authreq)
932+
if not headers:
933+
# no header found
934+
return
906935

907-
if authreq:
908-
scheme = authreq.split()[0]
909-
if scheme.lower() != 'basic':
910-
raise ValueError("AbstractBasicAuthHandler does not"
911-
" support the following scheme: '%s'" %
912-
scheme)
913-
else:
914-
mo = AbstractBasicAuthHandler.rx.search(authreq)
915-
if mo:
916-
scheme, quote, realm = mo.groups()
917-
if quote not in ['"',"'"]:
918-
warnings.warn("Basic Auth Realm was unquoted",
919-
UserWarning, 2)
920-
if scheme.lower() == 'basic':
921-
return self.retry_http_basic_auth(host, req, realm)
936+
unsupported = None
937+
for header in headers:
938+
for scheme, realm in self._parse_realm(header):
939+
if scheme.lower() != 'basic':
940+
unsupported = scheme
941+
continue
942+
943+
if realm is not None:
944+
# Use the first matching Basic challenge.
945+
# Ignore following challenges even if they use the Basic
946+
# scheme.
947+
return self.retry_http_basic_auth(host, req, realm)
948+
949+
if unsupported is not None:
950+
raise ValueError("AbstractBasicAuthHandler does not "
951+
"support the following scheme: %r"
952+
% (scheme,))
922953

923954
def retry_http_basic_auth(self, host, req, realm):
924955
user, pw = self.passwd.find_user_password(realm, host)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`~urllib.request.AbstractBasicAuthHandler` of :mod:`urllib.request`
2+
now parses all WWW-Authenticate HTTP headers and accepts multiple challenges
3+
per header: use the realm of the first Basic challenge.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CVE-2020-8492: The :class:`~urllib.request.AbstractBasicAuthHandler` class of the
2+
:mod:`urllib.request` module uses an inefficient regular expression which can
3+
be exploited by an attacker to cause a denial of service. Fix the regex to
4+
prevent the catastrophic backtracking. Vulnerability reported by Ben Caller
5+
and Matt Schwager.

0 commit comments

Comments
0