Skip to content

Update MAX_COOKIE_SIZE #13948

Description

@felix-tjernberg

Describe the problem

const MAX_COOKIE_SIZE = 4196 might be set by an inaccurate assumption

From what I have gathered it seems that chromium and firefox sets max byte size to 4096 for name + value attributes, chromium also seems to limit all the other attributes to 1024 each whilst firefox does not have a limit for the other attributes. webkit however seems to have set 5000 maximum for the whole string (I'm not to sure on if this webkit reference is correct though as I had problems understanding that code base, however it seems weird that they have some internal thing called curl that is not curl it self, so not sure if this code is even used)

The extra 100 bytes assumption that came from when rich tested the maximums might have been from attributes that are not name & value

Describe the proposed solution

  • Check if name + value attributes is maximally 4096 bytes to match chromium & firefox & upcoming cookie consumer specification behavior
  • Check if each other attribute is maximally 1024 bytes to match chromium & upcoming cookie consumer specification behavior
  • Check if the whole string is maximally 5000 bytes to match webkit behavior (however suggestion is to also add a monitor on the webkit code base to see if they change their behavior after the new specification comes into effect, see comment below comment for more information)
  • console.warn when limits are hit instead of throwing to follow the cookie producer specification behavior (see below comment for more information)

Alternatives considered

No response

Importance

nice to have

Additional Information

I have not tested these limits myself after I found the references in code

I personally do no have a need for this change, however I think it is important to match the browsers behavior, so maybe something worth adding in 3.0 as I saw you guys are planning on enforcing that the string only contains valid characters in 3.0

Also it might be worth documenting in the sveltekit docs that the standard encourages the server to design as few and as small states as small as possible, or in funnier words please do not treat cookies as a database and if you do please use a sane encoding to make the data small

Servers SHOULD use as few and as small cookies as possible to avoid reaching these implementation limits and to minimize network bandwidth due to the Cookie header being included in every request
RFC 6265

Another thing worth documenting in the sveltekit docs is that the original purpose for "cooparative programs keys", is in the general web case to share a database key in requests, which lets the website become stateful as the server can serve the user specific content for each request, usually a shopping cart. But because the cookie limits are large enough you can treat them as small databases that you can modify on the client without making a request when javascript is available using httpOnly=false, which allows for keeping track of user settings without using a "real" server only database

Something I love about your docs is that that it helped me "become a better developer" (a phrase I think was in a previous version of the docs), not sure though if above is in the spirit of what you want to teach about the web in your docs

Another thing I might as well note is that the maximum number of cookies seems to be 150 in chromium & firefox (I do not really understand the quota business, but I'm guessing 150 is max where you can have 30 more for some edge case where cookies have not yet been garbage collected) and 80 is max in webkit, so if you are going to warn about minimum number of cookies I would suggest adding a maximum warning at 80 (if you decide to add my documentation suggestions in sveltekit docs, I would also emphasize that the server should not come close to this limit, and if it does please consider combining cookies to not get close to the limit, ie you have 10 user settings, consider serializing them into 1 cookie with some split token)

Whilst at it I could also inform that the total cookie limit seems to be 3000 in both chromium & firefox, however I could not find a reference in the webkit code base so I'm guessing they do not have a limit (however not finding a reference might have been a skill issue from my part) (also ~96gb (if they all are maxed out) of cookie data is however a resonable maximum xD)

Hopefully you find this rabbit hole information useful :D

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions