fix(security)!: mitigate session token hashing vulnerability - #246
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
📝 WalkthroughWalkthroughStateful session handling now stores hashed session tokens, hashes cookie tokens before adapter lookups, and returns plaintext tokens for cookies and authentication responses. Tests were updated to reflect the new token representation. ChangesStateful session-token hashing
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested labels: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/core/test/actions/providers/connected/stateful.test.ts (1)
76-92: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winAssert that every session lookup uses the hash.
Both tests configure two sequential
getSessionByTokenMockresults.toHaveBeenCalledWith(tokenHash)only checks that one call matches. A raw-token lookup in the other call can pass this test. Assert both calls withtoHaveBeenNthCalledWithand assert the expected call count.Proposed test assertion
- expect(getSessionByTokenMock).toHaveBeenCalledWith(tokenHash) + expect(getSessionByTokenMock).toHaveBeenCalledTimes(2) + expect(getSessionByTokenMock).toHaveBeenNthCalledWith(1, tokenHash) + expect(getSessionByTokenMock).toHaveBeenNthCalledWith(2, tokenHash)Also applies to: 111-128
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/core/test/actions/providers/connected/stateful.test.ts` around lines 76 - 92, Update the session lookup assertions in both affected tests around getSessionByTokenMock to verify the exact number of calls and assert each sequential call with toHaveBeenNthCalledWith using tokenHash, ensuring no lookup uses the raw token.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/core/src/session/stateful/destroySession.ts`:
- Around line 34-35: Update stateful session lookup paths, including
destroySession, refreshSession, getSession, and token-provider lookups, to
recognize legacy cookies containing the stored tokenHash without adding a
permanent raw-cookie fallback. Implement a bounded migration/rotation that
replaces legacy cookies with the current format and preserves revocation,
refresh, and session validation; alternatively, explicitly document and enforce
forced logout consistently.</code>
In `@packages/core/src/shared/utils/api.ts`:
- Around line 59-61: Guard sessionToken in the session lookup flow before
calling createHash, matching the presence check used by getProviderTokens and
revokeToken. When the cookie is missing, follow the existing explicit
missing-session path and return without invoking getSessionByToken; preserve
hashing and adapter lookup for present tokens.
---
Outside diff comments:
In `@packages/core/test/actions/providers/connected/stateful.test.ts`:
- Around line 76-92: Update the session lookup assertions in both affected tests
around getSessionByTokenMock to verify the exact number of calls and assert each
sequential call with toHaveBeenNthCalledWith using tokenHash, ensuring no lookup
uses the raw token.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: a9238ec8-782b-4c02-b9aa-1e5980f88272
📒 Files selected for processing (25)
packages/core/src/session/stateful/createSession.tspackages/core/src/session/stateful/destroySession.tspackages/core/src/session/stateful/getProviderTokens.tspackages/core/src/session/stateful/getSession.tspackages/core/src/session/stateful/isProviderConnected.tspackages/core/src/session/stateful/oauthCallback.tspackages/core/src/session/stateful/refreshSession.tspackages/core/src/session/stateful/revokeToken.tspackages/core/src/session/stateful/signInCredentials.tspackages/core/src/session/stateful/signUp.tspackages/core/src/shared/utils/api.tspackages/core/test/actions/providers/connected/stateful.test.tspackages/core/test/actions/providers/tokens/revoke/stateful.test.tspackages/core/test/actions/providers/tokens/tokens/stateful.test.tspackages/core/test/actions/providers/user/refresh/stateful.test.tspackages/core/test/actions/session/session/stateful.test.tspackages/core/test/api/stateful/getAccessToken.test.tspackages/core/test/api/stateful/getProviderTokens.test.tspackages/core/test/api/stateful/getSession.test.tspackages/core/test/api/stateful/isProviderConnected.test.tspackages/core/test/api/stateful/refreshUserInfo.test.tspackages/core/test/api/stateful/revokeToken.test.tspackages/core/test/api/stateful/signOut.test.tspackages/core/test/api/stateful/updateSession.test.tspackages/elysia/test/stateful/index.test.ts
Description
This pull request fixes a security vulnerability in the Stateful session strategy related to session token handling.
Previously, the internal session creation flow returned the hashed session token instead of the original randomly generated secret. As a result, the value stored in the database was also used as the authentication credential. If an attacker obtained a copy of the database, they could reuse the stored token hash directly to authenticate requests and hijack active sessions.
This PR changes the session token model to follow a more secure split secret / hash design:
Because the database never stores the raw session token, a database compromise no longer exposes reusable session credentials.
Key Changes
Security Benefits
Previously
Now
Note
This change is internal and fully backward-compatible from the API perspective. Applications do not need to modify their authentication flows, as the token hashing and verification process is handled transparently by the Stateful session strategy.
@coderabbitai ignore