JWT güvenlik açıklarından korunmak için kapsamlı güvenlik önlemleri alınmalıdır.
💡 Temel Koruma Prensipleri:
- • Güncel JWT kütüphaneleri kullanın
- • Robust signature verification yapın
- • Güçlü ve unique secret key'ler kullanın
- • Header parametrelerini validate edin
- • Algorithm whitelist uygulayın
Signature Verification Best Practices
- • Asla decode() metodunu tek başına kullanmayın
- • Her zaman verify() metodunu kullanın
- • "none" algoritmasını whitelist'e almayın
- • Algorithm confusion'a karşı koruma sağlayın
Güvenli JWT Implementation:
// Doğru - Güvenli verification
const allowedAlgorithms = ['HS256', 'RS256'];
const decoded = jwt.verify(token, secretKey, {
algorithms: allowedAlgorithms
});
// Yanlış - Güvenlik açığı
const decoded = jwt.decode(token); // İmza doğrulanmaz!
const decoded = jwt.verify(token, secretKey); // Algorithm belirtilmemiş
Header Parameter Security:
• jku: Strict whitelist of trusted hosts
• kid: Path traversal ve SQL injection kontrolü
• jwk: Embedded key'leri reject edin
• x5c: Certificate validation yapın
Secret Key Management
- • Minimum 256-bit entropy ile random generation
- • Environment variables veya secure vault'ta saklama
- • Asla hardcode etmeyin
- • Regular key rotation uygulayın
- • Development/production key'lerini ayırın
JWT'ler için proper lifecycle management: short expiration times, refresh token patterns, logout시 token revocation, ve aud (audience) claims kullanımı.
Additional Security Measures
- • Always set expiration date (exp claim)
- • Use aud (audience) claim for intended recipients
- • Avoid sending tokens in URL parameters
- • Implement proper logout/revocation mechanism
- • Use HTTPS only for token transmission
- • Monitor for suspicious JWT usage patterns
Secure JWT Configuration Örneği:
const jwtConfig = {
algorithm: 'HS256',
expiresIn: '15m',
issuer: 'your-app.com',
audience: 'your-app.com',
notBefore: 0,
clockTolerance: 30
};
// Token generation
const token = jwt.sign(payload, secretKey, jwtConfig);
// Token verification
const decoded = jwt.verify(token, secretKey, {
algorithms: ['HS256'],
issuer: 'your-app.com',
audience: 'your-app.com'
});