fix: preserve attributes when expiring cookies#7363
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
better-auth
@better-auth/cli
@better-auth/core
@better-auth/expo
@better-auth/mcp
@better-auth/oauth-provider
@better-auth/passkey
@better-auth/scim
@better-auth/sso
@better-auth/stripe
@better-auth/telemetry
commit: |
There was a problem hiding this comment.
Pull request overview
This PR centralizes cookie expiration logic by introducing a new expireCookie utility function that preserves cookie attributes (path, domain, httpOnly, etc.) when expiring cookies, in accordance with RFC 6265 section 5.3. Previously, cookie expiration was scattered across the codebase with inconsistent attribute handling.
Changes:
- Introduced
expireCookieutility function to centralize cookie expiration with attribute preservation - Replaced all manual cookie expiration calls (
setCookie(name, "", { maxAge: 0 })) with the new utility - Added comprehensive test coverage for the new utility function
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/core/src/types/index.ts | Export new BetterAuthCookie type for public API |
| packages/core/src/types/cookie.ts | Define BetterAuthCookie type with TODO for future cleanup |
| packages/better-auth/src/state.ts | Replace manual cookie expiration with expireCookie utility |
| packages/better-auth/src/plugins/two-factor/verify-two-factor.ts | Replace manual expiration and improve variable naming |
| packages/better-auth/src/plugins/oidc-provider/index.ts | Replace manual expiration for multiple cookies |
| packages/better-auth/src/plugins/multi-session/index.ts | Replace manual expiration with proper prefix handling |
| packages/better-auth/src/plugins/mcp/index.ts | Replace manual cookie expiration |
| packages/better-auth/src/plugins/generic-oauth/generic-oauth.test.ts | Add test verifying path attribute preservation |
| packages/better-auth/src/plugins/admin/routes.ts | Replace signed cookie expiration and improve variable naming |
| packages/better-auth/src/cookies/index.ts | Implement expireCookie utility and refactor deleteSessionCookie |
| packages/better-auth/src/cookies/cookies.test.ts | Add comprehensive tests for expireCookie |
| packages/better-auth/src/api/routes/session.ts | Replace manual session data cookie expiration |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
packages/core/src/types/cookie.ts
Outdated
| // TODO: keep only one of options or attributes | ||
| export type BetterAuthCookie = | ||
| | { name: string; options: CookieOptions } | ||
| | { name: string; attributes: CookieOptions }; |
There was a problem hiding this comment.
The TODO comment indicates incomplete work. Since this type is being exported in the public API, the inconsistency between 'options' and 'attributes' should be resolved before merging. This union type will make the API confusing for consumers who need to know which property to use.
| // TODO: keep only one of options or attributes | |
| export type BetterAuthCookie = | |
| | { name: string; options: CookieOptions } | |
| | { name: string; attributes: CookieOptions }; | |
| export type BetterAuthCookie = { name: string; options: CookieOptions }; |
There was a problem hiding this comment.
Changing the return type of createAuthCookie() could be a breaking change.. will adjust at an appropriate time later.
| const cookieOptions = | ||
| "options" in cookie ? cookie.options : cookie.attributes; |
There was a problem hiding this comment.
This conditional logic to handle both 'options' and 'attributes' properties adds complexity. Consider standardizing on one property name throughout the codebase to improve maintainability and type safety.
There was a problem hiding this comment.
Changing the return type of createAuthCookie() could be a breaking change.. will adjust at an appropriate time later.
|
Review if this PR goal is correct and whether refactoring to the centralized logic breaks any functionality. |
|
Claude finished @bytaesu's task in 2m 44s —— View job Review: Cookie Expiration RefactoringSummaryThe PR goal is correct and the refactoring is well-implemented. The centralization of cookie expiration logic properly addresses issue #7352 by preserving cookie attributes (path, domain, secure, httpOnly) when expiring cookies, in accordance with RFC 6265 section 5.3. PR Goal Assessment ✅Goal: Centralize cookie expiration and preserve attributes to ensure cookies are properly deleted across all flows. Status: Correctly implemented. The RFC 6265 requirement states:
This PR ensures that when expiring cookies, all original attributes are preserved, which is critical for browsers to match and properly delete the cookie. Implementation ReviewCore Implementation (
|
There was a problem hiding this comment.
1 issue found across 9 files (changes from recent commits).
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="packages/better-auth/src/cookies/cookies.test.ts">
<violation number="1" location="packages/better-auth/src/cookies/cookies.test.ts:1230">
P2: The "preserves attributes with attributes format" test no longer exercises the `attributes` input because it now passes `options`, so regressions in the attributes path won’t be caught. Update the test to use the `attributes` property as intended.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
|
Have a discussion with Bereket, and we use |
|
@claude, can you rename related code to use |
|
Claude finished @himself65's task in 5m 31s —— View job Renaming cookie
|
4b87be5 to
83409f0
Compare
|
@claude Please fix typecheck error Error: packages/better-auth/src/plugins/last-login-method/index.ts(156,50): error TS2339: Property 'options' does not exist on type 'BetterAuthCookie'.
Error: packages/better-auth/src/plugins/mcp/index.ts(210,8): error TS2353: Object literal may only specify known properties, and 'options' does not exist in type 'BetterAuthCookie'.
Error: packages/better-auth/src/plugins/oidc-provider/index.ts(406,8): error TS2353: Object literal may only specify known properties, and 'options' does not exist in type 'BetterAuthCookie'.
Error: packages/better-auth/src/plugins/oidc-provider/index.ts(586,7): error TS2353: Object literal may only specify known properties, and 'options' does not exist in type 'BetterAuthCookie'.
ELIFECYCLE Command failed with exit code 2. |
|
Claude finished @himself65's task in 1m 36s —— View job Fixing TypeScript errorsFixed all remaining Tasks
Changes MadeAll four TypeScript errors have been resolved by renaming
The changes are consistent with the earlier refactoring and the Commit: |
- Update BetterAuthCookie type to use 'attributes' instead of union type - Rename all cookie.options references to cookie.attributes across codebase - Update core cookie implementation (cookies/index.ts, session-store.ts) - Update plugin files (multi-session, two-factor, admin, oidc-provider, mcp) - Update state management (state.ts) - Update external packages (expo, passkey) - Update tests to reflect the change This standardizes the API and avoids breaking changes by using the same property name that createAuthCookie() returns. Co-authored-by: Alex Yang <himself65@users.noreply.github.com>
- Fix last-login-method plugin to use authCookies.sessionToken.attributes - Fix mcp and oidc-provider plugins to use 'attributes' in expireCookie calls - Resolves TypeScript compilation errors after cookie refactoring Co-authored-by: Alex Yang <himself65@users.noreply.github.com>
f1d272b to
41f4dab
Compare
Co-authored-by: Alex Yang <himself65@outlook.com> Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Alex Yang <himself65@outlook.com> Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Alex Yang <himself65@outlook.com> Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Alex Yang <himself65@outlook.com> Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Alex Yang <himself65@outlook.com> Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Alex Yang <himself65@outlook.com> Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Alex Yang <himself65@outlook.com> Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Note
Reference:
If the cookie store contains a cookie with the same
name,domain, andpathas the newly created cookie ...https://datatracker.ietf.org/doc/html/rfc6265#section-5.3
This PR centralizes cookie expiration logic and includes attributes in the expiration process.
oauth_statecookie is not removed after consumption #7352Summary by cubic
Fixes cookie expiration by preserving original attributes (path, domain, secure, httpOnly) so deletions work reliably across all flows. Centralizes the logic with expireCookie and adds tests; closes #7352.
Bug Fixes
Refactors
Written for commit 41f4dab. Summary will update on new commits.