-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathrequestdata.ts
More file actions
225 lines (188 loc) · 6.89 KB
/
requestdata.ts
File metadata and controls
225 lines (188 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { getIsolationScope } from '../currentScopes';
import { defineIntegration } from '../integration';
import { SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS } from '../semanticAttributes';
import type { Event } from '../types/event';
import type { IntegrationFn } from '../types/integration';
import type { QueryParams, RequestEventData } from '../types/request';
import type { StreamedSpanJSON } from '../types/span';
import { parseCookie } from '../utils/cookie';
import { httpHeadersToSpanAttributes } from '../utils/request';
import { getClientIPAddress, ipHeaderNames } from '../vendor/getIpAddress';
import { safeSetSpanJSONAttributes } from '../tracing/spans/captureSpan';
interface RequestDataIncludeOptions {
cookies?: boolean;
data?: boolean;
headers?: boolean;
ip?: boolean;
query_string?: boolean;
url?: boolean;
}
type RequestDataIntegrationOptions = {
/**
* Controls what data is pulled from the request and added to the event.
*/
include?: RequestDataIncludeOptions;
};
// TODO(v11): Change defaults based on `sendDefaultPii`
const DEFAULT_INCLUDE: RequestDataIncludeOptions = {
cookies: true,
data: true,
headers: true,
query_string: true,
url: true,
};
const INTEGRATION_NAME = 'RequestData';
const _requestDataIntegration = ((options: RequestDataIntegrationOptions = {}) => {
const include = {
...DEFAULT_INCLUDE,
...options.include,
};
return {
name: INTEGRATION_NAME,
processEvent(event, _hint, client) {
const { sdkProcessingMetadata = {} } = event;
const { normalizedRequest, ipAddress } = sdkProcessingMetadata;
const includeWithDefaultPiiApplied: RequestDataIncludeOptions = {
...include,
ip: include.ip ?? client.getOptions().sendDefaultPii,
};
if (normalizedRequest) {
addNormalizedRequestDataToEvent(event, normalizedRequest, { ipAddress }, includeWithDefaultPiiApplied);
}
return event;
},
processSegmentSpan(span, client) {
const { sdkProcessingMetadata = {} } = getIsolationScope().getScopeData();
const { normalizedRequest, ipAddress } = sdkProcessingMetadata;
if (!normalizedRequest) {
return;
}
const { sendDefaultPii } = client.getOptions();
const includeWithDefaultPiiApplied: RequestDataIncludeOptions = {
...include,
ip: include.ip ?? sendDefaultPii,
};
addNormalizedRequestDataToSpan(span, normalizedRequest, ipAddress, includeWithDefaultPiiApplied, sendDefaultPii);
},
};
}) satisfies IntegrationFn;
/**
* 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`.
*/
export const requestDataIntegration = defineIntegration(_requestDataIntegration);
/**
* Add already normalized request data to an event.
* This mutates the passed in event.
*/
function addNormalizedRequestDataToEvent(
event: Event,
req: RequestEventData,
// Data that should not go into `event.request` but is somehow related to requests
additionalData: { ipAddress?: string },
include: RequestDataIncludeOptions,
): void {
event.request = {
...event.request,
...extractNormalizedRequestData(req, include),
};
if (include.ip) {
const ip = (req.headers && getClientIPAddress(req.headers)) || additionalData.ipAddress;
if (ip) {
event.user = {
...event.user,
ip_address: ip,
};
}
}
}
function addNormalizedRequestDataToSpan(
span: StreamedSpanJSON,
normalizedRequest: RequestEventData,
ipAddress: string | undefined,
include: RequestDataIncludeOptions,
sendDefaultPii: boolean | undefined,
): void {
const requestData = extractNormalizedRequestData(normalizedRequest, include);
const attributes: Record<string, unknown> = {};
if (requestData.url) {
attributes['url.full'] = requestData.url;
}
if (requestData.method) {
attributes['http.request.method'] = requestData.method;
}
if (requestData.query_string) {
attributes['url.query'] = normalizeQueryString(requestData.query_string);
}
safeSetSpanJSONAttributes(span, attributes);
// Process cookies before headers so normalizedRequest.cookies takes precedence
// over the raw cookie header (matching the processEvent path).
if (requestData.cookies && Object.keys(requestData.cookies).length > 0) {
const cookieString = Object.entries(requestData.cookies)
.map(([name, value]) => `${name}=${value}`)
.join('; ');
const cookieAttributes = httpHeadersToSpanAttributes({ cookie: cookieString }, sendDefaultPii ?? false, 'request');
safeSetSpanJSONAttributes(span, cookieAttributes);
}
if (requestData.headers) {
const headerAttributes = httpHeadersToSpanAttributes(requestData.headers, sendDefaultPii ?? false, '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 || undefined;
if (ip) {
safeSetSpanJSONAttributes(span, { [SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS]: ip });
}
}
}
function extractNormalizedRequestData(
normalizedRequest: RequestEventData,
include: RequestDataIncludeOptions,
): RequestEventData {
const requestData: RequestEventData = {};
const headers = { ...normalizedRequest.headers };
if (include.headers) {
requestData.headers = headers;
if (!include.cookies) {
delete (headers as { cookie?: string }).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())) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete (headers as Record<string, unknown>)[key];
}
}
}
}
requestData.method = normalizedRequest.method;
if (include.url) {
requestData.url = normalizedRequest.url;
}
if (include.cookies) {
const cookies = normalizedRequest.cookies || (headers?.cookie ? parseCookie(headers.cookie) : undefined);
requestData.cookies = cookies || {};
}
if (include.query_string) {
requestData.query_string = normalizedRequest.query_string;
}
if (include.data) {
requestData.data = normalizedRequest.data;
}
return requestData;
}
function normalizeQueryString(queryString: QueryParams): string | undefined {
if (typeof queryString === 'string') {
return queryString || undefined;
}
const pairs = Array.isArray(queryString) ? queryString : Object.entries(queryString);
const result = pairs.map(([key, value]) => `${key}=${value}`).join('&');
return result || undefined;
}