Skip to content

bpo-991266: Fix quoting of the Comment attribute of SimpleCookie #6555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/http/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ def OutputString(self, attrs=None):
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)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch looks like it correctly solves this problem, however is there a reason not to quote all values? (line417)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I said in the issue (https://bugs.python.org/issue991266#msg315499) I'm a bit reluctant on that idea. The situation with cookie specs is a bit painful to deal :) Also, I'm not sure we can do that in bugfix releases because of backwards compatibility reasons.

Perhaps we can be a little bit conservative and merge this for now and discuss (like taking a look at other programming languages) a broader change like you and Mark mentioned in the issue for 3.8? I wrote this patch almost two years ago and to be honest I don't remember RFC 6265 and other relevant specs (2109, 2068 etc.) anymore :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'm fine to 👍 this since it is correct.

elif key in self._flags:
if value:
append(str(self._reserved[key]))
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_http_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ def test_illegal_chars(self):
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."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix quoting of the ``Comment`` attribute of :class:`http.cookies.SimpleCookie`.