A feature-rich command-line audio/video downloader
[YoutubeDL] urlopen: disable the 'file:' protocol (#8227)
Jaime Marquínez Ferrándize37afbe0b8a1
diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.pyindex 0388c0bf32..0caa43843a 100644--- a/test/test_YoutubeDL.py+++ b/test/test_YoutubeDL.py@@ -12,7 +12,7 @@ import copyfrom test.helper import FakeYDL, assertRegexpMatchesfrom youtube_dl import YoutubeDL-from youtube_dl.compat import compat_str+from youtube_dl.compat import compat_str, compat_urllib_errorfrom youtube_dl.extractor import YoutubeIEfrom youtube_dl.postprocessor.common import PostProcessorfrom youtube_dl.utils import ExtractorError, match_filter_func@@ -631,6 +631,11 @@ class TestYoutubeDL(unittest.TestCase):result = get_ids({'playlist_items': '10'})self.assertEqual(result, [])+ def test_urlopen_no_file_protocol(self):+ # see https://github.com/rg3/youtube-dl/issues/8227+ ydl = YDL()+ self.assertRaises(compat_urllib_error.URLError, ydl.urlopen, 'file:///etc/passwd')+if __name__ == '__main__':unittest.main()diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.pyindex d50b7cfed3..e8ce586042 100755--- a/youtube_dl/YoutubeDL.py+++ b/youtube_dl/YoutubeDL.py@@ -1986,8 +1986,14 @@ class YoutubeDL(object):https_handler = make_HTTPS_handler(self.params, debuglevel=debuglevel)ydlh = YoutubeDLHandler(self.params, debuglevel=debuglevel)data_handler = compat_urllib_request_DataHandler()- opener = compat_urllib_request.build_opener(- proxy_handler, https_handler, cookie_processor, ydlh, data_handler)+ unknown_handler = compat_urllib_request.UnknownHandler()+ handlers = (proxy_handler, https_handler, cookie_processor, ydlh, data_handler, unknown_handler)+ # we don't use build_opener because it automatically adds FileHandler,+ # which can be used for malicious purposes (see+ # https://github.com/rg3/youtube-dl/issues/8227)+ opener = compat_urllib_request.OpenerDirector()+ for handler in handlers:+ opener.add_handler(handler)# Delete the default user-agent header, which would otherwise apply in# cases where our custom HTTP handler doesn't come into play