Skip to content

Commit 4fe82a8

Browse files
vstinnerpush0ebp
authored andcommitted
bpo-35907, CVE-2019-9948: urllib rejects local_file:// scheme (GH-13474) (GH-13505) (#13510)
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 <[email protected]>
1 parent 43a0ae9 commit 4fe82a8

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/test_urllib.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
ssl = None
1717
import sys
1818
import tempfile
19+
import warnings
1920
from nturl2path import url2pathname, pathname2url
2021

2122
from base64 import b64encode
@@ -1409,6 +1410,23 @@ def open_spam(self, url):
14091410
"spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
14101411
"//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
14111412

1413+
def test_local_file_open(self):
1414+
# bpo-35907, CVE-2019-9948: urllib must reject local_file:// scheme
1415+
class DummyURLopener(urllib.request.URLopener):
1416+
def open_local_file(self, url):
1417+
return url
1418+
1419+
with warnings.catch_warnings(record=True):
1420+
warnings.simplefilter("ignore", DeprecationWarning)
1421+
1422+
for url in ('local_file://example', 'local-file://example'):
1423+
self.assertRaises(OSError, urllib.request.urlopen, url)
1424+
self.assertRaises(OSError, urllib.request.URLopener().open, url)
1425+
self.assertRaises(OSError, urllib.request.URLopener().retrieve, url)
1426+
self.assertRaises(OSError, DummyURLopener().open, url)
1427+
self.assertRaises(OSError, DummyURLopener().retrieve, url)
1428+
1429+
14121430
# Just commented them out.
14131431
# Can't really tell why keep failing in windows and sparc.
14141432
# Everywhere else they work ok, but on those machines, sometimes

Lib/urllib/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,7 @@ def open(self, fullurl, data=None):
16831683
name = 'open_' + urltype
16841684
self.type = urltype
16851685
name = name.replace('-', '_')
1686-
if not hasattr(self, name):
1686+
if not hasattr(self, name) or name == 'open_local_file':
16871687
if proxy:
16881688
return self.open_unknown_proxy(proxy, fullurl, data)
16891689
else:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CVE-2019-9948: Avoid file reading by disallowing ``local-file://`` and
2+
``local_file://`` URL schemes in ``URLopener().open()`` and
3+
``URLopener().retrieve()`` of :mod:`urllib.request`.

0 commit comments

Comments
 (0)