fix(connect): validate OAuth state nonce server-side to prevent CSRF (#223)#558
Open
Ridanshi wants to merge 2 commits into
Open
fix(connect): validate OAuth state nonce server-side to prevent CSRF (#223)#558Ridanshi wants to merge 2 commits into
Ridanshi wants to merge 2 commits into
Conversation
|
@Ridanshi is attempting to deploy a commit to the Prashantkumar Khatri's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @Ridanshi, Thanks for opening this pull request. This PR has been automatically classified based on the files modified. Applied Labels
Primary Review Area
Reviewer@Harxhit has been identified as the primary reviewer for this pull request. If you have any questions regarding the affected area or implementation details, feel free to reach out to the assigned reviewer. Thank you for your contribution! |
CI — Checks FailedBackend — FAIL
Mobile — SKIP
Web — SKIP
Last updated: |
…loses Dev-Card#223) Replace client-side-only state validation in the GitHub connect flow with server-side nonce verification backed by Redis. - Generate a 32-byte cryptographically secure nonce on connect initiation - Store {userId} in Redis under oauth:connect-nonce:<nonce> with a 10-minute TTL - State parameter carries only the nonce; userId is never trusted from the callback - On callback: validate nonce format, look up Redis, reject unknown/expired nonces - Delete nonce immediately after first successful validation (replay protection) - Drop Math.random()-based state generation in favour of randomBytes(32) - Add 13 tests covering: valid flow, missing params, malformed state, unknown nonce, expired nonce, forged state, replay attack, corrupt Redis data, token exchange failure
…ormat Update the connect-route test harness in oauth-scope.test.ts to match the new state format: state now carries only a 64-char hex nonce, with userId stored server-side in Redis. Add a Redis mock to buildConnectApp so the callback route can look up the userId during tests.
47763f7 to
544181d
Compare
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
Closes #223.
The GitHub connect flow (
/api/connect/github) accepteduserIddirectly from the unverifiedstatecallback parameter and usedMath.random()to generate the state value. An attacker could forge a callback with a crafted state, causing the server to associate the victim's account with an attacker-controlled GitHub credential.The login flow (
/auth/github,/auth/google) is addressed separately via the cookie-based double-submit CSRF fix already present onmain(#481).Changes
apps/backend/src/routes/connect.ts/connect/githubinitiation{ userId }in Redis underoauth:connect-nonce:<nonce>with a 10-minute TTLstateparameter —userIdnever travels through the callback URLMath.random()withrandomBytes(32)github_followplatform key to prevent auth-token overwritesapps/backend/src/__tests__/connect.test.tsapps/backend/src/__tests__/oauth-scope.test.tsbuildConnectAppandmakeConnectStateto match new nonce-only state formatSecurity properties
Math.random)randomBytes(32))userIdsource in callbackTests (connect.test.ts — 14 tests)
github_followplatform assertionuserIdand TTLcodeparameterstateparameterTest plan
npx vitest run src/__tests__/connect.test.ts src/__tests__/oauth-scope.test.ts src/__tests__/follow.test.ts src/__tests__/profiles.test.ts— 52/52 passevent.test.tsandanalytics.test.tsare unrelated to this change (upstream test infrastructure issue withrequest.usernot being set by thejwtVerifymock)