language: go
name: go_jwt_none_algorithm
message: "Avoid using the 'none' algorithm in JWT as it disables signature verification and exposes the application to token forgery attacks."
category: security
severity: critical
pattern: >
  [
    (selector_expression
  operand: (identifier) @jwt
  field: (field_identifier) @method
  (#eq? @jwt "jwt")
  (#match? @method "^(SigningMethodNone|UnsafeAllowNoneSignatureType)$")) @go_jwt_none_algorithm
  ]
exclude:
  - "test/**"
  - "*_test.go"
  - "tests/**"
  - "__tests__/**"
description: |
  Issue: 
  The 'none' algorithm in JWT disables signature verification, allowing attackers to forge tokens and gain unauthorized access.  
  This vulnerability has been exploited in the past (e.g., [JWT 'none' algorithm vulnerability - Auth0 Blog](https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/)) and poses a serious security risk.

  Impact: 
  If an attacker sends a JWT with the 'none' algorithm, the server may accept it as valid, compromising authentication and authorization mechanisms.

  Remediation:  
  - Do not use `SigningMethodNone` or `UnsafeAllowNoneSignatureType`.  
  - Use secure algorithms like `HS256` (HMAC), `RS256` (RSA), or `ES256` (ECDSA).  
  - Always validate and specify the expected signing method when parsing JWTs:
    ```go
    token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
      if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
        return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
      }
      return []byte(os.Getenv("SECRET")), nil
    })
    ```