WICG / cookie-store

Asynchronous access to cookies from JavaScript
https://wicg.github.io/cookie-store/
Apache License 2.0
143 stars 35 forks source link

Change get() to return null instead of undefined #210

Closed inexorabletash closed 1 year ago

inexorabletash commented 1 year ago

When get() was implemented in Chrome, we failed to match our own spec and made get() return null instead of undefined if no matching cookie was found. The WPT tests match the implementation, not the spec. Oops.

Someone noticed and filed https://bugs.chromium.org/p/chromium/issues/detail?id=1355228

@ayuishii investigated HTTP Archive and found several sites/libraries that seem to do strict comparisons against null.

Some examples from the the first 1000 mobile results:

Notably, JSON.parse(undefined) throws while JSON.parse(null) returns null, and JSON-parsing cookies seems to be fairly common (a handful of other pages, e.g. https://www.justfab.se/assets/dist/js/vueapp_m-c5291be04a.js has t=this.cookieStore.get(this.$options.cookie.name),t=JSON.parse(t)

So... I suspect it is not web-compatible to fix and we should instead update the spec to have get() return null. sigh

Thoughts?

domenic commented 1 year ago

I was going to say this would be unfortunate as it would make this fail to match all other maplike get()s on the web platform. But then I found out that Fetch's Headers and URL's URLSearchParams are already using null for their get()s. So, I guess maybe it's fine. /cc @annevk.

annevk commented 1 year ago

Yeah, we spotted the mismatch too late there. Not entirely sure what maplike and such in IDL are doing.

annevk commented 1 year ago

It seems this has been addressed in the specification at some point? I.e., get() returns null. Chromium bug is still open though.

inexorabletash commented 1 year ago

It seems this has been addressed in the specification at some point? I.e., get() returns null. Chromium bug is still open though.

get(name) step 6.3 still says "If list is empty, then resolve p with undefined." - where are you seeing it return null?

annevk commented 1 year ago

Oh, I was just looking at the IDL, which doesn't allow undefined. My bad.