-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathtraceData.test.ts
231 lines (199 loc) · 7.57 KB
/
traceData.test.ts
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
226
227
228
229
230
231
import { SentrySpan, getTraceData } from '../../../src/';
import * as SentryCoreCurrentScopes from '../../../src/currentScopes';
import * as SentryCoreExports from '../../../src/exports';
import * as SentryCoreTracing from '../../../src/tracing';
import * as SentryCoreSpanUtils from '../../../src/utils/spanUtils';
import { isValidBaggageString } from '../../../src/utils/traceData';
const TRACE_FLAG_SAMPLED = 1;
const mockedSpan = new SentrySpan({
traceId: '12345678901234567890123456789012',
spanId: '1234567890123456',
sampled: true,
});
const mockedClient = {} as any;
const mockedScope = {
getPropagationContext: () => ({
traceId: '123',
}),
} as any;
describe('getTraceData', () => {
beforeEach(() => {
jest.spyOn(SentryCoreExports, 'isEnabled').mockReturnValue(true);
});
afterEach(() => {
jest.clearAllMocks();
});
it('returns the tracing data from the span, if a span is available', () => {
{
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromSpan').mockReturnValueOnce({
environment: 'production',
});
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => mockedSpan);
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
const data = getTraceData();
expect(data).toEqual({
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
baggage: 'sentry-environment=production',
});
}
});
it('returns propagationContext DSC data if no span is available', () => {
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => undefined);
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(
() =>
({
getPropagationContext: () => ({
traceId: '12345678901234567890123456789012',
sampled: true,
spanId: '1234567890123456',
dsc: {
environment: 'staging',
public_key: 'key',
trace_id: '12345678901234567890123456789012',
},
}),
}) as any,
);
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => mockedClient);
const traceData = getTraceData();
expect(traceData).toEqual({
'sentry-trace': expect.stringMatching(/12345678901234567890123456789012-(.{16})-1/),
baggage: 'sentry-environment=staging,sentry-public_key=key,sentry-trace_id=12345678901234567890123456789012',
});
});
it('returns only the `sentry-trace` value if no DSC is available', () => {
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
trace_id: '',
public_key: undefined,
});
// @ts-expect-error - we don't need to provide all the properties
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
isRecording: () => true,
spanContext: () => {
return {
traceId: '12345678901234567890123456789012',
spanId: '1234567890123456',
traceFlags: TRACE_FLAG_SAMPLED,
};
},
}));
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => mockedClient);
const traceData = getTraceData();
expect(traceData).toEqual({
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
});
});
it('returns only the `sentry-trace` tag if no DSC is available without a client', () => {
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
trace_id: '',
public_key: undefined,
});
// @ts-expect-error - we don't need to provide all the properties
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
isRecording: () => true,
spanContext: () => {
return {
traceId: '12345678901234567890123456789012',
spanId: '1234567890123456',
traceFlags: TRACE_FLAG_SAMPLED,
};
},
}));
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => undefined);
const traceData = getTraceData();
expect(traceData).toEqual({
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
});
expect('baggage' in traceData).toBe(false);
});
it('returns an empty object if the `sentry-trace` value is invalid', () => {
// @ts-expect-error - we don't need to provide all the properties
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
isRecording: () => true,
spanContext: () => {
return {
traceId: '1234567890123456789012345678901+',
spanId: '1234567890123456',
traceFlags: TRACE_FLAG_SAMPLED,
};
},
}));
const traceData = getTraceData();
expect(traceData).toEqual({});
});
it('returns an empty object if the SDK is disabled', () => {
jest.spyOn(SentryCoreExports, 'isEnabled').mockReturnValueOnce(false);
const traceData = getTraceData();
expect(traceData).toEqual({});
});
});
describe('isValidBaggageString', () => {
it.each([
'sentry-environment=production',
'sentry-environment=staging,sentry-public_key=key,sentry-trace_id=abc',
// @ is allowed in values
// spaces are allowed around the delimiters
'sentry-environment=staging , sentry-public_key=key ,[email protected]',
'sentry-environment=staging , thirdparty=value ,[email protected]',
// these characters are explicitly allowed for keys in the baggage spec:
"!#$%&'*+-.^_`|~1234567890abcxyzABCXYZ=true",
// special characters in values are fine (except for ",;\ - see other test)
'key=(value)',
'key=[{(value)}]',
'key=some$value',
'key=more#value',
'key=max&value',
'key=max:value',
'key=x=value',
])('returns true if the baggage string is valid (%s)', baggageString => {
expect(isValidBaggageString(baggageString)).toBe(true);
});
it.each([
// baggage spec doesn't permit leading spaces
' sentry-environment=production,sentry-publickey=key,sentry-trace_id=abc',
// no spaces in keys or values
'sentry-public key=key',
'sentry-publickey=my key',
// no delimiters ("(),/:;<=>?@[\]{}") in keys
'asdf(x=value',
'asdf)x=value',
'asdf,x=value',
'asdf/x=value',
'asdf:x=value',
'asdf;x=value',
'asdf<x=value',
'asdf>x=value',
'asdf?x=value',
'asdf@x=value',
'asdf[x=value',
'asdf]x=value',
'asdf\\x=value',
'asdf{x=value',
'asdf}x=value',
// no ,;\" in values
'key=va,lue',
'key=va;lue',
'key=va\\lue',
'key=va"lue"',
// baggage headers can have properties but we currently don't support them
'sentry-environment=production;prop1=foo;prop2=bar,nextkey=value',
// no fishy stuff
'absolutely not a valid baggage string',
'val"/><script>alert("xss")</script>',
'something"/>',
'<script>alert("xss")</script>',
'/>',
'" onblur="alert("xss")',
])('returns false if the baggage string is invalid (%s)', baggageString => {
expect(isValidBaggageString(baggageString)).toBe(false);
});
it('returns false if the baggage string is empty', () => {
expect(isValidBaggageString('')).toBe(false);
});
it('returns false if the baggage string is empty', () => {
expect(isValidBaggageString(undefined)).toBe(false);
});
});