CRITICAL
Rule Definition
If the expiration time requirement is disabled, a JWT token will be valid beyond its expiry time. An attacker would be able to authenticate with an expired token.
Remediation
Do not disable the expiration time requirement of a JWT token.
Violation Code Sample
// .NET
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
RequireSignedTokens = true,
RequireExpirationTime = false, // issue
ValidateIssuerSigningKey = true,
};
});
// Java
String token =
Jwts.builder()
.setSubject(subject)
.signWith(key, SignatureAlgorithm.HS512)
.compact(); // missing setExpiration method call
Fixed Code Sample
// .NET
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
RequireSignedTokens = true,
RequireExpirationTime = true, // OK
ValidateIssuerSigningKey = true,
};
});
// Java
String token =
Jwts.builder()
.setSubject(subject)
.signWith(key, SignatureAlgorithm.HS512)
.setExpiration(validity) // OK
.compact();
Reference
CWE-287: Improper Authentication
https://cwe.mitre.org/data/definitions/287.html
Related Technologies
Technical Criterion
Secure Coding - Weak Security Features
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.