CVE-2019-9740.patch 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. From afe3a4975cf93c97e5d6eb8800e48f368011d37a Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= <miro@hroncok.cz>
  3. Date: Sun, 14 Jul 2019 11:07:11 +0200
  4. Subject: [PATCH] bpo-30458: Disallow control chars in http URLs. (GH-12755)
  5. (#13207)
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. Disallow control chars in http URLs in urllib.urlopen. This addresses a potential security problem for applications that do not sanity check their URLs where http request headers could be injected.
  10. Disable https related urllib tests on a build without ssl (GH-13032)
  11. These tests require an SSL enabled build. Skip these tests when python is built without SSL to fix test failures.
  12. Use http.client.InvalidURL instead of ValueError as the new error case's exception. (GH-13044)
  13. Co-Authored-By: Miro Hrončok <miro@hroncok.cz>
  14. Upstream-Status: Backport[https://github.com/python/cpython/commit/afe3a4975cf93c97e5d6eb8800e48f368011d37a]
  15. CVE: CVE-2019-9740
  16. CVE: CVE-2019-9947
  17. Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
  18. ---
  19. Lib/http/client.py | 16 ++++++
  20. Lib/test/test_urllib.py | 55 +++++++++++++++++++
  21. Lib/test/test_xmlrpc.py | 8 ++-
  22. .../2019-04-10-08-53-30.bpo-30458.51E-DA.rst | 1 +
  23. 4 files changed, 79 insertions(+), 1 deletion(-)
  24. create mode 100644 Misc/NEWS.d/next/Security/2019-04-10-08-53-30.bpo-30458.51E-DA.rst
  25. diff --git a/Lib/http/client.py b/Lib/http/client.py
  26. index 352c1017adce..76b9be69a374 100644
  27. --- a/Lib/http/client.py
  28. +++ b/Lib/http/client.py
  29. @@ -141,6 +141,16 @@
  30. _is_legal_header_name = re.compile(rb'[^:\s][^:\r\n]*').fullmatch
  31. _is_illegal_header_value = re.compile(rb'\n(?![ \t])|\r(?![ \t\n])').search
  32. +# These characters are not allowed within HTTP URL paths.
  33. +# See https://tools.ietf.org/html/rfc3986#section-3.3 and the
  34. +# https://tools.ietf.org/html/rfc3986#appendix-A pchar definition.
  35. +# Prevents CVE-2019-9740. Includes control characters such as \r\n.
  36. +# We don't restrict chars above \x7f as putrequest() limits us to ASCII.
  37. +_contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f]')
  38. +# Arguably only these _should_ allowed:
  39. +# _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
  40. +# We are more lenient for assumed real world compatibility purposes.
  41. +
  42. # We always set the Content-Length header for these methods because some
  43. # servers will otherwise respond with a 411
  44. _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
  45. @@ -978,6 +988,12 @@ def putrequest(self, method, url, skip_host=False,
  46. self._method = method
  47. if not url:
  48. url = '/'
  49. + # Prevent CVE-2019-9740.
  50. + match = _contains_disallowed_url_pchar_re.search(url)
  51. + if match:
  52. + raise InvalidURL("URL can't contain control characters. {!r} "
  53. + "(found at least {!r})".format(url,
  54. + match.group()))
  55. request = '%s %s %s' % (method, url, self._http_vsn_str)
  56. # Non-ASCII characters should have been eliminated earlier
  57. diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
  58. index 3afb1312de32..1e2c622e29fd 100644
  59. --- a/Lib/test/test_urllib.py
  60. +++ b/Lib/test/test_urllib.py
  61. @@ -330,6 +330,61 @@ def test_willclose(self):
  62. finally:
  63. self.unfakehttp()
  64. + @unittest.skipUnless(ssl, "ssl module required")
  65. + def test_url_with_control_char_rejected(self):
  66. + for char_no in list(range(0, 0x21)) + [0x7f]:
  67. + char = chr(char_no)
  68. + schemeless_url = "//localhost:7777/test{}/".format(char)
  69. + self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello.")
  70. + try:
  71. + # We explicitly test urllib.request.urlopen() instead of the top
  72. + # level 'def urlopen()' function defined in this... (quite ugly)
  73. + # test suite. They use different url opening codepaths. Plain
  74. + # urlopen uses FancyURLOpener which goes via a codepath that
  75. + # calls urllib.parse.quote() on the URL which makes all of the
  76. + # above attempts at injection within the url _path_ safe.
  77. + escaped_char_repr = repr(char).replace('\\', r'\\')
  78. + InvalidURL = http.client.InvalidURL
  79. + with self.assertRaisesRegex(
  80. + InvalidURL,
  81. + "contain control.*{}".format(escaped_char_repr)):
  82. + urllib.request.urlopen("http:{}".format(schemeless_url))
  83. + with self.assertRaisesRegex(
  84. + InvalidURL,
  85. + "contain control.*{}".format(escaped_char_repr)):
  86. + urllib.request.urlopen("https:{}".format(schemeless_url))
  87. + # This code path quotes the URL so there is no injection.
  88. + resp = urlopen("http:{}".format(schemeless_url))
  89. + self.assertNotIn(char, resp.geturl())
  90. + finally:
  91. + self.unfakehttp()
  92. +
  93. + @unittest.skipUnless(ssl, "ssl module required")
  94. + def test_url_with_newline_header_injection_rejected(self):
  95. + self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello.")
  96. + host = "localhost:7777?a=1 HTTP/1.1\r\nX-injected: header\r\nTEST: 123"
  97. + schemeless_url = "//" + host + ":8080/test/?test=a"
  98. + try:
  99. + # We explicitly test urllib.request.urlopen() instead of the top
  100. + # level 'def urlopen()' function defined in this... (quite ugly)
  101. + # test suite. They use different url opening codepaths. Plain
  102. + # urlopen uses FancyURLOpener which goes via a codepath that
  103. + # calls urllib.parse.quote() on the URL which makes all of the
  104. + # above attempts at injection within the url _path_ safe.
  105. + InvalidURL = http.client.InvalidURL
  106. + with self.assertRaisesRegex(
  107. + InvalidURL, r"contain control.*\\r.*(found at least . .)"):
  108. + urllib.request.urlopen("http:{}".format(schemeless_url))
  109. + with self.assertRaisesRegex(InvalidURL, r"contain control.*\\n"):
  110. + urllib.request.urlopen("https:{}".format(schemeless_url))
  111. + # This code path quotes the URL so there is no injection.
  112. + resp = urlopen("http:{}".format(schemeless_url))
  113. + self.assertNotIn(' ', resp.geturl())
  114. + self.assertNotIn('\r', resp.geturl())
  115. + self.assertNotIn('\n', resp.geturl())
  116. + finally:
  117. + self.unfakehttp()
  118. +
  119. def test_read_0_9(self):
  120. # "0.9" response accepted (but not "simple responses" without
  121. # a status line)
  122. diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py
  123. index c2de057ecbfa..99e510fcee86 100644
  124. --- a/Lib/test/test_xmlrpc.py
  125. +++ b/Lib/test/test_xmlrpc.py
  126. @@ -896,7 +896,13 @@ def test_unicode_host(self):
  127. def test_partial_post(self):
  128. # Check that a partial POST doesn't make the server loop: issue #14001.
  129. conn = http.client.HTTPConnection(ADDR, PORT)
  130. - conn.request('POST', '/RPC2 HTTP/1.0\r\nContent-Length: 100\r\n\r\nbye')
  131. + conn.send('POST /RPC2 HTTP/1.0\r\n'
  132. + 'Content-Length: 100\r\n\r\n'
  133. + 'bye HTTP/1.1\r\n'
  134. + 'Host: {}:{}\r\n'
  135. + 'Accept-Encoding: identity\r\n'
  136. + 'Content-Length: 0\r\n\r\n'
  137. + .format(ADDR, PORT).encode('ascii'))
  138. conn.close()
  139. def test_context_manager(self):
  140. diff --git a/Misc/NEWS.d/next/Security/2019-04-10-08-53-30.bpo-30458.51E-DA.rst b/Misc/NEWS.d/next/Security/2019-04-10-08-53-30.bpo-30458.51E-DA.rst
  141. new file mode 100644
  142. index 000000000000..ed8027fb4d64
  143. --- /dev/null
  144. +++ b/Misc/NEWS.d/next/Security/2019-04-10-08-53-30.bpo-30458.51E-DA.rst
  145. @@ -0,0 +1 @@
  146. +Address CVE-2019-9740 by disallowing URL paths with embedded whitespace or control characters through into the underlying http client request. Such potentially malicious header injection URLs now cause an http.client.InvalidURL exception to be raised.