Skip to content

Commit 299d24b

Browse files
committed
Fix overflow when using Long.MAX_VALUE as leeway
1 parent ee7332b commit 299d24b

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/src/main/java/com/auth0/jwt/JWTVerifier.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,13 @@ private boolean assertValidInstantClaim(String claimName, Claim claim, long leew
356356
}
357357

358358
private boolean assertInstantIsFuture(Instant claimVal, long leeway, Instant now) {
359-
return claimVal == null || now.minus(Duration.ofSeconds(leeway)).isBefore(claimVal);
359+
long safeLeeway = Math.min(leeway, now.getEpochSecond() - Instant.MIN.getEpochSecond());
360+
return claimVal == null || now.minus(Duration.ofSeconds(safeLeeway)).isBefore(claimVal);
360361
}
361362

362363
private boolean assertInstantIsLessThanOrEqualToNow(Instant claimVal, long leeway, Instant now) {
363-
return !(claimVal != null && now.plus(Duration.ofSeconds(leeway)).isBefore(claimVal));
364+
long safeLeeway = Math.min(leeway, Instant.MAX.getEpochSecond() - now.getEpochSecond());
365+
return !(claimVal != null && now.plus(Duration.ofSeconds(safeLeeway)).isBefore(claimVal));
364366
}
365367

366368
private boolean assertValidAudienceClaim(

lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,18 @@ public void shouldValidateExpiresAtWithLeeway() {
685685
assertThat(jwt, is(notNullValue()));
686686
}
687687

688+
@Test
689+
public void shouldValidateExpiresAtWithMaxLeeway() {
690+
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0Nzc1OTJ9.isvT0Pqx0yjnZk53mUFSeYFJLDs-Ls9IsNAm86gIdZo";
691+
JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"))
692+
.acceptExpiresAt(Long.MAX_VALUE);
693+
DecodedJWT jwt = verification
694+
.build(mockOneSecondLater)
695+
.verify(token);
696+
697+
assertThat(jwt, is(notNullValue()));
698+
}
699+
688700
@Test
689701
public void shouldValidateExpiresAtIfPresent() {
690702
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0Nzc1OTJ9.isvT0Pqx0yjnZk53mUFSeYFJLDs-Ls9IsNAm86gIdZo";
@@ -745,6 +757,18 @@ public void shouldValidateNotBeforeWithLeeway() {
745757
assertThat(jwt, is(notNullValue()));
746758
}
747759

760+
@Test
761+
public void shouldValidateNotBeforeWithMaxLeeway() {
762+
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0Nzc1OTJ9.wq4ZmnSF2VOxcQBxPLfeh1J2Ozy1Tj5iUaERm3FKaw8";
763+
JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"))
764+
.acceptNotBefore(Long.MAX_VALUE);
765+
DecodedJWT jwt = verification
766+
.build(mockOneSecondEarlier)
767+
.verify(token);
768+
769+
assertThat(jwt, is(notNullValue()));
770+
}
771+
748772
@Test
749773
public void shouldThrowOnInvalidNotBeforeIfPresent() {
750774
IncorrectClaimException e = assertThrows(null, IncorrectClaimException.class, () -> {

0 commit comments

Comments
 (0)