[PR #7363] [MERGED] fix: preserve attributes when expiring cookies #32860

Closed
opened 2026-04-17 23:34:23 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7363
Author: @bytaesu
Created: 1/14/2026
Status: Merged
Merged: 1/14/2026
Merged by: @himself65

Base: canaryHead: 2026-01-15/fix/expire-cookie


📝 Commits (9)

  • f2a682e test: add regression test case for state cookie attribute
  • aad31f2 chore: add BetterAuthCookie type for cookie configuration
  • 243b0c7 feat: add expireCookie helper to preserve attributes on deletion
  • 02bb683 refactor: migrate all cookie deletion logic to expireCookie helper
  • f14afe9 refactor: rename cookie options
  • 0404abc fix: test
  • 2e15907 refactor: rename cookie 'options' to 'attributes' for consistency
  • be120ef chore: lint fix
  • 41f4dab fix: replace remaining cookie 'options' references with 'attributes'

📊 Changes

15 files changed (+156 additions, -133 deletions)

View changed files

📝 packages/better-auth/src/api/routes/session.ts (+6 -20)
📝 packages/better-auth/src/cookies/cookies.test.ts (+23 -4)
📝 packages/better-auth/src/cookies/index.ts (+30 -33)
📝 packages/better-auth/src/cookies/session-store.ts (+5 -5)
📝 packages/better-auth/src/plugins/admin/routes.ts (+10 -9)
📝 packages/better-auth/src/plugins/generic-oauth/generic-oauth.test.ts (+25 -1)
📝 packages/better-auth/src/plugins/last-login-method/index.ts (+1 -1)
📝 packages/better-auth/src/plugins/mcp/index.ts (+4 -3)
📝 packages/better-auth/src/plugins/multi-session/index.ts (+19 -15)
📝 packages/better-auth/src/plugins/oidc-provider/index.ts (+8 -13)
📝 packages/better-auth/src/plugins/two-factor/verify-two-factor.ts (+13 -13)
📝 packages/better-auth/src/state.ts (+3 -6)
📝 packages/core/src/types/context.ts (+2 -5)
📝 packages/core/src/types/cookie.ts (+6 -4)
📝 packages/core/src/types/index.ts (+1 -1)

📄 Description

Note

Reference:
If the cookie store contains a cookie with the same name, domain, and path as 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.


Summary 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

    • Expire cookies with maxAge: 0 while keeping existing attributes.
    • Correctly delete state/admin/OIDC/multi-session/two-factor cookies with custom paths.
    • Add regression tests for expireCookie and OAuth state cookie attributes.
  • Refactors

    • Introduce expireCookie(ctx, cookie) and replace manual deletions (session, account, oauth_state, etc.).
    • Standardize cookie config to BetterAuthCookie { name, attributes } and migrate all cookie.options usages to cookie.attributes.

Written for commit 41f4dabcec. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7363 **Author:** [@bytaesu](https://github.com/bytaesu) **Created:** 1/14/2026 **Status:** ✅ Merged **Merged:** 1/14/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `2026-01-15/fix/expire-cookie` --- ### 📝 Commits (9) - [`f2a682e`](https://github.com/better-auth/better-auth/commit/f2a682e27be57e4dd817188de5493eb56451c0a9) test: add regression test case for state cookie attribute - [`aad31f2`](https://github.com/better-auth/better-auth/commit/aad31f239f1f2606d4a68d2235686e937ec11eb4) chore: add BetterAuthCookie type for cookie configuration - [`243b0c7`](https://github.com/better-auth/better-auth/commit/243b0c728aa34dcb5d1200f25ea707a1c2d59709) feat: add expireCookie helper to preserve attributes on deletion - [`02bb683`](https://github.com/better-auth/better-auth/commit/02bb683c539c110131ee67461d612535bf5c3569) refactor: migrate all cookie deletion logic to expireCookie helper - [`f14afe9`](https://github.com/better-auth/better-auth/commit/f14afe957939a5c67842613f991b9e806145fe96) refactor: rename cookie options - [`0404abc`](https://github.com/better-auth/better-auth/commit/0404abc6260f45e21ff0001f97eb4b938eb6f227) fix: test - [`2e15907`](https://github.com/better-auth/better-auth/commit/2e15907607999366bdb472240663c82742405783) refactor: rename cookie 'options' to 'attributes' for consistency - [`be120ef`](https://github.com/better-auth/better-auth/commit/be120ef3dad9b0799760566ae35fdc34ffb1cc32) chore: lint fix - [`41f4dab`](https://github.com/better-auth/better-auth/commit/41f4dabcec985dcef6460e6ce6f68e24651c737c) fix: replace remaining cookie 'options' references with 'attributes' ### 📊 Changes **15 files changed** (+156 additions, -133 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/routes/session.ts` (+6 -20) 📝 `packages/better-auth/src/cookies/cookies.test.ts` (+23 -4) 📝 `packages/better-auth/src/cookies/index.ts` (+30 -33) 📝 `packages/better-auth/src/cookies/session-store.ts` (+5 -5) 📝 `packages/better-auth/src/plugins/admin/routes.ts` (+10 -9) 📝 `packages/better-auth/src/plugins/generic-oauth/generic-oauth.test.ts` (+25 -1) 📝 `packages/better-auth/src/plugins/last-login-method/index.ts` (+1 -1) 📝 `packages/better-auth/src/plugins/mcp/index.ts` (+4 -3) 📝 `packages/better-auth/src/plugins/multi-session/index.ts` (+19 -15) 📝 `packages/better-auth/src/plugins/oidc-provider/index.ts` (+8 -13) 📝 `packages/better-auth/src/plugins/two-factor/verify-two-factor.ts` (+13 -13) 📝 `packages/better-auth/src/state.ts` (+3 -6) 📝 `packages/core/src/types/context.ts` (+2 -5) 📝 `packages/core/src/types/cookie.ts` (+6 -4) 📝 `packages/core/src/types/index.ts` (+1 -1) </details> ### 📄 Description > [!NOTE] > Reference: > If the cookie store contains a cookie with the same `name`, `domain`, and `path` as 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. - Closes #7352 <!-- This is an auto-generated description by cubic. --> --- ## Summary 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** - Expire cookies with maxAge: 0 while keeping existing attributes. - Correctly delete state/admin/OIDC/multi-session/two-factor cookies with custom paths. - Add regression tests for expireCookie and OAuth state cookie attributes. - **Refactors** - Introduce expireCookie(ctx, cookie) and replace manual deletions (session, account, oauth_state, etc.). - Standardize cookie config to BetterAuthCookie { name, attributes } and migrate all cookie.options usages to cookie.attributes. <sup>Written for commit 41f4dabcec985dcef6460e6ce6f68e24651c737c. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-17 23:34:23 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#32860