ssr: Error: Invalid UTF-8 sequence crashing Vercel
Bug report
- [x ] I confirm this is a bug with Supabase, not with my own application.
- [ x] I confirm I have searched the Docs, GitHub Discussions, and Discord.
Describe the bug
On version
"@supabase/ssr": "^0.4.0",
Error: Invalid UTF-8 sequence at (node_modules/@supabase/ssr/dist/module/utils/base64url.js:187:0) at (node_modules/@supabase/ssr/dist/module/utils/base64url.js:90:0) at (node_modules/@supabase/ssr/dist/module/cookies.js:246:30) at (node_modules/@supabase/auth-js/dist/module/lib/helpers.jsđź’Ż0) at (node_modules/@supabase/auth-js/dist/module/GoTrueClient.js:787:0) at (node_modules/@supabase/auth-js/dist/module/GoTrueClient.js:768:0) at (node_modules/@supabase/auth-js/dist/module/GoTrueClient.js:714:0)
This us a super weird bug where when I visit my website it just literally crashes. However clearing my cookies the website works for a bit then stops.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
I do not know how the bug occurs.
Screenshots
If applicable, add screenshots to help explain your problem.
System information
Running on a nextjs app with supabase. My app allows google logins
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 39 (9 by maintainers)
Solution: Progressive Token Fragment Parsing to Fix Invalid UTF-8 Sequence
I found the root cause and implemented a working solution for this UTF-8 sequence error.
Root Cause Analysis
The issue occurs when Supabase authentication tokens are split into multiple cookie chunks, but not all chunks are necessary for the current session. In my case:
How to Reproduce
You can reproduce this issue by manually creating extra cookie chunks:
sb-*-auth-token.0,sb-*-auth-token.1cookiessb-*-auth-token.2cookie with random base64 contentSolution: Progressive Token Fragment Parsing
I implemented a middleware solution that progressively parses token fragments until a valid token is found, then discards unnecessary fragments.
Core Logic
Implementation in Middleware
Additional Cleanup (Optional)
You can also delete the unnecessary cookie fragments from the client:
Results
This solution has been running in production for several weeks without issues.
Testing
To test this fix:
DEBUG='middleware:cookie'to see optimization logsHope this helps others experiencing the same issue!
Maybe I’m missing something, but it’s kind of insane. Any error shouldn’t crash the whole app for the user, instead it should show some feedback. But for some reason I cannot handle this error…
I did find a mechanism in the cookie-chunking logic, to where it’s possible to have a “middle” cookie size of less than the typical 3216. After the whole value is encoded with
encodeURIComponent(), it tries to make sure it’s not splitting a character escape sequence between cookie chunks.All of that to say it’s possible to have a couple of cookie sizes that could’ve been rolled into one cookie, but they were trying to do the above, so the content got split. If that makes sense.
Our users are facing this too - and it’s pretty debilitating as it happens very often
We’re having the same issue:
In our case, the auth cookie is split into 3 parts:
token.0,token.1andtoken.2.The combination of
token.0+token.1is valid base64 ✅token.0+token.2is valid base64 ✅token.0+token.1+token.2is invalid ❌token.0andtoken.2have the same cookie expiry date, so apparently the have been created at the same time,token.1has a different expiry date which is dated after the other two tokens.Maybe Supabase detects that the first part can stay unchanged, only sets the second (
.1) part and “forgets” to unset the third (.2) part?Hi @fincha, thanks for your insight! I notice the same thing that deleting all cookies resolves the issue; however only temporarily. This issue always comes back after the session times out (3600 seconds) and the user is on a page that checks
supabase.auth.getUser()I am not modifying the cookies/JWT via hooks or anything else. I also added the “cookieEncoding”: “raw” prop, too. I already updated the codebase so I don’t want to face any regressions by downgrading to
@supabase/ssr": "^0.0.10as suggested by @Nishchit14.Anyone else found a solution to this? If it applies I am authenticating through Azure but have not been able to resolve from that end.
Thanks! 👾🚀
Trying and will get back to you (Apologies, accidentally closed the task. reopened it now)
@k2xl
@supabase/ssr": "^0.0.10