Skip to content

Commit 0c2b6a3

Browse files
vstinnerpush0ebp
andauthored
bpo-35907, CVE-2019-9948: urllib rejects local_file:// scheme (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]>
1 parent 2ddbd21 commit 0c2b6a3

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Lib/test/test_urllib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,19 @@ def test_urlopener_retrieve_remote(self):
14811481
filename, _ = urllib.request.URLopener().retrieve(url)
14821482
self.assertEqual(os.path.splitext(filename)[1], ".txt")
14831483

1484+
@support.ignore_warnings(category=DeprecationWarning)
1485+
def test_local_file_open(self):
1486+
# bpo-35907, CVE-2019-9948: urllib must reject local_file:// scheme
1487+
class DummyURLopener(urllib.request.URLopener):
1488+
def open_local_file(self, url):
1489+
return url
1490+
for url in ('local_file://example', 'local-file://example'):
1491+
self.assertRaises(OSError, urllib.request.urlopen, url)
1492+
self.assertRaises(OSError, urllib.request.URLopener().open, url)
1493+
self.assertRaises(OSError, urllib.request.URLopener().retrieve, url)
1494+
self.assertRaises(OSError, DummyURLopener().open, url)
1495+
self.assertRaises(OSError, DummyURLopener().retrieve, url)
1496+
14841497

14851498
# Just commented them out.
14861499
# Can't really tell why keep failing in windows and sparc.

Lib/urllib/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,7 @@ def open(self, fullurl, data=None):
17451745
name = 'open_' + urltype
17461746
self.type = urltype
17471747
name = name.replace('-', '_')
1748-
if not hasattr(self, name):
1748+
if not hasattr(self, name) or name == 'open_local_file':
17491749
if proxy:
17501750
return self.open_unknown_proxy(proxy, fullurl, data)
17511751
else:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CVE-2019-9948: Avoid file reading as disallowing the unnecessary URL scheme in
2+
``URLopener().open()`` ``URLopener().retrieve()`` of :mod:`urllib.request`.

0 commit comments

Comments
 (0)