Live data from Hacker News

Handling cookies is a minefield

grayduck.mn

51–60 of 270 posts

Re: Handling cookies is a minefield

#51
post #19

IT IS a mess, but I never saw json inside a cookie. For json I use local storage or indexeddb.

In both cases (cookie vs localStorage) you're really just storing your data as a string value, not truly a JSON object, so whether you use a cookie or localStorage is more dependent on the use case. If you only ever need the stored data on the client, localStorage is your pick. If you need to pass it back to the server with each request, cookies.

JSON is explicitly a string serialization format.

Re: Handling cookies is a minefield

#52

[comment intended for a different post, but too old to delete]

None of this explicitly has anything specifically to do with HTML.

It sure doesn't, that was a comment for a completely different post. I have no idea why HN posted this comment on this article instead of the PHP 8.4 article I thought I was commenting on O_o

Re: Handling cookies is a minefield

#53

Cookies seem to be a big complicated mess, and meanwhile are almost impossible to change for backwards-compatibility reasons. Is this a case to create a new separate mechanism? For example a NewCookie mechanism could be specified instead, and redesigned from the ground-up to work consistently. It could have all the modern security measures built-in, a stricter specification, proper support for unicode, etc.

i think the main problem there is that cookies are so intractibly tied up with tracking, any attempt to create better cookies now will get shut down by privacy advocates who simply don't want the whole concept to exist. we're stuck with cookies because they exist.

Every privacy advocate I know hands over exquisitely detailed private and personal information to Google and/or Apple. It seems unfair to generalize as “privacy advocates” so much as it is people who are anti-ads.

Being anti-ads is a valid opinion. It has less intellectual cover than pro “privacy” though.

Re: Handling cookies is a minefield

#55

Earlier quoted context omitted.

Per the section 4.1.1 rules quoted in the article, cookie values can be optionally quoted: > cookie-value = cookie-octet / ( DQUOTE cookie-octet DQUOTE )

That is true, but in that case they are part of the value itself, they're not doing anything special: > Per the grammar above, the cookie-value MAY be wrapped in DQUOTE characters. Note that in this case, the initial and trailing DQUOTE characters are not stripped. They are part of the cookie-value, and will be included in Cookie header fields sent to the server.

Ah, thanks for the clarification!

Re: Handling cookies is a minefield

#56
post #23

Cookies seem to be a big complicated mess, and meanwhile are almost impossible to change for backwards-compatibility reasons. Is this a case to create a new separate mechanism? For example a NewCookie mechanism could be specified instead, and redesigned from the ground-up to work consistently. It could have all the modern security measures built-in, a stricter specification, proper support for unicode, etc.

It's funny that you mention NewCookie, there is actually a deprecated Set-Cookie2 header already: https://stackoverflow.com/q/9462180/3474615

[flagged]

Re: Handling cookies is a minefield

#57
One of the things I’ve always found frustrating about cookies is that you have to do your own encoding instead of the API doing it for you. I’m sure someone somewhere does but too often I’m doing my own urlencode calls.

Re: Handling cookies is a minefield

#59
post #5

Firefox accepts five characters which RFC recommends that servers not send: 0x09 (horizontal tab) 0x20 (spaces) 0x22 (double quotes) 0x2C (commas) 0x5C (backslashes) I agree with at least some of these. Cookies without commas? Quotes?

[deleted]

Re: Handling cookies is a minefield

#60

Earlier quoted context omitted.

In both cases (cookie vs localStorage) you're really just storing your data as a string value, not truly a JSON object, so whether you use a cookie or localStorage is more dependent on the use case. If you only ever need the stored data on the client, localStorage is your pick. If you need to pass it back to the server with each request, cookies.

JSON is explicitly a string serialization format.

Right, I meant it's not a JavaScript object. It's serialized into a string in any case, no matter which API you're stuffing it into. So it's a bit of a non-sequitur for the parent to suggest that it's somehow weird to store JSON in a cookie, but not in localStorage. It's all just strings.
Post reply on HN