fix: do not throw ERR_JWE_DECRYPTION_FAILED, but catch it and ignore the cookie. #2487
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📋 Changes
When the SDK can not decrypt the cookie, which typically happens because of a change in secret, the middleware just blows up:
Instead, we should handle this in the same way we handle an expired JWE, and ignore it.
After this PR, instead of an unhandled decryption error being thrown from the middleware, the cookie will be ignored and no session data will be available. The user can re-login to resolve. Alternatively, if the secret would have been accidentically changed, updating that again would restore the previous session (assuming the user did not re-login with the different secret)
📎 References
N/A
🎯 Testing
To test this:
git clone https://github.com/auth0/nextjs-auth0cd nextjs-auth0pnpm icd examples/with-shadcnpnpm ipnpm run devThen login, change the secret inside
.envafter logging in, refresh the page and see the middleware throw an error.To test this fix, use the same steps, but ensure you use the local version of the SDK by using:
pnpm pack(in the root)cd examples/with-shadcnpnpm remove @auth0/nextjs-auth0pnpm add ../path/to/tgzIn this case, upon refreshing the page after changing the secret, we should see no error. instead the user is considered not logged in.