-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Framework-agnostic Tunnel Handler #18892
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
nikolovlazar
wants to merge
2
commits into
develop
Choose a base branch
from
nikolovlazar/agnostic-tunnel-handler
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−0
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import type { DsnComponents } from '../types-hoist/dsn'; | ||
| import { debug } from './debug-logger'; | ||
| import { makeDsn } from './dsn'; | ||
| import { parseEnvelope } from './envelope'; | ||
|
|
||
| export interface TunnelResult { | ||
| status: number; | ||
| body: string; | ||
| contentType: string; | ||
| } | ||
|
|
||
| /** | ||
| * Core Sentry tunnel handler - framework agnostic. | ||
| * | ||
| * Validates the envelope DSN against allowed DSNs and forwards to Sentry. | ||
| * | ||
| * @param body - Raw request body (Sentry envelope) | ||
| * @param allowedDsnComponents - Pre-parsed array of allowed DsnComponents | ||
| * @returns Promise resolving to status, body, and contentType | ||
| */ | ||
| export async function handleTunnelRequest( | ||
| body: string | Uint8Array, | ||
| allowedDsnComponents: Array<DsnComponents>, | ||
| ): Promise<TunnelResult> { | ||
| if (allowedDsnComponents.length === 0) { | ||
| return { | ||
| status: 500, | ||
| body: 'Tunnel not configured', | ||
| contentType: 'text/plain', | ||
| }; | ||
| } | ||
|
|
||
| const [envelopeHeader] = parseEnvelope(body); | ||
| if (!envelopeHeader) { | ||
| return { | ||
| status: 400, | ||
| body: 'Invalid envelope: missing header', | ||
| contentType: 'text/plain', | ||
| }; | ||
| } | ||
|
|
||
| const dsn = envelopeHeader.dsn; | ||
| if (!dsn) { | ||
| return { | ||
| status: 400, | ||
| body: 'Invalid envelope: missing DSN', | ||
| contentType: 'text/plain', | ||
| }; | ||
| } | ||
|
|
||
| const dsnComponents = makeDsn(dsn); | ||
| if (!dsnComponents) { | ||
| return { | ||
| status: 400, | ||
| body: 'Invalid DSN format', | ||
| contentType: 'text/plain', | ||
| }; | ||
| } | ||
|
|
||
| // SECURITY: Validate that the envelope DSN matches one of the allowed DSNs | ||
| // This prevents SSRF attacks where attackers send crafted envelopes | ||
| // with malicious DSNs pointing to arbitrary hosts | ||
| const isAllowed = allowedDsnComponents.some( | ||
| allowed => allowed.host === dsnComponents.host && allowed.projectId === dsnComponents.projectId, | ||
| ); | ||
|
|
||
| if (!isAllowed) { | ||
| debug.warn( | ||
| `Sentry tunnel: rejected request with unauthorized DSN (host: ${dsnComponents.host}, project: ${dsnComponents.projectId})`, | ||
| ); | ||
| return { | ||
| status: 403, | ||
| body: 'DSN not allowed', | ||
| contentType: 'text/plain', | ||
| }; | ||
| } | ||
|
|
||
| const sentryIngestUrl = `https://${dsnComponents.host}/api/${dsnComponents.projectId}/envelope/`; | ||
|
|
||
| const response = await fetch(sentryIngestUrl, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/x-sentry-envelope', | ||
| }, | ||
| body, | ||
| }); | ||
|
|
||
| return { | ||
| status: response.status, | ||
| body: await response.text(), | ||
| contentType: response.headers.get('Content-Type') || 'text/plain', | ||
| }; | ||
| } | ||
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
38 changes: 38 additions & 0 deletions
38
packages/tanstackstart-react/src/server/createTunnelHandler.ts
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,38 @@ | ||
| import { type DsnComponents, handleTunnelRequest, makeDsn } from '@sentry/core'; | ||
|
|
||
| /** | ||
| * Creates a Sentry tunnel handler for TanStack Start. | ||
| * | ||
| * @param allowedDsns - Array of DSN strings that this tunnel will accept. | ||
| * @returns TanStack Start compatible request handler | ||
| * | ||
| * @example | ||
| * const handler = createSentryTunnelHandler([process.env.SENTRY_DSN]) | ||
| * export const Route = createFileRoute('/tunnel')({ | ||
| * server: { handlers: { POST: handler } } | ||
| * }) | ||
| */ | ||
| export function createTunnelHandler( | ||
| allowedDsns: Array<string>, | ||
| ): (args: { request: Request }) => Promise<Response> { | ||
| const allowedDsnComponents = allowedDsns.map(makeDsn).filter((c): c is DsnComponents => c !== undefined); | ||
|
|
||
| if (allowedDsnComponents.length === 0) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn('Sentry tunnel: No valid DSNs provided. All requests will be rejected.'); | ||
| } | ||
|
|
||
| return async ({ request }: { request: Request }): Promise<Response> => { | ||
| try { | ||
| const body = await request.text(); | ||
| const result = await handleTunnelRequest(body, allowedDsnComponents); | ||
|
|
||
| return new Response(result.body, { | ||
| status: result.status, | ||
| headers: { 'Content-Type': result.contentType }, | ||
| }); | ||
| } catch (error) { | ||
| return new Response('Internal server error', { status: 500 }); | ||
| } | ||
| }; | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure it's enough to just check the host matches. We should have a list of allowed DSNs and only forward when they match.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the
allowedDsnComponentsare passed from the outside and they're exactly that - a list of allowed DSNs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe instead of turning them in components we should pass them as string arrays and do a plain string comparison?