Skip to content

feat(core): Add managed tunnel helpers that derive the tunnel path from the DSN - #25199

Open
logaretm wants to merge 40 commits into
developfrom
awad/tunnel-core-dsn-path
Open

logaretm wants to merge 40 commits into
developfrom
awad/tunnel-core-dsn-path

Conversation

@logaretm

@logaretm logaretm commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Overview

SDK Tunnel is a feature that allows client SDKs (e.g: browser) to send their requests through an endpoint that lives on the user's own application that forwards to Sentry. This bypasses adblockers and similar extensions.

Tunnels could be configured as a fixed endpoint defined by the user (defaulted to /monitoring) but that usually ends up in global adblocker lists. Alternatively, users can tell the SDK to use a random endpoint to bypass that.

Previously the tunnel logic mostly lived in client bundles and in server frameworks build time, this had two issues:

  • Both client and server bundles needed to share a single ENV during build, or a single process. So both can agree on the random URL generated.
  • CI/CD workflows means assets can drift, so older client bundles might be sending requests to older server bundles and vice versa, what happens then is during the rollout/phase-out, the random endpoints won't match and thus these envelopes will be getting 404'ed.

This stack of PRs re-works the idea by moving to more stable yet random URL generation based on the client's DSN and moves the tunnel handling into the runtime of the core SDK.

Meaning tunneling now is a core SDK capability rather than a meta framework feature.

Here is how it looks for users:

// client, posts to a path hashed from the DSN, e.g. `/k3x9q2a`
Sentry.init({ dsn, tunnel: true });

// server, nothing to do in a meta-framework, the SDK answers that path from its request hook.
// You only need this if the browser uses a different DSN than the server.
Sentry.init({ dsn, tunnel: { allowedDsns: [browserDsn] } });

And a tunneled request goes like this: the framework hook calls handleTunnelRequestIfMatched, if the path matches it checks the envelope's DSN against the allowed ones and forwards it to Sentry, otherwise the request continues as normal.

This PR

This sets up the core for the new tunnel mechanism that the rest of the stack will be implemented against, it does the following:

  • Sets up the tunnel generation and matching logic.
  • Sets up global span/breadcrumb ignore rules for tunneled requests.
  • Adds request checking, header forwarding, and the proxying to the Sentry SaaS DSN.

It also sets up a couple of key decisions that will carry over throughout the stack:

  • Base paths are respected always: if a user sets /app as a base path our tunnels handlers will expect that prefix.
  • Servers always allow their own DSNs: This avoids the footgun where a user misconfigures allowedDSNs on the server, this correction only applies if the client and server share the same DSN, if there is a different DSN involved then it is on the user to configure that correctly. Will work on documenting this.

Reviewing the stack

This PR and #25200 are where the actual behavior lives so these need the closest look, mainly forwardEnvelope and the base path handling in tunnel.ts.

The framework PRs all follow the same pattern: find the earliest request hook the framework gives us, call the core handler there, and pass the framework's base path along. They are small, so ideally whoever knows that framework best picks it up:

#25414 is tests only, and Next.js #25424 is on hold, I will split it up before it's ready.

@logaretm
logaretm added this pull request to stack #25201 October 8, 2026 15:45
@logaretm
logaretm force-pushed the awad/tunnel-core-dsn-path branch from af27973 to a2af1e1 Compare October 8, 2026 16:05
@logaretm logaretm changed the title feat(core): Derive the tunnel path from the DSN and match tunnel requests feat(core): Add managed tunnel helpers that derive the tunnel path from the DSN Oct 8, 2026
@logaretm
logaretm force-pushed the awad/tunnel-core-dsn-path branch 4 times, most recently from c544ac0 to 89a5b2c Compare October 15, 2026 15:36
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: e7eedd7 (PR test merge using develop at d4a4e81)

Against: ca4fc32 (develop) · base run #62950

⚠️ Baseline mismatch: 1 develop commit(s) in the PR build are missing from the baseline. Their size changes also affect this comparison. Compare commits. Re-run once develop's baseline build for d4a4e81 finishes.

Path Size % Change Change
@sentry/browser 29.91 kB - -
@sentry/browser - with treeshaking flags 28.05 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 27.95 kB - -
@sentry/browser (incl. Tracing) 52.1 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 52.1 kB - -
@sentry/browser (incl. Tracing, Profiling) 55.05 kB - -
@sentry/browser (incl. Tracing, Replay) 91.85 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 80.72 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 96.58 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 109.52 kB - -
@sentry/browser (incl. Feedback) 47.42 kB - -
@sentry/browser (incl. sendFeedback) 34.97 kB - -
@sentry/browser (incl. FeedbackAsync) 40.07 kB - -
@sentry/browser (incl. Metrics) 30.93 kB - -
@sentry/browser (incl. Logs) 31.21 kB - -
@sentry/browser (incl. Metrics & Logs) 31.84 kB - -
@sentry/react 31.74 kB - -
@sentry/react (incl. Tracing) 54.42 kB - -
@sentry/vue 38.07 kB - -
@sentry/vue (incl. Tracing) 55.03 kB - -
@sentry/svelte 29.93 kB - -
@sentry/remix (Remix 3 client bundle) 57.11 kB - -
CDN Bundle 31.67 kB - -
CDN Bundle (incl. Tracing) 52.61 kB - -
CDN Bundle (incl. Logs, Metrics) 33.87 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 54.55 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 74.79 kB - -
CDN Bundle (incl. Tracing, Replay) 90.28 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 92.21 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 96.42 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 98.37 kB - -
CDN Bundle - uncompressed 93.29 kB - -
CDN Bundle (incl. Tracing) - uncompressed 156.14 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 99.84 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 162.11 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 229.88 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 276.31 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 282.26 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 290.01 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 295.96 kB - -
@sentry/nextjs (client) 57.31 kB - -
@sentry/sveltekit (client) 52.48 kB - -
@sentry/core/server 43.52 kB +4.13% +1.73 kB 🔺
@sentry/core/browser 13.81 kB - -
@sentry/node 153.18 kB +0.37% +553 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 81.58 kB - -
@sentry/node - without tracing 94.8 kB +0.58% +546 B 🔺
@sentry/node - without channel injection 133.85 kB +0.42% +547 B 🔺
@sentry/aws-serverless 102.98 kB +0.53% +542 B 🔺
@sentry/cloudflare (withSentry) - minified 212.91 kB +0.86% +1.8 kB 🔺
@sentry/cloudflare (withSentry) 528.31 kB +0.86% +4.47 kB 🔺
@sentry/nextjs/cloudflare (withSentry) - minified 232.53 kB +0.78% +1.8 kB 🔺

