`isExpired` doesn't correctly validate `iat`

Open
#100 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
android, java

Research direction

Start in lib/src/main/java/com/auth0/android/jwt/JWT.java around lines 161-171, where isExpired computes the time bounds and validates iat. Reproduce the reported case with a JWT used immediately after creation, then verify that a valid iat is accepted without weakening the existing leeway or expiration checks.

Written by the indexing model from the issue text.

Description

bug
Checklist
  • I have looked into the Readme and Examples, and have not found a suitable solution or answer.
  • I have looked into the API documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • I agree to the terms within the Auth0 Code of Conduct.
Description

Due to the way the isExpired was implemented, the iat is invalid for the first 0.5 seconds of JWT existence, given this code here:

https://github.com/auth0/JWTDecode.Android/blob/df3eb302a2164abaef36a33fded58f99f516c731/lib/src/main/java/com/auth0/android/jwt/JWT.java#L161-L171

The line 165 is rounding the current time. If your server is very fast and generate, transmit and reaches your application in less than 0.5s, the futureToday.before(payload.iat) on the line 169 returns false. But the JWT is totally valid.

Solutions:

  • Also compare futureToday == payload.iat.
  • Stop truncating the todayTime.

I reimplemented the validation in Kotlin and added some logs:

fun isExpired(jwt: JWT, leeway: Int): Boolean {
  val todayTime = (floor((Date().time / 1000).toDouble()) * 1000).toLong() //truncate millis
  val futureToday = Date(todayTime + leeway * 1000)
  val pastToday = Date(todayTime - leeway * 1000)
  val expValid = jwt.expiresAt == null || !pastToday.after(jwt.expiresAt)
  val iatValid = jwt.issuedAt == null || !futureToday.before(jwt.issuedAt)
  return !expValid || !iatValid
}

This prints:

todayTime 1740576730000
futureToday 1740576730000
pastToday 1740576730000
expValid true
iatValid false
Reproduction

Probably create the JWT and use it straightway, it will fail on the iat.

Additional context

No response

JWTDecode.Android version

2.0.2

Android version(s)

API 32

Dominant language
Java
Stars
455
Forks
81
Avg merge
5h 23m
Merged PRs (30d)
8

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from auth0/JWTDecode.Android

All issues in auth0/JWTDecode.Android

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.