Skip to content

Commit 34bab21

Browse files
vstinnerpush0ebp
andauthored
bpo-35907, CVE-2019-9948: urllib rejects local_file:// scheme (GH-13474) (GH-13505)
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]> (cherry picked from commit 0c2b6a3)
1 parent 81c5ec9 commit 34bab21

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-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
@@ -1746,7 +1746,7 @@ def open(self, fullurl, data=None):
17461746
name = 'open_' + urltype
17471747
self.type = urltype
17481748
name = name.replace('-', '_')
1749-
if not hasattr(self, name):
1749+
if not hasattr(self, name) or name == 'open_local_file':
17501750
if proxy:
17511751
return self.open_unknown_proxy(proxy, fullurl, data)
17521752
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()`` and ``URLopener().retrieve()`` of :mod:`urllib.request`.

0 commit comments

Comments
 (0)