Summary
A flaw in how fast-jwt validates public key strings allows leading whitespace to bypass a regular expression check intended to enforce safe JWT algorithms.
Under certain conditions, this can re-enable an algorithm confusion attack, letting an attacker craft tokens that are incorrectly accepted as valid and potentially gain unauthorized access.
Affected context
This vulnerability affects applications that:
- Use fast-jwt (version 6.1.0 and earlier) for JSON Web Token verification, and
- Rely on public key PEM string validation inside fast-jwt (via
publicKeyPemMatcherinsrc/crypto.js), and - Accept or construct key strings that may contain leading whitespace before the PEM header.
In such contexts, the regex anchor (^) used for matching the PEM header is defeated by whitespace, weakening assumptions about how keys are validated and allowing previously patched JWT algorithm confusion behavior to resurface.
Exploit preconditions
- Attack surface: JWT verification endpoints (e.g., login, API authentication, session validation) that use fast-jwt.
- Required access: Ability to submit arbitrary JWTs to a verification endpoint; in some deployments, ability to influence or control the key material or key selection mechanism increases risk.
- Trigger point: Verification of a JWT where the library's key handling logic uses
publicKeyPemMatcherand leading whitespace is present in the key string. - Why exploitation is possible: The regex responsible for validating PEM-formatted public keys is anchored at the start of the string, but leading whitespace is not accounted for. This allows malformed or unexpected key formats to pass checks that were intended to enforce safe algorithm usage, reinstating an algorithm confusion path similar to the previously fixed CVE-2023-48223.
Why this happens
The underlying issue is an incomplete cryptographic validation pattern: the library relies on a regular expression (publicKeyPemMatcher) to verify that a key string is a proper PEM-encoded public key and to gate safe algorithm usage.
Because the regex anchor (^) does not handle leading whitespace, the check can be bypassed by prefixing the key string with whitespace characters. This breaks the assumption that only correctly structured public keys are used with asymmetric algorithms and allows the algorithm confusion scenario to reappear.
Conceptually, this aligns with CWE-327: Use of a Broken or Risky Cryptographic Algorithm because the security of the JWT verification process depends on a fragile parsing heuristic rather than robust, context-aware key and algorithm validation, enabling attackers to subvert the intended cryptographic guarantees.
Potential impact
- Attackers may forge JWTs that are accepted as valid, leading to authentication bypass.
- Privilege escalation if forged tokens grant higher roles or administrative capabilities.
- Unauthorized access to protected APIs and sensitive data.
- Impersonation of other users or services within a distributed system.
- Weakening of trust boundaries between microservices that rely on JWT-based identity.
General mitigation guidance
Effective mitigation focuses on hardening JWT verification and key handling rather than relying on fragile regex-based checks:
- Ensure algorithm selection is explicit and enforced server-side; do not derive allowed algorithms from token headers or loosely validated keys.
- Treat key material as structured data: use strict parsers for PEM/keys instead of regex-only validation, and normalize inputs (e.g., trimming whitespace) before validation.
- Enforce asymmetric vs. symmetric algorithm separation through configuration, preventing automatic fallback or negotiation influenced by token contents.
- Apply defense in depth by validating expected token claims (issuer, audience, expiration) and using short-lived tokens to limit the value of any forged token.
- Restrict where and how keys can be loaded or modified at runtime, and store key material in trusted, access-controlled locations.
How teams detect issues like this
Organizations can detect similar issues by combining code-level and behavioral analysis:
- Static analysis and code review:
- Flag cryptographic logic that relies on regex checks or string heuristics to decide algorithm type or key validity.
- Look for JWT verification flows where
algfrom the header influences verification behavior without strong server-side constraints. - Review key loading/normalization paths for unchecked whitespace, encoding anomalies, or multiple parsing paths.
- Automated tooling and policies:
- Use security linters or SAST tools with rules targeting algorithm confusion patterns and weak JWT validation practices.
- Enforce coding standards that require explicit algorithm whitelists and structured key parsing for all JWT-related code.
- Runtime and logging:
- Log rejected and accepted JWT verification attempts with algorithm and key metadata (without sensitive key material) to identify anomalous patterns.
- Monitor for unexpected spikes in JWT verification failures or unusual algorithm usage in production.

