Skip to content

fix(auth): raise AuthInvalidJwtError for malformed JWTs in decode_jwt - #2056

Merged
spydon merged 2 commits into
mainfrom
lukasklingsbo/sdk-2819-auth-malformed-jwts-raise
Nov 16, 2026
Merged

spydon merged 2 commits into
mainfrom
lukasklingsbo/sdk-2819-auth-malformed-jwts-raise

Conversation

@spydon

@spydon spydon commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Malformed tokens that passed base64 decoding escaped decode_jwt as a raw pydantic ValidationError or ValueError, so get_claims and set_session callers catching AuthError missed them.

  • decode_jwt validates the header and payload inside the try and maps every ValueError (base64, pydantic, non-ASCII) to AuthInvalidJwtError("Invalid JWT structure").
  • JWTHeader.typ is optional, as in RFC 7515.
  • Tests: decode_jwt cases for each malformed shape and a header without typ; async and sync get_claims raise AuthInvalidJwtError without any HTTP request.

SDK-2819

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 27 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 8 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 88846d12-7119-4000-a6c8-285766cc8789
Commits

Reviewing files that changed from the base of the PR and between c781578 and 84157f6.

Files selected for processing: 6

  • src/auth/tests/_async/conftest.py
  • src/auth/tests/_async/test_get_claims.py
  • src/auth/tests/_async/test_set_session.py
  • src/auth/tests/_sync/conftest.py
  • src/auth/tests/_sync/test_get_claims.py
  • src/auth/tests/_sync/test_set_session.py

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Invalid or malformed JWTs now produce a consistent authentication error instead of causing unexpected failures.
    • Claims retrieval rejects malformed tokens before sending a network request.
  • New Features
    • JWT headers without a typ value are now accepted, while valid decoded fields remain available.

decode_jwt now validates decoded JWT headers and payloads within its error-handling block. It converts ValueError raised during decoding or validation into AuthInvalidJwtError. JWTHeader.typ is optional and defaults to None. New tests cover malformed tokens and confirm that sync and async get_claims calls reject them before sending HTTP requests.

Possibly related PRs

  • supabase/supabase-py#1920: Maps ValueError from decode_jwt to AuthInvalidJwtError and tests malformed tokens in set_session.

Change: Bug fix

Merge Risk: Minimal · previous review c7815

Malformed JWTs now raise AuthInvalidJwtError instead of raw validation errors, so callers catching AuthError handle them correctly. No merge-blocking risk was identified.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ce363e5d-9117-4c1e-b523-3daf5a2a3a9d
Commits

Reviewing files that changed from the base of the PR and between 569e6fa and c781578.

Files selected for processing: 5

  • src/auth/src/supabase_auth/helpers.py
  • src/auth/src/supabase_auth/types.py
  • src/auth/tests/_async/test_get_claims.py
  • src/auth/tests/_sync/test_get_claims.py
  • src/auth/tests/test_helpers.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.

@spydon
spydon merged commit 868f681 into main Nov 16, 2026
26 checks passed
@spydon
spydon deleted the lukasklingsbo/sdk-2819-auth-malformed-jwts-raise branch November 16, 2026 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant