Skip to contentSkip to conversation

package/python: add upstream security fix for CVE-2019-9948

PATCH New Peter Korsgaard 2 participants 7 messages Jun 16, 2019 – Jun 23, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 package/python: add upstream security fix for CVE-2019-9948

Fixes CVE-2019-9948: Unnecessary URL scheme exists to allow file:// reading file in urllib.

https://bugs.python.org/issue35907

---
 ...VE-2019-9948-urllib-rejects-local_file-sc.patch | 59 ++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 package/python/0035-bpo-35907-CVE-2019-9948-urllib-rejects-local_file-sc.patch
package/python/0035-bpo-35907-CVE-2019-9948-urllib-rejects-local_file-sc.patch new file +59 -0
new file mode 100644
index 0000000000..5ca1433465
@@ -0,0 +1,59 @@
+From b15bde8058e821b383d81fcae68b335a752083ca Mon Sep 17 00:00:00 2001
+From: SH <[email protected]>
+Date: Wed, 22 May 2019 06:12:23 +0900
+Subject: [PATCH] bpo-35907, CVE-2019-9948: urllib rejects local_file:// scheme
+  (GH-11842)
+
+ CVE-2019-9948: Avoid file reading as disallowing the unnecessary URL scheme in urllib.urlopen().
+
+Signed-off-by: Peter Korsgaard <[email protected]>
+---
+ Lib/test/test_urllib.py                                           | 7 +++++++
+ Lib/urllib.py                                                     | 4 +++-
+ Misc/NEWS.d/next/Library/2019-02-13-17-21-10.bpo-35907.ckk2zg.rst | 1 +
+ 3 files changed, 11 insertions(+), 1 deletion(-)
+ create mode 100644 Misc/NEWS.d/next/Library/2019-02-13-17-21-10.bpo-35907.ckk2zg.rst
+
+diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
+index d7778d4194..ae1f6c0b29 100644
+--- a/Lib/test/test_urllib.py
++++ b/Lib/test/test_urllib.py
+@@ -1048,6 +1048,13 @@ class URLopener_Tests(unittest.TestCase):
+             "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
+             "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
+ 
++    def test_local_file_open(self):
++        class DummyURLopener(urllib.URLopener):
++            def open_local_file(self, url):
++                return url
++        for url in ('local_file://example', 'local-file://example'):
++            self.assertRaises(IOError, DummyURLopener().open, url)
++            self.assertRaises(IOError, urllib.urlopen, url)
+ 
+ # Just commented them out.
+ # Can't really tell why keep failing in windows and sparc.
+diff --git a/Lib/urllib.py b/Lib/urllib.py
+index d85504a5cb..156879dd0a 100644
+--- a/Lib/urllib.py
++++ b/Lib/urllib.py
+@@ -203,7 +203,9 @@ class URLopener:
+         name = 'open_' + urltype
+         self.type = urltype
+         name = name.replace('-', '_')
+-        if not hasattr(self, name):
++
++        # bpo-35907: disallow the file reading with the type not allowed
++        if not hasattr(self, name) or name == 'open_local_file':
+             if proxy:
+                 return self.open_unknown_proxy(proxy, fullurl, data)
+             else:
+diff --git a/Misc/NEWS.d/next/Library/2019-02-13-17-21-10.bpo-35907.ckk2zg.rst b/Misc/NEWS.d/next/Library/2019-02-13-17-21-10.bpo-35907.ckk2zg.rst
+new file mode 100644
+index 0000000000..bb187d8d65
+--- /dev/null
++++ b/Misc/NEWS.d/next/Library/2019-02-13-17-21-10.bpo-35907.ckk2zg.rst
+@@ -0,0 +1 @@
++CVE-2019-9948: Avoid file reading as disallowing the unnecessary URL scheme in urllib.urlopen
+-- 
+2.11.0
+
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/3 package/python: add upstream security fix for CVE-2019-9948

Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Peter" == Peter Korsgaard <[email protected]> writes:

Fixes CVE-2019-9948: Unnecessary URL scheme exists to allow file:// reading file in urllib.

https://bugs.python.org/issue35907

