Repository navigation
fix(api): Send the browser CSRF token only to similar origins - #131230
Conversation
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]>
Co-Authored-By: BYK <[email protected]>
| * 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 { |
There was a problem hiding this comment.
Shall we also enforce https -> https all the time?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Opened #131274: from an https: page (or origin), the token now goes only to https: targets. http: pages keep today's behavior.
…f-origin Co-Authored-By: BYK <[email protected]>
#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]>
Part of #131203.
Hardening fix for
@sentry/api/browser.createBrowserFetchattachedX-CSRFTokento 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.iofromacme.sentry.io).The page origin is
location.origin, and a neworiginoption overrides it. Without either one, such as in Node or Workers, only relative URLs get the token.Behavior change outside browsers: an absolute
baseUrlthere no longer gets a token unlessoriginis set. In a browser, requests to similar origins behave as before.credentials: 'include'is unchanged.via @BYK.
--
View Session [Sentry]