Fix graceful handling of invalid anonymous JWT cookies#2585
Fix graceful handling of invalid anonymous JWT cookies#2585matthewvolk merged 7 commits intocanaryfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
3 Skipped Deployments
|
🦋 Changeset detectedLatest commit: 0efb7fd The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
360ea5f to
5ee2775
Compare
Co-Authored-By: bookernath <8922457+bookernath@users.noreply.github.com>
5ee2775 to
a343d0f
Compare
|
@copilot I've moved the approach to middleware because there were bugs with the initial approach, can you clean it up? |
Co-authored-by: bookernath <8922457+bookernath@users.noreply.github.com>
Co-authored-by: bookernath <8922457+bookernath@users.noreply.github.com>
I've cleaned up the implementation in commit 3655888. The approach is now properly contained within the
The implementation is now cleaner and follows better separation of concerns. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
| node_modules | ||
| dist | ||
| .turbo | ||
| package-lock.json |
There was a problem hiding this comment.
Do not use npm, only use pnpm
There was a problem hiding this comment.
@copilot Do not use npm, only use pnpm
There was a problem hiding this comment.
Removed package-lock.json from .gitignore since this is a pnpm-only workspace. Fixed in commit ebdef36.
| const useSecureCookies = await shouldUseSecureCookie(); | ||
| const cookiePrefix = useSecureCookies ? '__Secure-' : ''; | ||
| // Clear the invalid cookie | ||
| await clearAnonymousSession(); |
There was a problem hiding this comment.
@copilot Can't this entire PR be simplified if we simply omit this line and just return null, and allow the anonymousSignIn() to trigger in this condition?
catalyst/core/middlewares/with-auth.ts
Lines 21 to 24 in 3655888
We can rely on that condition because creating an anonymous session is independent of req.auth. When you create the anonymous session only sets the anonymous cookie and does not change the NextAuth session that drives req.auth. So if the jwt cannot be decoded and we simply return null, so long as the user is not actually signed in, it should trigger the logic above.
| await clearAnonymousSession(); |
There was a problem hiding this comment.
@copilot Can't this entire PR be simplified if we simply omit this line and just return null, and allow the anonymousSignIn() to trigger in this condition?
There was a problem hiding this comment.
You're absolutely right! Simplified the implementation by removing the explicit clearAnonymousSession() call. Now the error handler just returns null and lets the existing middleware logic at lines 21-24 handle creating a new anonymous session, which automatically overwrites the invalid cookie. This is much cleaner and avoids duplicating cookie management logic. Fixed in commit ebdef36.
There was a problem hiding this comment.
Simplified the error handling as suggested by removing the console.error line entirely. The function now just returns null when JWT decoding fails, which triggers the middleware logic at lines 21-24 to create a new anonymous session that overwrites the invalid cookie. This approach is cleaner and avoids duplicating logging logic. Fixed in commit 24eda3f.
Co-authored-by: bookernath <8922457+bookernath@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Co-authored-by: bookernath <8922457+bookernath@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Now that I've contributed to this PR, I'd prefer that someone else gives final approval

Problem
Catalyst was throwing ungraceful errors when users had invalid anonymous JWT cookies. This typically occurred when:
AUTH_SECRETThe error would propagate up and crash the request, providing a poor user experience.
Solution
This PR adds graceful error handling to the
getAnonymousSession()function incore/auth/anonymous-session.ts. The changes:decode()call fromnext-auth/jwtis now wrapped in error handlingnullwhich triggers the middleware to create a new anonymous session that overwrites the invalid cookieImplementation
Behavior
The error handling is cleanly encapsulated within the auth module, leveraging existing middleware logic. When
getAnonymousSession()returnsnulldue to an invalid JWT, the middleware detects the missing anonymous session and creates a new one, which automatically overwrites the invalid cookie.Testing
Added E2E test coverage to verify:
Additional Changes
package-lock.jsonentry since this is a pnpm-only workspace✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.