Repository navigation
fix(admin): revoke the session token at logout, not only its cookie #1145
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
Merged
cameri
merged 5 commits into
cameri:main
from
Priyanshubhartistm:fix/admin-session-logout-revocation
Nov 16, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
afb381f
fix(admin): revoke the session token at logout, not only its cookie
Priyanshubhartistm faea75e
fix(admin): bound the revocation cache calls and report a failed logout
Priyanshubhartistm 3c34b72
Merge branch 'main' into fix/admin-session-logout-revocation
Priyanshubhartistm 010173c
fix(admin): word the failed sign-out message without a false retry
Priyanshubhartistm 47b18b2
Merge branch 'main' into fix/admin-session-logout-revocation
cameri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| 'nostream': patch | ||
| --- | ||
|
|
||
| fix(admin): revoke the session token at logout, not only its cookie | ||
|
|
||
| `POST /admin/logout` deleted the browser cookie but the session token itself stayed valid until it expired (24 hours by default), because the token was only `<expiresAt>.<signature>` and nothing on the server recorded a logout. A copy of the token kept elsewhere (a `Bearer` client, a log, a proxy) could restore administrator access after sign-out. The token now carries a random session id under the same signature, logout records that id in Redis until the token would have expired, and every admin request checks it, so a logout on one worker ends the session on all of them and ends only that session. If Redis cannot be read the token is refused, and a logout that cannot record the id answers 500 (and still clears the cookie). | ||
|
|
||
| Tokens issued before this change have no session id and are refused, so every administrator signs in once more after the upgrade. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { ICacheAdapter } from '../@types/adapters' | ||
| import { IAdminSessionRevocations } from '../@types/admin' | ||
| import { RedisAdapter } from '../adapters/redis-adapter' | ||
| import { getCacheClient } from '../cache/client' | ||
|
|
||
| let cacheAdapter: ICacheAdapter | undefined | ||
|
|
||
| const getCache = (): ICacheAdapter => { | ||
| if (!cacheAdapter) { | ||
| cacheAdapter = new RedisAdapter(getCacheClient()) | ||
| } | ||
|
|
||
| return cacheAdapter | ||
| } | ||
|
|
||
| /** A stalled Redis connection must not leave an admin request waiting, so each cache call gets a deadline. */ | ||
| export const ADMIN_SESSION_REVOCATION_TIMEOUT_MS = 2000 | ||
|
|
||
| const withTimeout = <T>(operation: () => Promise<T>, timeoutMs: number, name: string): Promise<T> => | ||
| new Promise<T>((resolve, reject) => { | ||
| const timer = setTimeout(() => reject(new Error(`${name} timed out after ${timeoutMs} ms`)), timeoutMs) | ||
|
|
||
| Promise.resolve() | ||
| .then(operation) | ||
| .then(resolve, reject) | ||
| .finally(() => clearTimeout(timer)) | ||
| }) | ||
|
|
||
| export const adminSessionRevocationCacheKey = (sessionId: string): string => `admin:session:revoked:${sessionId}` | ||
|
|
||
| /** | ||
| * Revoked session ids live in the shared cache, so a logout handled by one worker ends the session on all of | ||
| * them. An entry only has to outlast the token, which is rejected on its own once it has expired. | ||
| */ | ||
| export class CacheAdminSessionRevocations implements IAdminSessionRevocations { | ||
| public constructor( | ||
| private readonly cache: () => ICacheAdapter = getCache, | ||
| private readonly timeoutMs: number = ADMIN_SESSION_REVOCATION_TIMEOUT_MS, | ||
| ) {} | ||
|
|
||
| public async revoke(sessionId: string, ttlSeconds: number): Promise<void> { | ||
| const expirySeconds = Number.isSafeInteger(ttlSeconds) && ttlSeconds > 0 ? ttlSeconds : 1 | ||
|
|
||
| await withTimeout( | ||
| () => this.cache().setKey(adminSessionRevocationCacheKey(sessionId), '1', expirySeconds), | ||
| this.timeoutMs, | ||
| 'revoking the admin session', | ||
| ) | ||
| } | ||
|
|
||
| public async isRevoked(sessionId: string): Promise<boolean> { | ||
| return withTimeout( | ||
| () => this.cache().hasKey(adminSessionRevocationCacheKey(sessionId)), | ||
| this.timeoutMs, | ||
| 'checking the admin session revocation', | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export const resetAdminSessionRevocationCacheAdapterForTests = (): void => { | ||
| cacheAdapter = undefined | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.