Missing JWT signature verification

Require a valid JWT signature before trusting authentication claims

Description

In JJWT 0.11.x, parse() can accept an unsigned JWT even when a verification key is configured. JJWT 0.12 and later reject unsigned JWTs by default.

Potential impact

Accepting unverified identity or permission claims, or a token meant for another service, can allow someone to read or change data with another user’s permissions.

Remediation

  • In JJWT 0.12 and later, use Jwts.parser().verifyWith(key).build().parseSignedClaims(token) and leave unsigned tokens disabled. In 0.11.x, use parseClaimsJws().
  • Select keys and algorithms from trusted configuration. For rotation, use a reviewed key set. JJWT 0.12 and later also support keyLocator. Never fetch keys from an arbitrary URL in a token.
  • Check the issuer, audience, expiration, required claims and user permissions. Reject authentication on JwtException or any other validation failure.

Examples

These Spring excerpts use JJWT 0.11.x. Inject a SecretKey of the required length for the HMAC algorithm from trusted configuration. Configure claim policies and error responses for your application.

Before

java
import io.jsonwebtoken.*;
import javax.crypto.SecretKey;
import org.springframework.web.bind.annotation.*;

@RestController
class TokenController {
    private final SecretKey key;

    TokenController(SecretKey key) {
        this.key = key;
    }

    @GetMapping("/me")
    public String me(@RequestHeader("Authorization") String authz) {
        if (!authz.startsWith("Bearer ")) {
            throw new JwtException("missing bearer token");
        }
        String token = authz.substring(7);
        // BAD: parse() can also accept an unsigned JWT
        Jwt<?, ?> jwt = Jwts.parserBuilder()
                .setSigningKey(key)
                .build()
                .parse(token);
        Claims claims = (Claims) jwt.getBody(); // Claims may not have a verified signature
        return claims.getSubject();
    }
}

After

java
import io.jsonwebtoken.*;
import javax.crypto.SecretKey;
import org.springframework.web.bind.annotation.*;

@RestController
class TokenControllerSafe {
    private final SecretKey key;

    TokenControllerSafe(SecretKey key) {
        this.key = key;
    }

    @GetMapping("/me")
    public String me(@RequestHeader("Authorization") String authz) {
        if (!authz.startsWith("Bearer ")) {
            throw new JwtException("missing bearer token");
        }
        String token = authz.substring(7);
        // GOOD: parseClaimsJws() requires signature verification
        Jws<Claims> jws = Jwts.parserBuilder()
                .setSigningKey(key)
                .build()
                .parseClaimsJws(token);

        Claims claims = jws.getBody();
        // Additional validation
        if (!"my-issuer".equals(claims.getIssuer())) {
            throw new JwtException("invalid iss");
        }
        return claims.getSubject();
    }
}

The revised handler verifies the signature before checking my-issuer. Replace that value with your trusted issuer.

References