Repository navigation
fix(auth): return AuthInvalidJwtError when a JWT header or payload is not a JSON object - #2720
iamhuman-cheolheelee wants to merge 1 commit into
Conversation
… not a JSON object
decodeJWT called JSON.parse on the decoded header and payload without a guard,
so a token whose segments are valid base64url but not JSON made getClaims(),
setSession() and getAuthenticatorAssuranceLevel(jwt) throw a raw SyntaxError
instead of returning { data: null, error }. A JSON null payload surfaced as
"Cannot read properties of null (reading 'exp')".
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (3)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. 📝 SummarySummary by CodeRabbit
Walkthrough
Priority: ⬇️ Low Change: Bug fix Merge Risk: ⚪ Minimal · up to The JWT validation change appears ready to merge after normal checks. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to Malformed JWT sections now produce a consistent authentication error instead of an uncaught parsing error. The reviewed paths reject those tokens before returning claims or making a verification request. No introduced security issue was identified, though downstream coverage is incomplete. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🔍 Description
What changed?
decodeJWT(packages/core/auth-js/src/lib/helpers.ts) now guards the twoJSON.parsecalls on the decoded header and payload:AuthInvalidJwtError('JWT header or payload is not valid JSON').null, an array, a number), it throwsAuthInvalidJwtError('JWT header or payload is not a JSON object').Tests:
test/helpers.test.ts: two newdecodeJWTcases.test/GoTrueClient.getClaims.test.ts(new): an offline test with mocked fetch. It checks thatgetClaims()returns{ data: null, error: AuthInvalidJwtError }for all three malformed shapes and never calls the network.Why was this change needed?
decodeJWTalready rejects tokens that don't have three base64url segments, but it passed the decoded segments straight toJSON.parse. A token likebase64url("not json").e30.c2lnpasses the segment check. TheSyntaxErrorfromJSON.parseis not anAuthError, so callers that only mapAuthErrors rethrow it:getClaims()(GoTrueClient.ts~L6727): itscatchreturns{ error }only forisAuthError(error). Otherwise it rethrows, soawait supabase.auth.getClaims(token)rejects instead of resolving._setSession(~L3627) and_getAuthenticatorAssuranceLevel(jwt)(~L6094) follow the same pattern.A payload of
nullis valid JSON, so it gets pastJSON.parse. It then fails later withCannot read properties of null (reading 'exp').Behaviour on
master(6d21e3f):not jsongetClaimsrejects withSyntaxError{ data: null, error: AuthInvalidJwtError }{badgetClaimsrejects withSyntaxError{ data: null, error: AuthInvalidJwtError }nullAuthInvalidJwtError: Cannot read properties of null (reading 'exp'){ data: null, error: AuthInvalidJwtError }with a clear messageThis is the same contract #2395 established for expired JWTs:
getClaimsreports bad input througherror, not by throwing.Flow (mermaid)
flowchart TD A[getClaims token] --> B[decodeJWT] B --> C{3 base64url segments?} C -- no --> E[AuthInvalidJwtError] C -- yes --> D{JSON.parse header and payload} D -- throws --> F[before: SyntaxError escapes, getClaims rejects] D -- throws --> E2[after: AuthInvalidJwtError] D -- ok --> G{both plain objects?} G -- no --> H[before: TypeError on payload.exp] G -- no --> E2 G -- yes --> I[verify signature / claims] E2 --> J[getClaims returns data null, error] E --> J📸 Screenshots/Examples
🔄 Breaking changes
Well-formed tokens behave exactly as before. Malformed tokens that used to throw now return an
AuthInvalidJwtError, which is what thegetClaims/setSessionreturn type already promises.📋 Checklist
<type>(<scope>): <description>pnpm nx formatto ensure consistent code formatting📝 Additional notes
Testing (
packages/core/auth-js,npx jest --config jest.config.cli.js test/helpers.test.ts test/GoTrueClient.getClaims.test.ts):tsc --noEmitis clean fortsconfig.jsonandtsconfig.test.json, and the build succeeds.cc @mandarini, since this extends the
getClaimserror contract you tightened in #2395.I used an AI assistant (Claude Code) while investigating and writing this change, and I reviewed and tested it by hand.
🤖 Generated with Claude Code