Fix Date claims before the Unix epoch rounding toward zero - #813
Open
hossam1244 wants to merge 1 commit into
Open
hossam1244 wants to merge 1 commit into
hossam1244 wants to merge 1 commit into
Conversation
Date claims were serialized with truncating division, so any Date before 1970-01-01T00:00:00Z serialized to the wrong (later) second: new Date(-500) became 0 instead of -1, silently corrupting the claim. Instant claims already floor correctly via getEpochSecond(). Use Math.floorDiv so both types agree on the boundary second. Fixes auth0#806
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
ClaimsSerializer.dateToSecondsserializedjava.util.Dateclaims with truncating integer division (date.getTime() / 1000), so any date before the Unix epoch rounded toward zero and produced a later timestamp than the one set:new Date(-500)→ serialized as0(should be-1)Instant.ofEpochMilli(-500)(same moment) → correctly serialized as-1So the same instant silently produced different tokens depending on whether it was provided as a
Dateor anInstant.The fix replaces the truncating division with
Math.floorDiv(date.getTime(), 1000L), making theDatepath agree with the existingInstantpath (getEpochSecond(), which floors). Public API is unchanged; only the (incorrect) serialized output for pre-epoch dates changes.Classes changed:
com.auth0.jwt.impl.ClaimsSerializer(private static method),JWTCreatorTest(new tests).References
Fixes #806
Testing
shouldAddExpiresAtBeforeEpochConsistentlyWithInstant: signsnew Date(-500)and its equivalentInstantand asserts both serialize to{"exp":-1}and are identical to each other.shouldAddNotBeforeBeforeEpoch:new Date(-500)→{"nbf":-1}.masterand pass with this change; full:java-jwt:testsuite passes (run on JDK 11, Gradle 6.9.2).