Pin the algorithm — the only real defense
Every reputable JWT library now lets you pass an `algorithms` allowlist to the verifier. The library rejects any token whose header `alg` is not in that list before attempting verification. This makes algorithm confusion attacks structurally impossible — the verifier never reaches the part where it picks an algorithm based on attacker input.
In `jose`: `jwtVerify(token, key, { algorithms: ["RS256"] })`. In `jsonwebtoken`: `jwt.verify(token, key, { algorithms: ["RS256"] })`. Always pass it. There is no scenario in production where you legitimately need to accept any algorithm.