[Django] #19802: HttpResponse.set_cookie doesn't handle unicode data

58 views
Skip to first unread message

Django

unread,
Feb 11, 2013, 9:51:40 AM2/11/13
to django-...@googlegroups.com
#19802: HttpResponse.set_cookie doesn't handle unicode data
-------------------------------+--------------------
Reporter: django@… | Owner: nobody
Type: Uncategorized | Status: new
Component: Core (Other) | Version: 1.4
Severity: Normal | Keywords: cookie
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
When calling HttpResponse.set_cookie as below, you get an error from
`translate` saying it doesn't expect two variables. The backtrace is
(relevant parts only):


{{{
TypeError at /js/utils/cookie/set/
translate() takes exactly one argument (2 given)

...

Traceback:
File "/home/patrick/spng/lib/python2.7/site-
packages/django/core/handlers/base.py" in get_response
111. response = callback(request,
*callback_args, **callback_kwargs)
File "/home/patrick/spng/src/utils/views.py" in ajax_set_cookie
210. response.set_cookie(key, value)
File "/home/patrick/spng/lib/python2.7/site-
packages/django/http/__init__.py" in set_cookie
657. self.cookies[key] = value
File "/usr/lib/python2.7/Cookie.py" in __setitem__
592. self.__set(key, rval, cval)
File "/home/patrick/spng/lib/python2.7/site-
packages/django/http/__init__.py" in _BaseCookie__set
103. M.set(key, real_value, coded_value)
File "/usr/lib/python2.7/Cookie.py" in set
459. if "" != translate(key, idmap, LegalChars):
File "/usr/lib/python2.7/string.py" in translate
493. return s.translate(table, deletions)

Exception Type: TypeError at /js/utils/cookie/set/
Exception Value: translate() takes exactly one argument (2 given)
}}}

This is happening under Django 1.4.2 and Python 2.7.3 (both latest from
the Ubuntu 12.10 repository). The code used to trigger this bug is:

{{{
from django.http import HttpResponse

def view(request):
response = HttpResponse()
response.set_cookie(u'unicode_string', 'does not work!')
return response

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19802>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 11, 2013, 4:55:14 PM2/11/13
to django-...@googlegroups.com
#19802: HttpResponse.set_cookie doesn't handle unicode data
-------------------------------+--------------------------------------
Reporter: django@… | Owner: nobody
Type: Uncategorized | Status: closed
Component: HTTP handling | Version: 1.4
Severity: Normal | Resolution: worksforme

Keywords: cookie | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by claudep):

* status: new => closed
* needs_better_patch: => 0
* component: Core (Other) => HTTP handling
* needs_tests: => 0
* needs_docs: => 0
* resolution: => worksforme


Comment:

Django 1.5 should support unicode cookie keys. On 1.4, you can workaround
this by encoding the key (e.g. `u'unicode_string'.encode('utf-8')`).

--
Ticket URL: <https://code.djangoproject.com/ticket/19802#comment:1>

Django

unread,
Mar 14, 2014, 4:09:14 AM3/14/14
to django-...@googlegroups.com
#19802: HttpResponse.set_cookie doesn't handle unicode data
-------------------------------+--------------------------------------
Reporter: django@… | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 1.5
Severity: Normal | Resolution:

Keywords: cookie | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by semenov):

* status: closed => new
* resolution: worksforme =>
* version: 1.4 => 1.5
* type: Uncategorized => Bug
* cc: semenov@… (added)


Comment:

This is still broken in 1.5. To reproduce the bug, use the experiment file
below:
{{{#!python
# Put this in a file called experiment19802.py and run it with:
# django-admin.py runserver --settings=experiment19802

from django.conf.urls import patterns, url
from django.http import HttpResponse

DEBUG = True
ROOT_URLCONF = 'experiment19802'
SECRET_KEY = 'whatever'

def view(request):
res = HttpResponse('OK')
res.set_cookie('test', request.GET.get('cookie', ''))
return res

urlpatterns = patterns('',
url(r'.*', view),
)
}}}

and try to open: http://localhost:8000/?cookie=M%C3%BCller

It will raise the exception:
{{{
Traceback:
File "/Users/semenov/work/xxx/venv/lib/python2.7/site-
packages/django/core/handlers/base.py" in get_response
115. response = callback(request,
*callback_args, **callback_kwargs)
File "/Users/semenov/tmp/19802/experiment19802.py" in view
13. res.set_cookie('test', request.GET.get('cookie', ''))
File "/Users/semenov/work/xxx/venv/lib/python2.7/site-
packages/django/http/response.py" in set_cookie
157. self.cookies[key] = value
File
"/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/Cookie.py"
in __setitem__
591. rval, cval = self.value_encode(value)
File
"/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/Cookie.py"
in value_encode
680. strval = str(val)

Exception Type: UnicodeEncodeError at /
Exception Value: 'ascii' codec can't encode character u'\xfc' in position
1: ordinal not in range(128)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19802#comment:2>

Django

unread,
Mar 22, 2014, 4:44:45 AM3/22/14
to django-...@googlegroups.com
#19802: HttpResponse.set_cookie doesn't handle unicode data
-------------------------------+------------------------------------
Reporter: django@… | Owner: nobody

Type: Bug | Status: new
Component: HTTP handling | Version: 1.5
Severity: Normal | Resolution:
Keywords: cookie | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by erikr):

* stage: Unreviewed => Accepted


Comment:

I can reproduce this issue on current master.

--
Ticket URL: <https://code.djangoproject.com/ticket/19802#comment:3>

Django

unread,
Mar 22, 2014, 4:49:21 AM3/22/14
to django-...@googlegroups.com
#19802: HttpResponse.set_cookie doesn't handle unicode data
-------------------------------+------------------------------------
Reporter: django@… | Owner: nobody

Type: Bug | Status: new
Component: HTTP handling | Version: 1.5
Severity: Normal | Resolution:
Keywords: cookie | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by erikr):

However, it is only reproducible on Python 2 - it works fine for me in
Python 3.3. So, it seems the issue is that Python 2 cookielib coerces
`str()` on the cookie contents. Not sure what the proper solution would be
here.

--
Ticket URL: <https://code.djangoproject.com/ticket/19802#comment:4>

Django

unread,
Jul 27, 2014, 5:58:27 AM7/27/14
to django-...@googlegroups.com
#19802: HttpResponse.set_cookie doesn't handle unicode data
-------------------------------+-------------------------------------
Reporter: django@… | Owner: anonymous
Type: Bug | Status: assigned

Component: HTTP handling | Version: 1.5
Severity: Normal | Resolution:
Keywords: cookie | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+-------------------------------------
Changes (by anonymous):

* owner: nobody => anonymous
* status: new => assigned
* has_patch: 0 => 1
* needs_tests: 0 => 1


Comment:

Fiexd, pls, review, thanks

https://github.com/django/django/pull/2984

--
Ticket URL: <https://code.djangoproject.com/ticket/19802#comment:5>

Django

unread,
Jul 27, 2014, 5:59:26 AM7/27/14
to django-...@googlegroups.com
#19802: HttpResponse.set_cookie doesn't handle unicode data
-------------------------------+------------------------------------
Reporter: django@… | Owner: qingfeng

Type: Bug | Status: assigned
Component: HTTP handling | Version: 1.5
Severity: Normal | Resolution:
Keywords: cookie | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by qingfeng):

* owner: anonymous => qingfeng


--
Ticket URL: <https://code.djangoproject.com/ticket/19802#comment:6>

Django

unread,
Jul 30, 2014, 12:44:59 PM7/30/14
to django-...@googlegroups.com
#19802: HttpResponse.set_cookie doesn't handle unicode data
-------------------------------+------------------------------------
Reporter: django@… | Owner: qingfeng
Type: Bug | Status: assigned
Component: HTTP handling | Version: 1.5
Severity: Normal | Resolution:
Keywords: cookie | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by qingfeng):

