This rule helps maintain the integrity and security of JSON Web Tokens (JWTs) in your Kotlin applications. JWTs are often used for authentication and information exchange, which makes them a prime target for malicious attacks. Using the none algorithm in JWT creation means that the tokens are not signed or validated, which can lead to token forgery and unauthorized access to sensitive data.
This rule is important because an attacker can modify the token payload when the algorithm is none. In this case, because there is no signature to verify that the content was not tampered with, the attacker can impersonate any user. This can lead to serious security breaches.
To adhere to this rule, always use a secure algorithm when creating JWTs. For instance, use HMAC combined with SHA-256 (HMAC256). This ensures that the tokens are signed and validated, preventing token forgery. Additionally, handle exceptions properly to ensure your application can respond effectively to any JWT creation errors.
Non-Compliant Code Examples
// Non-compliant: Using 'none' algorithm which allows token forgery
funcreateUnsafeJwtToken(issuer:String):String{try{// WARNING: This allows attackers to forge tokens
valalgorithm=Algorithm.none()returnJWT.create().withIssuer(issuer).sign(algorithm)}catch(e:JWTCreationException){throwSecurityException("Failed to create JWT token",e)}}
Compliant Code Examples
// Compliant: Using secure HMAC256 algorithm
funcreateSecureJwtToken(issuer:String,secretKey:String):String{try{// Secure algorithm with proper key
valalgorithm=Algorithm.HMAC256(secretKey)returnJWT.create().withIssuer(issuer).withIssuedAt(Date()).sign(algorithm)}catch(e:JWTCreationException){throwSecurityException("Failed to create JWT token",e)}}
원활한 통합. Datadog Code Security를 경험해 보세요
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드를 분석하세요
규칙 사용 방법
1
2
rulesets:- kotlin-security # Rules to enforce Kotlin security.
리포지토리 루트에 위의 내용을 포함하는 static-analysis.datadog.yml을 만듭니다
무료 IDE 플러그인을 사용하거나 CI 파이프라인에 Code Security 검사를 추가합니다