Does your JWT tell you how to verify it? Why you pick the algorithm
A JWT carries the name of its signing algorithm in the alg header, and an attacker can change it to none or from RS256 to HS256. The fix: your server decides which algorithms it accepts and rejects the rest.

In most apps today, after a user logs in, the server hands them a JWT, and every request after that comes with the token. The odd part is that the token itself tells the server "here's how to check me". Here you'll see why that's dangerous, and how to make your server the one that decides.
What is the alg header in a JWT?
The alg header is part of a JWT's header that says which algorithm signed the token, such as RS256 or HS256. The problem is that this header comes from the token, which means from the user, not from your settings.
According to RFC 8725, the JSON Web Token best current practices, a signed token carries the name of its algorithm in alg, and that opened the door to two well-known attacks.
How does an attacker abuse it?
The attacker changes the alg header to a value that makes a weak library accept the token without a valid signature. The same standard describes two ways:
They change alg to none. Some libraries see none and treat the token as valid without checking any signature.
They change RS256 to HS256. The real token is signed with a private RSA key, and the server verifies it with the public key. Once it says HS256, some libraries use that same public key as the HMAC shared secret. The public key is published, so the attacker signs their own token with it.
Either way, the attacker writes "I'm the admin" in the token, and the server believes it.
Why does this bug live on in so much code?
Because the code works fine in testing. A normal user never changes a single character of the token, so every request passes, and nothing tells you the check is open.
The bug is usually one line: the verify function is called with only the token and the key, and the library takes the algorithm from the token's header. It's the same idea as the "forgot password" token: anything that comes from the user, even when signed, needs checking on your terms.
How do you close the door?
You close the door when the server sets the accepted algorithms itself, and rejects any token whose algorithm is outside that list. The standard itself requires libraries to let the caller specify a set of algorithms, and to use no others.
Open every place in your code that verifies a JWT, and check whether an algorithm list is written out. If not, add one.
Keep the list to what you need: if you sign with RS256, the list holds RS256 only.
Keep each key for one algorithm. The standard says each key must be used with exactly one algorithm.
Test it: take a valid token, change alg to none, drop the signature, and send it. It must be rejected.
Once the signature checks out, the cookie that carries the token needs its flags too, as in the session cookie flags article.
Where do you start now?
The lesson for us as developers: the token doesn't decide how it's checked, the server does, because an attacker can write anything in the token. Open your verify line today, make sure it has an algorithm list, and try a none token.
More app security articles are in the app security topic, and if you want someone to try these attacks on your app before someone else does, there's Muhassan. Which library verifies JWTs in your code, and does it have an algorithm list?
FAQ
If I use a well-known JWT library, am I safe?
Not by itself. A good library gives you a way to set the accepted algorithms, but you have to use it. Open the verify line in your code and check it has an algorithm list.
Is the public key secret?
No. The public key is usually published, which is why it's dangerous if the server accepts HS256 and uses it as a shared secret: anyone with the public key can sign.
When is alg none acceptable?
Almost never in a normal web app. RFC 8725 says it should only be used when the token is protected by other means.


