|
| 1 | +import type { DsnComponents } from '../types-hoist/dsn'; |
| 2 | +import { debug } from './debug-logger'; |
| 3 | +import { makeDsn } from './dsn'; |
| 4 | + |
| 5 | +export interface TunnelResult { |
| 6 | + status: number; |
| 7 | + body: string; |
| 8 | + contentType: string; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Core Sentry tunnel handler - framework agnostic. |
| 13 | + * |
| 14 | + * Validates the envelope DSN against allowed DSNs and forwards to Sentry. |
| 15 | + * |
| 16 | + * @param body - Raw request body (Sentry envelope) |
| 17 | + * @param allowedDsnComponents - Pre-parsed array of allowed DsnComponents |
| 18 | + * @returns Promise resolving to status, body, and contentType |
| 19 | + */ |
| 20 | +export async function handleTunnelRequest( |
| 21 | + body: string, |
| 22 | + allowedDsnComponents: Array<DsnComponents>, |
| 23 | +): Promise<TunnelResult> { |
| 24 | + if (allowedDsnComponents.length === 0) { |
| 25 | + return { |
| 26 | + status: 500, |
| 27 | + body: 'Tunnel not configured', |
| 28 | + contentType: 'text/plain', |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + // Sentry envelope format: first line is JSON header with DSN |
| 33 | + const [headerLine] = body.split('\n'); |
| 34 | + if (!headerLine) { |
| 35 | + return { |
| 36 | + status: 400, |
| 37 | + body: 'Invalid envelope: missing header', |
| 38 | + contentType: 'text/plain', |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + let envelopeHeader: { dsn?: string }; |
| 43 | + try { |
| 44 | + envelopeHeader = JSON.parse(headerLine); |
| 45 | + } catch { |
| 46 | + return { |
| 47 | + status: 400, |
| 48 | + body: 'Invalid envelope: malformed header JSON', |
| 49 | + contentType: 'text/plain', |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + const dsn = envelopeHeader.dsn; |
| 54 | + if (!dsn) { |
| 55 | + return { |
| 56 | + status: 400, |
| 57 | + body: 'Invalid envelope: missing DSN', |
| 58 | + contentType: 'text/plain', |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + const dsnComponents = makeDsn(dsn); |
| 63 | + if (!dsnComponents) { |
| 64 | + return { |
| 65 | + status: 400, |
| 66 | + body: 'Invalid DSN format', |
| 67 | + contentType: 'text/plain', |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + // SECURITY: Validate that the envelope DSN matches one of the allowed DSNs |
| 72 | + // This prevents SSRF attacks where attackers send crafted envelopes |
| 73 | + // with malicious DSNs pointing to arbitrary hosts |
| 74 | + const isAllowed = allowedDsnComponents.some( |
| 75 | + allowed => allowed.host === dsnComponents.host && allowed.projectId === dsnComponents.projectId, |
| 76 | + ); |
| 77 | + |
| 78 | + if (!isAllowed) { |
| 79 | + debug.warn( |
| 80 | + `Sentry tunnel: rejected request with unauthorized DSN (host: ${dsnComponents.host}, project: ${dsnComponents.projectId})`, |
| 81 | + ); |
| 82 | + return { |
| 83 | + status: 403, |
| 84 | + body: 'DSN not allowed', |
| 85 | + contentType: 'text/plain', |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + const sentryIngestUrl = `https://${dsnComponents.host}/api/${dsnComponents.projectId}/envelope/`; |
| 90 | + |
| 91 | + const response = await fetch(sentryIngestUrl, { |
| 92 | + method: 'POST', |
| 93 | + headers: { |
| 94 | + 'Content-Type': 'application/x-sentry-envelope', |
| 95 | + }, |
| 96 | + body, |
| 97 | + }); |
| 98 | + |
| 99 | + return { |
| 100 | + status: response.status, |
| 101 | + body: await response.text(), |
| 102 | + contentType: response.headers.get('Content-Type') || 'text/plain', |
| 103 | + }; |
| 104 | +} |
0 commit comments