[PR #7235] [CLOSED] fix: fallback to isProduction when baseURL is empty string #32766

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7235
Author: @yoshifumi-kondo
Created: 1/10/2026
Status: Closed

Base: canaryHead: fix/cookie-secure-flag-fallback


📝 Commits (1)

  • 3a86537 fix: fallback to isProduction when baseURL is empty string

📊 Changes

2 files changed (+47 additions, -3 deletions)

View changed files

📝 packages/better-auth/src/cookies/cookies.test.ts (+46 -0)
📝 packages/better-auth/src/cookies/index.ts (+1 -3)

📄 Description

Summary

Fixes #7156

When baseURL is not configured, create-context.ts sets it to an empty string "". The previous condition options.baseURL !== undefined treated empty string as a valid value, causing the secure flag to be determined by "".startsWith("https://") which always returns false.

This bypasses the intended fallback to isProduction, potentially leaving cookies insecure in production environments where baseURL is not explicitly set.

Changes

  • Changed the condition from options.baseURL !== undefined to a truthy check (options.baseURL)
  • Empty string now correctly falls back to isProduction like undefined does
  • Added tests to verify the fix and prevent regression

Test Plan

  • Added test: should use secure cookies for https baseURL
  • Added test: should not use secure cookies for http baseURL
  • Added test: should fallback to isProduction when baseURL is empty string
  • Added test: should fallback to isProduction when baseURL is undefined
  • Added test: should treat empty string baseURL the same as undefined baseURL
  • All existing tests pass
  • pnpm lint passes
  • pnpm format passes

Summary by cubic

Fixes secure cookie detection when baseURL is empty, so production cookies default to secure even if baseURL isn’t set.

  • Bug Fixes
    • Treat empty baseURL as unset; secure flag now falls back to isProduction (avoids false insecure cookies).
    • Added tests for https/http baseURL, empty string, and undefined cases.

Written for commit 3a86537249. 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/7235 **Author:** [@yoshifumi-kondo](https://github.com/yoshifumi-kondo) **Created:** 1/10/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/cookie-secure-flag-fallback` --- ### 📝 Commits (1) - [`3a86537`](https://github.com/better-auth/better-auth/commit/3a86537249d8bcaa908bdf267f994407b1a9fd32) fix: fallback to isProduction when baseURL is empty string ### 📊 Changes **2 files changed** (+47 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/cookies/cookies.test.ts` (+46 -0) 📝 `packages/better-auth/src/cookies/index.ts` (+1 -3) </details> ### 📄 Description ## Summary Fixes #7156 When `baseURL` is not configured, `create-context.ts` sets it to an empty string `""`. The previous condition `options.baseURL !== undefined` treated empty string as a valid value, causing the secure flag to be determined by `"".startsWith("https://")` which always returns `false`. This bypasses the intended fallback to `isProduction`, potentially leaving cookies insecure in production environments where `baseURL` is not explicitly set. ## Changes - Changed the condition from `options.baseURL !== undefined` to a truthy check (`options.baseURL`) - Empty string now correctly falls back to `isProduction` like `undefined` does - Added tests to verify the fix and prevent regression ## Test Plan - [x] Added test: `should use secure cookies for https baseURL` - [x] Added test: `should not use secure cookies for http baseURL` - [x] Added test: `should fallback to isProduction when baseURL is empty string` - [x] Added test: `should fallback to isProduction when baseURL is undefined` - [x] Added test: `should treat empty string baseURL the same as undefined baseURL` - [x] All existing tests pass - [x] `pnpm lint` passes - [x] `pnpm format` passes <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes secure cookie detection when baseURL is empty, so production cookies default to secure even if baseURL isn’t set. - **Bug Fixes** - Treat empty baseURL as unset; secure flag now falls back to isProduction (avoids false insecure cookies). - Added tests for https/http baseURL, empty string, and undefined cases. <sup>Written for commit 3a86537249d8bcaa908bdf267f994407b1a9fd32. 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:30:30 -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#32766