From 29e9442c2da138a2d8e541d98f08187cbf605a5b Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Tue, 27 Aug 2024 19:42:40 +0100 Subject: [PATCH] Sentry tunneling is broken in their example --- packages/website/src/routes/sntry/_server.ts | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 packages/website/src/routes/sntry/_server.ts diff --git a/packages/website/src/routes/sntry/_server.ts b/packages/website/src/routes/sntry/_server.ts new file mode 100644 index 00000000..24be2a30 --- /dev/null +++ b/packages/website/src/routes/sntry/_server.ts @@ -0,0 +1,36 @@ + +import { SENTRY_HOST, SENTRY_TUNNEL_ALLOWED_IDS } from '$lib/config'; +import { json, type RequestHandler } from '@sveltejs/kit'; + +export const POST: RequestHandler = async ({ request }) => { + try { + const envelopeBytes = await request.arrayBuffer(); + const envelope = new TextDecoder().decode(envelopeBytes); + const piece = envelope.split("\n")[0]; + const header = JSON.parse(piece); + // Sometime the DSN header is not set + + const dsn = new URL(header["dsn"]); + const project_id = dsn.pathname?.replace("/", ""); + + if (dsn.hostname !== SENTRY_HOST) { + throw new Error(`Invalid sentry hostname: ${dsn.hostname}`); + } + + if (!project_id || !SENTRY_TUNNEL_ALLOWED_IDS.includes(project_id)) { + throw new Error(`Invalid sentry project id: ${project_id}`); + } + + const upstream_sentry_url = `https://${SENTRY_HOST}/api/${project_id}/envelope/`; + + await fetch(upstream_sentry_url, { + method: "POST", + body: envelopeBytes, + }); + + return json({}, { status: 200 }); + } catch (e) { + console.error("error tunneling to sentry", e); + return json({ error: "error tunneling to sentry" }, { status: 500 }); + } +}; \ No newline at end of file