UNPKG

@sentry/core

Version:
163 lines (159 loc) • 6.62 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const currentScopes = require('../currentScopes.js'); const attributes = require('@sentry/conventions/attributes'); const debugBuild = require('../debug-build.js'); const escapeStringForRegex = require('../vendor/escapeStringForRegex.js'); const debugLogger = require('./debug-logger.js'); const dsn = require('./dsn.js'); const tunnelStore = require('./tunnelStore.js'); const url = require('./url.js'); function setServedTunnel(client, tunnelOptions, forward) { const allowedDsns = resolveAllowedDsns(tunnelOptions.allowedDsns, client.getDsn()); const servedTunnel = { paths: resolveTunnelPaths(tunnelOptions, allowedDsns), basePath: tunnelOptions.basePath, allowedDsns, forward, intercept: tunnelOptions.intercept !== false }; tunnelStore.setClientTunnel(client, { served: servedTunnel }); return servedTunnel; } function getServedTunnel(client = currentScopes.getClient()) { return client && tunnelStore.getClientTunnel(client)?.served; } function applyBrowserTunnelDefaults(tunnel, defaults) { if (!tunnel || typeof tunnel === "string" || tunnel !== true && tunnel.path) { return tunnel; } const { basePath, ...rest } = defaults ?? {}; const normalizedBasePath = normalizeTunnelBasePath(basePath); const resolvedDefaults = normalizedBasePath ? { ...rest, basePath: normalizedBasePath } : rest; if (!Object.keys(resolvedDefaults).length) { return tunnel; } return { ...resolvedDefaults, ...tunnel === true ? {} : tunnel }; } function getTunnelPath(dsn$1) { const components = dsn$1 ? dsn.makeDsn(dsn$1) : currentScopes.getClient()?.getDsn(); return components ? `/${fnv1a32(dsn.dsnToString(components)).toString(36)}` : void 0; } function getTunnelPaths() { return getServedTunnel()?.paths ?? []; } function resolveTunnelPaths(tunnelOptions, allowedDsns) { const fixedPath = tunnelOptions.path?.replace(/\/$/, ""); if (fixedPath) { return [fixedPath]; } const paths = allowedDsns.map((dsn) => getTunnelPath(dsn)); return [...new Set(paths.filter((path) => !!path))]; } function getTunnelIgnoreSpanFilters(tunnelOptions, paths) { if (!paths.length) { return []; } const basePath = escapeStringForRegex.escapeStringForRegex(normalizeTunnelBasePath(tunnelOptions.basePath)); const pathPattern = `${basePath}(?:${paths.map(escapeStringForRegex.escapeStringForRegex).join("|")})/?`; return [ // oxlint-disable-next-line sdk/no-regexp-constructor -- built from escaped config values, not end user input { attributes: { [attributes.URL_PATH]: new RegExp(`^${pathPattern}$`) } }, // oxlint-disable-next-line sdk/no-regexp-constructor -- built from escaped config values, not end user input { attributes: { "http.url": new RegExp(`^[a-z][a-z\\d+.-]*://[^/]+${pathPattern}(?:[?#].*)?$`) } } ]; } function normalizeTunnelBasePath(basePath) { const path = (basePath?.startsWith("./") ? basePath.slice(1) : basePath ?? "").replace(/\/$/, ""); return !path || path.startsWith("/") ? path : `/${path}`; } function resolveTunnelMatch(options) { const servedTunnel = !options && getServedTunnel(); if (servedTunnel) { return servedTunnel; } const allowedDsns = resolveAllowedDsns(options?.allowedDsns, currentScopes.getClient()?.getDsn()); return { paths: resolveTunnelPaths(options ?? {}, allowedDsns), basePath: options?.basePath, allowedDsns }; } function matchesTunnel(request, tunnel) { if (request.method !== "POST" || !request.url) { return false; } const path = stripBasePath(url.parseUrl(request.url).path || "/", tunnel.basePath); return path !== void 0 && tunnel.paths.includes(path.replace(/\/$/, "")); } function isTunnelRequest(request, options) { return matchesTunnel(request, resolveTunnelMatch(options)); } function isServedTunnelRequest(request, client) { try { return !!matchServedTunnel(request, client); } catch (error) { debugBuild.DEBUG_BUILD && debugLogger.debug.error("Sentry tunnel: failed to match a request", error); return false; } } function getServedTunnelMatch(request, client) { const servedTunnel = matchServedTunnel(request, client); return servedTunnel?.intercept ? servedTunnel : void 0; } function matchServedTunnel(request, client) { const servedTunnel = getServedTunnel(client); return servedTunnel?.allowedDsns.length && matchesTunnel(request, servedTunnel) ? servedTunnel : void 0; } function handleServedTunnelRequest(request, client) { let servedTunnel; try { servedTunnel = request.body && getServedTunnelMatch(request, client); if (!servedTunnel) { return void 0; } } catch (error) { debugBuild.DEBUG_BUILD && debugLogger.debug.error("Sentry tunnel: failed to match a request", error); return void 0; } const { forward, allowedDsns } = servedTunnel; return Promise.resolve().then(() => forward(request, allowedDsns)).catch((error) => { debugBuild.DEBUG_BUILD && debugLogger.debug.error("Sentry tunnel: failed to forward a request", error); return new Response(null, { status: 502 }); }); } function resolveAllowedDsns(allowedDsns, ownDsn) { const dsns = /* @__PURE__ */ new Set(); for (const components of [...(allowedDsns ?? []).map((dsn$1) => dsn.makeDsn(dsn$1)), ownDsn]) { if (components) { dsns.add(dsn.dsnToString(components)); } } return [...dsns]; } function stripBasePath(pathname, basePath) { const base = normalizeTunnelBasePath(basePath); if (!base) { return pathname; } return pathname === base || pathname.startsWith(`${base}/`) ? pathname.slice(base.length) : void 0; } function fnv1a32(input) { let hash = 2166136261; for (let i = 0; i < input.length; i++) { hash ^= input.charCodeAt(i); hash = Math.imul(hash, 16777619); } return hash >>> 0; } exports.applyBrowserTunnelDefaults = applyBrowserTunnelDefaults; exports.getServedTunnel = getServedTunnel; exports.getServedTunnelMatch = getServedTunnelMatch; exports.getTunnelIgnoreSpanFilters = getTunnelIgnoreSpanFilters; exports.getTunnelPath = getTunnelPath; exports.getTunnelPaths = getTunnelPaths; exports.handleServedTunnelRequest = handleServedTunnelRequest; exports.isServedTunnelRequest = isServedTunnelRequest; exports.isTunnelRequest = isTunnelRequest; exports.matchesTunnel = matchesTunnel; exports.normalizeTunnelBasePath = normalizeTunnelBasePath; exports.resolveAllowedDsns = resolveAllowedDsns; exports.resolveTunnelMatch = resolveTunnelMatch; exports.resolveTunnelPaths = resolveTunnelPaths; exports.setServedTunnel = setServedTunnel; //# sourceMappingURL=tunnel.js.map