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

fix: limit proxy session warning to once per client instance #900

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1113,14 +1113,16 @@ export default class GoTrueClient {

if (!hasExpired) {
if (this.storage.isServer) {
const suppressWarning = this.suppressGetSessionWarning
let suppressWarning = this.suppressGetSessionWarning
const proxySession: Session = new Proxy(currentSession, {
get(target: any, prop: string, receiver: any) {
get: (target: any, prop: string, receiver: any) => {
if (!suppressWarning && prop === 'user') {
// only show warning when the user object is being accessed from the server
console.warn(
'Using the user object as returned from supabase.auth.getSession() or from some supabase.auth.onAuthStateChange() events could be insecure! This value comes directly from the storage medium (usually cookies on the server) and many not be authentic. Use supabase.auth.getUser() instead which authenticates the data by contacting the Supabase Auth server.'
)
suppressWarning = true // keeps this proxy instance from logging additional warnings
this.suppressGetSessionWarning = true // keeps this client's future proxy instances from warning
}
return Reflect.get(target, prop, receiver)
},
Expand Down
16 changes: 14 additions & 2 deletions test/GoTrueClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ describe('GoTrueClient with storageisServer = true', () => {
expect(warnings.length).toEqual(0)
})

test('getSession() emits insecure warning when user object is accessed', async () => {
test('getSession() emits insecure warning, once per server client, when user object is accessed', async () => {
const storage = memoryLocalStorageAdapter({
[STORAGE_KEY]: JSON.stringify({
access_token: 'jwt.accesstoken.signature',
Expand All @@ -966,14 +966,26 @@ describe('GoTrueClient with storageisServer = true', () => {
data: { session },
} = await client.getSession()

const user = session?.user // accessing the user object from getSession should emit a warning
const user = session?.user // accessing the user object from getSession should emit a warning the first time
expect(user).not.toBeNull()
expect(warnings.length).toEqual(1)
expect(
warnings[0][0].startsWith(
'Using the user object as returned from supabase.auth.getSession() '
)
).toEqual(true)

const user2 = session?.user // accessing the user object further should not emit a warning
expect(user2).not.toBeNull()
expect(warnings.length).toEqual(1)

const {
data: { session: session2 },
} = await client.getSession() // create new proxy instance

const user3 = session2?.user // accessing the user object in subsequent proxy instances, for this client, should not emit a warning
expect(user3).not.toBeNull()
expect(warnings.length).toEqual(1)
})

test('getSession emits no warnings if getUser is called prior', async () => {
Expand Down