CVE-2018-20852.patch 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. From 31c16d62fc762ab87e66e7f47e36dbfcfc8b5224 Mon Sep 17 00:00:00 2001
  2. From: Xtreak <tir.karthi@gmail.com>
  3. Date: Sun, 17 Mar 2019 05:33:39 +0530
  4. Subject: [PATCH] [3.5] bpo-35121: prefix dot in domain for proper subdomain
  5. validation (GH-10258) (#12281)
  6. Don't send cookies of domain A without Domain attribute to domain B when domain A is a suffix match of domain B while using a cookiejar with `http.cookiejar.DefaultCookiePolicy` policy. Patch by Karthikeyan Singaravelan.
  7. (cherry picked from commit ca7fe5063593958e5efdf90f068582837f07bd14)
  8. Co-authored-by: Xtreak <tir.karthi@gmail.com>
  9. CVE: CVE-2018-20852
  10. Upstream-Status: Backport
  11. [https://github.com/python/cpython/commit/4749f1b69000259e23b4cc6f63c542a9bdc62f1b]
  12. Signed-off-by: Dan Tran <dantran@microsoft.com>
  13. ---
  14. Lib/http/cookiejar.py | 13 ++++++--
  15. Lib/test/test_http_cookiejar.py | 30 +++++++++++++++++++
  16. .../2018-10-31-15-39-17.bpo-35121.EgHv9k.rst | 4 +++
  17. 3 files changed, 45 insertions(+), 2 deletions(-)
  18. create mode 100644 Misc/NEWS.d/next/Security/2018-10-31-15-39-17.bpo-35121.EgHv9k.rst
  19. diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py
  20. index 6d4572af03..1cc9378ae4 100644
  21. --- a/Lib/http/cookiejar.py
  22. +++ b/Lib/http/cookiejar.py
  23. @@ -1148,6 +1148,11 @@ class DefaultCookiePolicy(CookiePolicy):
  24. req_host, erhn = eff_request_host(request)
  25. domain = cookie.domain
  26. + if domain and not domain.startswith("."):
  27. + dotdomain = "." + domain
  28. + else:
  29. + dotdomain = domain
  30. +
  31. # strict check of non-domain cookies: Mozilla does this, MSIE5 doesn't
  32. if (cookie.version == 0 and
  33. (self.strict_ns_domain & self.DomainStrictNonDomain) and
  34. @@ -1160,7 +1165,7 @@ class DefaultCookiePolicy(CookiePolicy):
  35. _debug(" effective request-host name %s does not domain-match "
  36. "RFC 2965 cookie domain %s", erhn, domain)
  37. return False
  38. - if cookie.version == 0 and not ("."+erhn).endswith(domain):
  39. + if cookie.version == 0 and not ("."+erhn).endswith(dotdomain):
  40. _debug(" request-host %s does not match Netscape cookie domain "
  41. "%s", req_host, domain)
  42. return False
  43. @@ -1174,7 +1179,11 @@ class DefaultCookiePolicy(CookiePolicy):
  44. req_host = "."+req_host
  45. if not erhn.startswith("."):
  46. erhn = "."+erhn
  47. - if not (req_host.endswith(domain) or erhn.endswith(domain)):
  48. + if domain and not domain.startswith("."):
  49. + dotdomain = "." + domain
  50. + else:
  51. + dotdomain = domain
  52. + if not (req_host.endswith(dotdomain) or erhn.endswith(dotdomain)):
  53. #_debug(" request domain %s does not match cookie domain %s",
  54. # req_host, domain)
  55. return False
  56. diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py
  57. index 49c01ae489..e67e6ae780 100644
  58. --- a/Lib/test/test_http_cookiejar.py
  59. +++ b/Lib/test/test_http_cookiejar.py
  60. @@ -417,6 +417,7 @@ class CookieTests(unittest.TestCase):
  61. ("http://foo.bar.com/", ".foo.bar.com", True),
  62. ("http://foo.bar.com/", "foo.bar.com", True),
  63. ("http://foo.bar.com/", ".bar.com", True),
  64. + ("http://foo.bar.com/", "bar.com", True),
  65. ("http://foo.bar.com/", "com", True),
  66. ("http://foo.com/", "rhubarb.foo.com", False),
  67. ("http://foo.com/", ".foo.com", True),
  68. @@ -427,6 +428,8 @@ class CookieTests(unittest.TestCase):
  69. ("http://foo/", "foo", True),
  70. ("http://foo/", "foo.local", True),
  71. ("http://foo/", ".local", True),
  72. + ("http://barfoo.com", ".foo.com", False),
  73. + ("http://barfoo.com", "foo.com", False),
  74. ]:
  75. request = urllib.request.Request(url)
  76. r = pol.domain_return_ok(domain, request)
  77. @@ -961,6 +964,33 @@ class CookieTests(unittest.TestCase):
  78. c.add_cookie_header(req)
  79. self.assertFalse(req.has_header("Cookie"))
  80. + c.clear()
  81. +
  82. + pol.set_blocked_domains([])
  83. + req = urllib.request.Request("http://acme.com/")
  84. + res = FakeResponse(headers, "http://acme.com/")
  85. + cookies = c.make_cookies(res, req)
  86. + c.extract_cookies(res, req)
  87. + self.assertEqual(len(c), 1)
  88. +
  89. + req = urllib.request.Request("http://acme.com/")
  90. + c.add_cookie_header(req)
  91. + self.assertTrue(req.has_header("Cookie"))
  92. +
  93. + req = urllib.request.Request("http://badacme.com/")
  94. + c.add_cookie_header(req)
  95. + self.assertFalse(pol.return_ok(cookies[0], req))
  96. + self.assertFalse(req.has_header("Cookie"))
  97. +
  98. + p = pol.set_blocked_domains(["acme.com"])
  99. + req = urllib.request.Request("http://acme.com/")
  100. + c.add_cookie_header(req)
  101. + self.assertFalse(req.has_header("Cookie"))
  102. +
  103. + req = urllib.request.Request("http://badacme.com/")
  104. + c.add_cookie_header(req)
  105. + self.assertFalse(req.has_header("Cookie"))
  106. +
  107. def test_secure(self):
  108. for ns in True, False:
  109. for whitespace in " ", "":
  110. diff --git a/Misc/NEWS.d/next/Security/2018-10-31-15-39-17.bpo-35121.EgHv9k.rst b/Misc/NEWS.d/next/Security/2018-10-31-15-39-17.bpo-35121.EgHv9k.rst
  111. new file mode 100644
  112. index 0000000000..d2eb8f1f35
  113. --- /dev/null
  114. +++ b/Misc/NEWS.d/next/Security/2018-10-31-15-39-17.bpo-35121.EgHv9k.rst
  115. @@ -0,0 +1,4 @@
  116. +Don't send cookies of domain A without Domain attribute to domain B
  117. +when domain A is a suffix match of domain B while using a cookiejar
  118. +with :class:`http.cookiejar.DefaultCookiePolicy` policy. Patch by
  119. +Karthikeyan Singaravelan.
  120. --
  121. 2.22.0.vfs.1.1.57.gbaf16c8