Skip to content
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

feat: improved insecure getSession() warning #1024

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hf
Copy link
Contributor

@hf hf commented Jan 17, 2025

Previously when attempting to use getSession() from insecure storage such as cookies in SSR on the server, a disruptive warning was logged.

It's been a long while and most developers should already be conditioned that general use of getSession() for authorization on the server should not be used. There are, however, some valid use cases for getSession() that used to log a warning but now won't:

Does (any) session exist or not?

Most applications want to redirect the user to a login page if there's no session and probably are not interested in validating that the session is actually OK. This can be achieved with the following sample code:

const supabase = createServerClient(/* ... */)

const { data: session, error } = await supabase.auth.getSession()

if (!session) {
  // no warning logged
  return NextResponse.redirect('/login')
}

Simple boolean user check?

Note that this does not answer what user is there.

const supabase = createServerClient(/* ... */)

const { data: session, error } = await supabase.auth.getSession()

if (!session?.user) {
  // no warning logged
  return NextResponse.redirect('/login')
}

// but if you use
if (session?.user?.id === '284e7abb-785c-453c-bca4-a93da0825776') {
  // this will be true but a warning will be logged
}

// same for app_metadata or user_metadata

Just access an up-to-date access token to send to a private API?

const supabase = createServerClient(/* ... */)

const { data: session, error } = await supabase.auth.getSession()

await fetch(URL, {
  headers: {
    Authorization: `Bearer ${session.access_token}`
  },
})

@hf hf force-pushed the hf/improve-insecure-user-warning branch 2 times, most recently from f7fa563 to abf2001 Compare January 17, 2025 12:56
@hf hf force-pushed the hf/improve-insecure-user-warning branch from abf2001 to 79b41a6 Compare January 17, 2025 12:58
src/GoTrueClient.ts Outdated Show resolved Hide resolved
src/lib/helpers.ts Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants