Skip to content

Commit

Permalink
fix: limit proxy session warning to once per instance
Browse files Browse the repository at this point in the history
  • Loading branch information
j4w8n committed May 6, 2024
1 parent fc145af commit 4c7d906
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1113,14 +1113,15 @@ 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) {
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
}
return Reflect.get(target, prop, receiver)
},
Expand Down
4 changes: 4 additions & 0 deletions test/GoTrueClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,10 @@ describe('GoTrueClient with storageisServer = true', () => {
'Using the user object as returned from supabase.auth.getSession() '
)
).toEqual(true)

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

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

0 comments on commit 4c7d906

Please sign in to comment.