Skip to content

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

Open
@localden

Description

@localden

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");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions