Skip to content

Token expiration check in middleware might pass an expired token through #173

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
localden opened this issue Mar 7, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@localden
Copy link

localden commented Mar 7, 2025

This is the logic I am referring to:

// Check if the token is expired
if (!!authInfo.expiresAt && authInfo.expiresAt < Date.now() / 1000) {
  throw new InvalidTokenError("Token has expired");
}

If, for whatever reason, the token expiration is set to 0 (e.g., a dummy value to signal need for refresh), then !!authInfo.expiresAt will evaluate to false, meaning that there will be no error caught and the token might be conisdered verified.

And for what it's worth, 0 should not be treated as "token doesn't expire" - that is signaled by the lack of exp claims (as it is optional).

I rewrote it like this in my custom middleware handler:

if (!authInfo.expiresAt) {
  throw new InvalidTokenError("Token has no expiration time");
} else if (authInfo.expiresAt < Date.now() / 1000) {
  throw new InvalidTokenError("Token has expired");
}
@christian-bromann
Copy link

@localden I raised a PR for this in #446 and would love your feedback, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants