You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
3.2 KiB

  1. From 3fa72516a390fa8e3552007814e8dc1248686eb5 Mon Sep 17 00:00:00 2001
  2. From: Victor Stinner <victor.stinner@gmail.com>
  3. Date: Wed, 22 May 2019 22:15:01 +0200
  4. Subject: [PATCH] bpo-35907, CVE-2019-9948: urllib rejects local_file:// scheme
  5. (GH-13474)
  6. CVE-2019-9948: Avoid file reading as disallowing the unnecessary URL
  7. scheme in URLopener().open() and URLopener().retrieve()
  8. of urllib.request.
  9. Co-Authored-By: SH <push0ebp@gmail.com>
  10. (cherry picked from commit 0c2b6a3943aa7b022e8eb4bfd9bffcddebf9a587)
  11. ---
  12. Lib/test/test_urllib.py | 18 ++++++++++++++++++
  13. Lib/urllib/request.py | 2 +-
  14. .../2019-05-21-23-20-18.bpo-35907.NC_zNK.rst | 2 ++
  15. 3 files changed, 21 insertions(+), 1 deletion(-)
  16. create mode 100644 Misc/NEWS.d/next/Security/2019-05-21-23-20-18.bpo-35907.NC_zNK.rst
  17. diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
  18. index 7214492eca9d..7ec365b928a5 100644
  19. --- a/Lib/test/test_urllib.py
  20. +++ b/Lib/test/test_urllib.py
  21. @@ -16,6 +16,7 @@
  22. ssl = None
  23. import sys
  24. import tempfile
  25. +import warnings
  26. from nturl2path import url2pathname, pathname2url
  27. from base64 import b64encode
  28. @@ -1463,6 +1464,23 @@ def open_spam(self, url):
  29. "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
  30. "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
  31. + def test_local_file_open(self):
  32. + # bpo-35907, CVE-2019-9948: urllib must reject local_file:// scheme
  33. + class DummyURLopener(urllib.request.URLopener):
  34. + def open_local_file(self, url):
  35. + return url
  36. +
  37. + with warnings.catch_warnings(record=True):
  38. + warnings.simplefilter("ignore", DeprecationWarning)
  39. +
  40. + for url in ('local_file://example', 'local-file://example'):
  41. + self.assertRaises(OSError, urllib.request.urlopen, url)
  42. + self.assertRaises(OSError, urllib.request.URLopener().open, url)
  43. + self.assertRaises(OSError, urllib.request.URLopener().retrieve, url)
  44. + self.assertRaises(OSError, DummyURLopener().open, url)
  45. + self.assertRaises(OSError, DummyURLopener().retrieve, url)
  46. +
  47. +
  48. # Just commented them out.
  49. # Can't really tell why keep failing in windows and sparc.
  50. # Everywhere else they work ok, but on those machines, sometimes
  51. diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
  52. index d38f725d8e9f..37b254862887 100644
  53. --- a/Lib/urllib/request.py
  54. +++ b/Lib/urllib/request.py
  55. @@ -1746,7 +1746,7 @@ def open(self, fullurl, data=None):
  56. name = 'open_' + urltype
  57. self.type = urltype
  58. name = name.replace('-', '_')
  59. - if not hasattr(self, name):
  60. + if not hasattr(self, name) or name == 'open_local_file':
  61. if proxy:
  62. return self.open_unknown_proxy(proxy, fullurl, data)
  63. else:
  64. diff --git a/Misc/NEWS.d/next/Security/2019-05-21-23-20-18.bpo-35907.NC_zNK.rst b/Misc/NEWS.d/next/Security/2019-05-21-23-20-18.bpo-35907.NC_zNK.rst
  65. new file mode 100644
  66. index 000000000000..16adc7a94e2f
  67. --- /dev/null
  68. +++ b/Misc/NEWS.d/next/Security/2019-05-21-23-20-18.bpo-35907.NC_zNK.rst
  69. @@ -0,0 +1,2 @@
  70. +CVE-2019-9948: Avoid file reading as disallowing the unnecessary URL scheme in
  71. +``URLopener().open()`` and ``URLopener().retrieve()`` of :mod:`urllib.request`.