Skip to content

Commit

Permalink
bpo-35907, CVE-2019-9948: urllib rejects local_file:// scheme (GH-13513)
Browse files Browse the repository at this point in the history
CVE-2019-9948: Avoid file reading by disallowing local-file:// and
local_file:// URL schemes in URLopener().open() and
URLopener().retrieve() of urllib.request.

Co-Authored-By: SH <push0ebp@gmail.com>
(cherry picked from commit 0c2b6a3)
(cherry picked from commit 34bab21)
  • Loading branch information
vstinner authored and ned-deily committed May 29, 2019
1 parent 8ab624b commit 4f06dae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
18 changes: 18 additions & 0 deletions Lib/test/test_urllib.py
Expand Up @@ -16,6 +16,7 @@
ssl = None
import sys
import tempfile
import warnings
from nturl2path import url2pathname, pathname2url

from base64 import b64encode
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Lib/urllib/request.py
Expand Up @@ -1747,7 +1747,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:
Expand Down
@@ -0,0 +1,3 @@
CVE-2019-9948: Avoid file reading by disallowing ``local-file://`` and
``local_file://`` URL schemes in ``URLopener().open()`` and
``URLopener().retrieve()`` of :mod:`urllib.request`.

0 comments on commit 4f06dae

Please sign in to comment.