Security research

Five JWT mistakes we keep finding in API penetration tests

iSpyCyber logo
iSpyCyber
Securing Digital Culture
iSpyCyber – Securing digital Culture: VAPT, threat intelligence, security audit and compliance, risk quantification
Table of Contents

JSON Web Tokens (JWTs) sit at the heart of most modern APIs. They are also one of the most common places we find serious authentication flaws during API penetration tests. The five mistakes below come up again and again, and each one can be found and fixed quickly once you know where to look.

1. Accepting unsigned tokens

Some libraries and gateways still accept a token whose header says "alg": "none". If the server trusts the claims without checking a signature, anyone can create a token for any user, including an administrator.

{"alg":"none","typ":"JWT"}.{"sub":"user-1042","role":"admin"}.

Fix: allow only the algorithms you actually use, such as RS256, and reject everything else before reading any claim.

2. Algorithm confusion

When a server uses RSA keys but also accepts HMAC, an attacker can sign a token using the public key as the HMAC secret. The server then verifies it with the same public key and accepts it as genuine.

Fix: bind each key to exactly one algorithm, and never let the token header decide how it is verified.

3. Weak or shared secrets

HMAC secrets copied from documentation, short passwords or the same secret reused across environments can be cracked or reused. A weak secret can often be recovered offline from a single captured token, after which an attacker can sign tokens of their own.

Fix: use long random secrets from a secrets manager, a different one for each environment, and rotate them.

4. Missing expiry and audience checks

  • Tokens without an exp claim, or servers that ignore it
  • Tokens issued for one service that another service accepts because aud is not checked
  • Refresh tokens that never expire and are not revoked on logout

Fix: check issuer, audience and expiry on every request, keep access tokens short-lived and revoke refresh tokens on logout.

5. Trusting claims for authorisation

A valid signature proves who issued a token. It does not prove that the user should see a given record. Many of the most serious API findings combine a correct JWT check with no ownership check on the object being requested, the issue OWASP lists first in its API Security Top 10 as broken object level authorisation.

Fix: treat the token as proof of identity only, and check permissions on the server for every object and action.

How to test your own APIs

Add negative test cases to your CI pipeline. Each of these should be rejected:

  • An unsigned token with alg set to none
  • An expired token, and one with no expiry at all
  • A token issued for a different audience
  • A token re-signed with the wrong key or algorithm
  • A valid token used to request another user’s records

For an independent view, our API security testing covers these checks and much more, with every finding retested once it is fixed.

References

  • IETF RFC 8725: JSON Web Token Best Current Practices
  • OWASP API Security Top 10 (2023): API1 Broken Object Level Authorization and API2 Broken Authentication
  • OWASP Cheat Sheet Series: JSON Web Token Cheat Sheet for Java
Keep reading

More security research

A live webinar for security and risk leaders on setting up practical AI governance: AI inventory, policy, risk assessment, AI security testing and ISO/IEC
A practical guide to Continuous Threat Exposure Management (CTEM): the five stages, the metrics that matter and a 90-day plan to get started.
How fintech and lending teams use iSpyCyber to test their APIs by hand before release, find the flaws scanners miss and ship with evidence
Scroll to Top