Skip to content

fix(api): Send the browser CSRF token only to similar origins - #131230

Merged
BYK merged 3 commits into
masterfrom
fix/api-browser-csrf-origin
Nov 16, 2026
Merged

BYK merged 3 commits into
masterfrom
fix/api-browser-csrf-origin

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Part of #131203.

Hardening fix for @sentry/api/browser. createBrowserFetch attached X-CSRFToken to every unsafe-method request, wherever it went. It now sends the token only to the page's origin or a similar one.

The token goes on an unsafe-method request only when the target host is the page's host, a parent or subdomain of it, or a sibling subdomain under a parent of at least two labels (us.sentry.io from acme.sentry.io).

The page origin is location.origin, and a new origin option overrides it. Without either one, such as in Node or Workers, only relative URLs get the token.

Behavior change outside browsers: an absolute baseUrl there no longer gets a token unless origin is set. In a browser, requests to similar origins behave as before. credentials: 'include' is unchanged.

via @BYK.

--

View Session [Sentry]

createBrowserFetch attached X-CSRFToken to every unsafe-method request,
whatever its destination. Match the Sentry frontend's rule instead: send
it only when the target is the page's origin or a similar one (same
host, parent or subdomain, or sibling subdomain), comparing hosts by
whole labels. The page origin is location.origin, or the new origin
option; without either, only relative URLs get the token.

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot requested a review from a team as a code owner November 16, 2026 13:30
@sentry-junior
sentry-junior Bot requested a review from a team as a code owner November 16, 2026 13:30
@sentry-junior
sentry-junior Bot requested a review from BYK November 16, 2026 13:30
@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Nov 16, 2026
@BYK
BYK enabled auto-merge (squash) November 16, 2026 13:31
* subdomains under a parent of at least two labels (`us.sentry.io` and
* `acme.sentry.io`). Hosts are compared by whole DNS labels.
*/
function isSimilarOrigin(target: URL, origin: URL): boolean {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we also enforce https -> https all the time?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. The rule only compares hosts, not schemes, so an https page would still attach the token to a request to http:// on a matching host, and the token would travel in plaintext. I'd require the target's scheme to match when the page is on https, and leave http pages as they are so local dev keeps working. This PR is merged, so the fix would be a small follow-up PR with tests. Want me to open it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #131274: from an https: page (or origin), the token now goes only to https: targets. http: pages keep today's behavior.

@BYK
BYK merged commit 851138b into master Nov 16, 2026
87 checks passed
@BYK
BYK deleted the fix/api-browser-csrf-origin branch November 16, 2026 14:06
BYK added a commit that referenced this pull request Nov 16, 2026
#131274)

Part of #131203. Follow-up to #131230.

Hardening for `@sentry/api/browser`, so a page served over https keeps
its CSRF token on https. When the page, or the `origin` option, is
`https:`, `createBrowserFetch` now attaches `X-CSRFToken` only to
`https:` targets. The host rule stays the same. `http:` pages, such as
local development servers, keep today's behavior, so nothing breaks for
them.

The README's "Browser sessions" paragraph and the `createBrowserFetch`
JSDoc describe the rule.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A9cddb8264df48e793bc61c15cd431e51 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A9cddb8264df48e793bc61c15cd431e51)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A9cddb8264df48e793bc61c15cd431e51/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>

This branch was successfully deployed

1 active deployment
Preview — a6e5f137 Deployed Nov 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant