-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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>, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd also refactor this a bit to an |
||
| ): 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, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| ); | ||
|
|
||
| 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', | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,13 @@ export { init } from './sdk'; | |
| export function wrapMiddlewaresWithSentry<T extends TanStackMiddlewareBase>(middlewares: Record<string, T>): T[] { | ||
| return Object.values(middlewares); | ||
| } | ||
|
|
||
| /** | ||
| * No-op stub for client-side builds. | ||
| * The actual implementation is server-only, but this stub is needed to prevent build errors. | ||
| */ | ||
| export function createTunnelHandler( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. l: is this really the case? Do we have to create stubs from all of our server exports in TSS? (cc @nicohrubec) |
||
| _allowedDsns: Array<string>, | ||
| ): (args: { request: Request }) => Promise<Response> { | ||
| return async () => new Response('Tunnel handler is not available on the client', { status: 500 }); | ||
| } | ||
| 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 }); | ||
| } | ||
| }; | ||
| } |
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.
l: I think we should always assume bytes here, to avoid callers accidentally already strinfifying compressed/binary payloads