]> granicus.if.org Git - python/commitdiff
bpo-30458: Use InvalidURL instead of ValueError. (GH-13044)
authorGregory P. Smith <greg@krypto.org>
Wed, 1 May 2019 20:39:21 +0000 (16:39 -0400)
committerGitHub <noreply@github.com>
Wed, 1 May 2019 20:39:21 +0000 (16:39 -0400)
Use http.client.InvalidURL instead of ValueError as the new error case's exception.

Lib/http/client.py
Lib/test/test_urllib.py
Misc/NEWS.d/next/Security/2019-04-10-08-53-30.bpo-30458.51E-DA.rst [moved from Misc/NEWS.d/next/Security/2019-04-10-08-53-30.bpo-36276.51E-DA.rst with 75% similarity]

index 99d6a68cf42823a21708075fc9e2ef33450526f0..f71a062d2b5783e4fd92b44153a453460f29e699 100644 (file)
@@ -1091,7 +1091,7 @@ class HTTPConnection:
             url = '/'
         # Prevent CVE-2019-9740.
         if match := _contains_disallowed_url_pchar_re.search(url):
-            raise ValueError(f"URL can't contain control characters. {url!r} "
+            raise InvalidURL(f"URL can't contain control characters. {url!r} "
                              f"(found at least {match.group()!r})")
         request = '%s %s %s' % (method, url, self._http_vsn_str)
 
index c5b23f935b275bfdfc46295875c5ad6f80ecdca6..7214492eca9d88805ab95398e545bc380c1b4ef9 100644 (file)
@@ -343,11 +343,12 @@ class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin, FakeFTPMixin):
                 # calls urllib.parse.quote() on the URL which makes all of the
                 # above attempts at injection within the url _path_ safe.
                 escaped_char_repr = repr(char).replace('\\', r'\\')
+                InvalidURL = http.client.InvalidURL
                 with self.assertRaisesRegex(
-                    ValueError, f"contain control.*{escaped_char_repr}"):
+                    InvalidURL, f"contain control.*{escaped_char_repr}"):
                     urllib.request.urlopen(f"http:{schemeless_url}")
                 with self.assertRaisesRegex(
-                    ValueError, f"contain control.*{escaped_char_repr}"):
+                    InvalidURL, f"contain control.*{escaped_char_repr}"):
                     urllib.request.urlopen(f"https:{schemeless_url}")
                 # This code path quotes the URL so there is no injection.
                 resp = urlopen(f"http:{schemeless_url}")
@@ -367,10 +368,11 @@ class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin, FakeFTPMixin):
             # urlopen uses FancyURLOpener which goes via a codepath that
             # calls urllib.parse.quote() on the URL which makes all of the
             # above attempts at injection within the url _path_ safe.
+            InvalidURL = http.client.InvalidURL
             with self.assertRaisesRegex(
-                ValueError, r"contain control.*\\r.*(found at least . .)"):
+                InvalidURL, r"contain control.*\\r.*(found at least . .)"):
                 urllib.request.urlopen(f"http:{schemeless_url}")
-            with self.assertRaisesRegex(ValueError, r"contain control.*\\n"):
+            with self.assertRaisesRegex(InvalidURL, r"contain control.*\\n"):
                 urllib.request.urlopen(f"https:{schemeless_url}")
             # This code path quotes the URL so there is no injection.
             resp = urlopen(f"http:{schemeless_url}")
similarity index 75%
rename from Misc/NEWS.d/next/Security/2019-04-10-08-53-30.bpo-36276.51E-DA.rst
rename to Misc/NEWS.d/next/Security/2019-04-10-08-53-30.bpo-30458.51E-DA.rst
index 4fed4d545040e9caf05b13bcd4c9c22c987649fb..ed8027fb4d64202335a8fa0d80ebd848ed890d3e 100644 (file)
@@ -1 +1 @@
-Address CVE-2019-9740 by disallowing URL paths with embedded whitespace or control characters through into the underlying http client request.  Such potentially malicious header injection URLs now cause a ValueError to be raised.
\ No newline at end of file
+Address CVE-2019-9740 by disallowing URL paths with embedded whitespace or control characters through into the underlying http client request.  Such potentially malicious header injection URLs now cause an http.client.InvalidURL exception to be raised.