Improper Verification of Cryptographic Signature
The product does not verify, or incorrectly verifies, the cryptographic signature for data.
Cryptographic signatures are used to verify the authenticity and integrity of data. When signature verification is missing or incorrectly implemented, attackers can forge or tamper with data.
Como corrigir esta vulnerabilidade
Estratégias de prevenção para Improper Signature Verification baseadas em 4 regras de detecção do Shoulder.
Load JWT secret from environment and explicitly specify allowed algorithms
- from jose import jwt - - SECRET = "my-secret-key" - - def decode_token(token: str): - return jwt.decode(token, SECRET) + from pydantic_settings import BaseSettings + from jose import jwt + + class Settings(BaseSettings): + SECRET_KEY: str + ALGORITHM: str = "HS256" + + settings = Settings() + + def decode_token(token: str): + return jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
Always specify allowed algorithms explicitly when decoding JWT tokens
import jwt - from flask import request - - @app.route('/api/user') - def get_user(): - token = request.headers.get('Authorization') - payload = jwt.decode(token, SECRET_KEY, verify=False) - return {'user': payload['user_id']} + import os + from flask import request + + SECRET_KEY = os.environ['JWT_SECRET'] + + @app.route('/api/user') + def get_user(): + token = request.headers.get('Authorization') + try: + payload = jwt.decode(token, SECRET_KEY, algorithms=['HS256']) + return {'user': payload['user_id']} + except jwt.InvalidTokenError: + return {'error': 'Invalid token'}, 401
Validate JWT algorithm explicitly, use strong secrets, and set expiration
func parseToken(tokenString string) (*jwt.Token, error) { return jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { - return []byte("secret"), nil + if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { + return nil, fmt.Errorf("unexpected method: %v", token.Header["alg"]) + } + return []byte(os.Getenv("JWT_SECRET")), nil }) }
Use jwt.verify() instead of jwt.decode() to validate token signatures
- const decoded = jwt.decode(token); + const decoded = jwt.verify(token, process.env.JWT_SECRET, { + algorithms: ['RS256'] + }); req.user = decoded;
Práticas-chave
- Use of jwt
Encontre vulnerabilidades no seu código
Use o Shoulder para escanear seu código em busca de padrões Improper Verification of Cryptographic Signature. 4 regras.
# Scan with Shoulder CLI npx @shoulderdev/cli trust --cwe=347 # Or scan entire project npx @shoulderdev/cli trust .
Regras de Detecção (4)
O que observar nas revisões de código
Estes padrões indicam vulnerabilidades potenciais de Improper Verification of Cryptographic Signature. Procure-os durante revisões de código e auditorias de segurança.
Escaneie seu código para Improper Verification of Cryptographic Signature
O Shoulder CLI encontra padrões vulneráveis em todo o seu código.