Summary
A flaw in fast-jwt's async key-resolver logic allows empty strings from a JWKS-style resolver to be treated as valid secret keys, causing tokens to be verified using an HMAC with an effectively empty key.
An unauthenticated attacker who can control the JWT can forge arbitrary tokens by computing a valid HMAC over attacker-chosen claims, leading to full authentication bypass wherever fast-jwt is used for access control.
Affected context
This vulnerability appears in Node.js applications that:
- Use fast-jwt prior to 6.2.4 for JWT verification, and
- Rely on an async key-resolver to supply the verification key (for example, from JWKS or a key map), and
- Implement a fallback such as
keys[decoded.header.kid] || '', allowing the resolver to return an empty string when a key ID is missing or not found.
In such configurations, fast-jwt converts the empty string into a zero-length Buffer and derives an HMAC secret key from it, unintentionally accepting signatures generated with an effectively empty key as valid.
Exploit preconditions
- Attack surface: Any endpoint or service that accepts JWTs and verifies them using fast-jwt's async key resolver.
- Required access: No prior authentication is required; an attacker only needs the ability to submit arbitrary JWTs (e.g., Authorization header, cookies, query parameters).
- Trigger point: The key resolver returns an empty string (such as via
keys[decoded.header.kid] || ''when thekidis unknown or the key map is empty). - Why exploitation is possible: fast-jwt treats the empty string as a valid secret, constructs an HMAC key from it, assigns default
allowedAlgorithms = ['HS256','HS384','HS512'], and verifies the signature using HMAC with this empty key. Since the HMAC computation is deterministic and the key is effectively empty, attackers can generate matching signatures for any header and payload they choose.
Why this happens
The underlying flaw is a combination of improper authentication enforcement and insecure key handling:
- Improper Authentication (CWE-287): The library accepts tokens as authentic based on a cryptographic check that can succeed with an unintended, trivially guessable key (the empty string), allowing bypass of intended authentication logic.
- Inadequate Cryptographic Strength (CWE-326) / Weak or Null Keys (CWE-1391): An empty string returned by the key resolver is implicitly accepted as a valid secret key. This breaks the assumption that verification keys are strong, secret values and effectively reduces the verification secret to a known null-like key.
Conceptually, the bug stems from failing to enforce that key-resolver outputs are non-empty, valid cryptographic keys and from silently falling back to a default HMAC-based verification path with an invalid key, rather than rejecting the token outright.
Potential impact
- Attackers can forge JWTs that the application accepts as valid, without knowing any real signing keys.
- Arbitrary impersonation of users by setting
sub,email, or other identity claims to attacker-chosen values. - Escalation to administrative roles by injecting privileged claims (e.g.,
admin: true, high-privilege scopes/roles). - Bypass of API gateway or backend access controls that rely on JWT verification for authorization decisions.
- Potential lateral movement within distributed systems where JWTs are used for service-to-service trust.
General mitigation guidance
Effective mitigation focuses on enforcing strict key validity and hardening JWT verification flows:
- Ensure that key resolvers never return empty strings, null, or other invalid values; explicitly treat unknown or missing
kidvalues as verification failures. - Introduce validation logic that rejects tokens if the derived verification key is empty, too short, or otherwise does not meet cryptographic strength requirements.
- Avoid permissive fallbacks such as
keys[decoded.header.kid] || ''; instead, fail closed when a key cannot be resolved. - Constrain allowed algorithms explicitly and avoid automatic inference of HMAC algorithms when keys are ambiguous.
- Apply least-privilege patterns around JWT usage: separate tokens used for authentication from those used for non-critical metadata, and limit the authority conferred by individual tokens.
- Where possible, centralize JWT verification in a well-reviewed component and treat any key resolution anomaly as a hard error.
How teams detect issues like this
Detection can leverage both code-level analysis and behavioral monitoring:
- Static analysis and code review:
- Search for fast-jwt usage with async key resolvers and inspect for fallbacks like
|| '',|| null, or similar patterns returning empty or default values. - Review key-resolution logic for cases where unknown
kidvalues or missing keys do not immediately cause verification failure. - Check for implicit acceptance of default
allowedAlgorithmsdriven by resolver outputs, rather than explicit configuration. - Configuration and policy checks:
- Enforce internal development policies that prohibit empty, zero-length, or trivially weak keys from being used in JWT verification.
- Use automated linters or security scanners to flag JWT verification code that does not validate resolver outputs.
- Runtime monitoring:
- Log JWT verification errors and include the resolved
kid/key metadata (without exposing secrets) to identify suspicious patterns such as frequent verification with missing or unknownkidvalues. - Look for anomalous authentication events, such as sudden surges in logins for high-privilege accounts or tokens with unusual claim combinations being accepted.

