Opened 7 years ago

Last modified 5 months ago

#10554 new New feature

Response.set_cookie should allow setting two cookies of the same name.

Reported by: jdunck Owned by: nobody
Component: HTTP handling Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Since cookies have domain and path properties, this is a sensible thing to do:

response.set_cookie('x', path='/foo/', expires=<some expired date>)
response.set_cookie('x', path='/bar/', expires=<some future date>)

It'd be nice if Django allowed this. Sadly, I think this would mean moving away from Cookie.

Change History (15)

comment:1 Changed 7 years ago by jdunck

OK, here's a plan for fix:

Currently, Django's response.cookies is an instance of stdlib's Cookie.SimpleCookie(BaseCookie).

BaseCookie is a subclass of dict, and so makes the assumption that there will be only one Morsel (that is, serializable cookie) per key. Key is assumed to be string, because BaseCookie does things like .lower and .translate on it, and therefore, there can only be one morsel per cookie name.

Here's the fix/hack: create a class which responds to the basestring methods, and takes all the HTTP-pertinent parameters (that is, name, path, domain, secure) into account for cmp and hash. Alter set_cookie to create an instance of that class as the key given to SimpleCookie. Respond to str with just the cookie name.

comment:2 Changed 7 years ago by jacob

  • milestone set to 1.2
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 7 years ago by ccahoon

  • Owner changed from nobody to ccahoon

comment:4 Changed 7 years ago by ubernostrum

  • milestone 1.2 deleted

1.2 is feature-frozen, moving this feature request off the milestone.

comment:5 Changed 5 years ago by SmileyChris

  • Severity set to Normal
  • Type set to New feature

comment:6 Changed 5 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:7 Changed 5 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:8 Changed 4 years ago by aaugustin

  • Owner changed from ccahoon to nobody
  • Version changed from 1.0 to master

comment:9 Changed 3 years ago by unaizalakain

Would a MorselKey class implementing the aforementioned methods in django.http.cookie be right? If so, I'll submit a patch.

comment:10 Changed 3 years ago by jdunck

I believe so, yes. Jacob accepted this ticket; there's been no debate on my suggested fix. I am now a core committer and feel this is a decent way to fix the problem.

I would point out that in the years since I wrote these notes, the versions of both django and supported python versions have changed - it's possible there's a better way now, though I don't have time to dig into it at the moment.

Thanks for your interest. :)

comment:11 Changed 3 years ago by unaizalakain

I have been fooling around with this little fix and one problem arises from the proposed solution: While the custom hash method prevents dict collisions, it also prevents from checking if some cookie already exists (as done by many contrib apps).

comment:12 Changed 3 years ago by unaizalakain

While a possible workaround could be to redefine SimpleCookie's method to check if some cookie exists, some structural issues would rise. What should we do if there're two cookies with the same name and SimpleCookie.get('cookie') is called?

MorselKey's could be used to grab cookies from cookies dict but a lot of external code would change.

comment:14 Changed 3 years ago by stavros

We are currently getting a bug when a user has two sessionid cookies with different domains. The user then is completely unable to log in, getting redirected back to the homepage. It is related to this issue, but I'm not sure whether I should file a new ticket or not. I would suggest that, if the sessionid is expired, the cookies are deleted, but I'm not sure if it's actually expired or not. Login works, the user gets redirected to the root, and then the root sees that the user isn't authenticated and sends them back to login for ever. The user can only get out of this if they clear their cookies, which is a very significant bug.

comment:15 Changed 5 months ago by collinanderson

The latest says we should not do this, which makes me think it's not worth it. Is there a real-world problem that this would actually solve?

   Servers SHOULD NOT include more than one Set-Cookie header field in
   the same response with the same cookie-name.
Note: See TracTickets for help on using tickets.
Back to Top