From d5a2377c3d70e4143bcbee4a765b3434e21f683a Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Mon, 23 Apr 2018 02:48:11 +0300 Subject: [PATCH] bpo-991266: Fix quoting of Comment attribute of SimpleCookie (GH-6555) --- Lib/http/cookies.py | 2 ++ Lib/test/test_http_cookies.py | 10 ++++++++++ .../Library/2018-04-21-00-24-08.bpo-991266.h93TP_.rst | 1 + 3 files changed, 13 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-04-21-00-24-08.bpo-991266.h93TP_.rst diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index b2e995c0c47..6694f5478bd 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -408,6 +408,8 @@ class Morsel(dict): append("%s=%s" % (self._reserved[key], _getdate(value))) elif key == "max-age" and isinstance(value, int): append("%s=%d" % (self._reserved[key], value)) + elif key == "comment" and isinstance(value, str): + append("%s=%s" % (self._reserved[key], _quote(value))) elif key in self._flags: if value: append(str(self._reserved[key])) diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py index 447f883390f..6072c7e15e9 100644 --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -220,6 +220,16 @@ class CookieTests(unittest.TestCase): with self.assertRaises(cookies.CookieError): C.load(rawdata) + def test_comment_quoting(self): + c = cookies.SimpleCookie() + c['foo'] = '\N{COPYRIGHT SIGN}' + self.assertEqual(str(c['foo']), 'Set-Cookie: foo="\\251"') + c['foo']['comment'] = 'comment \N{COPYRIGHT SIGN}' + self.assertEqual( + str(c['foo']), + 'Set-Cookie: foo="\\251"; Comment="comment \\251"' + ) + class MorselTests(unittest.TestCase): """Tests for the Morsel object.""" diff --git a/Misc/NEWS.d/next/Library/2018-04-21-00-24-08.bpo-991266.h93TP_.rst b/Misc/NEWS.d/next/Library/2018-04-21-00-24-08.bpo-991266.h93TP_.rst new file mode 100644 index 00000000000..3af6c27cf21 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-04-21-00-24-08.bpo-991266.h93TP_.rst @@ -0,0 +1 @@ +Fix quoting of the ``Comment`` attribute of :class:`http.cookies.SimpleCookie`.