Summary
PyJWT versions prior to 2.13.0 contain a flaw in how JSON Web Keys (JWKs) are handled when verifying tokens that may use both asymmetric and HMAC algorithms.
An attacker can misuse an issuer's public key as the HMAC secret, potentially forging tokens that appear correctly signed and bypassing authentication or authorization checks in applications relying on PyJWT.
Affected context
This vulnerability affects Python applications that:
- Use PyJWT for verifying JSON Web Tokens (JWTs)
- Accept or process tokens with both asymmetric (e.g., RS256) and HMAC (e.g., HS256) algorithms
- Rely on JSON Web Keys (JWKs) or key material associated with issuers
- Use token signatures as a primary trust signal for user identity or authorization
It is most risky in services where JWTs are accepted from external clients or third-party identity providers without strict binding between keys and algorithms.
Exploit preconditions
- Attack surface: JWT verification endpoints (e.g., login callbacks, API gateways, resource servers).
- Required access: Ability to submit crafted JWTs to a PyJWT-based verifier that supports both asymmetric and HMAC algorithms.
- Trigger point: Decoding and verifying a JWT where the algorithm and key selection permit treating an issuer's public key as an HMAC secret.
- Why exploitation is possible: The library does not sufficiently validate how JSON Web Keys are used with HMAC algorithms, enabling the public key—intended only for asymmetric verification—to be repurposed as the symmetric secret, breaking the expected separation of key usage.
Why this happens
The underlying issue is an insufficient validation of JWT signatures and associated cryptographic parameters, aligning with:
- CWE-347: Improper Verification of Cryptographic Signature, where the system accepts signatures without enforcing strict rules about how keys and algorithms are combined.
- CWE-287: Improper Authentication, because trust in token authenticity can be subverted.
In abstract terms, the verification logic treats key material too generically, allowing a key intended as a public verification key for asymmetric algorithms to be repurposed as a secret for HMAC algorithms. This blurs the boundary between symmetric and asymmetric key use, allowing attackers to construct tokens that will verify successfully under unintended algorithm-key combinations.
Potential impact
- Attackers may forge JWTs that appear valid to the application.
- Authentication checks can be bypassed, allowing impersonation of arbitrary users or service accounts.
- Authorization controls based on JWT claims (roles, scopes, tenant IDs) can be subverted.
- Privilege escalation is possible if forged tokens carry elevated roles or administrative permissions.
- Cross-service trust relationships that rely on JWTs may be compromised, enabling lateral movement across APIs or microservices.
General mitigation guidance
Effective mitigation centers on strict cryptographic configuration and defensive JWT handling patterns, including:
- Enforcing a fixed, whitelisted algorithm per use case rather than accepting multiple algorithm families for the same token audience.
- Separating key material and configuration between symmetric (HMAC) and asymmetric (public/private key) signing paths, and avoiding any overlap in how keys are represented or loaded.
- Applying strong validation of JWKs, ensuring keys marked or intended as public verification keys cannot be used as HMAC secrets.
- Binding each token issuer to explicit verification parameters (per-issuer algorithm and key configuration) instead of relying on token-provided
algheaders alone. - Minimizing privileges associated with JWT claims and avoiding single tokens that grant broad, unscoped access.
- Designing systems so that authentication does not rely solely on token validity, but also on contextual checks such as audience, issuer, and token lifetime.
How teams detect issues like this
Detection efforts can focus on both code-level patterns and runtime behavior:
- Static analysis or code review to identify:
- PyJWT usage where
algorithmslists contain both HMAC (HS*) and asymmetric (RS*, ES*, PS*) algorithms for the same verification path. - Verification code that derives keys generically from JWKs or metadata without distinguishing symmetric from asymmetric keys.
- Logic that trusts the
algheader from incoming tokens without enforcing local policy. - Policy-based checks in CI/CD that:
- Flag configuration where a single issuer is allowed multiple algorithm families without strong justification.
- Enforce separation of key stores or configuration for HMAC versus public-key verification.
- Runtime monitoring to:
- Log and alert on unexpected JWT algorithms or sudden changes in accepted algorithms for a given client or issuer.
- Correlate suspicious authentication events, such as logins or API calls with unusual claim sets or source patterns.
Automated security scanning tools and JWT-focused linters can help surface risky verification patterns early in the development lifecycle.

