feat: implement refresh token revocation#1541
feat: implement refresh token revocation#1541yogeshchoudhary147 wants to merge 1 commit intomainfrom
Conversation
| } else if (event.data.type === 'refresh') { | ||
| messageHandler(event as MessageEvent<WorkerRefreshTokenMessage>); | ||
| } | ||
| }; |
Check warning
Code scanning / CodeQL
Missing origin verification in `postMessage` handler Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 12 days ago
In general, to fix a missing origin verification in a postMessage handler you must (a) restrict which senders are trusted (by checking event.origin and/or event.source in window contexts), and (b) validate that the incoming data is of the expected structure before using it. In a worker context, origin may not always be present, but we can still defend by checking for the presence of origin when available and by validating the message type.
For this code, the minimal and safest fix without changing functionality is:
- Guard
messageRouterso it only processes messages whosedataobject is of the expected shape and explicitly matches the allowedtypevalues ('revoke'or'refresh'). - Add an origin check that:
- If
event.originis defined (e.g., in environments where workers do get an origin), requires it to match a whitelist of trusted origins. - If
event.originisundefined, we fall back to existing behavior to avoid breaking dedicated-worker usage, but we still benefit from stricttypechecks.
- If
- Optionally, ignore or log all messages that fail these checks instead of routing them.
Because we must not assume anything about configuration elsewhere, we’ll define a small local helper to decide whether an event is trusted and use it inside messageRouter. All changes are confined to src/worker/token.worker.ts, around the messageRouter definition (lines 268–275). No new imports or external libraries are required.
| @@ -267,9 +267,27 @@ | ||
|
|
||
| // Message router to handle both refresh and revoke operations | ||
| const messageRouter = (event: MessageEvent) => { | ||
| if (event.data.type === 'revoke') { | ||
| const data: any = event && event.data; | ||
|
|
||
| // Basic validation of incoming message shape | ||
| if (!data || (data.type !== 'revoke' && data.type !== 'refresh')) { | ||
| return; | ||
| } | ||
|
|
||
| // If an origin is available (e.g. in some environments), enforce a simple | ||
| // allowlist check. In environments where origin is not provided for worker | ||
| // messages, this check is skipped to preserve existing behavior. | ||
| const allowedOrigins = ['https://www.example.com']; | ||
| if (typeof (event as any).origin === 'string') { | ||
| const origin = (event as any).origin; | ||
| if (!allowedOrigins.includes(origin)) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (data.type === 'revoke') { | ||
| revokeMessageHandler(event as MessageEvent<WorkerRevokeTokenMessage>); | ||
| } else if (event.data.type === 'refresh') { | ||
| } else if (data.type === 'refresh') { | ||
| messageHandler(event as MessageEvent<WorkerRefreshTokenMessage>); | ||
| } | ||
| }; |
c3f184d to
724448e
Compare
bdd24d8 to
0f91a7d
Compare
361af92 to
55cf7f6
Compare
55cf7f6 to
c5df451
Compare
Summary
Adds a
revoke()method toAuth0Clientthat allows applications to explicitly revoke refresh tokens via the OAuth 2.0/oauth/revokeendpoint.refresh_tokenis cleared, allowing continued use until natural expiryuseRefreshTokensFallback) or surfaces amissing_refresh_tokenerror for the app to handleUsage
Test plan
Auth0Client.revoke(),revokeMessageHandler,revokeToken, andremoveByRefreshTokencovering success, error, timeout, MRRT, and edge cases