next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

Anything above 1900 characters assigned to the user object breaks session creation

Open brett-chisholm opened this issue 6 years ago • 4 comments

Describe the bug Anything above 1900 characters assigned to the user object breaks session creation.

To Reproduce On authorize in the credentials provider, assign a string to the user object which is above 1900 characters.

Expected behavior The session should still be created.

Screenshots or error logs There aren't actually any errors explaining what has gone wrong, but I'm presuming it is because the user object is too big?

Additional context I am using the credentials provider to make a request to my own auth service. On success I assign the token returned from my service to the user. If this token is above 1900 characters then the session isn't created. Fully aware that the recommended char length for JWT tokens is 2000 or below, but unfortunately this isn't an option for me personally at the moment!

Documentation feedback Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • [x] Found the documentation helpful
  • [ ] Found documentation but was incomplete
  • [ ] Could not find relevant documentation
  • [x] Found the example project helpful
  • [ ] Did not find the example project helpful

brett-chisholm avatar Jul 06 '20 10:07 brett-chisholm

Unfortunately limits on JWT sizes for sessions used as secure session tokens (e.g. encrypted and in a server readable only cookie) is not something that we can really do anything about.

It varies per browser, but for real world compatibility (with browsers, proxies and CDNs) the effective limit for the size of cookies is only 4093 bytes (in total) for all cookies on a domain.

The JWT used by NextAuth.js is encrypted by default - you can save bytes if you know you don't need encryption, using custom encode/decode functions that only sign cookies and doesn't encrypt them.

However, as the JWT uses encryption and signing, if all you need is a unique identifier for the user as a property in the JWT (like User ID or an Access Key that maps to that user in a database); that should typically be sufficiently secure as a means to uniquely identify a client from the JWT alone.

If you need to store a large amount of data and it has to be client side, I suggest storing the data you need in localStorage. It could be as a signed JWT if you need to ensure it's tamper proof, or it could just be a blob of JSON. However, for security reasons I would only ever use that in conjunction with checking the JWT session token on a request, as localStorage is accessible to third party scripts that run client side, while session tokens are not.

Alternatively, depending on how much data you need to store and how you need to use it, you may be better off using a database to the store data and look that up against the JWT when a request is made.

tl;dr JSON Web Tokens are inherently constrained in what they can be used for when used to store session state because of cookie size limits. The only work around if you need to store a lot of data for a user is to store that data somewhere else.

iaincollins avatar Jul 06 '20 12:07 iaincollins

This isn't a bug we can fix but but I'm happy to consider making it easier to debug by totalling the cookie size on requests and spitting out a warning on the console if the resulting JWT size is dangerously large and likely to cause a problem.

This is not something other frameworks normally do and it's really the problem for a library like NextAuth.js and am inclined to suggest we improve this through documentation rather than code.

Anything we do in code has little bearing on what happens in production – e.g. you might have a 2 KB cookie in dev/test, and it works fine there, but when you add analytics and tracking cookies and advertisements to a site in production, you might start finding it breaks - and combined with being behind a CDN (which often filter what cookies servers see, something that is very common in AWS CloudFront configurations) it is not something we can even reliably detect might be happening.

iaincollins avatar Jul 06 '20 12:07 iaincollins

Thank you for the speedy and detailed response! I thought this may be the case, and I agree with the sentiment of improving this through documentation rather than code, a note in the docs could be useful, this issue was a head scratcher at first! I will look at a temporary secure storage in the meantime!

brett-chisholm avatar Jul 06 '20 13:07 brett-chisholm

Thanks for the feedback! I'll leave this open till I've taken an action to update the documentation for this (will likely roll it into the update for the docs for v3).

iaincollins avatar Jul 06 '20 13:07 iaincollins