Skip to content

Commit 4f06dae

Browse files
vstinnerpush0ebp
authored andcommitted
bpo-35907, CVE-2019-9948: urllib rejects local_file:// scheme (GH-13513)
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)
1 parent 8ab624b commit 4f06dae

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
@@ -1463,6 +1464,23 @@ def open_spam(self, url):
14631464
"spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
14641465
"//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
14651466

1467+
def test_local_file_open(self):
1468+
# bpo-35907, CVE-2019-9948: urllib must reject local_file:// scheme
1469+
class DummyURLopener(urllib.request.URLopener):
1470+
def open_local_file(self, url):
1471+
return url
1472+
1473+
with warnings.catch_warnings(record=True):
1474+
warnings.simplefilter("ignore", DeprecationWarning)
1475+
1476+
for url in ('local_file://example', 'local-file://example'):
1477+
self.assertRaises(OSError, urllib.request.urlopen, url)
1478+
self.assertRaises(OSError, urllib.request.URLopener().open, url)
1479+
self.assertRaises(OSError, urllib.request.URLopener().retrieve, url)
1480+
self.assertRaises(OSError, DummyURLopener().open, url)
1481+
self.assertRaises(OSError, DummyURLopener().retrieve, url)
1482+
1483+
14661484
# Just commented them out.
14671485
# Can't really tell why keep failing in windows and sparc.
14681486
# 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
@@ -1747,7 +1747,7 @@ def open(self, fullurl, data=None):
17471747
name = 'open_' + urltype
17481748
self.type = urltype
17491749
name = name.replace('-', '_')
1750-
if not hasattr(self, name):
1750+
if not hasattr(self, name) or name == 'open_local_file':
17511751
if proxy:
17521752
return self.open_unknown_proxy(proxy, fullurl, data)
17531753
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)