Skip to content

Commit

Permalink
bpo-35907, CVE-2019-9948: urllib rejects local_file:// scheme (python…
Browse files Browse the repository at this point in the history
…GH-13474)

CVE-2019-9948: Avoid file reading as disallowing the unnecessary URL
scheme in URLopener().open() and URLopener().retrieve()
of urllib.request.

Co-Authored-By: SH <[email protected]>
  • Loading branch information
vstinner and push0ebp authored May 22, 2019
1 parent 2ddbd21 commit 0c2b6a3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,19 @@ def test_urlopener_retrieve_remote(self):
filename, _ = urllib.request.URLopener().retrieve(url)
self.assertEqual(os.path.splitext(filename)[1], ".txt")

@support.ignore_warnings(category=DeprecationWarning)
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
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.
Expand Down
2 changes: 1 addition & 1 deletion Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CVE-2019-9948: Avoid file reading as disallowing the unnecessary URL scheme in
``URLopener().open()`` ``URLopener().retrieve()`` of :mod:`urllib.request`.

0 comments on commit 0c2b6a3

Please sign in to comment.