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

Open
opened 2026-04-13 10:31:09 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/better-auth/better-auth/pull/8738

State: open
Merged: No


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.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/8738 **State:** open **Merged:** No --- ## 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. -->
GiteaMirror added the pull-request label 2026-04-13 10:31:09 -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#16429