nextjs-auth0: Middleware crashes when JWT is expired

Checklist

Description

In v4.5.1 (latest version at the time of writing), a security patch was released to ensure JWTs are expired alongside the cookies. However, that has caused some side-effects - the middleware now crashes with HTTP 500 whenever an expired JWT hits the server:

[JWTExpired: "exp" claim timestamp check failed]

I am not sure this is an intentional behaviour or an oversight? Coming from the user’s perspective, I felt like the middleware should have caught this and handled as if there is no session, instead of throwing. How it is handled could dependant on the route - e.g. profile should return 401, and for non-auth routes probably short circuit the session updates.

Reproduction

  1. Obtain an expired JWT
  2. Hit the Next.js server wired up with the Auth0 middleware
  3. Boom, HTTP 500

Additional context

No response

nextjs-auth0 version

4.5.1

Next.js version

14.2.28

Node.js version

22.14.0

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 13
  • Comments: 27 (9 by maintainers)

Most upvoted comments

FYI, #2082 has been merged and we’ll make a release soon.

Any updates on this? We’re currently asking users to clear their cookies as a workaround until this PR is merged. Appreciate the quick turnaround.

is there any updates for this?

Thanks, that allows me to reproduce. I think the key here is indeed transient.

https://github.com/auth0/nextjs-auth0/pull/2082 does solve it, but we need to consider if we also need to be dropping the cookie.

Even though I understand that, and I see how and why it crashes, that is not a valid scenario and I would like to ensure I can reproduce a valid scenario here as well (it’s a different error when the JWE is expired vs when the JWE is invalid).

I have a PR ready that I think could solve this and aligns with what you are saying: #2082 But this PR is built purely theoretical, and I am unable to verify the changes as I can not reproduce the behavior so I want to be able to verify this before releasing.

Faire enough, I’ve reproduced the exact same error again.

Make the below changes:

export const auth0 = new Auth0Client({
    authorizationParameters: {
        audience: 'The audience',
    },
    // Short session settings to trigger JWTExpired ("exp" claim) reliably.
    session: {
        rolling: true,
        absoluteDuration: 3,
        inactivityDuration: 1,
        // Transient session cookie so JWE expires before browser drops it
        cookie: { transient: true },
    }
})

Then load the page, login, and wait for 5 seconds, and then refresh the whole page.

I guess the root issue is that the browser sometimes still sends the cookie to the server after expiry or that expiry time was somehow overwritten. Anyway, the server fails to decode the token, and exception was thrown.

@frederikprijck I’ve followed your guide and spin up an instance locally and can reproduce the issue.

The problem is that whenever the return await auth0.middleware(request); has encountered an error, the error directly crashes the middleware and causes 500 error to return. It needs to have an error boundary.

To simulate the error in middleware, I first login normally with my account. Instead of changing timeout and things, I simply go to the browser console, and find the cookie named __session__1 , and change the last character in its content to something else, so it can’t successfully decrypt the content. Though it is a different scenario, it simulates the same JWT decryption error others and I have been encountering for various reasons.

After changing the cookie value, I simply reload the web page, and now it throws the error like below:

Image

I think the fix would be to catch the errors in middleware and treat the cookie as not valid or not present, and clear the user session. So my expectation is that the page either displays to a guest user, or if the page is protected, navigate to the login page.

As mentioned above we have been unable to reproduce this and have asked for a reproduction to help this move forward.

Even more so, above it was called out by someone to be an issue on their end. I am not saying this can not be an issue in the SDK, but I have not been able to reproduce it.

Any chance you can elaborate what you exacly mean when you say to obtain an expired JWT? And how do you use it?

We have a fairly standard set up actually - with the middleware and extra auth checks on every page. Obtaining an expired JWT is just a way to reproduce the problem.

The only special thing (that makes the issue very pronounced for us) is@ that we use short absolute/idle session durations (both 1h).