-
-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Security and performance risk with getUser
and getSession
#898
Comments
Be careful here. The code doesn't seem to take into account when cookies are chunked for storage. |
@j4w8n The supabase client is a |
@Zanzofily ok. I was just going by grabbing the cookie with next stuff here and thought that might be an issue if the the session is broken into two cookies. |
@j4w8n You are right, I see what you mean now.
|
Both |
I see. Someone opened a PR to handle this on the workaround package I created, would merge it once it's ready. |
Can we use react 19's cache function to wrap supabase.auth.getUser? If we then call this function in multiple RSCs it should only make a single request to the auth server, right? |
|
I think you meant |
Bug report
Describe the bug
When using the
getUser
function from the Supabase auth client in Next.js server components, excessive database queries are generated upon each page refresh. When trying to usegetUser
to go around that, it solely checks JWT format and expiry without verifying authenticity.To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
getUser()
in multiple components -> database overloadgetSession()
-> unsafeExpected behavior
Please refer to the class I wrote, and let me know if it's correct, it should be proper Jwt verification.
An alternative solution would be endpoint that only returns decoded and verified Jwt
Screenshots
System information
Additional context
While implementing authentication with Next.js server components using this package, I observed an excessive number of database queries being generated with each page refresh. Initially, I was using the
getUser
function, which retrieves user data from the database on every invocation. This approach led to the performance issue noted.Investigation
Upon reviewing the documentation, I realized that
getSession
should have been used instead ofgetUser
for maintaining sessions without repeatedly hitting the database. However, further investigation revealed thatgetSession
does not provide proper security checks, as it only verifies the format and expiry of JWTs without validating their authenticity.Relevant Discussions and Changes
I came across a discussion in GitHub issue #873 from the
supabase/auth-js
repository, which suggested usinggetUser
initially to eliminate invalid cookies beforegetSession
. This method was flawed as it triggered warnings and was inefficient.A recent commit (59ec9aff) addressed these warnings but introduced a potential security risk by failing to properly verify JWTs. If this new implementation applied to getSession as well it would only check the format and expiry of the tokens, which are vulnerable to spoofing.
Temporary Workaround
I have developed a workaround supabase-safesession that ensures more secure session handling in my application. Feel free to use it for now if the maintainers verified its correctness.
The text was updated successfully, but these errors were encountered: