Skip to content
This repository was archived by the owner on Nov 16, 2026. It is now read-only.

feat: add @sentry/api/browser entry point for cookie+CSRF auth - #73

Merged
MathurAditya724 merged 6 commits into
mainfrom
feat/browser-client
Jun 3, 2026
Merged

MathurAditya724 merged 6 commits into
mainfrom
feat/browser-client

Conversation

@betegon

@betegon betegon commented Jun 2, 2026

Copy link
Copy Markdown
Member

Adds a @sentry/api/browser subpath export with two helpers for using the SDK inside a browser app that authenticates via session cookies rather than a Bearer token.

The Sentry frontend (and any self-hosted frontend) can't use the existing SDK as-is because it has no CSRF token support and assumes an Authorization header. This change closes that gap without touching the main entry point or affecting existing consumers.

Usage

import {client} from '@sentry/api';
import {createBrowserSdkConfig} from '@sentry/api/browser';

// Once at app startup — reads the CSRF token from the 'sc' cookie automatically
client.setConfig(createBrowserSdkConfig());

Custom cookie name or token getter (e.g. for testing):

client.setConfig(createBrowserSdkConfig({
  csrfCookieName: 'csrftoken',      // Django default outside Sentry
  getCsrfToken: () => myTokenFn(),  // override entirely
  baseUrl: 'https://sentry.io',     // for non-same-origin use
}));

What changed

  • lib/browser-client.ts: new hand-written module (same pattern as lib/sentry-pagination.ts) — createBrowserFetch wraps globalThis.fetch to inject X-CSRFToken on non-safe methods and force credentials: 'include'; createBrowserSdkConfig is a convenience wrapper that also sets baseUrl: '' for same-origin requests
  • test/browser.test.ts: 13 tests covering CSRF injection on POST/PUT/PATCH/DELETE, safe method exclusions, empty token handling, credentials override, and the no-init default
  • build.mjs: copies browser-client.ts into src/, creates src/browser.ts entry, bundles to dist/browser.js
  • package.json: adds "./browser" to exports

@betegon
betegon marked this pull request as ready for review June 2, 2026 17:42
@betegon
betegon requested a review from MathurAditya724 June 2, 2026 17:43
Comment thread lib/browser-client.ts Resolved
Comment thread lib/browser-client.ts Resolved

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ee749ea. Configure here.

Comment thread lib/browser-client.ts Resolved

@MathurAditya724 MathurAditya724 left a comment

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.

LGTM 🚀

@MathurAditya724
MathurAditya724 merged commit 1e0b811 into main Jun 3, 2026
10 checks passed
@MathurAditya724
MathurAditya724 deleted the feat/browser-client branch June 3, 2026 04:54
@betegon betegon mentioned this pull request Nov 5, 2026
8 of 39 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants