ScottHamper / Cookies

JavaScript Client-Side Cookie Manipulation Library
The Unlicense
1.77k stars 169 forks source link

handle double-quoted cookie value #25

Closed xuyang2 closed 10 years ago

xuyang2 commented 10 years ago

according to rfc6265 (http://tools.ietf.org/html/rfc6265#section-4.1.1), cookie-value can be double quoted.

set-cookie-header = "Set-Cookie:" SP set-cookie-string
set-cookie-string = cookie-pair *( ";" SP cookie-av )
cookie-pair       = cookie-name "=" cookie-value
cookie-name       = token
cookie-value      = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )
cookie-octet      = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
                   ; US-ASCII characters excluding CTLs,
                   ; whitespace DQUOTE, comma, semicolon,
                   ; and backslash
token             = <token, defined in [RFC2616], Section 2.2>

When a server send a cookie:

< Set-Cookie: foo="MTYyODNlMTc1ODIyNTA4NGMzMDg4N2RhNDg0NTZjZmQxMDJiNA==";Path=/;Expires=Mon, 19-May-2014 10:32:57 GMT

my brower (Chrome) got:

var foo = Cookies.get('foo')
// foo == '"MTYyODNlMTc1ODIyNTA4NGMzMDg4N2RhNDg0NTZjZmQxMDJiNA=="'

which is unexpected.

xuyang2 commented 10 years ago

The server (Jetty 7) double-quoted my cookie value internally because it contains '='.

ScottHamper commented 10 years ago

Hey Xuyang,

Thanks for checking out the library! However, I consider this issue a "wontfix" - when you call Cookies.get, you're getting the cookie-value, not the cookie-octet.

On a more practical note, someone has to lose out in this situation - if I update Cookies.js to automatically remove wrapping quotes, then it no longer becomes possible to legitimately have a literal quoted string as the cookie value. I prefer the current functionality of the library, as I believe it to be the more accurate/semantic choice.

As for a solution to your problem - you could see if there's a web server configuration setting you can change to prevent it from automatically wrapping cookie values in quotes (this seems like inappropriate behavior by the server to begin with), or alternatively, you could write some JavaScript to strip the quotes for you after you've gotten the raw value from Cookies.js.