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
+451
−13
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5a0ddf8
feat: Accept `tunnel: true` in `init` to enable the managed tunnel
logaretm bf1178f
feat: Explain a managed tunnel that nothing on the server answers
logaretm 9fcc375
fix(core): Keep the managed tunnel warning out of production bundles
logaretm 1237306
fix(aws-serverless): Keep the managed tunnel when the Lambda extensio…
logaretm b904659
fix(core): Point Next.js users to `tunnelRoute` when the managed tunn…
logaretm e1905f7
feat(browser): Send directly to Sentry when nothing answers the manag…
logaretm 2f84ea9
fix(browser): Normalize the managed tunnel's base path like the serve…
logaretm 87198eb
fix(browser): Keep the managed tunnel when Sentry answers 404 or 405 …
logaretm 4238303
fix(browser): Send directly when anything but the tunnel answers a ma…
logaretm 04cf6f0
ref(browser): Warn about a rejected DSN from the managed tunnel trans…
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
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,124 @@ | ||
| import type { Envelope, Transport, TransportMakeRequestResponse, TunnelOptions } from '@sentry/core'; | ||
| import { | ||
| debug, | ||
| getEnvelopeEndpointWithUrlEncodedAuth, | ||
| getTunnelPath, | ||
| makeDsn, | ||
| normalizeTunnelBasePath, | ||
| } from '@sentry/core'; | ||
| import { DEBUG_BUILD } from '../debug-build'; | ||
| import type { BrowserOptions } from '../client'; | ||
| import type { BrowserTransportOptions } from '../transports/types'; | ||
|
|
||
| /** | ||
| * 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 = normalizeTunnelBasePath(tunnelOptions.basePath); | ||
| 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.
|
||
|
|
||
| let hasWarnedAboutRejectedDsn = false; | ||
|
|
||
| /** | ||
| * Sends to Sentry directly once it's clear nothing on this origin serves the managed tunnel, e.g. in a static build, | ||
| * on an SPA host, or when the server SDK isn't set up. An envelope is only sent again when the tunnel's response | ||
| * proves it was never ingested, so nothing reaches Sentry twice. | ||
| */ | ||
| export function makeManagedTunnelTransport( | ||
| makeTransport: (options: BrowserTransportOptions) => Transport, | ||
| directHeaders: BrowserTransportOptions['headers'], | ||
| ): (options: BrowserTransportOptions) => Transport { | ||
| return options => { | ||
| const tunnelTransport = makeTransport(options); | ||
| let directTransport: Transport | undefined; | ||
|
|
||
| const switchToDirect = (envelope: Envelope, reason: string): Transport | undefined => { | ||
| const { dsn, sdk } = envelope[0]; | ||
| const components = dsn && makeDsn(dsn); | ||
| if (components && !directTransport) { | ||
| DEBUG_BUILD && debug.warn(`Sending to Sentry directly: ${reason}`); | ||
| directTransport = makeTransport({ | ||
| ...options, | ||
| url: getEnvelopeEndpointWithUrlEncodedAuth(components, undefined, sdk), | ||
| headers: directHeaders, | ||
| }); | ||
| } | ||
|
|
||
| return directTransport; | ||
| }; | ||
|
|
||
| return { | ||
| async send(envelope: Envelope): Promise<TransportMakeRequestResponse> { | ||
| if (directTransport) { | ||
| return directTransport.send(envelope); | ||
| } | ||
|
|
||
| const response = await tunnelTransport.send(envelope); | ||
| const { statusCode } = response; | ||
|
|
||
| // The tunnel handler answered, so whatever status it passes on is Sentry's. | ||
| if (response.headers?.['x-sentry-tunnel']) { | ||
| if (DEBUG_BUILD && statusCode === 403 && !hasWarnedAboutRejectedDsn) { | ||
| hasWarnedAboutRejectedDsn = true; | ||
| debug.warn( | ||
| `The managed tunnel at "${options.url}" rejected this DSN, so events sent through it are lost. Add it to the server's \`allowedDsns\`.`, | ||
| ); | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| // Something other than the tunnel handler rejected the request, e.g. nothing serves the path (404) or an auth | ||
| // middleware answered first (401). Only the handler forwards, so the envelope never reached Sentry and is safe | ||
| // to send once more. | ||
| if (statusCode !== undefined && statusCode >= 400 && statusCode < 500) { | ||
| const transport = switchToDirect(envelope, `"${options.url}" answered ${statusCode} instead of the tunnel.`); | ||
|
|
||
| return transport ? transport.send(envelope) : response; | ||
| } | ||
|
|
||
| // Something other than the tunnel handler answered, like a static host serving `index.html`. This envelope | ||
| // may have been delivered all the same, so it isn't sent again, only the ones after it go directly. | ||
| if (statusCode !== undefined && statusCode >= 200 && statusCode < 300) { | ||
| switchToDirect(envelope, `the response from "${options.url}" didn't come from a Sentry tunnel handler.`); | ||
| } | ||
|
|
||
| return response; | ||
| }, | ||
| async flush(timeout?: number): Promise<boolean> { | ||
| const results = await Promise.all([tunnelTransport.flush(timeout), directTransport?.flush(timeout) ?? true]); | ||
|
|
||
| return results.every(Boolean); | ||
| }, | ||
| }; | ||
| }; | ||
| } | ||
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.