Repository navigation
feat: Accept tunnel: true in init to enable the managed tunnel
#25200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+661
−366
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
05a8023
feat: Accept `tunnel: true` in `init` to enable the managed tunnel
logaretm 42ce951
feat: Explain a managed tunnel that nothing on the server answers
logaretm b9db322
fix(core): Keep the managed tunnel warning out of production bundles
logaretm dfb453b
fix(aws-serverless): Keep the managed tunnel when the Lambda extensio…
logaretm 8f6d026
fix(core): Point Next.js users to `tunnelRoute` when the managed tunn…
logaretm 8c16e68
feat(browser): Send directly to Sentry when nothing answers the manag…
logaretm 0259e8a
fix(browser): Normalize the managed tunnel's base path like the serve…
logaretm f2f47f7
fix(browser): Keep the managed tunnel when Sentry answers 404 or 405 …
logaretm 3b0d84f
fix(browser): Send directly when anything but the tunnel answers a ma…
logaretm 90e2009
ref(browser): Warn about a rejected DSN from the managed tunnel trans…
logaretm 3262f00
ref(browser): Switch the managed tunnel to direct sends in one place
logaretm f3f20a3
test(browser): Drop the server-side tunnel match from the browser tun…
logaretm a0edcba
ref(browser): Trim the managed tunnel comments
logaretm 6e0076c
fix(aws-serverless): Keep the Lambda extension for a relative tunnel …
logaretm e30055a
ref: Import the internal tunnel helpers under their `_INTERNAL_` names
logaretm 8836f15
ref: Narrow `tunnel` to the browser or server tunnel options per SDK
logaretm 583ac7e
feat(browser): Treat a relative tunnel path as a managed one and only…
logaretm 3262ead
ref(browser): Fold the managed tunnel fallback into the fetch transport
logaretm be8aae9
fix(browser): Resend to Sentry after the tunnel request leaves the ke…
logaretm 2cf4d9f
test(vercel-edge): Check that `init` resolves the managed tunnel option
logaretm 351f3f1
fix(core): Use a tunnel path the user wrote as written, without the f…
logaretm 0e16bcc
ref(browser): Drop the direct send fallback for the managed tunnel
logaretm 8b0a9e3
ref(core): Drop the `X-Sentry-Tunnel` response header
logaretm 5bddfc0
ref(browser): Leave a string `tunnel` as written
logaretm f4750d5
docs(core): Describe a relative `tunnel` string as used as written
logaretm 4215ada
feat: Serve the managed tunnel with `serveTunnelIntegration` instead …
logaretm 2ddc3f9
fix(remix): Pass the Remix options to the Node `init` as Node options
logaretm 7cd8dcb
fix(core): Find the tunnel forwarder through the client, for framewor…
logaretm 2f697bd
test(core): Await the forward in the bundled core copy test
logaretm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import type { BrowserTunnelOptions } from '@sentry/core'; | ||
| import { | ||
| debug, | ||
| getTunnelPath, | ||
| makeDsn, | ||
| _INTERNAL_normalizeTunnelBasePath as normalizeTunnelBasePath, | ||
| } from '@sentry/core'; | ||
| import { DEBUG_BUILD } from '../debug-build'; | ||
| import type { BrowserOptions } from '../client'; | ||
|
|
||
| /** | ||
| * Resolves `tunnel: true` or {@link BrowserTunnelOptions} to the managed tunnel's same-origin path. A string is left as | ||
| * written, so a tunnel the app serves itself gets the same requests as before. | ||
| */ | ||
| export function resolveBrowserTunnelOption( | ||
| options: BrowserOptions, | ||
| ): Pick<BrowserOptions, 'transportOptions'> & { tunnel: string | undefined } { | ||
| const { tunnel, dsn, transportOptions } = options; | ||
| if (!tunnel || typeof tunnel === 'string') { | ||
| return { tunnel, transportOptions }; | ||
| } | ||
|
|
||
| const tunnelOptions: BrowserTunnelOptions = tunnel === true ? {} : tunnel; | ||
| const dsnComponents = dsn && makeDsn(dsn); | ||
| const tunnelPath = tunnelOptions.path ?? (dsnComponents && getTunnelPath(dsnComponents)); | ||
| if (!tunnelPath) { | ||
| return { tunnel: undefined, transportOptions }; | ||
| } | ||
|
|
||
| const basePath = normalizeTunnelBasePath(tunnelOptions.basePath); | ||
| const resolvedTunnel = `${basePath}${tunnelPath}${tunnelOptions.trailingSlash ? '/' : ''}`; | ||
|
|
||
| DEBUG_BUILD && debug.log(`Tunneling events to "${resolvedTunnel}"`); | ||
|
|
||
| return { | ||
| tunnel: resolvedTunnel, | ||
| transportOptions: { | ||
| ...transportOptions, | ||
| // CSRF protection (SvelteKit, Astro) rejects the default `text/plain` POST to the tunnel as a form submission. | ||
| headers: { 'Content-Type': 'application/x-sentry-envelope', ...transportOptions?.headers }, | ||
| }, | ||
| }; | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { getTunnelPath } from '@sentry/core'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
| import { getClient, getCurrentScope, init } from '../../src'; | ||
| import { makeFetchTransport } from '../../src/transports/fetch'; | ||
| import { resolveBrowserTunnelOption } from '../../src/utils/managedTunnel'; | ||
|
|
||
| const DSN = 'https://[email protected]/1337'; | ||
|
|
||
| describe('resolveBrowserTunnelOption', () => { | ||
| it('resolves `tunnel: true` to the path the server derives from the same DSN', () => { | ||
| const { tunnel } = resolveBrowserTunnelOption({ dsn: DSN, tunnel: true }); | ||
|
|
||
| expect(tunnel).toBe(getTunnelPath(DSN)); | ||
| }); | ||
|
|
||
| it('sends to a managed tunnel as application/x-sentry-envelope so CSRF checks do not treat it as a form post', () => { | ||
| expect(resolveBrowserTunnelOption({ dsn: DSN, tunnel: true }).transportOptions).toEqual({ | ||
| headers: { 'Content-Type': 'application/x-sentry-envelope' }, | ||
| }); | ||
| expect( | ||
| resolveBrowserTunnelOption({ dsn: DSN, tunnel: true, transportOptions: { headers: { 'X-Custom': '1' } } }) | ||
| .transportOptions, | ||
| ).toEqual({ headers: { 'Content-Type': 'application/x-sentry-envelope', 'X-Custom': '1' } }); | ||
| }); | ||
|
|
||
| it('prefixes the base path and appends a trailing slash when asked', () => { | ||
| const { tunnel } = resolveBrowserTunnelOption({ dsn: DSN, tunnel: { basePath: '/docs/', trailingSlash: true } }); | ||
|
|
||
| expect(tunnel).toBe(`/docs${getTunnelPath(DSN)}/`); | ||
| }); | ||
|
|
||
| it('normalizes the base path like the server does, so the tunnel URL is never page-relative', () => { | ||
| expect(resolveBrowserTunnelOption({ dsn: DSN, tunnel: { basePath: 'docs' } }).tunnel).toBe( | ||
| `/docs${getTunnelPath(DSN)}`, | ||
| ); | ||
| expect(resolveBrowserTunnelOption({ dsn: DSN, tunnel: { basePath: './docs/' } }).tunnel).toBe( | ||
| `/docs${getTunnelPath(DSN)}`, | ||
| ); | ||
| expect(resolveBrowserTunnelOption({ dsn: DSN, tunnel: { basePath: './' } }).tunnel).toBe(getTunnelPath(DSN)); | ||
| }); | ||
|
|
||
| it('uses a fixed path instead of the DSN-derived one', () => { | ||
| expect(resolveBrowserTunnelOption({ dsn: DSN, tunnel: { path: '/monitor' } }).tunnel).toBe('/monitor'); | ||
| }); | ||
|
|
||
| it.each(['https://proxy.example.com/tunnel', '/monitor'])( | ||
| 'keeps the string tunnel %s and its transport options untouched', | ||
| tunnel => { | ||
| const transportOptions = { headers: { 'x-custom': '1' } }; | ||
|
|
||
| expect(resolveBrowserTunnelOption({ dsn: DSN, tunnel, transportOptions })).toEqual({ tunnel, transportOptions }); | ||
| }, | ||
| ); | ||
|
|
||
| it('turns the tunnel off without a valid DSN', () => { | ||
| expect(resolveBrowserTunnelOption({ tunnel: true }).tunnel).toBeUndefined(); | ||
| expect(resolveBrowserTunnelOption({ dsn: 'not-a-dsn', tunnel: true }).tunnel).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('init with `tunnel: true`', () => { | ||
| afterEach(() => { | ||
| getCurrentScope().setClient(undefined); | ||
| }); | ||
|
|
||
| it('gives the client a resolved tunnel URL', () => { | ||
| init({ dsn: DSN, tunnel: true, defaultIntegrations: false }); | ||
|
|
||
| expect(getClient()?.getOptions().tunnel).toBe(getTunnelPath(DSN)); | ||
| }); | ||
|
|
||
| it('only sets the envelope content type for a managed tunnel', () => { | ||
| const transport = vi.fn(makeFetchTransport); | ||
|
|
||
| init({ dsn: DSN, tunnel: true, transport }); | ||
| const managedTransportOptions = transport.mock.calls[0]?.[0]; | ||
| init({ dsn: DSN, tunnel: 'https://proxy.example.com', transport }); | ||
|
|
||
| expect(managedTransportOptions).toEqual( | ||
| expect.objectContaining({ | ||
| url: getTunnelPath(DSN), | ||
| headers: { 'Content-Type': 'application/x-sentry-envelope' }, | ||
| }), | ||
| ); | ||
| expect(transport).toHaveBeenLastCalledWith(expect.not.objectContaining({ headers: expect.anything() })); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,11 @@ import type { CloudflareOptions } from '../src/client'; | |
| import { CloudflareClient } from '../src/client'; | ||
| import { getDefaultIntegrations, init } from '../src/sdk'; | ||
| import { resetSdk } from './testUtils'; | ||
| import { mysqlIntegration as exportedMysqlIntegration, spanStreamingIntegration } from '../src/'; | ||
| import { | ||
| mysqlIntegration as exportedMysqlIntegration, | ||
| spanStreamingIntegration, | ||
| serveTunnelIntegration, | ||
| } from '../src/'; | ||
|
|
||
| describe('init', () => { | ||
| beforeEach(() => { | ||
|
|
@@ -23,6 +27,18 @@ describe('init', () => { | |
| expect(client).toBeInstanceOf(CloudflareClient); | ||
| }); | ||
|
|
||
| test('serves the managed tunnel with `serveTunnelIntegration`', () => { | ||
| const browserDsn = 'https://[email protected]/42'; | ||
| init({ | ||
| dsn: 'https://[email protected]/1337', | ||
| integrations: [serveTunnelIntegration({ allowedDsns: [browserDsn] })], | ||
| }); | ||
|
|
||
| const options = getClient()?.getOptions(); | ||
| expect(options?.tunnel).toBeUndefined(); | ||
| expect(options?._managedTunnel).toEqual({ allowedDsns: [browserDsn] }); | ||
| }); | ||
|
|
||
| test('installs SpanStreaming integration by default', () => { | ||
| init({ | ||
| dsn: 'https://[email protected]/1337', | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { defineIntegration } from '../integration'; | ||
| import type { IntegrationFn } from '../types/integration'; | ||
| import type { ServerTunnelOptions } from '../types/options'; | ||
| import { enableManagedTunnel, SERVE_TUNNEL_INTEGRATION_NAME } from '../utils/managedTunnelDefaults'; | ||
| import { forwardTunnelRequest } from '../utils/tunnelForward'; | ||
|
|
||
| const _serveTunnelIntegration = ((options: ServerTunnelOptions = {}) => { | ||
| return { | ||
| name: SERVE_TUNNEL_INTEGRATION_NAME, | ||
| forward: forwardTunnelRequest, | ||
| setup(client) { | ||
| enableManagedTunnel(client, options, forwardTunnelRequest); | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
|
|
||
| /** | ||
| * Serves the managed tunnel, so a browser `init` with `tunnel: true` sends its envelopes through the app's own origin | ||
| * instead of the Sentry ingest URL, where ad-blockers can't drop them. The meta-framework SDKs answer the tunnel | ||
| * requests from their request hooks. | ||
| * | ||
| * By default the tunnel path is derived from the DSN, under the app's base path, and only envelopes for the server's | ||
| * own DSN are forwarded. A `path` is served as written. | ||
| * | ||
| * @example | ||
| * ```js | ||
| * Sentry.init({ | ||
| * dsn: '__DSN__', | ||
| * integrations: [Sentry.serveTunnelIntegration()], | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export const serveTunnelIntegration = defineIntegration(_serveTunnelIntegration); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.