nextjs-auth0: v4: Calls to Auth0 middleware or getAccessToken in Edge runtime can cause unhandled 403 errors

Checklist

Description

What happens

The problem we are experiencing appears to ultimately be caused by this library’s calls to oauth resulting from our middleware calling either auth0.middleware or auth0.getAccessToken. When it happens, users see a 403 page, and we are unable to catch and handle the error in our middleware because it is unhandled in the Auth0 middleware / route handlers.

The actual cause of the underlying OAuth errors are issues with the OAuth providers we use, and are not the focus of this bug report.

What we would expect to happen

We would expect calls to auth0.middleware or auth0.getAccessToken to be safe to call from our middleware, either not erroring or erroring in a way which we are able to handle. But because these errors are unhandled in the Auth0 middleware / route handlers we cannot handle them with a try...catch; my understanding here is that this is a limitation of error handling in Next.js/ the Edge runtime.

Additionally, the 403 page users wee when this happens is just a plain, unstyled 403 Forbidden page, and we don’t appear to have any control over this page or the ability to override this behavior.

Reproduction

This issue can be reproduced in the Next.js Edge runtime by calling auth0.middleware or auth0.getAccessToken from the middleware in a configuration which would produce an OAuth error. Reproduction of the underlying OAuth error would depend on the OAuth error you are attempting to reproduce, your Auth0 configuration, your SSO configuration, etc. In our experience, any OAuth error reproduces the issue.

Additional context

OAuth errors that cause this issue

I assume any OAuth error could cause this issue, but the ones we have seen are:

  1. { code: 'OAUTH_RESPONSE_BODY_ERROR', error: 'mfa_required', status: 403, error_description: 'Multifactor authentication required' }
  2. { code: 'OAUTH_RESPONSE_IS_NOT_CONFORM' }

When caused by calls to auth0.middleware

  1. We call await auth0.middleware(request) in our middleware
  2. Middleware calls: handleAccessToken
  3. handleAccessToken calls: getTokenSet
  4. getTokenSet calls oauth which raises this error
iF: server responded with an error in the response body
  at ae (.next/server/middleware.js:13:109533)
  at async aG.getTokenSet (.next/server/middleware.js:13:129399)
  at async aG.handleAccessToken (.next/server/middleware.js:13:128080)
  at async oZ (.next/server/middleware.js:13:171367)
  at async handler (.next/server/middleware.js:13:173296)
  at async (.next/server/middleware.js:13:32412)
  at async ti (.next/server/middleware.js:13:29268) {
    code: 'OAUTH_RESPONSE_BODY_ERROR',
    error: 'mfa_required',
    status: 403,
    error_description: 'Multifactor authentication required'
  }

When caused by calls to auth0.getAccessToken

  1. We call await auth0.getAccessToken() in our middleware 2.getAccessToken definition: getAccessToken
  2. getAccessToken calls: getTokenSet
  3. getTokenSet calls oauth which raises this error
iF: server responded with an error in the response body
  at ae (.next/server/middleware.js:13:109533)
  at async aG.getTokenSet (.next/server/middleware.js:13:129399)
  at async oV.getAccessToken (.next/server/middleware.js:13:167594)
  at async oZ (.next/server/middleware.js:13:171624)
  at async handler (.next/server/middleware.js:13:173296)
  at async (.next/server/middleware.js:13:32412)
  at async ti (.next/server/middleware.js:13:29268) {
    code: 'OAUTH_RESPONSE_BODY_ERROR',
    error: 'mfa_required',
    status: 403,
    error_description: 'Multifactor authentication required'
  }

nextjs-auth0 version

4.0.0-beta.13

Next.js version

15.1.3

Node.js version

22.12.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (5 by maintainers)

Most upvoted comments

@gyaneshgouraw-okta @nandan-bhat @arpit-jn @guabu Could this issue please be reopened? This issue still exists and v4 has been released without a resolution for it.

