Skip to content

Fix overflow when using Long.MAX_VALUE as leeway #723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/src/main/java/com/auth0/jwt/JWTVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,13 @@ private boolean assertValidInstantClaim(String claimName, Claim claim, long leew
}

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

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

private boolean assertValidAudienceClaim(
Expand Down
24 changes: 24 additions & 0 deletions lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,18 @@ public void shouldValidateExpiresAtWithLeeway() {
assertThat(jwt, is(notNullValue()));
}

@Test
public void shouldValidateExpiresAtWithMaxLeeway() {
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0Nzc1OTJ9.isvT0Pqx0yjnZk53mUFSeYFJLDs-Ls9IsNAm86gIdZo";
JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"))
.acceptExpiresAt(Long.MAX_VALUE);
DecodedJWT jwt = verification
.build(mockOneSecondLater)
.verify(token);

assertThat(jwt, is(notNullValue()));
}

@Test
public void shouldValidateExpiresAtIfPresent() {
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0Nzc1OTJ9.isvT0Pqx0yjnZk53mUFSeYFJLDs-Ls9IsNAm86gIdZo";
Expand Down Expand Up @@ -745,6 +757,18 @@ public void shouldValidateNotBeforeWithLeeway() {
assertThat(jwt, is(notNullValue()));
}

@Test
public void shouldValidateNotBeforeWithMaxLeeway() {
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0Nzc1OTJ9.wq4ZmnSF2VOxcQBxPLfeh1J2Ozy1Tj5iUaERm3FKaw8";
JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"))
.acceptNotBefore(Long.MAX_VALUE);
DecodedJWT jwt = verification
.build(mockOneSecondEarlier)
.verify(token);

assertThat(jwt, is(notNullValue()));
}

@Test
public void shouldThrowOnInvalidNotBeforeIfPresent() {
IncorrectClaimException e = assertThrows(null, IncorrectClaimException.class, () -> {
Expand Down
Loading