|
| 1 | +From fe5bddd7ac531140101cdf82aa952c7e3dd6c97d Mon Sep 17 00:00:00 2001 |
| 2 | +From: Seth Larson <seth@python.org> |
| 3 | +Date: Wed, 22 Apr 2026 14:22:31 -0500 |
| 4 | +Subject: [PATCH] gh-90309: Base64-encode cookie values embedded in JS (cherry |
| 5 | + picked from commit 76b3923d688c0efc580658476c5f525ec8735104) |
| 6 | + |
| 7 | +Co-authored-by: Seth Larson <seth@python.org> |
| 8 | +--- |
| 9 | + Lib/http/cookies.py | 8 +++-- |
| 10 | + Lib/test/test_http_cookies.py | 29 ++++++++++++------- |
| 11 | + ...6-04-21-13-46-30.gh-issue-90309.srvj9q.rst | 3 ++ |
| 12 | + 3 files changed, 27 insertions(+), 13 deletions(-) |
| 13 | + create mode 100644 Misc/NEWS.d/next/Security/2026-04-21-13-46-30.gh-issue-90309.srvj9q.rst |
| 14 | + |
| 15 | +diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py |
| 16 | +index d5b8ba939bee7c..5c5b14788dc2f0 100644 |
| 17 | +--- a/Lib/http/cookies.py |
| 18 | ++++ b/Lib/http/cookies.py |
| 19 | +@@ -391,17 +391,21 @@ def __repr__(self): |
| 20 | + return '<%s: %s>' % (self.__class__.__name__, self.OutputString()) |
| 21 | + |
| 22 | + def js_output(self, attrs=None): |
| 23 | ++ import base64 |
| 24 | + # Print javascript |
| 25 | + output_string = self.OutputString(attrs) |
| 26 | + if _has_control_character(output_string): |
| 27 | + raise CookieError("Control characters are not allowed in cookies") |
| 28 | ++ # Base64-encode value to avoid template |
| 29 | ++ # injection in cookie values. |
| 30 | ++ output_encoded = base64.b64encode(output_string.encode('utf-8')).decode("ascii") |
| 31 | + return """ |
| 32 | + <script type="text/javascript"> |
| 33 | + <!-- begin hiding |
| 34 | +- document.cookie = \"%s\"; |
| 35 | ++ document.cookie = atob(\"%s\"); |
| 36 | + // end hiding --> |
| 37 | + </script> |
| 38 | +- """ % (output_string.replace('"', r'\"')) |
| 39 | ++ """ % (output_encoded,) |
| 40 | + |
| 41 | + def OutputString(self, attrs=None): |
| 42 | + # Build up our result |
| 43 | +diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py |
| 44 | +index 33da395717deb0..4884b07c95b9c5 100644 |
| 45 | +--- a/Lib/test/test_http_cookies.py |
| 46 | ++++ b/Lib/test/test_http_cookies.py |
| 47 | +@@ -1,5 +1,5 @@ |
| 48 | + # Simple test suite for http/cookies.py |
| 49 | +- |
| 50 | ++import base64 |
| 51 | + import copy |
| 52 | + import unittest |
| 53 | + import doctest |
| 54 | +@@ -152,17 +152,19 @@ def test_load(self): |
| 55 | + |
| 56 | + self.assertEqual(C.output(['path']), |
| 57 | + 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') |
| 58 | +- self.assertEqual(C.js_output(), r""" |
| 59 | ++ cookie_encoded = base64.b64encode(b'Customer="WILE_E_COYOTE"; Path=/acme; Version=1').decode('ascii') |
| 60 | ++ self.assertEqual(C.js_output(), fr""" |
| 61 | + <script type="text/javascript"> |
| 62 | + <!-- begin hiding |
| 63 | +- document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1"; |
| 64 | ++ document.cookie = atob("{cookie_encoded}"); |
| 65 | + // end hiding --> |
| 66 | + </script> |
| 67 | + """) |
| 68 | +- self.assertEqual(C.js_output(['path']), r""" |
| 69 | ++ cookie_encoded = base64.b64encode(b'Customer="WILE_E_COYOTE"; Path=/acme').decode('ascii') |
| 70 | ++ self.assertEqual(C.js_output(['path']), fr""" |
| 71 | + <script type="text/javascript"> |
| 72 | + <!-- begin hiding |
| 73 | +- document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme"; |
| 74 | ++ document.cookie = atob("{cookie_encoded}"); |
| 75 | + // end hiding --> |
| 76 | + </script> |
| 77 | + """) |
| 78 | +@@ -267,17 +269,19 @@ def test_quoted_meta(self): |
| 79 | + |
| 80 | + self.assertEqual(C.output(['path']), |
| 81 | + 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') |
| 82 | +- self.assertEqual(C.js_output(), r""" |
| 83 | ++ expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1').decode('ascii') |
| 84 | ++ self.assertEqual(C.js_output(), fr""" |
| 85 | + <script type="text/javascript"> |
| 86 | + <!-- begin hiding |
| 87 | +- document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1"; |
| 88 | ++ document.cookie = atob("{expected_encoded_cookie}"); |
| 89 | + // end hiding --> |
| 90 | + </script> |
| 91 | + """) |
| 92 | +- self.assertEqual(C.js_output(['path']), r""" |
| 93 | ++ expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme').decode('ascii') |
| 94 | ++ self.assertEqual(C.js_output(['path']), fr""" |
| 95 | + <script type="text/javascript"> |
| 96 | + <!-- begin hiding |
| 97 | +- document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme"; |
| 98 | ++ document.cookie = atob("{expected_encoded_cookie}"); |
| 99 | + // end hiding --> |
| 100 | + </script> |
| 101 | + """) |
| 102 | +@@ -368,13 +372,16 @@ def test_setter(self): |
| 103 | + self.assertEqual( |
| 104 | + M.output(), |
| 105 | + "Set-Cookie: %s=%s; Path=/foo" % (i, "%s_coded_val" % i)) |
| 106 | ++ expected_encoded_cookie = base64.b64encode( |
| 107 | ++ ("%s=%s; Path=/foo" % (i, "%s_coded_val" % i)).encode("ascii") |
| 108 | ++ ).decode('ascii') |
| 109 | + expected_js_output = """ |
| 110 | + <script type="text/javascript"> |
| 111 | + <!-- begin hiding |
| 112 | +- document.cookie = "%s=%s; Path=/foo"; |
| 113 | ++ document.cookie = atob("%s"); |
| 114 | + // end hiding --> |
| 115 | + </script> |
| 116 | +- """ % (i, "%s_coded_val" % i) |
| 117 | ++ """ % (expected_encoded_cookie,) |
| 118 | + self.assertEqual(M.js_output(), expected_js_output) |
| 119 | + for i in ["foo bar", "foo@bar"]: |
| 120 | + # Try some illegal characters |
| 121 | +diff --git a/Misc/NEWS.d/next/Security/2026-04-21-13-46-30.gh-issue-90309.srvj9q.rst b/Misc/NEWS.d/next/Security/2026-04-21-13-46-30.gh-issue-90309.srvj9q.rst |
| 122 | +new file mode 100644 |
| 123 | +index 00000000000000..d7d376737e4ad1 |
| 124 | +--- /dev/null |
| 125 | ++++ b/Misc/NEWS.d/next/Security/2026-04-21-13-46-30.gh-issue-90309.srvj9q.rst |
| 126 | +@@ -0,0 +1,3 @@ |
| 127 | ++Base64-encode values when embedding cookies to JavaScript using the |
| 128 | ++:meth:`http.cookies.BaseCookie.js_output` method to avoid injection |
| 129 | ++and escaping. |
0 commit comments