Serve the JavaScript SDK managed tunnel (`tunnel: true`) from ASP.NET Core

Open
#5,992 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
15/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp, javascript
Domain
api, backend

Research direction

Start with SentryTunnelMiddleware and its tests, then read SentryStartupFilter and SentryAspNetCoreOptions/BindableSentryAspNetCoreOptions to see where an opt-in tunnel option and pre-routing IStartupFilter fit. Port the JS hash.ts FNV-1a and dsn.ts normalization from the linked files, checking the three DSN/path vectors, and model forwarding on tunnelForward.ts. Done means hashed paths are answered before routing, envelopes forwarded with the listed headers and Sentry status/rate-limit headers, and tests cover the vectors, DSN allow-list and streaming limit.

Written by the indexing model from the issue text.

Description

.NET Feature Framework: ASP.NET Core State: Needs Discussion

Problem Statement

Ad blockers drop browser requests to Sentry ingest, and block lists also pick up well-known tunnel paths. Our UseSentryTunneling() defaults to /tunnel, a static path that a filter list can target.

sentry-javascript 11.10.0 added a managed tunnel (getsentry/sentry-javascript#25199). The browser sets tunnel: true and posts to a same-origin path derived from its DSN. On the server, serveTunnelIntegration() answers that path and forwards the envelope to Sentry. Since 11.12.0 the server answers before the request reaches the app (getsentry/sentry-javascript#25997). Because the path is a hash of the DSN, browser and server agree on it without sharing build state, and it stays the same across deploys.

ASP.NET Core apps often serve a JavaScript frontend (an SPA, or Razor/MVC pages using the browser SDK), so the .NET server should be able to answer these paths.

How the path is computed

"/" + base36(FNV-1a-32(normalizedDsn)), where normalizedDsn is protocol://publicKey@host[:port]/[path/]projectId with the secret key dropped. The hash runs over UTF-16 code units (charCodeAt), so a C# loop over the string's chars matches.

  • hash.ts: the hash. The JS SDK treats it as fixed, since browser and server of different versions must agree.
  • tunnel.ts: getTunnelPath, resolveTunnelPaths, resolveAllowedDsns.
  • dsn.ts: dsnFromString and dsnToString, which define the normalization.

Test vectors, all reproduced by a C# port:

DSN Path
https://[email protected]/1337 /fdv680
https://public:[email protected]/1337 /fdv680
https://[email protected]/1337 /16bejtj

Parse the DSN with the same regex as dsnFromString, not with Dsn.Parse or System.Uri. Uri lowercases the host and drops a default port, and either change gives a different path: https://[email protected]:443/1337 is /1fbdot5 in JS.

Solution Brainstorm

An opt-in, additive option on SentryAspNetCoreOptions. The shape below is only a sketch:

builder.WebHost.UseSentry(o =>
{
    o.Dsn = "<server DSN>";
    o.Tunnel.Enabled = true;
    o.Tunnel.AllowedDsns.Add("<browser DSN>");
});
  1. Paths. One per allowed DSN (the server's own DSN plus AllowedDsns), POST only. Most .NET backends report to a different Sentry project than their frontend, so AllowedDsns is the usual case here rather than the exception. Optionally a fixed Path and a BasePath, like the JS ServerTunnelOptions. ASP.NET Core already strips an IIS sub-app's PathBase, so BasePath only matters for a UsePathBase inside the app's own pipeline.
  2. Answer before the app. Register an IStartupFilter next to SentryStartupFilter, so the tunnel runs before routing, authentication, CORS and antiforgery. This is the ASP.NET Core equivalent of the JS runtime-level interception.
  3. Forwarder, following tunnelForward.ts:
    • Read only the envelope header line, up to 64 KB. Answer 400 if it is missing or not JSON.
    • Normalize the header's dsn and require an exact match with an allowed DSN. Answer 403 otherwise.
    • Stream the rest of the body to that DSN's envelope endpoint, keeping its scheme, port and path prefix, with a 30-second timeout.
    • Pass on user-agent, sec-ch-ua*, origin, referer and the client IP headers (cf-connecting-ip, x-vercel-forwarded-for, x-forwarded-for), and set X-Sentry-Forwarded-For. Without Origin or Referer, Relay skips the project's allowed-domains check.
    • Return Sentry's status with content-type, retry-after and x-sentry-rate-limits, so the browser SDK still backs off.
    • Use a plain HttpClient. SentryHttpMessageHandlerBuilderFilter adds SentryHttpMessageHandler to every IHttpClientFactory client, which would add a breadcrumb, a span and trace headers to every forward.
  4. Tracing. Tunnel requests shouldn't produce transactions. Running before SentryMiddleware covers the SDK's own tracing. The hosting layer starts the request Activity before any middleware, though, so Sentry.OpenTelemetry needs to drop those server spans. JS uses ignoreSpans for this.
  5. Configuration and AOT. Matching entries in BindableSentryAspNetCoreOptions, so the tunnel can be configured from appsettings.json.
  6. Docs. The ASP.NET Core guide, including the browser side (tunnel: true, sentry-javascript 11.10 or later).
The existing middleware

SentryTunnelMiddleware can keep its API and move onto the new forwarder, which fixes these without a breaking change:

  • The ingest URL is hardcoded to https://{host}/api/{id}/envelope/, so a self-hosted Sentry on http, on a non-default port or under a path prefix doesn't work.
  • It replaces the browser's User-Agent and doesn't pass on Origin or Referer.
  • It doesn't return the rate-limit headers.
  • A DSN that isn't allowed gets an empty 200.
  • It reads the whole body into memory with no size limit.

Its allow rule accepts any *.sentry.io host plus the listed hosts, so anyone can use the server as a relay into any Sentry SaaS project. Requiring an exact DSN match is a breaking change, so it belongs in the next major. There, UseSentryTunneling(path) could become a fixed Path on the managed tunnel, which also gives the one-step setup asked for in #4377.

Migration path for users
  1. Deploy the server with the managed tunnel on and the browser DSN in AllowedDsns, keeping UseSentryTunneling(). The two answer different paths.
  2. Switch the browser to tunnel: true. Doing the server first means the browser never posts to a path nothing answers, and tabs still running an old bundle keep posting to /tunnel.
  3. Remove AddSentryTunneling and UseSentryTunneling once old bundles are gone.

When rotating a DSN, keep both DSNs in AllowedDsns until builds using the old one are gone.

Dominant language
C#
Stars
775
Forks
262
Avg merge
1d 16h
Merged PRs (30d)
118

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from getsentry/sentry-dotnet

All issues in getsentry/sentry-dotnet

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.