UNPKG

@sentry/core

Version:
169 lines (168 loc) • 8.88 kB
import { getIsolationScope } from "../currentScopes.js"; import { defineIntegration } from "../integration.js"; import { cookiePairsToRecord, parseCookieHeader } from "../utils/cookie.js"; import { SENSITIVE_COOKIE_NAME_SNIPPETS } from "../utils/data-collection/filtering-snippets.js"; import { filterKeyValueData } from "../utils/data-collection/filterKeyValueData.js"; import { isLocalhostRequest } from "../utils/localhost.js"; import { filterQueryParams } from "../utils/data-collection/filterQueryParams.js"; import { filterUrlQuery } from "../utils/data-collection/filterUrlQuery.js"; import { filterCookiePairs, httpHeadersToSpanAttributes } from "../utils/request.js"; import { getUrlQuery } from "../utils/url.js"; import { getClientIPAddress } from "../utils/clientIPAddress.js"; import { ipHeaderNames } from "../vendor/getIpAddress.js"; import { safeSetSpanJSONAttributes } from "../tracing/spans/captureSpan.js"; import { HTTP_REQUEST_BODY_DATA, HTTP_REQUEST_METHOD, SENTRY_IS_LOCALHOST, URL_FULL, URL_QUERY, USER_IP_ADDRESS } from "@sentry/conventions/attributes"; // src/integrations/requestdata.ts const INTEGRATION_NAME = "RequestData"; const _requestDataIntegration = ((options = {}) => { function resolveRequestDataOptions(client) { const dataCollection = client.getDataCollectionOptions(); const include = { cookies: options.include?.cookies ?? dataCollection.cookies !== false, data: options.include?.data ?? true, headers: options.include?.headers ?? dataCollection.httpHeaders.request !== false, ip: options.include?.ip ?? dataCollection.userInfo, query_string: options.include?.query_string ?? dataCollection.urlQueryParams !== false, url: options.include?.url ?? true }; return { include, dataCollection: { ...dataCollection, cookies: resolveFilteringBehavior(include.cookies, dataCollection.cookies), httpHeaders: { ...dataCollection.httpHeaders, request: resolveFilteringBehavior(include.headers, dataCollection.httpHeaders.request) }, urlQueryParams: resolveFilteringBehavior(include.query_string, dataCollection.urlQueryParams) } }; } return { name: INTEGRATION_NAME, processEvent(event, _hint, client) { const { sdkProcessingMetadata = {} } = event; const { normalizedRequest, ipAddress } = sdkProcessingMetadata; if (!normalizedRequest) return event; const { include, dataCollection } = resolveRequestDataOptions(client); addNormalizedRequestDataToEvent(event, normalizedRequest, { ipAddress }, include, dataCollection); return event; }, processSpan(span) { const { user, sdkProcessingMetadata } = getIsolationScope().getScopeData(); safeSetSpanJSONAttributes(span, { [SENTRY_IS_LOCALHOST]: isLocalhostSpan(sdkProcessingMetadata, user.ip_address) }); }, processSegmentSpan(span, client) { const { sdkProcessingMetadata = {} } = getIsolationScope().getScopeData(); const { normalizedRequest, ipAddress } = sdkProcessingMetadata; if (!normalizedRequest) return; const { include, dataCollection } = resolveRequestDataOptions(client); addNormalizedRequestDataToSpan(span, normalizedRequest, ipAddress, include, dataCollection); } }; }); const localhostByRequest = /* @__PURE__ */ new WeakMap(); /** * Whether a span belongs to a request served from the developer's own machine. * * The client IP is resolved exactly as {@link addNormalizedRequestDataToEvent} resolves the IP it * writes to `user.ip_address`, so a span and the event for the same request always agree. Crucially * a forwarding header wins over `ipAddress`, which is the raw socket address: a reverse proxy on the * same host connects over loopback, so trusting the socket would mark genuine production traffic as * localhost and let the backend filter silently drop it. */ function isLocalhostSpan(sdkProcessingMetadata, scopeUserIpAddress) { const { normalizedRequest, ipAddress } = sdkProcessingMetadata; if (!normalizedRequest) return isLocalhostRequest(void 0, ipAddress || scopeUserIpAddress); const cached = localhostByRequest.get(normalizedRequest); if (cached !== void 0) return cached; const headers = normalizedRequest.headers; const clientIpAddress = headers && getClientIPAddress(headers) || ipAddress || scopeUserIpAddress; const isLocalhost = isLocalhostRequest(normalizedRequest, clientIpAddress); localhostByRequest.set(normalizedRequest, isLocalhost); return isLocalhost; } /** * Add data about a request to an event. Primarily for use in Node-based SDKs, but included in `@sentry/core` * so it can be used in cross-platform SDKs like `@sentry/nextjs`. */ const requestDataIntegration = defineIntegration(_requestDataIntegration); /** * Add already normalized request data to an event. * This mutates the passed in event. */ function addNormalizedRequestDataToEvent(event, req, additionalData, include, dataCollection) { const requestData = extractNormalizedRequestData(req, include); if (requestData.cookies) requestData.cookies = filterKeyValueData(requestData.cookies, dataCollection.cookies, SENSITIVE_COOKIE_NAME_SNIPPETS); if (requestData.headers) requestData.headers = filterKeyValueData(requestData.headers, dataCollection.httpHeaders.request); if (requestData.query_string) requestData.query_string = normalizeAndFilterQueryString(requestData.query_string, dataCollection.urlQueryParams); if (requestData.url) requestData.url = filterUrlQuery(requestData.url, dataCollection.urlQueryParams); event.request = { ...event.request, ...requestData }; if (include.ip) { const ip = req.headers && getClientIPAddress(req.headers) || additionalData.ipAddress; if (ip) event.user = { ...event.user, ip_address: ip }; } } function addNormalizedRequestDataToSpan(span, normalizedRequest, ipAddress, include, dataCollection) { const requestData = extractNormalizedRequestData(normalizedRequest, include); const attributes = {}; if (requestData.url) attributes[URL_FULL] = filterUrlQuery(requestData.url, dataCollection.urlQueryParams); if (requestData.method) attributes[HTTP_REQUEST_METHOD] = requestData.method; if (requestData.query_string) attributes[URL_QUERY] = normalizeAndFilterQueryString(requestData.query_string, dataCollection.urlQueryParams); safeSetSpanJSONAttributes(span, attributes); if (include.cookies) { const cookieHeader = normalizedRequest.headers?.cookie; const cookiePairs = normalizedRequest.cookies ? Object.entries(normalizedRequest.cookies) : cookieHeader ? parseCookieHeader(cookieHeader, "cookie") : []; if (cookiePairs.length > 0) safeSetSpanJSONAttributes(span, { "http.request.header.cookie": filterCookiePairs(cookiePairs, dataCollection.cookies) }); } if (requestData.headers) { const headerAttributes = httpHeadersToSpanAttributes(requestData.headers, dataCollection, "request"); safeSetSpanJSONAttributes(span, headerAttributes); } if (requestData.data != null) { const serialized = typeof requestData.data === "string" ? requestData.data : JSON.stringify(requestData.data); if (serialized) safeSetSpanJSONAttributes(span, { [HTTP_REQUEST_BODY_DATA]: serialized }); } if (include.ip) { const ip = normalizedRequest.headers && getClientIPAddress(normalizedRequest.headers) || ipAddress || void 0; if (ip) safeSetSpanJSONAttributes(span, { [USER_IP_ADDRESS]: ip }); } } function extractNormalizedRequestData(normalizedRequest, include) { const requestData = {}; const headers = { ...normalizedRequest.headers }; if (include.headers) { requestData.headers = headers; if (!include.cookies) delete headers.cookie; if (!include.ip) { const ipHeaderNamesLower = new Set(ipHeaderNames.map((name) => name.toLowerCase())); for (const key of Object.keys(headers)) if (ipHeaderNamesLower.has(key.toLowerCase())) delete headers[key]; } } requestData.method = normalizedRequest.method; if (include.url) requestData.url = normalizedRequest.url; if (include.cookies) requestData.cookies = normalizedRequest.cookies || (headers?.cookie ? cookiePairsToRecord(parseCookieHeader(headers.cookie, "cookie")) : void 0) || {}; if (include.query_string) requestData.query_string = normalizedRequest.query_string; if (include.data) requestData.data = normalizedRequest.data; return requestData; } function resolveFilteringBehavior(isIncluded, behavior) { return isIncluded && behavior === false ? true : behavior; } function normalizeAndFilterQueryString(queryString, behavior) { const normalized = normalizeQueryString(queryString); return normalized ? filterQueryParams(normalized, behavior) : void 0; } function normalizeQueryString(queryString) { if (typeof queryString === "string") return getUrlQuery(queryString); const pairs = Array.isArray(queryString) ? queryString : Object.entries(queryString); return new URLSearchParams(pairs).toString() || void 0; } export { requestDataIntegration }; //# sourceMappingURL=requestdata.js.map