b42408
From 9f6aa6b5f06ecfcfea2084d88f377c6e9dba5ce2 Mon Sep 17 00:00:00 2001
b42408
From: Ryan Petrello <rpetrell@redhat.com>
b42408
Date: Tue, 30 Apr 2019 12:36:48 -0400
b42408
Subject: [PATCH 1/3] prevent CVE-2019-9740 in 1.24.x
b42408
b42408
adapted from https://github.com/python/cpython/pull/12755
b42408
---
b42408
 test/test_util.py   | 5 +++++
b42408
 src/urllib3/util/url.py | 8 ++++++++
b42408
 2 files changed, 13 insertions(+)
b42408
b42408
diff --git a/test/test_util.py b/test/test_util.py
b42408
index 73d9452..dc6ffd0 100644
b42408
--- a/test/test_util.py
b42408
+++ b/test/test_util.py
b42408
@@ -200,6 +200,11 @@ class TestUtil(object):
b42408
         with pytest.raises(ValueError):
b42408
             parse_url('[::1')
b42408
 
b42408
+    def test_parse_url_contains_control_characters(self):
b42408
+        # see CVE-2019-9740
b42408
+        with pytest.raises(LocationParseError):
b42408
+            parse_url('http://localhost:8000/ HTTP/1.1\r\nHEADER: INJECTED\r\nIgnore:')
b42408
+
b42408
     def test_Url_str(self):
b42408
         U = Url('http', host='google.com')
b42408
         assert str(U) == U.url
b42408
diff --git a/src/urllib3/util/url.py b/src/urllib3/util/url.py
b42408
index 6b6f996..e8e1bd7 100644
b42408
--- a/src/urllib3/util/url.py
b42408
+++ b/src/urllib3/util/url.py
b42408
@@ -1,5 +1,6 @@
b42408
 from __future__ import absolute_import
b42408
 from collections import namedtuple
b42408
+import re
b42408
 
b42408
 from ..exceptions import LocationParseError
b42408
 
b42408
@@ -10,6 +11,8 @@ url_attrs = ['scheme', 'auth', 'host', 'port', 'path', 'query', 'fragment']
b42408
 # urllib3 infers URLs without a scheme (None) to be http.
b42408
 NORMALIZABLE_SCHEMES = ('http', 'https', None)
b42408
 
b42408
+_contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f]')
b42408
+
b42408
 
b42408
 class Url(namedtuple('Url', url_attrs)):
