Parsing user-controlled JWTs without signature verification

Parsing user-controlled JWTs without signature verification

Description

In golang-jwt/jwt/v5, Parser.ParseUnverified reads the JWT header and claims without verifying the signature. The returned Token.Valid is false.

Conditions for unverified parsing

Both parser forms call ParseUnverified. Use it only when the same token’s signature has already been verified or will be verified independently before you rely on its claims.

go
parser := jwt.NewParser()
parser.ParseUnverified(rawToken, jwt.MapClaims{})

parser = &jwt.Parser{}
parser.ParseUnverified(rawToken, jwt.MapClaims{})

The verification example uses github.com/golang-jwt/jwt/v5. Check support and API differences for older major versions or github.com/dgrijalva/jwt-go.

Potential impact

Trusting an unverified sub, role or permission can grant someone another user’s access. Unverified alg or kid values can also influence key or algorithm selection. Versions before v5.2.2 can allocate excessive memory while parsing a token (CVE-2025-30204).

Remediation

  • Use jwt.Parse or jwt.ParseWithClaims. Use claims only when there is no error, the token is non-nil and Valid is true.
  • Restrict algorithms with jwt.WithValidMethods. Load the key from trusted configuration or a validated JWKS.
  • Check required claims with jwt.WithExpirationRequired, jwt.WithIssuer, jwt.WithAudience or jwt.WithAllAudiences as appropriate. Use jwt.WithLeeway only for a defined clock-skew allowance.
  • Use a maintained v5 release, 5.3.1 or later, and apply security updates. Version 5.2.2 was the first fix for the memory issue above.

Examples

Before

go
func unverifiedClaims(r *http.Request) (jwt.MapClaims, error) {
    rawToken := r.FormValue("token")
    token, _, err := jwt.NewParser().ParseUnverified(rawToken, jwt.MapClaims{})
    if err != nil {
        return nil, err
    }
    return token.Claims.(jwt.MapClaims), nil
}

This function returns claims from the request’s rawToken without verifying them.

After

go
func verifiedToken(rawToken string, verificationKey *rsa.PublicKey) (*jwt.Token, error) {
    token, err := jwt.Parse(
        rawToken,
        func(token *jwt.Token) (any, error) {
            // Select the key from trusted issuer configuration
            // or a validated JWKS cache in production.
            return verificationKey, nil
        },
        jwt.WithValidMethods([]string{jwt.SigningMethodRS256.Alg()}),
        jwt.WithExpirationRequired(),
        jwt.WithIssuer("https://issuer.example"),
        jwt.WithAudience("api.example"),
    )
    if err != nil {
        return nil, fmt.Errorf("JWT verification failed: %w", err)
    }
    if token == nil || !token.Valid {
        return nil, errors.New("invalid JWT")
    }
    return token, nil
}

The example verifies RS256 with a trusted RSA public key and checks expiration, issuer and audience. Apply the same options and key policy to jwt.ParseWithClaims for custom claims.

References