Limit session exposure when trustedOrigins:["*"] by scoping cookies to the Request Origin #677

Closed
opened 2026-03-13 07:59:58 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @tyuen on GitHub (Feb 15, 2025).

Is this suited for github?

  • Yes, this is suited for github

The use of betterAuth({ trustedOrigins:["*"] }) (with accompanying express.cors({ origin: true })) allows any website to read the visitor's session data and perform actions on behalf of the visitor. This has its valid use cases.

I think Better Auth should support an additional (optional) feature to scope the cookie to the website that made the request.

i.e. the website http://example.com accessing the API backend would utilize the cookie name "example.com.better-auth.session_token"

This feature allows a visitor on different websites to connect to the same API provider without the session being shared on the different websites.

As an added bonus, if a visitor on a rogue website connects to the API provider, the website won't be able to see the visitor's logged in session.

Describe the solution you'd like

This can easily be accomplished by using the Origin header of the Request in a similar way to the options.advanced.cookiePrefix value.

i.e. the website http://example.com accessing the API backend would utilize the cookie name "example.com.better-auth.session_token"

The Origin header value (e.g. http://localhost:3000) would also need to cleaned up to remove the protocol and escape the ":"s from IPv6 addresses into "_"s since ":"s aren't allowed in cookie names.

If an Origin header doesn't exist, which happens when the route is accessed directly like a page navigation, then a fallback cookiePrefix of the server's own host (identified in the Host header) can be used.

Describe alternatives you've considered

An alternative is to write a before+after hook and rewrite the cookie names.

This assumes that Better Auth allows the cookie data in the hook's Context to be modified and will use the modified copy.

This also assumes that Better Auth will allow the responseHeader in the hook's Context object to be modified by hooks.

Additional context

No response

Originally created by @tyuen on GitHub (Feb 15, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. The use of `betterAuth({ trustedOrigins:["*"] })` (with accompanying `express.cors({ origin: true })`) allows any website to read the visitor's session data and perform actions on behalf of the visitor. This has its valid use cases. I think Better Auth should support an additional (optional) feature to scope the cookie to the website that made the request. i.e. the website `http://example.com` accessing the API backend would utilize the cookie name "`example.com.better-auth.session_token`" This feature allows a visitor on different websites to connect to the same API provider without the session being shared on the different websites. As an added bonus, if a visitor on a rogue website connects to the API provider, the website won't be able to see the visitor's logged in session. ### Describe the solution you'd like This can easily be accomplished by using the `Origin` header of the Request in a similar way to the `options.advanced.cookiePrefix` value. i.e. the website `http://example.com` accessing the API backend would utilize the cookie name "`example.com.better-auth.session_token`" The Origin header value (e.g. `http://localhost:3000`) would also need to cleaned up to remove the protocol and escape the ":"s from IPv6 addresses into "_"s since ":"s aren't allowed in cookie names. If an `Origin` header doesn't exist, which happens when the route is accessed directly like a page navigation, then a fallback cookiePrefix of the server's own host (identified in the `Host` header) can be used. ### Describe alternatives you've considered An alternative is to write a before+after hook and rewrite the cookie names. This assumes that Better Auth allows the cookie data in the hook's `Context` to be modified and will use the modified copy. This also assumes that Better Auth will allow the `responseHeader` in the hook's `Context` object to be modified by hooks. ### Additional context _No response_
Author
Owner

@dosubot[bot] commented on GitHub (Jun 11, 2025):

Hi, @tyuen. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • You proposed a security enhancement to scope cookies to the request origin.
  • The enhancement aims to limit session exposure when using trustedOrigins:["*"].
  • Suggested creating origin-specific cookie names based on the Origin header.
  • An alternative approach involves using hooks to modify cookie names.
  • No comments or further activity have occurred on this issue.

Next Steps:

  • Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting here.
  • If no updates are provided, the issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Jun 11, 2025): Hi, @tyuen. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - You proposed a security enhancement to scope cookies to the request origin. - The enhancement aims to limit session exposure when using `trustedOrigins:["*"]`. - Suggested creating origin-specific cookie names based on the `Origin` header. - An alternative approach involves using hooks to modify cookie names. - No comments or further activity have occurred on this issue. **Next Steps:** - Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting here. - If no updates are provided, the issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#677