-
-
Notifications
You must be signed in to change notification settings - Fork 719
Fix #1051: Replace lru_cache with TTL-based key caching #1070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix #1051: Replace lru_cache with TTL-based key caching #1070
Conversation
Replace functools.lru_cache with TTL-aware cache to prevent indefinite caching of potentially revoked signing keys. - Remove lru_cache which cached keys forever - Implement TTL-based individual key caching - Keys now expire after configured lifespan - Maintains backward compatibility with existing API - Add test demonstrating fix works Fixes jpadilla#1051
4eaa549 to
11b357a
Compare
for more information, see https://pre-commit.ci
There was a problem hiding this 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 fixes issue #1051 by replacing the permanent lru_cache with a TTL-aware cache for individual keys, ensuring that revoked keys are no longer served indefinitely.
- Replaces lru_cache with a TTL-based caching mechanism in jwt/jwks_client.py.
- Adds tests in tests/test_jwks_client.py to verify key expiration and proper cache eviction.
- Updates the CHANGELOG to document the change.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/test_jwks_client.py | Adds new tests verifying that expired keys are rejected and cache eviction works correctly. |
| jwt/jwks_client.py | Refactors caching logic by adding TTL-based caching methods and removing lru_cache. |
| CHANGELOG.rst | Updates changelog to capture the new TTL-aware key caching fix. |
Comments suppressed due to low confidence (1)
CHANGELOG.rst:12
- The issue reference in the changelog appears to be inconsistent with the PR title (#1051). Please update the issue reference to #1051 if that is the correct identifier.
- Fix indefinite key caching in PyJWKClient by replacing lru_cache with TTL-aware cache in `#1070 <https://github.com/jpadilla/pyjwt/pull/1070>`__
Co-authored-by: Copilot <[email protected]>
for more information, see https://pre-commit.ci
Use MagicMock with controllable return_value for time.monotonic
for more information, see https://pre-commit.ci
|
Hi @auvipy (or @jpadilla) My changes only touch Could you advise if I should:
The actual functionality tests are all passing - it's just the linting that's catching this pre-existing issue. Would appreciate any feedback on next steps to move this forward. Happy to make any adjustments needed! |
Summary
This PR fixes issue #1051 where PyJWKClient with
cache_keys=Trueserves potentially revoked keys indefinitely.Problem
When
cache_keys=Trueis enabled, PyJWKClient applies@lru_cacheto theget_signing_keymethod. This caches keys permanently until LRU eviction or process restart. If an identity provider removes a key from their JWKS, applications continue accepting tokens signed with that key.Solution
lru_cachewith TTL-aware cachinglifespan(default 300 seconds)Changes
Testing
All existing tests pass. New test verifies that:
Backward Compatibility
No breaking changes. All existing parameters work identically:
cache_keys=Truestill enables individual key cachingmax_cached_keysstill limits cache sizeFixes #1051