[PR #8738] fix(cookies): getCookieCache prefix mismatch with http baseURL in production #25083

Open
opened 2026-04-15 22:43:05 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8738
Author: @sleepe229
Created: 3/23/2026
Status: 🔄 Open

Base: mainHead: fix/cookie-cache-prefix-mismatch


📝 Commits (2)

  • b1b75f6 fix(cookies): fall back to both cookie name variants in getCookieCache
  • 447f26b fix(cookies): retry fallback name when validation fails due to stale chunks

📊 Changes

2 files changed (+68 additions, -32 deletions)

View changed files

📝 packages/better-auth/src/cookies/cookies.test.ts (+39 -0)
📝 packages/better-auth/src/cookies/index.ts (+29 -32)

📄 Description

Problem

When NODE_ENV=production but baseURL uses HTTP (e.g. http://localhost:8787),
createCookieGetter correctly omits the __Secure- prefix (it checks the URL
scheme), but getCookieCache only checks isProduction — so it looks for
__Secure-better-auth.session_data while the actual cookie is
better-auth.session_data, and returns null.

Fix

When isSecure is not explicitly set, getCookieCache now tries both cookie
name variants: the isProduction-preferred one first, then the other as a
fallback. Explicit isSecure config keeps its current behaviour unchanged.

This follows the same pattern already used in getSessionCookie.

Closes #8737


Summary by cubic

Fixes cookie cache misses in production when baseURL is HTTP by making getCookieCache try both __Secure- and plain cookie names when isSecure is unset, and by falling back to the alternate name if parsing or validation fails (e.g., stale chunks). This prevents null session reads and matches getSessionCookie.

  • Bug Fixes
    • When isSecure is unset, try the env-preferred name first; if missing or invalid across compact/jwt/jwe, try the other. Explicit isSecure keeps current behavior.
    • Added a test to ensure fallback finds cached session data with advanced.useSecureCookies.

Written for commit 447f26bf12. 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/8738 **Author:** [@sleepe229](https://github.com/sleepe229) **Created:** 3/23/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/cookie-cache-prefix-mismatch` --- ### 📝 Commits (2) - [`b1b75f6`](https://github.com/better-auth/better-auth/commit/b1b75f658c93decde6f94c813e2fc6d598d8e42a) fix(cookies): fall back to both cookie name variants in getCookieCache - [`447f26b`](https://github.com/better-auth/better-auth/commit/447f26bf12e1b357f21e41b522faffcb0286bfed) fix(cookies): retry fallback name when validation fails due to stale chunks ### 📊 Changes **2 files changed** (+68 additions, -32 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/cookies/cookies.test.ts` (+39 -0) 📝 `packages/better-auth/src/cookies/index.ts` (+29 -32) </details> ### 📄 Description ## Problem When `NODE_ENV=production` but `baseURL` uses HTTP (e.g. `http://localhost:8787`), `createCookieGetter` correctly omits the `__Secure-` prefix (it checks the URL scheme), but `getCookieCache` only checks `isProduction` — so it looks for `__Secure-better-auth.session_data` while the actual cookie is `better-auth.session_data`, and returns `null`. ## Fix When `isSecure` is not explicitly set, `getCookieCache` now tries both cookie name variants: the `isProduction`-preferred one first, then the other as a fallback. Explicit `isSecure` config keeps its current behaviour unchanged. This follows the same pattern already used in `getSessionCookie`. Closes #8737 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes cookie cache misses in production when `baseURL` is HTTP by making `getCookieCache` try both `__Secure-` and plain cookie names when `isSecure` is unset, and by falling back to the alternate name if parsing or validation fails (e.g., stale chunks). This prevents null session reads and matches `getSessionCookie`. - Bug Fixes - When `isSecure` is unset, try the env-preferred name first; if missing or invalid across `compact`/`jwt`/`jwe`, try the other. Explicit `isSecure` keeps current behavior. - Added a test to ensure fallback finds cached session data with `advanced.useSecureCookies`. <sup>Written for commit 447f26bf12e1b357f21e41b522faffcb0286bfed. 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-15 22:43:05 -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#25083