Committed to 2019.02.x and 2019.05.x, thanks.

Signature · 1 line
Bye, Peter Korsgaard
#2 lore raw

PATCH 2/3 package/python: add upstream security fix for CVE-2019-9636

In reply to Peter Korsgaard

Fixes CVE-2019-9636: urlsplit does not handle NFKC normalization

https://bugs.python.org/issue36216

The fix unfortunately introduced regressions, so also apply the followup fixes.

https://bugs.python.org/issue36742

---
 ...dd-check-for-characters-in-netloc-that-no.patch | 159 +++++++++++++++++++++
 ...16-Only-print-test-messages-when-verbose-.patch |  28 ++++
 ...ixes-handling-of-pre-normalization-charac.patch |  66 +++++++++
 ...orrects-fix-to-handle-decomposition-in-us.patch |  67 +++++++++
 ...42-Fix-urlparse.urlsplit-error-message-fo.patch |  67 +++++++++
 5 files changed, 387 insertions(+)
 create mode 100644 package/python/0036-bpo-36216-Add-check-for-characters-in-netloc-that-no.patch
 create mode 100644 package/python/0037-3.7-bpo-36216-Only-print-test-messages-when-verbose-.patch
 create mode 100644 package/python/0038-bpo-36742-Fixes-handling-of-pre-normalization-charac.patch
 create mode 100644 package/python/0039-bpo-36742-Corrects-fix-to-handle-decomposition-in-us.patch
 create mode 100644 package/python/0040-2.7-bpo-36742-Fix-urlparse.urlsplit-error-message-fo.patch
