The vulnerability is a classic JWT Algorithm Confusion issue in the Hono framework. It stems from two main flaws in the JWT middleware, which have been fixed in the provided patch.
- Unsafe Default Algorithm: The core
verify function in src/utils/jwt/jwt.ts defaulted to the HS256 (symmetric) algorithm if no algorithm was explicitly provided by the calling code.
- Missing Algorithm Validation: The
verify function did not check if the alg field in the unverified JWT header matched the algorithm the server was configured to use.
The combination of these flaws allowed an attacker to bypass authentication. For instance, in a setup expecting an RS256 (asymmetric) signed token, an attacker could:
- Take the public key used for
RS256 verification.
- Create a malicious JWT payload.
- Set the token's header to
{"alg": "HS256", "typ": "JWT"}.
- Sign the token using the
RS256 public key as the secret for the HS256 algorithm.
- Send this forged token to the server.
Because the jwt middleware in src/middleware/jwt/jwt.ts did not require developers to specify an alg, the vulnerable verify function would be called without an algorithm. It would then default to HS256 and, crucially, use the provided RS256 public key as the secret for HS256 verification, successfully validating the forged token.
The patch addresses this by:
- Making the
alg parameter mandatory in both the jwt middleware and the verify function, removing the unsafe default.
- Adding an explicit check in the
verify function to ensure the algorithm in the token header matches the one specified by the developer (header.alg === options.alg).
The identified vulnerable functions, jwt and verify, are the exact locations where this flawed logic existed. During an exploit, a profiler would show these functions being called as part of the authentication flow that processes the malicious JWT.