- Notifications
You must be signed in to change notification settings - Fork 0
feat(observability): gated client-side Sentry via same-origin tunnel#642
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // Same-origin Sentry tunnel. The browser SDK is configured with | ||
| // `tunnel: "/api/monitoring"` (src/instrumentation-client.ts) so event envelopes | ||
| // are POSTed here instead of directly to Sentry's ingest host. This keeps the | ||
| // strict clinical CSP intact — `connect-src 'self'` already allows this route, so | ||
| // no Sentry ingest host has to be added to the policy — and lets ad-blockers that | ||
| // block *.sentry.io not silently drop client error reports. | ||
| // | ||
| // The route forwards the raw envelope to Sentry server-side (server fetches are not | ||
| // bound by CSP). Hardening: it (1) validates the envelope's embedded DSN against the | ||
| // project's own configured DSN so the endpoint cannot relay to arbitrary projects, | ||
| // (2) caps the buffered body at 1 MB (Content-Length preflight + streamed byte | ||
| // count) since this is an unauthenticated public POST under a large proxy body | ||
| // allowance, and (3) bounds the upstream relay with a timeout. Inert (404) until | ||
| // SENTRY_DSN / NEXT_PUBLIC_SENTRY_DSN is set. | ||
| export const runtime = "nodejs"; | ||
| const MAX_ENVELOPE_BYTES = 1_000_000; // 1 MB — Sentry event envelopes are far smaller. | ||
| const UPSTREAM_TIMEOUT_MS = 5_000; | ||
| // Lightweight per-IP throttle. This is an unauthenticated public POST, so a | ||
| // spammer with a valid envelope could otherwise burn the project's Sentry ingest | ||
| // quota. A per-instance in-memory window is intentionally simple (unlike the | ||
| // durable answer/upload limiters this is best-effort telemetry relay, not a paid | ||
| // or state-changing path); Sentry's own ingest rate limits are the backstop. | ||
| const RATE_LIMIT_MAX = 120; | ||
| const RATE_LIMIT_WINDOW_MS = 60_000; | ||
| type RateWindow = { count: number; resetAt: number }; | ||
| const rateLimitByIp = new Map<string, RateWindow>(); | ||
| function isRateLimited(ip: string): boolean { | ||
| const now = Date.now(); | ||
| if (rateLimitByIp.size > 5_000) { | ||
| for (const [key, window] of rateLimitByIp) if (now >= window.resetAt) rateLimitByIp.delete(key); | ||
| } | ||
| const existing = rateLimitByIp.get(ip); | ||
| if (!existing || now >= existing.resetAt) { | ||
| rateLimitByIp.set(ip, { count: 1, resetAt: now + RATE_LIMIT_WINDOW_MS }); | ||
| return false; | ||
| } | ||
| existing.count += 1; | ||
| return existing.count > RATE_LIMIT_MAX; | ||
| } | ||
| type ParsedDsn = { host: string; projectId: string }; | ||
| function parseDsn(dsn: string): ParsedDsn { | ||
| const url = new URL(dsn); | ||
| const projectId = url.pathname.replace(/^\//, ""); | ||
| if (!url.host || !projectId) throw new Error("incomplete dsn"); | ||
| return { host: url.host, projectId }; | ||
| } | ||
| // Reads the request body as text but aborts once it exceeds `maxBytes`, returning | ||
| // null. Streams so an oversized body is never fully buffered. | ||
| async function readCappedText(request: Request, maxBytes: number): Promise<string | null> { | ||
| const reader = request.body?.getReader(); | ||
| if (!reader) { | ||
| const text = await request.text(); | ||
| return text.length > maxBytes ? null : text; | ||
| } | ||
| const decoder = new TextDecoder(); | ||
| let total = 0; | ||
| let out = ""; | ||
| for (;;) { | ||
| const { done, value } = await reader.read(); | ||
| if (done) break; | ||
| total += value.byteLength; | ||
| if (total > maxBytes) { | ||
| await reader.cancel(); | ||
| return null; | ||
| } | ||
| out += decoder.decode(value, { stream: true }); | ||
| } | ||
| out += decoder.decode(); | ||
| return out; | ||
| } | ||
| export async function POST(request: Request): Promise<Response> { | ||
| const configuredDsn = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN; | ||
| if (!configuredDsn) return new Response(null, { status: 404 }); | ||
| const ip = request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || "unknown"; | ||
| if (isRateLimited(ip)) { | ||
| return new Response("too many requests", { status: 429, headers: { "Retry-After": "60" } }); | ||
| } | ||
| let expected: ParsedDsn; | ||
| try { | ||
| expected = parseDsn(configuredDsn); | ||
| } catch { | ||
| // Misconfigured server DSN — do not leak detail, just refuse. | ||
| return new Response(null, { status: 404 }); | ||
| } | ||
| // Reject oversized payloads before buffering when the length is declared. | ||
| const declaredLength = Number(request.headers.get("content-length")); | ||
| if (Number.isFinite(declaredLength) && declaredLength > MAX_ENVELOPE_BYTES) { | ||
| return new Response("payload too large", { status: 413 }); | ||
| } | ||
| const envelope = await readCappedText(request, MAX_ENVELOPE_BYTES); | ||
| if (envelope === null) return new Response("payload too large", { status: 413 }); | ||
| const firstNewline = envelope.indexOf("\n"); | ||
| if (firstNewline === -1) return new Response("invalid envelope", { status: 400 }); | ||
| let header: { dsn?: unknown }; | ||
| try { | ||
| header = JSON.parse(envelope.slice(0, firstNewline)); | ||
| } catch { | ||
| return new Response("invalid envelope header", { status: 400 }); | ||
| } | ||
| if (typeof header.dsn !== "string") return new Response("missing dsn", { status: 400 }); | ||
| let incoming: ParsedDsn; | ||
| try { | ||
| incoming = parseDsn(header.dsn); | ||
| } catch { | ||
| return new Response("invalid dsn", { status: 400 }); | ||
| } | ||
| // Reject anything not addressed to this project's own DSN (no open relay). | ||
| if (incoming.host !== expected.host || incoming.projectId !== expected.projectId) { | ||
| return new Response("forbidden", { status: 403 }); | ||
| } | ||
| let upstream: Response; | ||
| try { | ||
| upstream = await fetch(`https://${expected.host}/api/${expected.projectId}/envelope/`, { | ||
| method: "POST", | ||
| body: envelope, | ||
| headers: { "Content-Type": "application/x-sentry-envelope" }, | ||
| signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS), | ||
| }); | ||
| } catch { | ||
| // Upstream slow/unreachable — fail fast without tying up the request thread. | ||
| return new Response("bad gateway", { status: 502 }); | ||
| } | ||
| const body = await upstream.text(); | ||
| return new Response(body, { status: upstream.status }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -10,5 +10,43 @@ | ||
| // (validation stays correct, just interpreted rather than compiled). The server | ||
| // has no CSP, so it keeps the faster JIT path — this is client-only by design. | ||
| import { config } from "zod"; | ||
| import { registerSentryClient } from "@/lib/observability/sentry-client"; | ||
| config({ jitless: true }); | ||
| // Gated browser error tracking, the client counterpart to the server-side | ||
| // @sentry/node capture in src/instrumentation.ts. `__SENTRY_ENABLED__` is a | ||
| // build-time literal boolean injected by DefinePlugin (next.config.ts) — true only | ||
| // when a public DSN is set at build. As a real compile-time constant it lets the | ||
| // webpack production build dead-code-eliminate this whole block (and the entire | ||
| // @sentry/browser chunk) when false, so an unconfigured build ships ZERO Sentry | ||
| // bytes. The `typeof` guard keeps it safe under the Turbopack dev server, which | ||
| // does not run the webpack DefinePlugin and so leaves the identifier undefined. | ||
| // Events go through the same-origin tunnel (/api/monitoring) so the strict | ||
| // clinical CSP (connect-src 'self' …) needs no Sentry ingest host. | ||
| declare const __SENTRY_ENABLED__: boolean; | ||
| if (typeof __SENTRY_ENABLED__ !== "undefined" && __SENTRY_ENABLED__) { | ||
| void (async () => { | ||
| const [Sentry, { scrubClientSentryEvent }] = await Promise.all([ | ||
| import("@sentry/browser"), | ||
| import("@/lib/observability/sentry-scrub"), | ||
| ]); | ||
| Sentry.init({ | ||
| dsn: process.env.NEXT_PUBLIC_SENTRY_DSN, | ||
| tunnel: "/api/monitoring", | ||
| environment: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT || process.env.NODE_ENV, | ||
| // Errors only: no performance tracing (avoids shipping navigation-timing/PII). | ||
| tracesSampleRate: 0, | ||
| sendDefaultPii: false, | ||
| // Same privacy boundary as the server init: strip request + breadcrumbs, and | ||
| // never record breadcrumbs in the first place. | ||
| beforeSend: scrubClientSentryEvent, | ||
| beforeBreadcrumb: () => null, | ||
| }); | ||
| registerSentryClient(Sentry); | ||
| })().catch(() => { | ||
| // Loading/initializing the SDK is best-effort observability; a failed dynamic | ||
| // import (e.g. blocked chunk) must never surface as an unhandled rejection. | ||
| }); | ||
| } | ||
BigSimmo marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Client-side Sentry access indirection. Error boundaries call | ||
| // captureClientException() without ever statically importing @sentry/browser, so | ||
| // a build WITHOUT NEXT_PUBLIC_SENTRY_DSN ships zero Sentry code: instrumentation- | ||
| // client.ts only loads the SDK (and registers it here) when a DSN is present at | ||
| // build time. Until then every call here is a no-op. | ||
| type SentryClientModule = { | ||
| captureException: (error: unknown) => string; | ||
| }; | ||
| let client: SentryClientModule | null = null; | ||
| export function registerSentryClient(mod: SentryClientModule): void { | ||
| client = mod; | ||
| } | ||
| export function captureClientException(error: unknown): void { | ||
| client?.captureException(error); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.