View base workflow run

@logaretm
logaretm force-pushed the awad/tunnel-core-dsn-path branch from 3bba67e to 4eea4c9 Compare October 15, 2026 17:36
@logaretm

Copy link
Copy Markdown
Member Author

bugbot run

@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.

Stale Bugbot comment from a previous run.

Comment thread packages/core/src/utils/tunnel.ts Outdated
Comment thread packages/core/test/lib/utils/tunnel.test.ts
@logaretm

Copy link
Copy Markdown
Member Author

bugbot run

1 similar comment
@logaretm

Copy link
Copy Markdown
Member Author

bugbot run

@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.

Stale Bugbot comment from a previous run.

@linear-code

linear-code Bot commented Oct 16, 2026

Copy link
Copy Markdown

JS-3942

Comment thread packages/core/src/utils/tunnel.ts Fixed
@logaretm
logaretm force-pushed the awad/tunnel-core-dsn-path branch 6 times, most recently from 8e34dd6 to f97d8a2 Compare October 19, 2026 19:12
@logaretm
logaretm marked this pull request as ready for review October 19, 2026 19:36
@logaretm
logaretm requested a review from a team as a code owner October 19, 2026 19:36
@logaretm
logaretm requested review from chargome and removed request for a team October 19, 2026 19:36
… init or framework defaults

Framework SDKs report tunnel options and the base path once with setManagedTunnelDefaults, and ServerRuntimeClient applies them when it's created, so per-request clients get the span filters before their first span starts. The span filters move out of the base Client into the server client, and the server-only helpers move to @sentry/core/server.
The core tunnel handler now streams the envelope to Sentry instead of buffering it, so the forwarded body is a stream.
The paths only depend on the client's tunnel options and DSN, so they're derived when the managed tunnel is settled
and read per request. This drops the path cache, which the browser never needed, and the options forms of
`isTunnelRequest` and `getTunnelPaths`.
The matcher forwarded through the public handler, which resolved the allowed DSNs again on every request. The
client now stores them next to the tunnel paths, and the own DSN is passed explicitly, so a client without a DSN
never picks up another client's. The header line is parsed as JSON directly, since it's all the handler reads.
… streamed body

Next.js's edge runtime in dev rejects a Content-Length header on a streamed fetch body, so every forward failed
with a 500. Relay takes the chunked upload.
They're derived per client, so core keeps them in its own map instead of on the public options type.
#25515's 411 and 413 checks run before the header line is read, so an oversized or unlengthed request is
rejected without reading its body.
@logaretm
logaretm force-pushed the awad/tunnel-core-dsn-path branch from feae0ed to 98ff881 Compare October 22, 2026 15:07

@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 98ff881. Configure here.

Comment thread packages/core/src/integrations/fetch.ts

@Lms24 Lms24 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.

Given @JPeer264 thorough review, I mainly focused on the forwarding logic itself and had some suggestions for the max size check and forwarding more headers.

I'm +1 for making the browser- and server-specific options browser/server client specific to avoid confusion.

Comment thread packages/core/src/utils/tunnel.ts Outdated
/**
* Bounds what one tunnel request may send. SDK envelopes stay far below it.
*/
export const MAX_TUNNEL_BODY_SIZE = 20 * 1024 * 1024;

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.

m: I know this already existed prior to the PR but given we now stream the request and we don't need to read the entire body, wdyt about removing this limit and just passing back a 413 response from Relay if the envelope was too large?

The 64 KB header limit should make it safe to raise the body limit.

Relay's limit currently is 200MB, so alternatively we can also increase it to that.

@logaretm logaretm Oct 22, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep, this wasn't part of the stack but was included to match develop. Agreed on dropping it, I prefer Sentry controls that since we stream the body, so the user's server doesn't pay for buffering it anymore.

Pushed it in b29bdaf

});
}

function forwardHeaders(request: Request): Record<string, string> {

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.

m: we probably also should forward the user agent of the client so that relay can the parse browser and version from it, wdyt?

more specifically, we should forward these headers, including client hints: https://github.com/getsentry/relay/blob/7a1a88fe1c85d1d4d4fac453a7a078bb02a5849d/relay-event-normalization/src/normalize/user_agent.rs#L117-L122

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh absolutely, really good catch!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Pushed it in 6023ac8

});
}

function forwardHeaders(request: Request): Record<string, string> {

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.

Related, we probably should also forward the Origin and Referer headers, as Relay otherwise skips the allowed domains check: https://github.com/getsentry/relay/blob/7a1a88fe1c85d1d4d4fac453a7a078bb02a5849d/relay-server/src/services/projects/project/info.rs#L215-L234

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, I forgot about these and the other other ones below. Thanks!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Pushed it in 6023ac8

@logaretm
logaretm requested review from JPeer264 and Lms24 October 22, 2026 15:48

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants