fix(security): stop exposing GitHub access token on client-visible session (#2845) - #3132
BCA-krishna wants to merge 2 commits into
Conversation
…ssion The NextAuth session() callback was copying the GitHub OAuth access token onto the session object, which is returned to the browser via useSession()/getSession(). Any JavaScript running on the page — including a successful XSS payload — could read it directly. Changes: - Remove session.accessToken assignment from the session() callback in src/lib/auth.ts; the token now stays only in the encrypted HttpOnly JWT cookie that NextAuth manages and never reaches the browser. - Add getAccessToken() to src/lib/get-session-token.ts: a server-only helper that reads the token directly from the JWT via next-auth/jwt getToken(), for use in API route handlers. - Update test/auth.test.ts to assert that accessToken is NOT present on the session object (was asserting the opposite before this fix). API routes that still read session.accessToken will be migrated to getAccessToken() in a follow-up PR to keep this change reviewable. 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):
|
|
Hi @Priyanshu-byte-coder , please review this PR. |
|
Same author, minimal version of #2875 — but removing |
Closes #2845
What changed and why
The NextAuth
session()callback insrc/lib/auth.tswas copying theraw GitHub OAuth access token onto the
sessionobject. Sincesessionis returned to the browser via
useSession()/getSession(), anyJavaScript on the page — including an XSS payload — could read the token
and gain full GitHub API access with the granted scopes.
Files changed (3 files only)
src/lib/auth.ts— removedsession.accessToken = token.accessTokenfrom the
session()callback. Token stays in the encrypted HttpOnlyJWT cookie only.
src/lib/get-session-token.ts— addedgetAccessToken(), aserver-onlyhelper that reads the token directly from the JWT vianext-auth/jwt'sgetToken(), for use in API route handlers.test/auth.test.ts— updated test to assert thataccessTokenisNOT present on the session object (previously asserted the opposite).
Follow-up
API routes that currently read
session.accessTokenwill be migrated togetAccessToken()in a follow-up PR. Those routes are all server-side(
src/app/api/) and never reach the browser, so they continue to workcorrectly via the session callback until migrated.
Testing
npm run type-check— 0 errorsnpm run lint— 0 errorsnpm test— auth.ts tests all pass (18/18)