fix(auth): stop exposing GitHub OAuth token in client-accessible session#874
Open
advikdivekar wants to merge 2 commits into
Open
fix(auth): stop exposing GitHub OAuth token in client-accessible session#874advikdivekar wants to merge 2 commits into
advikdivekar wants to merge 2 commits into
Conversation
Migrate all metrics API routes to retrieve the GitHub OAuth token server-side via getGitHubAccessToken(req) instead of reading the now-removed session.accessToken field. Covers contributions, prs, pr-review-time, streak, repos, repo-health, ci, and compare routes.
|
@advikdivekar is attempting to deploy a commit to the PRIYANSHU DOSHI's projects Team on Vercel. A member of the Team first needs to authorize it. |
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.
What the problem was
The GitHub OAuth access token — scoped to
repo(full private repository access) — was being copied into the NextAuthsessionobject insrc/lib/auth.tsand returned by the public/api/auth/sessionendpoint as plain JSON. Any client-side JavaScript on an authenticated page could callfetch('/api/auth/session')and read the raw token, making it trivially stealable via XSS.The
Sessioninterface insrc/types/next-auth.d.tsalso declaredaccessToken?: string, which made the leak part of the typed contract.What was changed and in which files
src/lib/auth.tsRemoved
session.accessToken = token.accessTokenfrom thesessioncallback. The token now stays exclusively inside the httpOnly JWT cookie and is never sent to the browser.src/types/next-auth.d.tsRemoved
accessTokenfrom theSessioninterface. Added a code comment explaining that the token lives only in the JWT and must be accessed server-side.src/lib/server-github-token.ts(new file)Added
getGitHubAccessToken(req: NextRequest): Promise<string | null>— a thin wrapper aroundgetToken({ req, secret })fromnext-auth/jwt. All server-side API routes call this instead of readingsession.accessToken.src/lib/validate-github-username.ts(new file)Added
isValidGitHubUsername(username)to reject strings that do not match GitHub's 1–39 character alphanumeric-plus-hyphen format, preventing injection of extra search qualifiers into GitHub API calls.Migrated API routes — all replaced
session.accessTokenwithgetGitHubAccessToken(req):src/app/api/metrics/contributions/route.tssrc/app/api/metrics/prs/route.tssrc/app/api/metrics/pr-review-time/route.tssrc/app/api/metrics/streak/route.tssrc/app/api/metrics/repos/route.tssrc/app/api/metrics/repo-health/route.tssrc/app/api/metrics/ci/route.tssrc/app/api/metrics/compare/route.tssrc/app/api/metrics/issues/route.tssrc/app/api/metrics/languages/route.tssrc/app/api/metrics/pinned-repos/route.tssrc/app/api/metrics/pr-breakdown/route.tssrc/app/api/metrics/weekly-summary/route.tsWhy this approach fixes the root cause
The root cause was architectural: the session callback was treating the access token as session data rather than server-only credential. By removing it from the session callback entirely and reading it only from the encrypted, httpOnly JWT inside API route handlers, the token can never appear in the
/api/auth/sessionJSON response — regardless of what client-side code does. ThegetGitHubAccessTokenhelper enforces this boundary for every call site.Multi-account paths (
accountId=combined, secondary accounts) were also updated — they pass the primary token retrieved from the JWT intogetAllAccounts, which then retrieves linked account tokens from the encrypted Supabase store.Steps to test
fetch('/api/auth/session').then(r => r.json()).then(console.log)githubLoginandgithubIdbut noaccessTokenfield/api/metrics/*routes return401Edge cases covered
accountIdparam): token read from JWT, passed to GitHub APIaccountId=combined): primary token retrieved from JWT, linked account tokens retrieved from SupabaseaccountId=<id>): primary account gets JWT token; secondary accounts get their stored tokensgetGitHubAccessTokenreturnsnull, route returns401getTokenreturnsnull, guarded by the null checkRegressions
None.
npm run type-checkandnpm run lintboth pass. All routes behave identically from the caller's perspective — only the token retrieval path changed fromsession.accessTokento server-side JWT extraction.Closes #856
Please review and merge this under GSSoC 2026.