fix(auth): sign session cookie to prevent forged-id account takeover#22
Open
prashant273000 wants to merge 1 commit into
Open
fix(auth): sign session cookie to prevent forged-id account takeover#22prashant273000 wants to merge 1 commit into
prashant273000 wants to merge 1 commit into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The
splitt_sessioncookie stored the rawuser.id, and the auth middleware trusted that value directly to look up the logged-in user. Because user IDs are not secret—they're returned in normal API responses (poster.id,participants[].userId,otherUser.id,getUserById, etc.)—any authenticated user could read another user's ID and replay it as their own cookie to fully impersonate that account.This PR signs the session cookie so its value can no longer be forged or derived from public data.
Vulnerability
backend/src/modules/auth/auth.routes.jsset the cookie touser.idverbatim, with no signature or encryption.backend/src/middleware/auth.jsreadreq.cookies.splitt_sessionand looked the user up directly, trusting whatever value the client sent.httpOnlyonly prevents JavaScript from reading the cookie (XSS). It does not protect against forged requests.Cookie: splitt_session=<victim-id>and be authenticated as the victim.
Fix
Sign the cookie using
cookie-parser's built-in HMAC signing (already a dependency, so no new packages are required).Changes
app.jsInitialize the parser with a secret:
auth.routes.jsSet both login cookies (
/dev-login,/google/token) with:signed: truemiddleware/auth.jsRead from:
instead of:
req.signedCookiesonly returns a value when the cookie's HMAC signature is valid..env.exampleDocument the new environment variable:
Security Impact
A forged cookie:
splitt_session=<some-id>does not contain a valid signature, so:
returns
undefined, and the request is treated as unauthenticated.An attacker cannot generate a valid signature without knowing
SESSION_SECRET.Why This Approach
cookie-parseralready provides signing functionality.Files Changed
backend/src/app.jsSESSION_SECRETtocookieParserbackend/src/middleware/auth.jsreq.signedCookiesbackend/src/modules/auth/auth.routes.jssigned: trueon both login cookiesbackend/.env.exampleSESSION_SECRETPlease add a long, random
SESSION_SECRETto .env file.Fixes #18