@guabu Let’s take a few steps back here. I think some important details were missed when reviewing my initial bug report.

The problem we are experiencing appears to ultimately be caused by this library’s calls to oauth resulting from our middleware calling either auth0.middleware or auth0.getAccessToken. When it happens, users see a 403 page, and we are unable to catch and handle the error in our middleware because it is unhandled in the Auth0 middleware / route handlers.

The bug report is specifically about error handling in our Next.js middleware, not in a server component as in your example.

The actual cause of the underlying OAuth errors are issues with the OAuth providers we use, and are not the focus of this bug report.

I think you’re getting caught up on how to fix the MFA issue specifically, but this bug report is not about the MFA issue or any specific issue - it’s about how this library handles issues when they occur.

As I mentioned in the report, another example of an underlying OAuth error which causes this error is { code: 'OAUTH_RESPONSE_IS_NOT_CONFORM' }, which is not an MFA issue.

This issue can be reproduced in the Next.js Edge runtime by calling auth0.middleware or auth0.getAccessToken from the middleware in a configuration which would produce an OAuth error.

Here is a code sample that should reproduce the issue for you, assuming you trigger one of the underlying OAuth issues I mentioned and then visit a page handled by the middleware:

export async function middleware(request: NextRequest) {
  let authResponse: NextResponse;
  try {
    authResponse = await auth0.middleware(request);

    if (request.nextUrl.pathname.startsWith('/auth')) {
      return authResponse;
    }
  } catch (error) {
    // When an underlying OAuth issue occurs, this never happens. Instead, the user sees a "403 Forbidden" page in
    // their browser
    return NextResponse.redirect('/auth/login');
  }

  const response = NextResponse.next();
  for (const [key, value] of authResponse.headers) {
    response.headers.set(key, value);
  }

  return response;
}

As far as I can tell, this is no different than the examples in the documentation of this library, except the introduction of the try...catch.

Additionally, the 403 page users wee when this happens is just a plain, unstyled 403 Forbidden page, and we don’t appear to have any control over this page or the ability to override this behavior.

When the underlying OAuth issues in my report occur, the try...catch does not catch the errors allowing us to handle them. Instead, the user sees a 403 Forbidden error in their browser, and cannot recover from the error unless they clear their cookies.

At first glance, this would appear to be because this library handles the error and returns an unauthorized response:

https://github.com/auth0/nextjs-auth0/blob/v4.0.0-beta.14/src/server/auth-client.ts#L486-L500

const [error, updatedTokenSet] = await this.getTokenSet(session.tokenSet)

if (error) {
  return NextResponse.json(
    {
      error: {
        message: error.message,
        code: error.code,
      },
    },
    {
      status: 401,
    }
  )
}

However, this is a 401, not a 403. And if this was the source of the error, we could handle it with the following adjustment to my middleware code sample, but this also does not handle the error:

export async function middleware(request: NextRequest) {
  let authResponse: NextResponse;
  try {
    authResponse = await auth0.middleware(request);

    if (request.nextUrl.pathname.startsWith('/auth')) {
      if (request.nextUrl.pathname === '/auth/access-token') {
        return authResponse;
      }

      if (authResponse.status >= 400 && authResponse.status <= 599) {
        // When an underlying OAuth issue occurs, this never happens. Instead, the user sees a "403 Forbidden" page in
        // their browser
        return NextResponse.redirect('/auth/login');
      }
      
      return authResponse;
    }
  } catch (error) {
    // When an underlying OAuth issue occurs, this never happens. Instead, the user sees a "403 Forbidden" page in
    // their browser
    return NextResponse.redirect('/auth/login');
  }

  const response = NextResponse.next();
  for (const [key, value] of authResponse.headers) {
    response.headers.set(key, value);
  }

  return response;
}

*In the above code, we never receive an authResponse from the middleware with a 403 error. The error appears to be internal to the library, meaning the user just sees a 403 Forbidden error page.

Please let me know if you need any additional details.

Thanks,