Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 2b4051f

Browse files
authored
fix: prevent sending expired tokens (#437)
code does a quick check on the exp of a token to prevent it from being sent to realtime
1 parent 82e9966 commit 2b4051f

File tree

4 files changed

+241
-39
lines changed

4 files changed

+241
-39
lines changed

package-lock.json

Lines changed: 132 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@supabase/node-fetch": "^2.6.14",
4242
"@types/phoenix": "^1.5.4",
4343
"@types/ws": "^8.5.10",
44-
"ws": "^8.14.2"
44+
"ws": "^8.18.0"
4545
},
4646
"devDependencies": {
4747
"@arethetypeswrong/cli": "^0.16.2",
@@ -51,6 +51,7 @@
5151
"esm": "^3.2.25",
5252
"jsdom": "^16.7.0",
5353
"jsdom-global": "3.0.0",
54+
"jsonwebtoken": "^9.0.2",
5455
"mock-socket": "^9.0.3",
5556
"npm-run-all": "^4.1.5",
5657
"nyc": "^15.1.0",
@@ -62,4 +63,4 @@
6263
"vitest": "^2.0.5",
6364
"web-worker": "1.2.0"
6465
}
65-
}
66+
}

src/RealtimeClient.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,24 @@ export default class RealtimeClient {
335335
* @param token A JWT string.
336336
*/
337337
setAuth(token: string | null): void {
338+
if (token) {
339+
let parsed = null
340+
try {
341+
parsed = JSON.parse(atob(token.split('.')[1]))
342+
} catch (_error) {}
343+
if (parsed && parsed.exp) {
344+
let now = Math.floor(Date.now() / 1000)
345+
let valid = now - parsed.exp < 0
346+
if (!valid) {
347+
this.log(
348+
'auth',
349+
`provided token has expired, not sending it to realtime`
350+
)
351+
return
352+
}
353+
}
354+
}
355+
338356
this.accessToken = token
339357

340358
this.channels.forEach((channel) => {

0 commit comments

Comments
 (0)