Live data from Hacker News

Handling cookies is a minefield

grayduck.mn

201–210 of 270 posts

Re: Handling cookies is a minefield

#201
post #163

Earlier quoted context omitted.

You just described how the whole web operates. It works just fine.

Even if you want client side, we have better ways now than cookies.

We do, but only cookies are universally available. Plenty of unusual user-agents in the world, or people like me that browse with JS off by default.

Re: Handling cookies is a minefield

#202
post #129

Earlier quoted context omitted.

So the problem with Postel's law is that people don't follow Postel's law?

The problem is that it's a prisoner's dilemma. And you can't cooperate on a prisoner's dilemma against the entire world.

So, just be as conservative as possible when you produce data and as liberal as possible when you receive something. Your code will then require the least cooperation from *any* other code to be compatible with.

Doing otherwise will require cooperation to adjust on the specificities clients expect, and you fall into the trap of the prisoner dilemna.

Re: Handling cookies is a minefield

#203
Literally everything in IT runs on decades old principles and technologies. The world simply refuses to fix things because "if ain't broken, don't fix it" philosophy. Look at TCP, HTML, JSON, SMTP..all good tech but insanely old and outdated and overtaxed for that it was invented for. When people joke that the entire banking industry runs on excel sheets, they are really not far from truth. Things will be shitty until they completely break down and people are forced to fix them. Look at JavaScript, this horribly stinking steaming pile of green diarrhea that rules over the entire front-end is still being worked on and developed and billions of money and countless work-hours have been wasted in order to make it somewhat usable, instead of just coming up with entirely new tech suitable for the 21st century. This is the entire internet and tech in general.

Re: Handling cookies is a minefield

#204

That is a bit of a minefield, I agree… The way around this, as a developer, is URL-safe-base64 encode the value. Then you have a bytes primitive & you can use whatever inner representation your heart desires. But the article does also note that you're not 100% in control, either. (Nor should you be, it is a user agent, after all.) I do wish more UAs opted for "obey the standard" over "bytes and an prayer on the wire"…

Cookie value can contain `=`, `/` and `+` characters so standard base64 encoding can be used as well :)

Re: Handling cookies is a minefield

#205

Earlier quoted context omitted.

The closest thing is https://en.wikipedia.org/wiki/Scone .

I've had something (in the US) that was called a "scone", and it was rigid, which disqualifies it from being similar to a biscuit in my mind. Is that generally true of scones?

The thing the US call scones is different from the thing the UK calls "scone".

Re: Handling cookies is a minefield

#206

The article mocks Postel's law, but if the setter of the cookie had been conservative in what they sent, there would have been no need for the article...

> The article mocks Postel's law As they should. Postel's Law was a terrible idea and has created minefields all over the place. Sometimes, those mines aren't just bugs, but create gaping security holes. If your client is sending data that doesn't conform to spec, you have a bug, and you need to fix it. It should never be up to the server to figure out what you meant and accept it.

Following Postel's law does not mean to accept anything. The received data should still be unambiguous.

You can see that in the case where ASN.1 data need to be exchanged. You could decide to always send them in the DER form (conservative) but accept BER (liberal). BER is still an unambiguous encoding for ASN.1 data but allow several representations for the same data.

The problem with BER mainly lies with cryptographic signature as the signature will only match a specific encoding so that's why DER is used in certificates. But you can still apply Postel's law, you may still accept BER fields when parsing file. If the field has been incorrectly encoded in a varied form which is incompatible with the signature, you will just reject it as you would reject it because it is not standard with DER. But still, you lessen the burden to make sure all parts follow exactly the standards the same way and things tend to work more reliably across server/clients combinations.

Re: Handling cookies is a minefield

#207
post #152

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.

the new thing should be called "cupcakes" or "candies" or "snacks" or "munchies"

We already have Macaroons

https://en.wikipedia.org/wiki/Macaroons_(computer_science)

https://en.wikipedia.org/wiki/Macaroon

Re: Handling cookies is a minefield

#208
post #19

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

You're really going to hate it when you learn about JSON Web Tokens, which exist exactly to hack past this sort of problem.

Jwt is encoded and it is used for data without a server session.

I'm not a fan for jwt and it used more often than it should, but sometimes it makes sense.

Re: Handling cookies is a minefield

#209
post #174

Earlier quoted context omitted.

And if I don't want any javascript to see my values, ever? Or how do you handle CSRF?

Httponly cookie is the way, but then you just don't use json as cookie value that is send on every request. Csrf is no problem as the data from service worker is only active on the site itself. If you speak about csrf with a website where you can't trust js, you're site is broken as xhr/fetch use the same httponly cookies and is affected as well.

Since when can you trust js?
Post reply on HN