Every JWT security article starts with alg: none, and every modern library already rejects it. That makes it easy to believe the problem is solved.
The vulnerabilities that remain are quieter. They are all variations of the same mistake: letting the token tell you how to verify the token.
Context
A JWT has a header, a payload and a signature. The header carries metadata about how the signature was made: the algorithm (alg), the key identifier (kid), and sometimes where to find the key (jku, x5u, jwk).
All of that is chosen by whoever created the token, and a token an attacker forged is still a token. Anything read from it before the signature is verified is attacker input.
Algorithm confusion
Suppose the issuer signs tokens with RS256, an asymmetric algorithm. The server verifies with the issuer's public key, which is, by definition, public.
A verification routine written as "take the key, take the algorithm from the header, verify" has a flaw. The attacker changes the header to HS256, which is a symmetric algorithm, and signs the token using the public key as the HMAC secret. The server reads HS256, uses the key it has, and the signature checks out.
The fix is to never let the header choose. The allowed algorithms belong in server configuration, tied to the key:
csharp
services.AddAuthentication().AddJwtBearer(options =>
{
options.Authority = "https://login.example.com";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "https://login.example.com",
ValidAudience = "billing-api",
ValidAlgorithms = new[] { SecurityAlgorithms.RsaSha256 },
ValidTypes = new[] { "at+jwt" },
ClockSkew = TimeSpan.FromSeconds(30)
};
});
kid helps the server find the right key among several. That lookup is where things go wrong when it is implemented loosely.
- If
kid is used to build a file path, it can become path traversal.
- If it ends up in a SQL query by string concatenation, it is an injection point.
- If it selects among keys without a strict allow-list, an attacker may steer verification toward a key they influence.
Treat kid as an opaque lookup into a fixed, already-loaded set of keys, and reject anything that is not in it.
The headers jku, x5u and jwk are worse: they let the token point at the key to verify itself with. A verifier that follows them is verifying a signature against a key the attacker supplied. Unless you have a specific need and a strict allow-list of URLs, ignore them.
JWKS caching
Most systems fetch the issuer's public keys from a JWKS endpoint and cache them. Both extremes are bad.
- Cache forever, and key rotation breaks your service. Tokens signed with the new key are rejected until a restart.
- Fetch on every unknown
kid, and an attacker can send tokens with random kid values, forcing your service to hammer the identity provider, or to wait on it.
The usual balance is a cache with a refresh interval, plus a refresh on unknown kid that is rate limited. ASP.NET Core exposes these through the configuration manager used by the JWT bearer handler, with settings for the automatic refresh interval and the minimum interval between refreshes. Check their defaults for your version and set them consciously.
During rotation, the issuer should publish the new key before it starts signing with it, and keep the old one until its tokens expire.
Claims are part of verification
A valid signature only proves who signed the token. It does not prove the token is meant for you.
- Validate
iss and aud. A token issued for service A must not be accepted by service B.
- Validate
exp and nbf with a small, explicit clock skew.
- Use the
typ header to keep different kinds of JWT from being accepted in place of each other, for example an ID token where an access token is expected. RFC 9068 defines at+jwt for access tokens.
A note on revocation
JWTs are self-contained, which is exactly why they cannot be revoked before they expire without extra machinery. Keep access tokens short-lived, and decide up front what happens when you need to kill one. If the answer is "wait 24 hours", that should be a conscious decision.
Refresh tokens
Short-lived access tokens only work if renewing them is safe. Refresh tokens are long-lived credentials, so they deserve the opposite treatment of an access token: store them server-side or in an HttpOnly cookie, bind them to the client where you can, and rotate them on every use, so that a stolen one is detected the moment the legitimate client and the thief both try to use it.
Conclusion
The pattern behind all of these attacks is that the verifier trusts a decision made by the party it is verifying.
The cure is the same in every case: configuration owns the algorithms, the keys and the key sources, the token only owns its claims, and even those are checked against what this service expects.
References