Skip to content

Reject non-object JSON in JWK deserialization - #1237

Open
FanWu-ai wants to merge 1 commit into
jpadilla:masterfrom
FanWu-ai:fix/jwk-json-object-validation-20261020
Open

FanWu-ai wants to merge 1 commit into
jpadilla:masterfrom
FanWu-ai:fix/jwk-json-object-validation-20261020

Conversation

@FanWu-ai

Copy link
Copy Markdown

Summary

Valid JSON that is not an object, such as null, [], or "key", currently causes raw AttributeError exceptions in JWK loaders. Applications catching PyJWT's documented key-format errors cannot handle these failures through the usual exception hierarchy.

Reject non-object JSON in the four supported algorithm loaders using their existing InvalidKeyError checks, and in PyJWK.from_json() / PyJWKSet.from_json() using InvalidKeyError / PyJWKSetError. Keep the high-level checks at the JSON boundaries so that existing from_dict() mapping behavior remains unchanged. Known-present EC/OKP coordinate fields use indexed access after the existing membership checks, preserving strict typing.

Add 36 regression cases covering arrays, null, booleans, numbers and strings across all six entry points, plus three key-set mapping controls for dict, UserDict and MappingProxyType. A changelog entry is included. Valid key construction, signature verification and malformed-JSON syntax handling are unchanged.

Related work

The endpoint-payload validation introduced in #1208 covers the HTTP JWKS client/cache path. This change covers the public JSON deserialization entry points that still leak AttributeError.

Validation

Based on master 16231abfd1d8eab4953e8779b3017b859c88565e, on Linux:

  • Pristine full Python 3.12.14 suite: 472 passed / 4 expected skips with cryptography; 286 passed / 190 expected skips without it.
  • All new cases on pristine source: 36 failures and 3 mapping controls passed.
  • Final full Python 3.12.14 suite: 511 passed / 4 expected skips with cryptography; 307 passed / 208 expected skips without it. The extra no-crypto skips are the 18 new RSA/EC/OKP cases.
  • Strict mypy passed in both crypto and no-crypto environments, 25 source files each, on pristine and final source.
  • All 13 configured pre-commit hooks passed on pristine and final source. Final lint ran through the configured Python 3.10 tox environment.
  • Python 3.11 strict Sphinx HTML/doctest builds and README/usage doctests passed on pristine and final source. Existing short-key example warnings are unchanged.
  • Configured package-description and coverage-report targets passed. Isolated source/wheel builds and strict Twine checks passed on pristine and final source.
  • An independently installed wheel passed 30 invalid-shape checks, a signed-token round trip and invalid-signature rejection. Both production modules match the tested source; the source distribution contains the exact tests and changelog too.
  • git diff --check passed. Independent automated review also checked 72 invalid JSON shapes and four valid key/mapping controls.

The lint environment needed writable cache-directory environment variables passed through tox. No project configuration or check was changed. The remaining interpreter/OS matrix has not run locally, and no hosted CI pass is claimed.

AI assistance was used to prepare, test and review this change. No human review is claimed.

@FanWu-ai
FanWu-ai marked this pull request as ready for review October 19, 2026 22:00

This branch has not been deployed

No deployments
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