Skip to content

Conversation

@gshmu
Copy link

@gshmu gshmu commented Aug 8, 2025

Apply "strict with self, lenient with others" philosophy to JWT subject validation:

  • Encode (strict): Validate sub must be string, raise TypeError if not
  • Decode (lenient): Remove string type validation for sub claim in _validate_sub
  • Tests: Update test to expect TypeError during encoding instead of decode error

Philosophy

This change embodies the principle of "严于律己,宽以待人" (be strict with yourself, lenient with others):

  • Strict encoding: Ensures PyJWT generates standards-compliant tokens
  • Lenient decoding: Allows PyJWT to consume tokens from other systems that may use non-string subject identifiers

Apply "strict with self, lenient with others" philosophy to subject validation:

- **Encode (strict)**: Validate sub must be string, raise TypeError if not
- **Decode (lenient)**: Remove string type validation for sub claim
- **Tests**: Update test_encode_with_invalid_int_sub_claim to expect TypeError

This change allows PyJWT to be strict when creating tokens (ensuring standards
compliance) while being forgiving when consuming tokens from other systems
that may use non-string subject identifiers.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@auvipy auvipy requested review from auvipy and Copilot August 8, 2025 11:58
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a "strict encode, lenient decode" approach for JWT subject claim validation. When encoding JWTs, PyJWT will strictly validate that the sub claim is a string and raise a TypeError if not. When decoding JWTs, PyJWT will be more lenient and accept non-string subject identifiers from other systems.

  • Adds string validation for sub claim during JWT encoding with TypeError for non-strings
  • Removes string type validation for sub claim during JWT decoding
  • Updates test to verify encoding validation instead of decode validation

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
jwt/api_jwt.py Adds sub string validation in encode method and removes string validation from decode's _validate_sub
tests/test_api_jwt.py Updates test to verify TypeError during encoding instead of InvalidSubjectError during decoding

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