package/python/0036-bpo-36216-Add-check-for-characters-in-netloc-that-no.patch new file +159 -0
new file mode 100644
index 0000000000..3b61144713
@@ -0,0 +1,159 @@
+From e37ef41289b77e0f0bb9a6aedb0360664c55bdd5 Mon Sep 17 00:00:00 2001
+From: Steve Dower <[email protected]>
+Date: Thu, 7 Mar 2019 09:08:45 -0800
+Subject: [PATCH] bpo-36216: Add check for characters in netloc that normalize
+ to separators (GH-12201)
+
+Signed-off-by: Peter Korsgaard <[email protected]>
+---
+ Doc/library/urlparse.rst                           | 20 ++++++++++++++++++
+ Lib/test/test_urlparse.py                          | 24 ++++++++++++++++++++++
+ Lib/urlparse.py                                    | 17 +++++++++++++++
+ .../2019-03-06-09-38-40.bpo-36216.6q1m4a.rst       |  3 +++
+ 4 files changed, 64 insertions(+)
+ create mode 100644 Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst
+
+diff --git a/Doc/library/urlparse.rst b/Doc/library/urlparse.rst
+index 22249da54f..0989c88c30 100644
+--- a/Doc/library/urlparse.rst
++++ b/Doc/library/urlparse.rst
+@@ -119,12 +119,22 @@ The :mod:`urlparse` module defines the following functions:
+    See section :ref:`urlparse-result-object` for more information on the result
+    object.
+ 
++   Characters in the :attr:`netloc` attribute that decompose under NFKC
++   normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
++   ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
++   decomposed before parsing, or is not a Unicode string, no error will be
++   raised.
++
+    .. versionchanged:: 2.5
+       Added attributes to return value.
+ 
+    .. versionchanged:: 2.7
+       Added IPv6 URL parsing capabilities.
+ 
++   .. versionchanged:: 2.7.17
++      Characters that affect netloc parsing under NFKC normalization will
++      now raise :exc:`ValueError`.
++
+ 
+ .. function:: parse_qs(qs[, keep_blank_values[, strict_parsing[, max_num_fields]]])
+ 
+@@ -232,11 +242,21 @@ The :mod:`urlparse` module defines the following functions:
+    See section :ref:`urlparse-result-object` for more information on the result
+    object.
+ 
++   Characters in the :attr:`netloc` attribute that decompose under NFKC
++   normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
++   ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
++   decomposed before parsing, or is not a Unicode string, no error will be
++   raised.
++
+    .. versionadded:: 2.2
+ 
+    .. versionchanged:: 2.5
+       Added attributes to return value.
+ 
++   .. versionchanged:: 2.7.17
++      Characters that affect netloc parsing under NFKC normalization will
++      now raise :exc:`ValueError`.
++
+ 
+ .. function:: urlunsplit(parts)
+ 
+diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
+index 4e1ded73c2..73b0228ea8 100644
+--- a/Lib/test/test_urlparse.py
++++ b/Lib/test/test_urlparse.py
+@@ -1,4 +1,6 @@
+ from test import test_support
++import sys
++import unicodedata
+ import unittest
+ import urlparse
+ 
+@@ -624,6 +626,28 @@ class UrlParseTestCase(unittest.TestCase):
+         self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
+                 ('http','www.python.org:80','','','',''))
+ 
++    def test_urlsplit_normalization(self):
++        # Certain characters should never occur in the netloc,
++        # including under normalization.
++        # Ensure that ALL of them are detected and cause an error
++        illegal_chars = u'/:#?@'
++        hex_chars = {'{:04X}'.format(ord(c)) for c in illegal_chars}
++        denorm_chars = [
++            c for c in map(unichr, range(128, sys.maxunicode))
++            if (hex_chars & set(unicodedata.decomposition(c).split()))
++            and c not in illegal_chars
++        ]
++        # Sanity check that we found at least one such character
++        self.assertIn(u'\u2100', denorm_chars)
++        self.assertIn(u'\uFF03', denorm_chars)
++
++        for scheme in [u"http", u"https", u"ftp"]:
++            for c in denorm_chars:
++                url = u"{}://netloc{}false.netloc/path".format(scheme, c)
++                print "Checking %r" % url
++                with self.assertRaises(ValueError):
++                    urlparse.urlsplit(url)
++
+ def test_main():
+     test_support.run_unittest(UrlParseTestCase)
+ 
+diff --git a/Lib/urlparse.py b/Lib/urlparse.py
+index f7c2b032b0..54eda08651 100644
+--- a/Lib/urlparse.py
++++ b/Lib/urlparse.py
+@@ -165,6 +165,21 @@ def _splitnetloc(url, start=0):
+             delim = min(delim, wdelim)     # use earliest delim position
+     return url[start:delim], url[delim:]   # return (domain, rest)
+ 
++def _checknetloc(netloc):
++    if not netloc or not isinstance(netloc, unicode):
++        return
++    # looking for characters like \u2100 that expand to 'a/c'
++    # IDNA uses NFKC equivalence, so normalize for this check
++    import unicodedata
++    netloc2 = unicodedata.normalize('NFKC', netloc)
++    if netloc == netloc2:
++        return
++    _, _, netloc = netloc.rpartition('@') # anything to the left of '@' is okay
++    for c in '/?#@:':
++        if c in netloc2:
++            raise ValueError("netloc '" + netloc2 + "' contains invalid " +
++                             "characters under NFKC normalization")
++
+ def urlsplit(url, scheme='', allow_fragments=True):
+     """Parse a URL into 5 components:
+     <scheme>://<netloc>/<path>?<query>#<fragment>
+@@ -193,6 +208,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
+                 url, fragment = url.split('#', 1)
+             if '?' in url:
+                 url, query = url.split('?', 1)
++            _checknetloc(netloc)
+             v = SplitResult(scheme, netloc, url, query, fragment)
+             _parse_cache[key] = v
+             return v
+@@ -216,6 +232,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
+         url, fragment = url.split('#', 1)
+     if '?' in url:
+         url, query = url.split('?', 1)
++    _checknetloc(netloc)
+     v = SplitResult(scheme, netloc, url, query, fragment)
+     _parse_cache[key] = v
+     return v
+diff --git a/Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst b/Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst
+new file mode 100644
+index 0000000000..1e1ad92c6f
+--- /dev/null
++++ b/Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst
+@@ -0,0 +1,3 @@
++Changes urlsplit() to raise ValueError when the URL contains characters that
++decompose under IDNA encoding (NFKC-normalization) into characters that
++affect how the URL is parsed.
+\ No newline at end of file
+-- 
+2.11.0
+
package/python/0037-3.7-bpo-36216-Only-print-test-messages-when-verbose-.patch new file +28 -0
new file mode 100644
index 0000000000..7e61fceb80
@@ -0,0 +1,28 @@
+From 507bd8cde60ced74d13a1ffa883bb9b0e73c38be Mon Sep 17 00:00:00 2001
+From: Steve Dower <[email protected]>
+Date: Tue, 12 Mar 2019 13:51:58 -0700
+Subject: [PATCH] [3.7] bpo-36216: Only print test messages when verbose
+ (GH-12291)
+
+Signed-off-by: Peter Korsgaard <[email protected]>
+---
+ Lib/test/test_urlparse.py | 3 ++-
+ 1 file changed, 2 insertions(+), 1 deletion(-)
+
+diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
+index 73b0228ea8..1830d0b286 100644
+--- a/Lib/test/test_urlparse.py
++++ b/Lib/test/test_urlparse.py
+@@ -644,7 +644,8 @@ class UrlParseTestCase(unittest.TestCase):
+         for scheme in [u"http", u"https", u"ftp"]:
+             for c in denorm_chars:
+                 url = u"{}://netloc{}false.netloc/path".format(scheme, c)
+-                print "Checking %r" % url
++                if test_support.verbose:
++                    print "Checking %r" % url
+                 with self.assertRaises(ValueError):
+                     urlparse.urlsplit(url)
+ 
+-- 
+2.11.0
+
package/python/0038-bpo-36742-Fixes-handling-of-pre-normalization-charac.patch new file +66 -0

66 lines not on this page · raw message

package/python/0039-bpo-36742-Corrects-fix-to-handle-decomposition-in-us.patch new file +67 -0

67 lines not on this page · raw message

package/python/0040-2.7-bpo-36742-Fix-urlparse.urlsplit-error-message-fo.patch new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
2.11.0
#3 lore raw

PATCH 2/3 package/python: add upstream security fix for CVE-2019-9636

Quoted 11 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Peter" == Peter Korsgaard <[email protected]> writes:

Fixes CVE-2019-9636: urlsplit does not handle NFKC normalization
https://bugs.python.org/issue36216

The fix unfortunately introduced regressions, so also apply the followup fixes.

https://bugs.python.org/issue36742

Committed to 2019.02.x and 2019.05.x, thanks.

Signature · 1 line
Bye, Peter Korsgaard
#4 lore raw

PATCH 1/3 package/python: add upstream security fix for CVE-2019-9948

In reply to Peter Korsgaard
Quoted 10 lines · On Sun, 16 Jun 2019 23:17:09 +0200 Peter Korsgaard <[email protected]> wrote:

Fixes CVE-2019-9948: Unnecessary URL scheme exists to allow file:// reading file in urllib.

https://bugs.python.org/issue35907

---
 ...VE-2019-9948-urllib-rejects-local_file-sc.patch | 59 ++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 package/python/0035-bpo-35907-CVE-2019-9948-urllib-rejects-local_file-sc.patch

All three patches applied to master. Thanks!

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#5 lore raw

PATCH 3/3 package/python3: add upstream security fix for CVE-2019-10160

In reply to Peter Korsgaard

Fixes CVE-2019-10160: urlsplit does not handle NFKC normalization (2nd fix)

While the fix for CVE-2019-9936 is included in 3.7.3, the followup regression fixes unfortunatly aren't.

https://bugs.python.org/issue36742

---
 ...ixes-handling-of-pre-normalization-charac.patch | 70 ++++++++++++++++++++++
 ...orrects-fix-to-handle-decomposition-in-us.patch | 58 ++++++++++++++++++
 2 files changed, 128 insertions(+)
 create mode 100644 package/python3/0033-bpo-36742-Fixes-handling-of-pre-normalization-charac.patch
 create mode 100644 package/python3/0034-bpo-36742-Corrects-fix-to-handle-decomposition-in-us.patch
package/python3/0033-bpo-36742-Fixes-handling-of-pre-normalization-charac.patch new file +70 -0
new file mode 100644
index 0000000000..38f8ed625b
@@ -0,0 +1,70 @@
+From 4d723e76e1ad17e9e7d5e828e59bb47e76f2174b Mon Sep 17 00:00:00 2001
+From: "Miss Islington (bot)"
+ <[email protected]>
+Date: Tue, 30 Apr 2019 05:21:02 -0700
+Subject: [PATCH] bpo-36742: Fixes handling of pre-normalization characters in
+ urlsplit() (GH-13017)
+
+(cherry picked from commit d537ab0ff9767ef024f26246899728f0116b1ec3)
+
+Co-authored-by: Steve Dower <[email protected]>
+Signed-off-by: Peter Korsgaard <[email protected]>
+---
+ Lib/test/test_urlparse.py                                     |  6 ++++++
+ Lib/urllib/parse.py                                           | 11 +++++++----
+ .../next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst    |  1 +
+ 3 files changed, 14 insertions(+), 4 deletions(-)
+ create mode 100644 Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst
+
+diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
+index e6638aee22..c262354494 100644
+--- a/Lib/test/test_urlparse.py
++++ b/Lib/test/test_urlparse.py
+@@ -1001,6 +1001,12 @@ class UrlParseTestCase(unittest.TestCase):
+         self.assertIn('\u2100', denorm_chars)
+         self.assertIn('\uFF03', denorm_chars)
+ 
++        # bpo-36742: Verify port separators are ignored when they
++        # existed prior to decomposition
++        urllib.parse.urlsplit('http://\u30d5\u309a:80')
++        with self.assertRaises(ValueError):
++            urllib.parse.urlsplit('http://\u30d5\u309a\ufe1380')
++
+         for scheme in ["http", "https", "ftp"]:
+             for c in denorm_chars:
+                 url = "{}://netloc{}false.netloc/path".format(scheme, c)
+diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py
+index 1eec26e0f1..f5b3487ea9 100644
+--- a/Lib/urllib/parse.py
++++ b/Lib/urllib/parse.py
+@@ -397,13 +397,16 @@ def _checknetloc(netloc):
+     # looking for characters like \u2100 that expand to 'a/c'
+     # IDNA uses NFKC equivalence, so normalize for this check
+     import unicodedata
+-    netloc2 = unicodedata.normalize('NFKC', netloc)
+-    if netloc == netloc2:
++    n = netloc.rpartition('@')[2] # ignore anything to the left of '@'
++    n = n.replace(':', '')        # ignore characters already included
++    n = n.replace('#', '')        # but not the surrounding text
++    n = n.replace('?', '')
++    netloc2 = unicodedata.normalize('NFKC', n)
++    if n == netloc2:
+         return
+-    _, _, netloc = netloc.rpartition('@') # anything to the left of '@' is okay
+     for c in '/?#@:':
+         if c in netloc2:
+-            raise ValueError("netloc '" + netloc2 + "' contains invalid " +
++            raise ValueError("netloc '" + netloc + "' contains invalid " +
+                              "characters under NFKC normalization")
+ 
+ def urlsplit(url, scheme='', allow_fragments=True):
+diff --git a/Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst b/Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst
+new file mode 100644
+index 0000000000..d729ed2f3c
+--- /dev/null
++++ b/Misc/NEWS.d/next/Security/2019-04-29-15-34-59.bpo-36742.QCUY0i.rst
+@@ -0,0 +1 @@
++Fixes mishandling of pre-normalization characters in urlsplit().
+-- 
+2.11.0
+
package/python3/0034-bpo-36742-Corrects-fix-to-handle-decomposition-in-us.patch new file +58 -0

58 lines not on this page · raw message

Signature · 1 line
2.11.0
#6 lore raw

PATCH 3/3 package/python3: add upstream security fix for CVE-2019-10160

Quoted 9 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Peter" == Peter Korsgaard <[email protected]> writes:

Fixes CVE-2019-10160: urlsplit does not handle NFKC normalization (2nd fix) While the fix for CVE-2019-9936 is included in 3.7.3, the followup regression fixes unfortunatly aren't.

https://bugs.python.org/issue36742

Committed to 2019.02.x and 2019.05.x, thanks.

Signature · 1 line
Bye, Peter Korsgaard