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
Draft
+282
−11
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a7cdf53
feat: Accept `tunnel: true` in `init` to enable the managed tunnel
logaretm a92cb4c
feat: Explain a managed tunnel that nothing on the server answers
logaretm 6a89f2b
fix(core): Keep the managed tunnel warning out of production bundles
logaretm f103bee
fix(aws-serverless): Keep the managed tunnel when the Lambda extensio…
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import type { TunnelOptions } from '@sentry/core'; | ||
| import { debug, getTunnelPath } from '@sentry/core'; | ||
| import { DEBUG_BUILD } from '../debug-build'; | ||
| import type { BrowserOptions } from '../client'; | ||
|
|
||
| /** | ||
| * Resolves the `tunnel` option `init` received. `true` or {@link TunnelOptions} point the tunnel at the same-origin | ||
| * path the server SDK derives from the same DSN. | ||
| */ | ||
| export function resolveBrowserTunnelOption( | ||
| options: BrowserOptions, | ||
| ): Pick<BrowserOptions, 'transportOptions' | '_managedTunnel'> & { tunnel: string | undefined } { | ||
| const { tunnel, dsn, transportOptions } = options; | ||
| if (typeof tunnel === 'string' || !tunnel) { | ||
| return { tunnel, transportOptions }; | ||
| } | ||
|
|
||
| const tunnelOptions: TunnelOptions = tunnel === true ? {} : tunnel; | ||
| const tunnelPath = tunnelOptions.path ?? (dsn && getTunnelPath(dsn)); | ||
| if (!tunnelPath) { | ||
| return { tunnel: undefined, transportOptions }; | ||
| } | ||
|
|
||
| const basePath = tunnelOptions.basePath?.replace(/\/$/, '') ?? ''; | ||
| const resolvedTunnel = `${basePath}${tunnelPath}${tunnelOptions.trailingSlash ? '/' : ''}`; | ||
|
|
||
| DEBUG_BUILD && debug.log(`Tunneling events to "${resolvedTunnel}"`); | ||
|
|
||
| return { | ||
| tunnel: resolvedTunnel, | ||
| // Lets the client explain a tunnel request that nothing on the server answers. | ||
| _managedTunnel: tunnelOptions, | ||
| // The fetch transport sends `text/plain` by default to avoid a CORS preflight. A same-origin request has no | ||
| // preflight, and frameworks with CSRF protection (SvelteKit, Astro) reject a `text/plain` POST as a form submission. | ||
| transportOptions: { | ||
| ...transportOptions, | ||
| headers: { 'Content-Type': 'application/x-sentry-envelope', ...transportOptions?.headers }, | ||
| }, | ||
| }; | ||
| } | ||
|
logaretm 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,100 @@ | ||
| import type { EventEnvelope, EventItem } from '@sentry/core'; | ||
| import { createEnvelope, getTunnelPath, isTunnelRequest } 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://public@o123.ingest.sentry.io/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)); | ||
| expect(isTunnelRequest({ method: 'POST', url: tunnel! }, { allowedDsns: [DSN] })).toBe(true); | ||
| }); | ||
|
|
||
| it('marks the tunnel as managed so the client can explain a server that does not answer it', () => { | ||
| expect(resolveBrowserTunnelOption({ dsn: DSN, tunnel: { basePath: '/docs' } })._managedTunnel).toEqual({ | ||
| basePath: '/docs', | ||
| }); | ||
| expect(resolveBrowserTunnelOption({ dsn: DSN, tunnel: '/my-tunnel' })._managedTunnel).toBeUndefined(); | ||
| }); | ||
|
|
||
| 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)}/`); | ||
| expect(isTunnelRequest({ method: 'POST', url: tunnel! }, { allowedDsns: [DSN], basePath: '/docs' })).toBe(true); | ||
| }); | ||
|
|
||
| it('uses a fixed path instead of the DSN-derived one', () => { | ||
| const { tunnel, transportOptions } = resolveBrowserTunnelOption({ dsn: DSN, tunnel: { path: '/monitor' } }); | ||
|
|
||
| expect(tunnel).toBe('/monitor'); | ||
| expect(transportOptions?.headers).toEqual({ 'Content-Type': 'application/x-sentry-envelope' }); | ||
| }); | ||
|
|
||
| it('keeps a URL tunnel and its transport options untouched', () => { | ||
| const transportOptions = { headers: { 'x-custom': '1' } }; | ||
|
|
||
| expect(resolveBrowserTunnelOption({ dsn: DSN, tunnel: '/my-tunnel', transportOptions })).toEqual({ | ||
| tunnel: '/my-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(); | ||
| }); | ||
|
|
||
| it('sends envelopes as application/x-sentry-envelope so CSRF checks do not treat them as form posts', async () => { | ||
| const { tunnel, transportOptions } = resolveBrowserTunnelOption({ | ||
| dsn: DSN, | ||
| tunnel: true, | ||
| transportOptions: { headers: { 'x-custom': '1' } }, | ||
| }); | ||
| const nativeFetch = vi.fn().mockResolvedValue(new Response(null, { status: 200 })); | ||
|
|
||
| const transport = makeFetchTransport( | ||
| { ...transportOptions, url: tunnel!, recordDroppedEvent: () => undefined }, | ||
| nativeFetch, | ||
| ); | ||
| await transport.send( | ||
| createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [ | ||
| [{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem, | ||
| ]), | ||
| ); | ||
|
|
||
| expect(nativeFetch).toHaveBeenCalledWith( | ||
| getTunnelPath(DSN), | ||
| expect.objectContaining({ | ||
| headers: { 'Content-Type': 'application/x-sentry-envelope', 'x-custom': '1' }, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('lets a user-provided Content-Type header win', () => { | ||
| const { transportOptions } = resolveBrowserTunnelOption({ | ||
| dsn: DSN, | ||
| tunnel: true, | ||
| transportOptions: { headers: { 'Content-Type': 'text/plain' } }, | ||
| }); | ||
|
|
||
| expect(transportOptions?.headers).toEqual({ 'Content-Type': 'text/plain' }); | ||
| }); | ||
| }); | ||
|
|
||
| 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)); | ||
| }); | ||
| }); | ||
|
logaretm 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
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
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.