nextjs-auth0: v4: Calls to Auth0 middleware or getAccessToken in Edge runtime can cause unhandled 403 errors
Checklist
- The issue can be reproduced in the nextjs-auth0 sample app (or N/A).
- I have looked into the Readme, Examples, and FAQ 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
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:
{ code: 'OAUTH_RESPONSE_BODY_ERROR', error: 'mfa_required', status: 403, error_description: 'Multifactor authentication required' }{ code: 'OAUTH_RESPONSE_IS_NOT_CONFORM' }
When caused by calls to auth0.middleware
- We call
await auth0.middleware(request)in our middleware - Middleware calls:
handleAccessToken handleAccessTokencalls:getTokenSetgetTokenSetcallsoauthwhich 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
- We call
await auth0.getAccessToken()in our middleware 2.getAccessTokendefinition:getAccessToken getAccessTokencalls:getTokenSetgetTokenSetcallsoauthwhich 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)
@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 bug report is specifically about error handling in our Next.js middleware, not in a server component as in your example.
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.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:
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.When the underlying OAuth issues in my report occur, the
try...catchdoes not catch the errors allowing us to handle them. Instead, the user sees a403 Forbiddenerror 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
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:
*In the above code, we never receive an
authResponsefrom the middleware with a403error. The error appears to be internal to the library, meaning the user just sees a403 Forbiddenerror page.Please let me know if you need any additional details.
Thanks,