Zope cookies unconditionally quoted

Bug #142226 reported by Bug Importer
2
Affects Status Importance Assigned to Milestone
Zope 2
Fix Released
Medium
Unassigned

Bug Description

Setting a cookie value via (adapt for your site...)

<dtml-call "RESPONSE.setCookie('cookie_name', 'value', expires='Wed, 19 Feb 2020 14:28:00 GMT', path='/', domain='kefta.com')">

will set a cookie, but the value will have double quotes around it. Zope appears to strip these quotes on retrieval so they are not visible from zope, but other severs in the same domain will see them, as well as mozilla's cookie manager.

No work around known, except for Javascript or setting cookies via a different server.

Tags: bug zope
Revision history for this message
Jamie Heilman (jamie-audible) wrote :

Its a little unclear why this is a bug, so I'll expound upon it some.

The quotes are added in the _cookie_list() function from
ZPublisher/HTTPResponse.py. The quotes themselves aren't a bug,
quoted strings are allowed if we are to believe RFC 2965 or 2109
(although not all bother to do so), nor do they interfere with the
infamously half-assed Nestcape Cookie specification. The problem is
that adding quotes without doing any further encoding on the cookie
value is a worthless gesture, and no further encoding is done, values
are passed completely raw.

Passing everything raw, leaving the setCookie methods a somewhat thin
conceptual wrapper around setHeader, means that author must do their
own encoding and decoding to ensure semicolons (and other special
characters) in the cookie value are properly translated. Adding
quotes the way ZPublisher does also means the author be must aware of
this and be sure to include quotes in list of 'special characters'
that require encoding, otherwise the regex in HTTPRequest.py is sure
to mangle that cookie if the browser doesn't. That the API
documentation doesn't mention any of this is rather suboptimal if
authors are expected to write robust code.

It could be said that if the author must already do all this work
preparing their data for transport, then there really isn't much merit
in giving them the quotes for "free." Alternatively setCookie could
generate a just header thats valid automatically without the author
having to do the hard part. Figuring out what 'valid' is these days,
given the rather sizable gap between the published standards and
current practices, should be entertaining.

Suffice it to say the cookie hooks and the documentation need work.

Revision history for this message
Charlie Reiman (reiman) wrote :

As the original poster, let me clarify why this is a problem. It has nothing to do with specs or free quoting. The problem is cookies are used across technologies: My Zope set cookies will be read by javascript and/or apache servers and god-knows-what else. Ramming quotes around them (indeed, any magic wrapping) makes zope cookies incompatible with 3rd party technologies. Zope might like to pretend it lives in a vacuum but my cookies certainly don't.

There is a work around at least. It is also too late to fix this as it would break existing cookies. I feel the existing behavior probably should remain but it needs to be documented and the setHeader workaround should be mentioned in the same place.

Revision history for this message
Lennart Regebro (regebro-gmail) wrote :

I don't see why it makes them incompatible with 3-party technologies. It means you have to strip the quotes in Javascript, for example, but that should work, right?

Revision history for this message
Evan Simpson (evan-4-am) wrote :

The incompatibility arises in two ways:

a. Zope can't set a cookie for consumption by another application that doesn't expect the quotes. Yes, you could change the other application to deal with the quotes -- why should you have to?

b. If another application sets unquoted cookies, it can break Zope's parsing. This has happened to me, and it was very mysterious and unpleasant.

Revision history for this message
Peter Sabaini (peter-sabaini) wrote :

I've just been bitten by this: Apache 2.2 supports load balancing with "sticky sessions", ie., depending on a session cookie a request gets proxied to a specific backend.

Except that doesn't work with Zope because Zope insists on wrapping my cookies in quotes.

I dont see what that wrapping is good for in the first place? Apache and as far as I can see also IIS and Javascript expect cookie vals to be bare, which also seems in the spirit of RFC2109 IMHO

Revision history for this message
miwa (miwa) wrote :

We've just been bitten by this whilst integrating a php bulletin board with zope.

I wanted to use the same session id across zope and php, in order that they can pull the required data from the database. Both apps are happy with the session id generated by the other and stored in a cookie. However, zope magically quotes the session id if it generates it, and I then have to customise the php app to remove the quotes.

Would an acceptable solution be a zope.conf directive that controls cookie quoting (default quoting on)? I'll happily provide a patch if so. Unless you are doing integration of this sort, the default behaviour is fine.

Revision history for this message
Andreas Jung (ajung) wrote :

How about adding an option to the setCookie() method that controls the quoting? I am not sure if were a really interested in a global configuration option.

Revision history for this message
Andres Herrera (andresh) wrote :

Hi, continues even with the problems in the newer versions?

Changed in zope2:
status: New → Incomplete
Revision history for this message
Ch. Tronche (ch-tronche) wrote :

Same thing with us while trying to integrate Zope with the phpCAS SSO system and using cookies to transport SSO tickets.

When Zope authenticates users, it sets an SSO ticket inside a cookie, so that other systems may know the user is authenticated.

Problem: Zope (2.9) magically adds the infamous quotes, while the phpCAS library does something like $value ~ /^ST-/ (value starts with the ST- string). No room for an opening quote, which makes the systems not interoperable.

Revision history for this message
Ch. Tronche (ch-tronche) wrote :

By the way, I support Andreas Jung suggestion to add an option to setCookie.

Revision history for this message
Hanno Schlichting (hannosch) wrote :

This is still the case in current Zope trunk.

Changed in zope2:
status: Incomplete → New
Tres Seaver (tseaver)
Changed in zope2:
status: New → Confirmed
Changed in zope2:
assignee: nobody → Jens Vagelpohl (jens-dataflake)
Revision history for this message
Jens Vagelpohl (dataflake-deactivatedaccount-deactivatedaccount) wrote :

I have gone for the simplest solution suggested by Andreas, a new keyword argument for the setCookie method.

Checked in on the 2.12 branch: http://svn.zope.org/?rev=112512&view=rev

Checked in on the trunk: http://svn.zope.org/?rev=112511&view=rev

Changed in zope2:
status: Confirmed → Fix Committed
Changed in zope2:
milestone: none → 2.12.7
Changed in zope2:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.