﻿# V5923\. OWASP\. Insecure JWT signature verification may lead to an authentication bypass\.

The analyzer has detected that the JWT is accepted without reliable signature verification\. An attacker can compromise such a token by inserting unauthorized data, such as another user's ID or an administrator role, and bypass authentication\.

Vulnerabilities related to incorrect authentication falls under a separate risk category in the OWASP Top 10 Application Security Risks 2025: [A07:2025 Authentication Failures](https://top10.owasp.org/2025/A07_2025-Authentication_Failures/)\.

A JWT consists of three parts, separated by dots: the header, the payload \(claims\), and the signature\. The header and payload are not encrypted; anyone can read and modify them\. Only the signature guarantees the token's authenticity; therefore, the data in the token can be trusted only if the signature is verified using the correct key and the correct algorithm\.

The example:

```cpp
func profileHandler(w http.ResponseWriter, r *http.Request) {
  auth := r.Header.Get("Authorization")
  tokenString := strings.TrimPrefix(auth, "Bearer ")

  p := jwt.NewParser()
  token, _, err := p.ParseUnverified(tokenString, jwt.MapClaims{})
  if err != nil {
    http.Error(w, "invalid token", http.StatusUnauthorized)
    return
  }

  claims := token.Claims.(jwt.MapClaims)
  userID, _ := claims["sub"].(string)
  writeProfile(w, userID)
}
```

The [`jwt.Parser.ParseUnverified`](https://pkg.go.dev/github.com/golang-jwt/jwt/v5#Parser.ParseUnverified) method parses the token from the request header but does not verify its signature\. An attacker can take any token, replace the `sub` value in it, and get another user's data\.

The analyzer issues a warning in the following cases:

* The signature is not verified:
  * the token is parsed using the `jwt.Parser.ParseUnverified` method;
  * `nil` is passed instead of the key retrieval function;
  * neither the parsing error nor the `Token.Valid` field are checked;
  * the payload is decoded manually using [`base64`](https://pkg.go.dev/encoding/base64) and [`json.Unmarshal`](https://pkg.go.dev/encoding/json#Unmarshal)\.
* The signature is verified via an insecure key:
  * the key retrieval function returns `nil` or an empty string;
  * [`jwt.UnsafeAllowNoneSignatureType`](https://pkg.go.dev/github.com/golang-jwt/jwt/v5#UnsafeAllowNoneSignatureType) as the HMAC secret\. The constant allows the `none` algorithm, that is, an unsigned token or a public key\.
  * the public key is used as the HMAC secret\. The public key is known to everyone, so anyone can use it to sign a token\.
* The signing algorithm is not validated: the key retrieval function does not access the `Token.Method` field, and the valid algorithms are not specified by the [`jwt.WithValidMethods`](https://pkg.go.dev/github.com/golang-jwt/jwt/v5#WithValidMethods) function\.

The signing algorithm is specified in the token header, which the attacker controls\. If the server does not validate it, an attacker can choose the algorithm it: for example, specify `none` or sign the token using the HMAC algorithm, using the server's public RSA key as the secret\.

The following example uses a key function that does not validate the signing algorithm:

```cpp
token, err := jwt.Parse(tokenString, func(t *jwt.Token) (any, error) {
  return []byte(os.Getenv("JWT_SECRET")), nil
})
```

The fixed code, in which the signature is verified and the algorithm is limited:

```cpp
func profileHandler(w http.ResponseWriter, r *http.Request) {
  auth := r.Header.Get("Authorization")
  tokenString := strings.TrimPrefix(auth, "Bearer ")
  secret := []byte(os.Getenv("JWT_SECRET"))

  keyFunc := func(t *jwt.Token) (any, error) {
    if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
      return nil, errors.New("unexpected signing method")
    }
    return secret, nil
  }

  token, err := jwt.Parse(tokenString, keyFunc)
  if err != nil || !token.Valid {
    http.Error(w, "invalid token", http.StatusUnauthorized)
    return
  }

  claims := token.Claims.(jwt.MapClaims)
  userID, _ := claims["sub"].(string)
  writeProfile(w, userID)
}
```

Instead of checking `t.Method`, restrict the algorithms using a parser option:

```cpp
jwt.WithValidMethods([]string{"HS256"})
```