Blame SOURCES/00346-CVE-2020-8492.patch

f8e6ca
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
f8e6ca
index 876fcd4..fe9a32b 100644
f8e6ca
--- a/Lib/test/test_urllib2.py
f8e6ca
+++ b/Lib/test/test_urllib2.py
f8e6ca
@@ -1445,40 +1445,64 @@ class HandlerTests(unittest.TestCase):
f8e6ca
         bypass = {'exclude_simple': True, 'exceptions': []}
f8e6ca
         self.assertTrue(_proxy_bypass_macosx_sysconf('test', bypass))
f8e6ca
 
f8e6ca
-    def test_basic_auth(self, quote_char='"'):
f8e6ca
-        opener = OpenerDirector()
f8e6ca
-        password_manager = MockPasswordManager()
f8e6ca
-        auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
f8e6ca
-        realm = "ACME Widget Store"
f8e6ca
-        http_handler = MockHTTPHandler(
f8e6ca
-            401, 'WWW-Authenticate: Basic realm=%s%s%s\r\n\r\n' %
f8e6ca
-            (quote_char, realm, quote_char))
f8e6ca
-        opener.add_handler(auth_handler)
f8e6ca
-        opener.add_handler(http_handler)
f8e6ca
-        self._test_basic_auth(opener, auth_handler, "Authorization",
f8e6ca
-                              realm, http_handler, password_manager,
f8e6ca
-                              "http://acme.example.com/protected",
f8e6ca
-                              "http://acme.example.com/protected",
f8e6ca
-                              )
f8e6ca
-
f8e6ca
-    def test_basic_auth_with_single_quoted_realm(self):
f8e6ca
-        self.test_basic_auth(quote_char="'")
f8e6ca
-
f8e6ca
-    def test_basic_auth_with_unquoted_realm(self):
f8e6ca
-        opener = OpenerDirector()
f8e6ca
-        password_manager = MockPasswordManager()
f8e6ca
-        auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
f8e6ca
-        realm = "ACME Widget Store"
f8e6ca
-        http_handler = MockHTTPHandler(
f8e6ca
-            401, 'WWW-Authenticate: Basic realm=%s\r\n\r\n' % realm)
f8e6ca
-        opener.add_handler(auth_handler)
f8e6ca
-        opener.add_handler(http_handler)
f8e6ca
-        with self.assertWarns(UserWarning):
f8e6ca
+    def check_basic_auth(self, headers, realm):
f8e6ca
+        with self.subTest(realm=realm, headers=headers):
f8e6ca
+            opener = OpenerDirector()
f8e6ca
+            password_manager = MockPasswordManager()
f8e6ca
+            auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
f8e6ca
+            body = '\r\n'.join(headers) + '\r\n\r\n'
f8e6ca
+            http_handler = MockHTTPHandler(401, body)
f8e6ca
+            opener.add_handler(auth_handler)
f8e6ca
+            opener.add_handler(http_handler)
f8e6ca
             self._test_basic_auth(opener, auth_handler, "Authorization",
f8e6ca
-                                realm, http_handler, password_manager,
f8e6ca
-                                "http://acme.example.com/protected",
f8e6ca
-                                "http://acme.example.com/protected",
f8e6ca
-                                )
f8e6ca
+                                  realm, http_handler, password_manager,
f8e6ca
+                                  "http://acme.example.com/protected",
f8e6ca
+                                  "http://acme.example.com/protected")
f8e6ca
+
f8e6ca
+    def test_basic_auth(self):
f8e6ca
+        realm = "realm2@example.com"
f8e6ca
+        realm2 = "realm2@example.com"
f8e6ca
+        basic = f'Basic realm="{realm}"'
f8e6ca
+        basic2 = f'Basic realm="{realm2}"'
f8e6ca
+        other_no_realm = 'Otherscheme xxx'
f8e6ca
+        digest = (f'Digest realm="{realm2}", '
f8e6ca
+                  f'qop="auth, auth-int", '
f8e6ca
+                  f'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", '
f8e6ca
+                  f'opaque="5ccc069c403ebaf9f0171e9517f40e41"')
f8e6ca
+        for realm_str in (
f8e6ca
+            # test "quote" and 'quote'
f8e6ca
+            f'Basic realm="{realm}"',
f8e6ca
+            f"Basic realm='{realm}'",
f8e6ca
+
f8e6ca
+            # charset is ignored
f8e6ca
+            f'Basic realm="{realm}", charset="UTF-8"',
f8e6ca
+
f8e6ca
+            # Multiple challenges per header
f8e6ca
+            f'{basic}, {basic2}',
f8e6ca
+            f'{basic}, {other_no_realm}',
f8e6ca
+            f'{other_no_realm}, {basic}',
f8e6ca
+            f'{basic}, {digest}',
f8e6ca
+            f'{digest}, {basic}',
f8e6ca
+        ):
f8e6ca
+            headers = [f'WWW-Authenticate: {realm_str}']
f8e6ca
+            self.check_basic_auth(headers, realm)
f8e6ca
+
f8e6ca
+        # no quote: expect a warning
f8e6ca
+        with support.check_warnings(("Basic Auth Realm was unquoted",
f8e6ca
+                                     UserWarning)):
f8e6ca
+            headers = [f'WWW-Authenticate: Basic realm={realm}']
f8e6ca
+            self.check_basic_auth(headers, realm)
f8e6ca
+
f8e6ca
+        # Multiple headers: one challenge per header.
f8e6ca
+        # Use the first Basic realm.
f8e6ca
+        for challenges in (
f8e6ca
+            [basic,  basic2],
f8e6ca
+            [basic,  digest],
f8e6ca
+            [digest, basic],
f8e6ca
+        ):
f8e6ca
+            headers = [f'WWW-Authenticate: {challenge}'
f8e6ca
+                       for challenge in challenges]
f8e6ca
+            self.check_basic_auth(headers, realm)
f8e6ca
 
