[PR #4482] [MERGED] feat: support cross-domain cookie #31005

Closed
opened 2026-04-17 21:57:52 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4482
Author: @HenriqueBragaMoreira
Created: 9/6/2025
Status: Merged
Merged: 9/10/2025
Merged by: @himself65

Base: canaryHead: canary


📝 Commits (8)

📊 Changes

11 files changed (+476 additions, -55 deletions)

View changed files

📝 docs/content/docs/concepts/cookies.mdx (+86 -8)
📝 e2e/integration/vanilla-node/e2e/app.ts (+47 -0)
📝 e2e/integration/vanilla-node/e2e/domain.spec.ts (+29 -6)
e2e/integration/vanilla-node/e2e/fixtures/certificate.pem (+19 -0)
e2e/integration/vanilla-node/e2e/fixtures/private-key.pem (+28 -0)
📝 e2e/integration/vanilla-node/e2e/utils.ts (+14 -6)
📝 e2e/integration/vanilla-node/src/main.ts (+2 -1)
📝 e2e/integration/vanilla-node/vite.config.ts (+7 -0)
📝 packages/better-auth/src/cookies/cookies.test.ts (+141 -2)
📝 packages/better-auth/src/cookies/index.ts (+66 -19)
📝 packages/better-auth/src/types/options.ts (+37 -13)

📄 Description

This PR is based on the excellent work done by @RikhiSingh in PR #3030
.
The implementation itself was already correct, but the related documentation contained some inconsistencies.

In this PR I’ve:

Kept the original implementation exactly as it was.

Fixed and clarified the documentation so it matches the behavior correctly.

All credits for the actual fix and implementation go to @RikhiSingh. I’ve only adjusted the docs to help get this merged and make it easier for others to use.

Note to maintainers:
Could you please review and validate this PR? Since the core logic was already resolved in the original contribution, this only aligns the docs with the actual behavior. Getting this merged would help the community move forward with a consistent reference. 🙏 @Bekacru @himself65 @Kinfe123

Fixes #2962


Summary by cubic

Add cross-origin cookie support and enforce SameSite=None + Secure rules. Updates docs and tests to make cross-domain and subdomain setups safe and straightforward.

  • New Features

    • advanced.crossOriginCookies with enabled, autoSecure (default true), and allowLocalhostUnsecure (default true).
    • Auto-sets Secure for SameSite=None (skips on localhost when allowed); supports Partitioned cookies.
    • Clear docs for cross-domain vs. cross-subdomain cookies, with examples, CORS/credentials guidance, and trustedOrigins usage.
  • Bug Fixes

    • Throws when SameSite=None is used without Secure on non-localhost.
    • Applies __Secure- cookie prefix based on final merged attributes.
    • Merges defaultCookieAttributes and overrides correctly; adds tests covering all cases.

🔄 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/4482 **Author:** [@HenriqueBragaMoreira](https://github.com/HenriqueBragaMoreira) **Created:** 9/6/2025 **Status:** ✅ Merged **Merged:** 9/10/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `canary` --- ### 📝 Commits (8) - [`ad95846`](https://github.com/better-auth/better-auth/commit/ad95846aed593c8bfb1e8bb0ebabeeb9a2faaa61) fix: cross-domain cookie - [`aa9caf5`](https://github.com/better-auth/better-auth/commit/aa9caf5f6d5f2ac5dd81f457127e22a52c17a70a) fix: improve formatting - [`518465a`](https://github.com/better-auth/better-auth/commit/518465a312c82973140b4bd4479f2ab18711296e) e2e: test case for cross-origin cookie - [`54e1a14`](https://github.com/better-auth/better-auth/commit/54e1a1481c588fb4d199b80039336bb908b26e34) Merge branch 'canary' into HenriqueBragaMoreira/canary - [`dd13f7a`](https://github.com/better-auth/better-auth/commit/dd13f7a778029088a95b0ee2391b4c2fcc2563ad) docs: add - [`3377444`](https://github.com/better-auth/better-auth/commit/337744417189f87f60b6e5f8345a223d3b872ba8) fix: https - [`fac8d46`](https://github.com/better-auth/better-auth/commit/fac8d460f691e03a58d1b941a223bf6b8cf33885) fix: base url - [`beb1edb`](https://github.com/better-auth/better-auth/commit/beb1edbce33da66fd747c6b690a6c91a4a6f3ea9) fix: base url ### 📊 Changes **11 files changed** (+476 additions, -55 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/concepts/cookies.mdx` (+86 -8) 📝 `e2e/integration/vanilla-node/e2e/app.ts` (+47 -0) 📝 `e2e/integration/vanilla-node/e2e/domain.spec.ts` (+29 -6) ➕ `e2e/integration/vanilla-node/e2e/fixtures/certificate.pem` (+19 -0) ➕ `e2e/integration/vanilla-node/e2e/fixtures/private-key.pem` (+28 -0) 📝 `e2e/integration/vanilla-node/e2e/utils.ts` (+14 -6) 📝 `e2e/integration/vanilla-node/src/main.ts` (+2 -1) 📝 `e2e/integration/vanilla-node/vite.config.ts` (+7 -0) 📝 `packages/better-auth/src/cookies/cookies.test.ts` (+141 -2) 📝 `packages/better-auth/src/cookies/index.ts` (+66 -19) 📝 `packages/better-auth/src/types/options.ts` (+37 -13) </details> ### 📄 Description This PR is based on the excellent work done by @RikhiSingh in PR #3030 . The implementation itself was already correct, but the related documentation contained some inconsistencies. In this PR I’ve: Kept the original implementation exactly as it was. Fixed and clarified the documentation so it matches the behavior correctly. All credits for the actual fix and implementation go to @RikhiSingh. I’ve only adjusted the docs to help get this merged and make it easier for others to use. Note to maintainers: Could you please review and validate this PR? Since the core logic was already resolved in the original contribution, this only aligns the docs with the actual behavior. Getting this merged would help the community move forward with a consistent reference. 🙏 @Bekacru @himself65 @Kinfe123 Fixes #2962 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Add cross-origin cookie support and enforce SameSite=None + Secure rules. Updates docs and tests to make cross-domain and subdomain setups safe and straightforward. - **New Features** - advanced.crossOriginCookies with enabled, autoSecure (default true), and allowLocalhostUnsecure (default true). - Auto-sets Secure for SameSite=None (skips on localhost when allowed); supports Partitioned cookies. - Clear docs for cross-domain vs. cross-subdomain cookies, with examples, CORS/credentials guidance, and trustedOrigins usage. - **Bug Fixes** - Throws when SameSite=None is used without Secure on non-localhost. - Applies __Secure- cookie prefix based on final merged attributes. - Merges defaultCookieAttributes and overrides correctly; adds tests covering all cases. <!-- 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 21:57:52 -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#31005