CVE-2019-9636.patch 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. From b0305339567b64e07df87620e97e4cb99332aef6 Mon Sep 17 00:00:00 2001
  2. From: Steve Dower <steve.dower@microsoft.com>
  3. Date: Sun, 10 Mar 2019 21:59:24 -0700
  4. Subject: [PATCH] bpo-36216: Add check for characters in netloc that normalize
  5. to separators (GH-12201) (#12223)
  6. CVE: CVE-2019-9636
  7. Upstream-Status: Backport
  8. [https://github.com/python/cpython/commit/c0d95113b070799679bcb9dc49d4960d82e8bb08]
  9. Signed-off-by: Dan Tran <dantran@microsoft.com>
  10. ---
  11. Doc/library/urllib.parse.rst | 18 +++++++++++++++
  12. Lib/test/test_urlparse.py | 23 +++++++++++++++++++
  13. Lib/urllib/parse.py | 17 ++++++++++++++
  14. .../2019-03-06-09-38-40.bpo-36216.6q1m4a.rst | 3 +++
  15. 4 files changed, 61 insertions(+)
  16. create mode 100644 Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst
  17. diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst
  18. index 6f722a8897..a4c6b6726e 100644
  19. --- a/Doc/library/urllib.parse.rst
  20. +++ b/Doc/library/urllib.parse.rst
  21. @@ -120,6 +120,11 @@ or on combining URL components into a URL string.
  22. Unmatched square brackets in the :attr:`netloc` attribute will raise a
  23. :exc:`ValueError`.
  24. + Characters in the :attr:`netloc` attribute that decompose under NFKC
  25. + normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
  26. + ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
  27. + decomposed before parsing, no error will be raised.
  28. +
  29. .. versionchanged:: 3.2
  30. Added IPv6 URL parsing capabilities.
  31. @@ -128,6 +133,10 @@ or on combining URL components into a URL string.
  32. false), in accordance with :rfc:`3986`. Previously, a whitelist of
  33. schemes that support fragments existed.
  34. + .. versionchanged:: 3.5.7
  35. + Characters that affect netloc parsing under NFKC normalization will
  36. + now raise :exc:`ValueError`.
  37. +
  38. .. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace')
  39. @@ -236,6 +245,15 @@ or on combining URL components into a URL string.
  40. Unmatched square brackets in the :attr:`netloc` attribute will raise a
  41. :exc:`ValueError`.
  42. + Characters in the :attr:`netloc` attribute that decompose under NFKC
  43. + normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
  44. + ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
  45. + decomposed before parsing, no error will be raised.
  46. +
  47. + .. versionchanged:: 3.5.7
  48. + Characters that affect netloc parsing under NFKC normalization will
  49. + now raise :exc:`ValueError`.
  50. +
  51. .. function:: urlunsplit(parts)
  52. diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
  53. index e2cf1b7e0f..d0420b0e74 100644
  54. --- a/Lib/test/test_urlparse.py
  55. +++ b/Lib/test/test_urlparse.py
  56. @@ -1,3 +1,5 @@
  57. +import sys
  58. +import unicodedata
  59. import unittest
  60. import urllib.parse
  61. @@ -970,6 +972,27 @@ class UrlParseTestCase(unittest.TestCase):
  62. expected.append(name)
  63. self.assertCountEqual(urllib.parse.__all__, expected)
  64. + def test_urlsplit_normalization(self):
  65. + # Certain characters should never occur in the netloc,
  66. + # including under normalization.
  67. + # Ensure that ALL of them are detected and cause an error
  68. + illegal_chars = '/:#?@'
  69. + hex_chars = {'{:04X}'.format(ord(c)) for c in illegal_chars}
  70. + denorm_chars = [
  71. + c for c in map(chr, range(128, sys.maxunicode))
  72. + if (hex_chars & set(unicodedata.decomposition(c).split()))
  73. + and c not in illegal_chars
  74. + ]
  75. + # Sanity check that we found at least one such character
  76. + self.assertIn('\u2100', denorm_chars)
  77. + self.assertIn('\uFF03', denorm_chars)
  78. +
  79. + for scheme in ["http", "https", "ftp"]:
  80. + for c in denorm_chars:
  81. + url = "{}://netloc{}false.netloc/path".format(scheme, c)
  82. + with self.subTest(url=url, char='{:04X}'.format(ord(c))):
  83. + with self.assertRaises(ValueError):
  84. + urllib.parse.urlsplit(url)
  85. class Utility_Tests(unittest.TestCase):
  86. """Testcase to test the various utility functions in the urllib."""
  87. diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py
  88. index 62e8ddf04b..7ba2b445f5 100644
  89. --- a/Lib/urllib/parse.py
  90. +++ b/Lib/urllib/parse.py
  91. @@ -327,6 +327,21 @@ def _splitnetloc(url, start=0):
  92. delim = min(delim, wdelim) # use earliest delim position
  93. return url[start:delim], url[delim:] # return (domain, rest)
  94. +def _checknetloc(netloc):
  95. + if not netloc or not any(ord(c) > 127 for c in netloc):
  96. + return
  97. + # looking for characters like \u2100 that expand to 'a/c'
  98. + # IDNA uses NFKC equivalence, so normalize for this check
  99. + import unicodedata
  100. + netloc2 = unicodedata.normalize('NFKC', netloc)
  101. + if netloc == netloc2:
  102. + return
  103. + _, _, netloc = netloc.rpartition('@') # anything to the left of '@' is okay
  104. + for c in '/?#@:':
  105. + if c in netloc2:
  106. + raise ValueError("netloc '" + netloc2 + "' contains invalid " +
  107. + "characters under NFKC normalization")
  108. +
  109. def urlsplit(url, scheme='', allow_fragments=True):
  110. """Parse a URL into 5 components:
  111. <scheme>://<netloc>/<path>?<query>#<fragment>
  112. @@ -356,6 +371,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
  113. url, fragment = url.split('#', 1)
  114. if '?' in url:
  115. url, query = url.split('?', 1)
  116. + _checknetloc(netloc)
  117. v = SplitResult(scheme, netloc, url, query, fragment)
  118. _parse_cache[key] = v
  119. return _coerce_result(v)
  120. @@ -379,6 +395,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
  121. url, fragment = url.split('#', 1)
  122. if '?' in url:
  123. url, query = url.split('?', 1)
  124. + _checknetloc(netloc)
  125. v = SplitResult(scheme, netloc, url, query, fragment)
  126. _parse_cache[key] = v
  127. return _coerce_result(v)
  128. diff --git a/Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst b/Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst
  129. new file mode 100644
  130. index 0000000000..5546394157
  131. --- /dev/null
  132. +++ b/Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst
  133. @@ -0,0 +1,3 @@
  134. +Changes urlsplit() to raise ValueError when the URL contains characters that
  135. +decompose under IDNA encoding (NFKC-normalization) into characters that
  136. +affect how the URL is parsed.
  137. --
  138. 2.22.0.vfs.1.1.57.gbaf16c8