Skip to content

isExpired doesn't correctly validate iat #100

@ppamorim

Description

@ppamorim

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:

public boolean isExpired(long leeway) {
if (leeway < 0) {
throw new IllegalArgumentException("The leeway must be a positive value.");
}
long todayTime = (long) (Math.floor(new Date().getTime() / 1000) * 1000); //truncate millis
Date futureToday = new Date(todayTime + leeway * 1000);
Date pastToday = new Date(todayTime - leeway * 1000);
boolean expValid = payload.exp == null || !pastToday.after(payload.exp);
boolean iatValid = payload.iat == null || !futureToday.before(payload.iat);
return !expValid || !iatValid;
}

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis points to a verified bug in the code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions