Skip to content

Commit 3661130

Browse files
authored
fix: update session warning (#879)
## What kind of change does this PR introduce? * removes the warning from being logged everytime `getSession` is called * only log the warning if the user property is being accessed from the session * addresses #873, supabase/supabase-js#1010
1 parent f66711d commit 3661130

File tree

3 files changed

+10
-51
lines changed

3 files changed

+10
-51
lines changed

.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"browser": true,
44
"es2021": true
55
},
6-
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
6+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"],
77
"parser": "@typescript-eslint/parser",
88
"parserOptions": {
99
"ecmaVersion": 12,

src/GoTrueClient.ts

+6-30
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ export default class GoTrueClient {
167167
protected logDebugMessages: boolean
168168
protected logger: (message: string, ...args: any[]) => void = console.log
169169

170-
protected insecureGetSessionWarningShown = false
171-
172170
/**
173171
* Create a new client for use in the browser.
174172
*/
@@ -932,15 +930,6 @@ export default class GoTrueClient {
932930
})
933931
})
934932

935-
if (result.data && this.storage.isServer) {
936-
if (!this.insecureGetSessionWarningShown) {
937-
console.warn(
938-
'Using supabase.auth.getSession() is potentially insecure as it loads data directly from the storage medium (typically cookies) which may not be authentic. Prefer using supabase.auth.getUser() instead. To suppress this warning call supabase.auth.getUser() before you call supabase.auth.getSession().'
939-
)
940-
this.insecureGetSessionWarningShown = true
941-
}
942-
}
943-
944933
return result
945934
}
946935

@@ -1120,26 +1109,18 @@ export default class GoTrueClient {
11201109

11211110
if (!hasExpired) {
11221111
if (this.storage.isServer) {
1123-
let user = currentSession.user
1124-
1125-
delete (currentSession as any).user
1126-
1127-
Object.defineProperty(currentSession, 'user', {
1128-
enumerable: true,
1129-
get: () => {
1130-
if (!(currentSession as any).__suppressUserWarning) {
1131-
// do not suppress this warning if insecureGetSessionWarningShown is true, as the data is still not authenticated
1112+
const proxySession: Session = new Proxy(currentSession, {
1113+
get(target: any, prop: string, receiver: any) {
1114+
if (prop === 'user') {
1115+
// only show warning when the user object is being accessed from the server
11321116
console.warn(
11331117
'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.'
11341118
)
11351119
}
1136-
1137-
return user
1138-
},
1139-
set: (value) => {
1140-
user = value
1120+
return Reflect.get(target, prop, receiver)
11411121
},
11421122
})
1123+
currentSession = proxySession
11431124
}
11441125

11451126
return { data: { session: currentSession }, error: null }
@@ -1174,11 +1155,6 @@ export default class GoTrueClient {
11741155
return await this._getUser()
11751156
})
11761157

1177-
if (result.data && this.storage.isServer) {
1178-
// no longer emit the insecure warning for getSession() as the access_token is now authenticated
1179-
this.insecureGetSessionWarningShown = true
1180-
}
1181-
11821158
return result
11831159
}
11841160

test/GoTrueClient.test.ts

+3-20
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ describe('GoTrueClient with storageisServer = true', () => {
927927
warnings = []
928928
})
929929

930-
test('getSession() emits two insecure warnings', async () => {
930+
test('getSession() emits no warnings', async () => {
931931
const storage = memoryLocalStorageAdapter({
932932
[STORAGE_KEY]: JSON.stringify({
933933
access_token: 'jwt.accesstoken.signature',
@@ -945,26 +945,9 @@ describe('GoTrueClient with storageisServer = true', () => {
945945
const client = new GoTrueClient({
946946
storage,
947947
})
948+
await client.getSession()
948949

949-
const {
950-
data: { session },
951-
} = await client.getSession()
952-
953-
console.log('User is ', session!.user!.id)
954-
955-
const firstWarning = warnings[0]
956-
const lastWarning = warnings[warnings.length - 1]
957-
958-
expect(
959-
firstWarning[0].startsWith(
960-
'Using supabase.auth.getSession() is potentially insecure as it loads data directly from the storage medium (typically cookies) which may not be authentic'
961-
)
962-
).toEqual(true)
963-
expect(
964-
lastWarning[0].startsWith(
965-
'Using the user object as returned from supabase.auth.getSession() '
966-
)
967-
).toEqual(true)
950+
expect(warnings.length).toEqual(0)
968951
})
969952

970953
test('getSession() emits one insecure warning', async () => {

0 commit comments

Comments
 (0)