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

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