diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 7214492eca9d88..7ec365b928a5cd 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -16,6 +16,7 @@ ssl = None import sys import tempfile +import warnings from nturl2path import url2pathname, pathname2url from base64 import b64encode @@ -1463,6 +1464,23 @@ def open_spam(self, url): "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"), "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/") + def test_local_file_open(self): + # bpo-35907, CVE-2019-9948: urllib must reject local_file:// scheme + class DummyURLopener(urllib.request.URLopener): + def open_local_file(self, url): + return url + + with warnings.catch_warnings(record=True): + warnings.simplefilter("ignore", DeprecationWarning) + + for url in ('local_file://example', 'local-file://example'): + self.assertRaises(OSError, urllib.request.urlopen, url) + self.assertRaises(OSError, urllib.request.URLopener().open, url) + self.assertRaises(OSError, urllib.request.URLopener().retrieve, url) + self.assertRaises(OSError, DummyURLopener().open, url) + self.assertRaises(OSError, DummyURLopener().retrieve, url) + + # Just commented them out. # Can't really tell why keep failing in windows and sparc. # Everywhere else they work ok, but on those machines, sometimes diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index d38f725d8e9fe6..37b2548628878f 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1746,7 +1746,7 @@ def open(self, fullurl, data=None): name = 'open_' + urltype self.type = urltype name = name.replace('-', '_') - if not hasattr(self, name): + 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/Security/2019-05-21-23-20-18.bpo-35907.NC_zNK.rst b/Misc/NEWS.d/next/Security/2019-05-21-23-20-18.bpo-35907.NC_zNK.rst new file mode 100644 index 00000000000000..16adc7a94e2f5f --- /dev/null +++ b/Misc/NEWS.d/next/Security/2019-05-21-23-20-18.bpo-35907.NC_zNK.rst @@ -0,0 +1,2 @@ +CVE-2019-9948: Avoid file reading as disallowing the unnecessary URL scheme in +``URLopener().open()`` and ``URLopener().retrieve()`` of :mod:`urllib.request`.