@erikr @claudep pls review, thanks :beer:

--
Ticket URL: <https://code.djangoproject.com/ticket/19802#comment:7>

Django

unread,
Jul 31, 2014, 1:56:29 PM7/31/14
to django-...@googlegroups.com
#19802: HttpResponse.set_cookie doesn't handle unicode data
-------------------------------+------------------------------------
Reporter: django@… | Owner: qingfeng
Type: Bug | Status: assigned
Component: HTTP handling | Version: 1.5
Severity: Normal | Resolution:
Keywords: cookie | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by timo):

* needs_better_patch: 0 => 1
* needs_tests: 1 => 0


Comment:

I left comments for improvement on PR. Please uncheck "Patch needs
improvement" when you update it, thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/19802#comment:8>

Django

unread,
Aug 3, 2014, 6:02:38 AM8/3/14
to django-...@googlegroups.com
#19802: HttpResponse.set_cookie doesn't handle unicode data
-------------------------------------+-------------------------------------

Reporter: django@… | Owner: qingfeng
Type: Bug | Status: assigned
Component: HTTP handling | Version: 1.5
Severity: Normal | Resolution:
Keywords: cookie | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by claudep):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


Comment:

For the committer, please just add in the commit message that this is a
Python 2 only issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/19802#comment:9>

Django

unread,
Aug 3, 2014, 12:51:27 PM8/3/14
to django-...@googlegroups.com
#19802: HttpResponse.set_cookie doesn't handle unicode data
-------------------------------------+-------------------------------------
Reporter: django@… | Owner: qingfeng
Type: Bug | Status: closed

Component: HTTP handling | Version: 1.5
Severity: Normal | Resolution: fixed

Keywords: cookie | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"0d23450e81dc355c354a873fe187687e4aaa4886"]:
{{{
#!CommitTicketReference repository=""
revision="0d23450e81dc355c354a873fe187687e4aaa4886"
Fixed #19802 -- Fixed HttpResponse.set_cookie() with unicode data on
Python 2.

Thanks django at patrickbregman.eu for the report.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19802#comment:10>

Reply all
Reply to author
Forward
0 new messages