b42408
     """
b42408
@@ -155,6 +158,11 @@ def parse_url(url):
b42408
         # Empty
b42408
         return Url()
b42408
 
b42408
+    # Prevent CVE-2019-9740.
b42408
+    # adapted from https://github.com/python/cpython/pull/12755
b42408
+    if _contains_disallowed_url_pchar_re.search(url):
b42408
+        raise LocationParseError("URL can't contain control characters. {!r}".format(url))
b42408
+
b42408
     scheme = None
b42408
     auth = None
b42408
     host = None
b42408
-- 
b42408
2.20.1
b42408
b42408
b42408
From ecc15bd412354ad916712113b0e426f8bc6cf52d Mon Sep 17 00:00:00 2001
b42408
From: Ryan Petrello <lists@ryanpetrello.com>
b42408
Date: Wed, 1 May 2019 16:46:44 -0400
b42408
Subject: [PATCH 2/3] avoid CVE-2019-9740 by percent-encoding invalid path
b42408
 characters
b42408
b42408
this is to avoid breaking changes in downstream libraries like requests
b42408
---
b42408
 test/test_util.py   | 4 ++--
b42408
 src/urllib3/util/url.py | 4 ++--
b42408
 2 files changed, 4 insertions(+), 4 deletions(-)
b42408
b42408
diff --git a/test/test_util.py b/test/test_util.py
b42408
index dc6ffd0..d139329 100644
b42408
--- a/test/test_util.py
b42408
+++ b/test/test_util.py
b42408
@@ -202,8 +202,8 @@ class TestUtil(object):
b42408
 
b42408
     def test_parse_url_contains_control_characters(self):
b42408
         # see CVE-2019-9740
b42408
-        with pytest.raises(LocationParseError):
b42408
-            parse_url('http://localhost:8000/ HTTP/1.1\r\nHEADER: INJECTED\r\nIgnore:')
b42408
+        url = parse_url('http://localhost:8000/ HTTP/1.1\r\nHEADER: INJECTED\r\nIgnore:')
b42408
+        assert url.path == '/%20HTTP/1.1%0D%0AHEADER:%20INJECTED%0D%0AIgnore:'
b42408
 
b42408
     def test_Url_str(self):
b42408
         U = Url('http', host='google.com')
b42408
diff --git a/src/urllib3/util/url.py b/src/urllib3/util/url.py
b42408
index e8e1bd7..12b8d55 100644
b42408
--- a/src/urllib3/util/url.py
b42408
+++ b/src/urllib3/util/url.py
b42408
@@ -3,6 +3,7 @@ from collections import namedtuple
b42408
 import re
b42408
 
b42408
 from ..exceptions import LocationParseError
b42408
+from ..packages.six.moves.urllib.parse import quote
b42408
 
b42408
 
b42408
 url_attrs = ['scheme', 'auth', 'host', 'port', 'path', 'query', 'fragment']
b42408
@@ -160,8 +161,7 @@ def parse_url(url):
b42408
 
b42408
     # Prevent CVE-2019-9740.
b42408
     # adapted from https://github.com/python/cpython/pull/12755
b42408
-    if _contains_disallowed_url_pchar_re.search(url):
b42408
-        raise LocationParseError("URL can't contain control characters. {!r}".format(url))
b42408
+    url = _contains_disallowed_url_pchar_re.sub(lambda match: quote(match.group()), url)
b42408
 
b42408
     scheme = None
b42408
     auth = None
b42408
-- 
b42408
2.20.1
b42408
b42408
b42408
From 6cda449df587fd37135ee76a9253dc8e12e53c05 Mon Sep 17 00:00:00 2001
b42408
From: Seth Michael Larson <sethmichaellarson@gmail.com>
b42408
Date: Thu, 2 May 2019 09:02:24 -0500
b42408
Subject: [PATCH 3/3] Also test unicode and query
b42408
b42408
---
b42408
 test/test_util.py | 22 +++++++++++++++++++---
b42408
 1 file changed, 19 insertions(+), 3 deletions(-)
b42408
b42408
diff --git a/test/test_util.py b/test/test_util.py
b42408
index d139329..fa53aaf 100644
b42408
--- a/test/test_util.py
b42408
+++ b/test/test_util.py
b42408
@@ -200,10 +200,26 @@ class TestUtil(object):
b42408
         with pytest.raises(ValueError):
b42408
             parse_url('[::1')
b42408
 
b42408
-    def test_parse_url_contains_control_characters(self):
b42408
+    @pytest.mark.parametrize('url, expected_url', [
b42408
+        (
b42408
+            'http://localhost/ HTTP/1.1\r\nHEADER: INJECTED\r\nIgnore:',
b42408
+            Url('http', host='localhost', port=None,
b42408
+                path='/%20HTTP/1.1%0D%0AHEADER:%20INJECTED%0D%0AIgnore:')
b42408
+        ),
b42408
+        (
b42408
+            u'http://localhost/ HTTP/1.1\r\nHEADER: INJECTED\r\nIgnore:',
b42408
+            Url('http', host='localhost', port=None,
b42408
+                path='/%20HTTP/1.1%0D%0AHEADER:%20INJECTED%0D%0AIgnore:')
b42408
+        ),
b42408
+        (
b42408
+            'http://localhost/ ?q=\r\n',
b42408
+            Url('http', host='localhost', path='/%20', query='q=%0D%0A')
b42408
+        ),
b42408
+    ])
b42408
+    def test_parse_url_contains_control_characters(self, url, expected_url):
b42408
         # see CVE-2019-9740
b42408
-        url = parse_url('http://localhost:8000/ HTTP/1.1\r\nHEADER: INJECTED\r\nIgnore:')
b42408
-        assert url.path == '/%20HTTP/1.1%0D%0AHEADER:%20INJECTED%0D%0AIgnore:'
b42408
+        url = parse_url(url)
b42408
+        assert url == expected_url
b42408
 
b42408
     def test_Url_str(self):
b42408
         U = Url('http', host='google.com')
b42408
-- 
b42408
2.20.1
b42408