f8e6ca
     def test_proxy_basic_auth(self):
f8e6ca
         opener = OpenerDirector()
f8e6ca
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
f8e6ca
index c9945d9..6624e04 100644
f8e6ca
--- a/Lib/urllib/request.py
f8e6ca
+++ b/Lib/urllib/request.py
f8e6ca
@@ -945,8 +945,15 @@ class AbstractBasicAuthHandler:
f8e6ca
 
f8e6ca
     # allow for double- and single-quoted realm values
f8e6ca
     # (single quotes are a violation of the RFC, but appear in the wild)
f8e6ca
-    rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'
f8e6ca
-                    'realm=(["\']?)([^"\']*)\\2', re.I)
f8e6ca
+    rx = re.compile('(?:^|,)'   # start of the string or ','
f8e6ca
+                    '[ \t]*'    # optional whitespaces
f8e6ca
+                    '([^ \t]+)' # scheme like "Basic"
f8e6ca
+                    '[ \t]+'    # mandatory whitespaces
f8e6ca
+                    # realm=xxx
f8e6ca
+                    # realm='xxx'
f8e6ca
+                    # realm="xxx"
f8e6ca
+                    'realm=(["\']?)([^"\']*)\\2',
f8e6ca
+                    re.I)
f8e6ca
 
f8e6ca
     # XXX could pre-emptively send auth info already accepted (RFC 2617,
f8e6ca
     # end of section 2, and section 1.2 immediately after "credentials"
f8e6ca
@@ -958,27 +965,51 @@ class AbstractBasicAuthHandler:
f8e6ca
         self.passwd = password_mgr
f8e6ca
         self.add_password = self.passwd.add_password
f8e6ca
 
f8e6ca
+    def _parse_realm(self, header):
f8e6ca
+        # parse WWW-Authenticate header: accept multiple challenges per header
f8e6ca
+        found_challenge = False
f8e6ca
+        for mo in AbstractBasicAuthHandler.rx.finditer(header):
f8e6ca
+            scheme, quote, realm = mo.groups()
f8e6ca
+            if quote not in ['"', "'"]:
f8e6ca
+                warnings.warn("Basic Auth Realm was unquoted",
f8e6ca
+                              UserWarning, 3)
f8e6ca
+
f8e6ca
+            yield (scheme, realm)
f8e6ca
+
f8e6ca
+            found_challenge = True
f8e6ca
+
f8e6ca
+        if not found_challenge:
f8e6ca
+            if header:
f8e6ca
+                scheme = header.split()[0]
f8e6ca
+            else:
f8e6ca
+                scheme = ''
f8e6ca
+            yield (scheme, None)
f8e6ca
+
f8e6ca
     def http_error_auth_reqed(self, authreq, host, req, headers):
f8e6ca
         # host may be an authority (without userinfo) or a URL with an
f8e6ca
         # authority
f8e6ca
-        # XXX could be multiple headers
f8e6ca
-        authreq = headers.get(authreq, None)
f8e6ca
+        headers = headers.get_all(authreq)
f8e6ca
+        if not headers:
f8e6ca
+            # no header found
f8e6ca
+            return
f8e6ca
 
f8e6ca
-        if authreq:
f8e6ca
-            scheme = authreq.split()[0]
f8e6ca
-            if scheme.lower() != 'basic':
f8e6ca
-                raise ValueError("AbstractBasicAuthHandler does not"
f8e6ca
-                                 " support the following scheme: '%s'" %
f8e6ca
-                                 scheme)
f8e6ca
-            else:
f8e6ca
-                mo = AbstractBasicAuthHandler.rx.search(authreq)
f8e6ca
-                if mo:
f8e6ca
-                    scheme, quote, realm = mo.groups()
f8e6ca
-                    if quote not in ['"',"'"]:
f8e6ca
-                        warnings.warn("Basic Auth Realm was unquoted",
f8e6ca
-                                      UserWarning, 2)
f8e6ca
-                    if scheme.lower() == 'basic':
f8e6ca
-                        return self.retry_http_basic_auth(host, req, realm)
f8e6ca
+        unsupported = None
f8e6ca
+        for header in headers:
f8e6ca
+            for scheme, realm in self._parse_realm(header):
f8e6ca
+                if scheme.lower() != 'basic':
f8e6ca
+                    unsupported = scheme
f8e6ca
+                    continue
f8e6ca
+
f8e6ca
+                if realm is not None:
f8e6ca
+                    # Use the first matching Basic challenge.
f8e6ca
+                    # Ignore following challenges even if they use the Basic
f8e6ca
+                    # scheme.
f8e6ca
+                    return self.retry_http_basic_auth(host, req, realm)
f8e6ca
+
f8e6ca
+        if unsupported is not None:
f8e6ca
+            raise ValueError("AbstractBasicAuthHandler does not "
f8e6ca
+                             "support the following scheme: %r"
f8e6ca
+                             % (scheme,))
f8e6ca
 
f8e6ca
     def retry_http_basic_auth(self, host, req, realm):
f8e6ca
         user, pw = self.passwd.find_user_password(realm, host)