| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -391,17 +391,21 @@ def __repr__(self): | |||
| 391 | 391 | return '<%s: %s>' % (self.__class__.__name__, self.OutputString()) | |
| 392 | 392 | ||
| 393 | 393 | def js_output(self, attrs=None): | |
| 394 | + import base64 | ||
| 394 | 395 | # Print javascript | |
| 395 | 396 | output_string = self.OutputString(attrs) | |
| 396 | 397 | if _has_control_character(output_string): | |
| 397 | 398 | raise CookieError("Control characters are not allowed in cookies") | |
| 399 | + # Base64-encode value to avoid template | ||
| 400 | + # injection in cookie values. | ||
| 401 | + output_encoded = base64.b64encode(output_string.encode('utf-8')).decode("ascii") | ||
| 398 | 402 | return """ | |
| 399 | 403 | <script type="text/javascript"> | |
| 400 | 404 | <!-- begin hiding | |
| 401 | - document.cookie = \"%s\"; | ||
| 405 | + document.cookie = atob(\"%s\"); | ||
| 402 | 406 | // end hiding --> | |
| 403 | 407 | </script> | |
| 404 | - """ % (output_string.replace('"', r'\"')) | ||
| 408 | + """ % (output_encoded,) | ||
| 405 | 409 | ||
| 406 | 410 | def OutputString(self, attrs=None): | |
| 407 | 411 | # Build up our result | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | # Simple test suite for http/cookies.py | |
| 2 | - | ||
| 2 | + import base64 | ||
| 3 | 3 | import copy | |
| 4 | 4 | import unittest | |
| 5 | 5 | import doctest | |
@@ -175,17 +175,19 @@ def test_load(self): | |||
| 175 | 175 | ||
| 176 | 176 | self.assertEqual(C.output(['path']), | |
| 177 | 177 | 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') | |
| 178 | - self.assertEqual(C.js_output(), r""" | ||
| 178 | + cookie_encoded = base64.b64encode(b'Customer="WILE_E_COYOTE"; Path=/acme; Version=1').decode('ascii') | ||
| 179 | + self.assertEqual(C.js_output(), fr""" | ||
| 179 | 180 | <script type="text/javascript"> | |
| 180 | 181 | <!-- begin hiding | |
| 181 | - document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1"; | ||
| 182 | + document.cookie = atob("{cookie_encoded}"); | ||
| 182 | 183 | // end hiding --> | |
| 183 | 184 | </script> | |
| 184 | 185 | """) | |
| 185 | - self.assertEqual(C.js_output(['path']), r""" | ||
| 186 | + cookie_encoded = base64.b64encode(b'Customer="WILE_E_COYOTE"; Path=/acme').decode('ascii') | ||
| 187 | + self.assertEqual(C.js_output(['path']), fr""" | ||
| 186 | 188 | <script type="text/javascript"> | |
| 187 | 189 | <!-- begin hiding | |
| 188 | - document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme"; | ||
| 190 | + document.cookie = atob("{cookie_encoded}"); | ||
| 189 | 191 | // end hiding --> | |
| 190 | 192 | </script> | |
| 191 | 193 | """) | |
@@ -290,17 +292,19 @@ def test_quoted_meta(self): | |||
| 290 | 292 | ||
| 291 | 293 | self.assertEqual(C.output(['path']), | |
| 292 | 294 | 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') | |
| 293 | - self.assertEqual(C.js_output(), r""" | ||
| 295 | + expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1').decode('ascii') | ||
| 296 | + self.assertEqual(C.js_output(), fr""" | ||
| 294 | 297 | <script type="text/javascript"> | |
| 295 | 298 | <!-- begin hiding | |
| 296 | - document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1"; | ||
| 299 | + document.cookie = atob("{expected_encoded_cookie}"); | ||
| 297 | 300 | // end hiding --> | |
| 298 | 301 | </script> | |
| 299 | 302 | """) | |
| 300 | - self.assertEqual(C.js_output(['path']), r""" | ||
| 303 | + expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme').decode('ascii') | ||
| 304 | + self.assertEqual(C.js_output(['path']), fr""" | ||
| 301 | 305 | <script type="text/javascript"> | |
| 302 | 306 | <!-- begin hiding | |
| 303 | - document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme"; | ||
| 307 | + document.cookie = atob("{expected_encoded_cookie}"); | ||
| 304 | 308 | // end hiding --> | |
| 305 | 309 | </script> | |
| 306 | 310 | """) | |
@@ -391,13 +395,16 @@ def test_setter(self): | |||
| 391 | 395 | self.assertEqual( | |
| 392 | 396 | M.output(), | |
| 393 | 397 | "Set-Cookie: %s=%s; Path=/foo" % (i, "%s_coded_val" % i)) | |
| 398 | + expected_encoded_cookie = base64.b64encode( | ||
| 399 | + ("%s=%s; Path=/foo" % (i, "%s_coded_val" % i)).encode("ascii") | ||
| 400 | + ).decode('ascii') | ||
| 394 | 401 | expected_js_output = """ | |
| 395 | 402 | <script type="text/javascript"> | |
| 396 | 403 | <!-- begin hiding | |
| 397 | - document.cookie = "%s=%s; Path=/foo"; | ||
| 404 | + document.cookie = atob("%s"); | ||
| 398 | 405 | // end hiding --> | |
| 399 | 406 | </script> | |
| 400 | - """ % (i, "%s_coded_val" % i) | ||
| 407 | + """ % (expected_encoded_cookie,) | ||
| 401 | 408 | self.assertEqual(M.js_output(), expected_js_output) | |
| 402 | 409 | for i in ["foo bar", "foo@bar"]: | |
| 403 | 410 | # Try some illegal characters | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + Base64-encode values when embedding cookies to JavaScript using the | ||
| 2 | + :meth:`http.cookies.BaseCookie.js_output` method to avoid injection | ||
| 3 | + and escaping. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments