Summary
PyJWT's PyJWKClient may follow an attacker-influenced redirect from a configured JWKS endpoint without revalidating the destination against the trusted JWKS boundary. This can cause forwarded credentials to be disclosed or allow redirected content to be consumed as verification key material when the trusted endpoint can be influenced.
Affected context
The issue occurs in applications that use PyJWT's PyJWKClient to retrieve JSON Web Key Sets from remote endpoints and trust the initial endpoint configuration. Risk increases when that endpoint, its responses, or an intermediary can influence redirect destinations, because the redirected location may fall outside the intended JWKS trust boundary.
Exploit preconditions
- Attack surface: Server-side JWKS retrieval performed by PyJWKClient - Required access: Ability to influence the trusted JWKS endpoint or
cause it to return an attacker-controlled redirect
- Trigger point: JWKS retrieval during JWT verification or key refresh - Why exploitation is possible: Redirect targets are followed without
sufficient revalidation, allowing responses from an unintended location to
be treated as trusted key material
Why this happens
The underlying bug is incomplete validation of redirects in a server-side HTTP request flow. The client establishes trust in the original JWKS endpoint but does not reapply that trust decision to the final redirect destination. This behavior maps primarily to CWE-918, Server-Side Request Forgery, and may also involve CWE-200 through unintended information disclosure and CWE-345 when untrusted key material is accepted as authentic.
Potential impact
- Authentication-related credentials may be disclosed to an unintended endpoint
- Verification keys could be substituted with attacker-controlled material
- JWT signature validation may be undermined
- Requests may reach internal or otherwise restricted network locations
- Applications could make trust decisions using untrusted key metadata
General mitigation guidance
Mitigation generally focuses on enforcing the JWKS trust boundary throughout the complete request lifecycle. Secure-by-default patterns include allowing redirects only when the final destination remains within an explicitly approved origin set, or disabling redirects for key retrieval when they are unnecessary. Applications can reduce input trust by validating schemes, hosts, ports, and resolved addresses, isolating outbound requests, limiting forwarded headers and credentials, and treating retrieved key material as untrusted until it satisfies strict schema and provenance checks. Network egress controls and least-privilege service identities can limit impact if a request is misdirected.
How teams detect issues like this
Detection approaches include static analysis and code review checks for HTTP clients that automatically follow redirects without validating the final URL. Reviewers can look for missing origin allowlists, unrestricted redirect handling, credential forwarding across redirects, and JWT verification logic that accepts key material solely because it was returned by a redirected request. Runtime monitoring can alert on JWKS requests to unexpected domains, private address ranges, unusual ports, or changes in established endpoint identity. Automated security review tools may help identify these patterns in dependency and application code.

