Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-lemons-say.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baseplate-dev/plugin-auth': patch
---

Make sure we clear the cookie properly when session is invalid
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class CookieUserSessionService implements UserSessionService {
} catch (err) {
// clear the cookie if it's invalid
if (err instanceof InvalidSessionError && reply) {
reply.clearCookie(cookieName);
reply.clearCookie(cookieName, COOKIE_OPTIONS);
}
throw err;
Comment on lines 239 to 243
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Bug: Passing COOKIE_OPTIONS to clearCookie likely prevents deletion (Max-Age overrides Expires).

COOKIE_OPTIONS includes a positive maxAge. In @fastify/cookie, clearCookie defaults maxAge to 0; overriding it with a positive value results in a Set-Cookie with Max-Age > 0, which takes precedence over Expires=1970 and can fail to clear the cookie. Fix by forcing maxAge: 0 when clearing.

Apply this diff:

-        reply.clearCookie(cookieName, COOKIE_OPTIONS);
+        reply.clearCookie(cookieName, { ...COOKIE_OPTIONS, maxAge: 0 });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// clear the cookie if it's invalid
if (err instanceof InvalidSessionError && reply) {
reply.clearCookie(cookieName);
reply.clearCookie(cookieName, COOKIE_OPTIONS);
}
throw err;
// clear the cookie if it's invalid
if (err instanceof InvalidSessionError && reply) {
reply.clearCookie(cookieName, { ...COOKIE_OPTIONS, maxAge: 0 });
}
throw err;
🤖 Prompt for AI Agents
In
plugins/plugin-auth/src/local-auth/core/generators/auth-module/templates/module/services/user-session.service.ts
around lines 239 to 243, the call to reply.clearCookie passes COOKIE_OPTIONS
which contains a positive maxAge and thus prevents the cookie from being
deleted; change the clearCookie invocation to explicitly force deletion by
overriding COOKIE_OPTIONS with maxAge: 0 (and optionally expires: new Date(0))
so the Set-Cookie header will clear the cookie as intended.

}
Expand Down