fix(security): remove GitHub access token from client-exposed session#2875
Open
BCA-krishna wants to merge 2 commits into
Open
fix(security): remove GitHub access token from client-exposed session#2875BCA-krishna wants to merge 2 commits into
BCA-krishna wants to merge 2 commits into
Conversation
- Stop copying accessToken onto the NextAuth session object in the session() callback (auth.ts), since session is exposed to client-side JS via useSession()/getSession() and was readable via XSS (Priyanshu-byte-coder#2845) - Add getAccessToken() helper (lib/get-session-token.ts) that reads the token server-side only, directly from the encrypted JWT cookie via next-auth/jwt's getToken() - Migrate all API routes that previously read session.accessToken to use the new server-only getAccessToken() helper instead - Update/add tests to mock getAccessToken() and verify accessToken is no longer present on the session object Closes Priyanshu-byte-coder#2845
GSSoC Label Checklist 🏷️@Priyanshu-byte-coder — please apply the appropriate labels before merging: Difficulty (pick one):
Quality (optional):
Validation (required to score):
|
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.
Closes #2845
Problem
The NextAuth
session()callback copied the GitHub access token ontothe
sessionobject. Sincesessionis exposed to client-side code viauseSession()/getSession(), the raw GitHub token was readable by anyJavaScript running on the page — including a successful XSS payload —
giving full access to the user's GitHub account with the granted scopes.
Fix
session.accessToken = token.accessTokenfrom thesession()callback in
src/lib/auth.ts. The token now stays only in theencrypted, HttpOnly JWT cookie that NextAuth manages — it never reaches
the browser.
getAccessToken()helper insrc/lib/get-session-token.tsthat reads the token server-side only, directly from the JWT via
next-auth/jwt'sgetToken().session.accessTokentocall
getAccessToken()instead (29 route files).getAccessToken()and to assert thataccessTokenis never present on the session object returned to theclient.
Testing
npm run type-check— 0 errorsnpm run lint— 0 errors (5 pre-existing unrelated<img>warnings)npm test— same failure count asmain(all pre-existing/unrelatedto this change — verified via
git stashcomparison); no new failuresintroduced by this fix