Skip to content

Commit 5ed5627

Browse files
authored
fix(core): Gracefully handle invalid baggage entries (#16257)
Fixes #16251
1 parent 8f5f2f3 commit 5ed5627

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

packages/core/src/utils-hoist/baggage.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,17 @@ export function parseBaggageHeader(
113113
function baggageHeaderToObject(baggageHeader: string): Record<string, string> {
114114
return baggageHeader
115115
.split(',')
116-
.map(baggageEntry => baggageEntry.split('=').map(keyOrValue => decodeURIComponent(keyOrValue.trim())))
116+
.map(baggageEntry =>
117+
baggageEntry.split('=').map(keyOrValue => {
118+
try {
119+
return decodeURIComponent(keyOrValue.trim());
120+
} catch {
121+
// We ignore errors here, e.g. if the value cannot be URL decoded.
122+
// This will then be skipped in the next step
123+
return;
124+
}
125+
}),
126+
)
117127
.reduce<Record<string, string>>((acc, [key, value]) => {
118128
if (key && value) {
119129
acc[key] = value;

packages/core/test/utils-hoist/baggage.test.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { expect, test } from 'vitest';
1+
import { describe, expect, test } from 'vitest';
22
import {
33
baggageHeaderToDynamicSamplingContext,
44
dynamicSamplingContextToSentryBaggageHeader,
5+
parseBaggageHeader,
56
} from '../../src/utils-hoist/baggage';
67

78
test.each([
@@ -27,7 +28,7 @@ test.each([
2728
{ environment: 'production', release: '1.0.1' },
2829
],
2930
[42, undefined],
30-
])('baggageHeaderToDynamicSamplingContext(%p) should return %p', (input, expectedOutput) => {
31+
])('baggageHeaderToDynamicSamplingContext(%j) should return %j', (input, expectedOutput) => {
3132
expect(baggageHeaderToDynamicSamplingContext(input)).toStrictEqual(expectedOutput);
3233
});
3334

@@ -40,6 +41,34 @@ test.each([
4041
{ release: 'abcdf', environment: '1234', someRandomKey: 'foo' },
4142
'sentry-release=abcdf,sentry-environment=1234,sentry-someRandomKey=foo',
4243
],
43-
])('dynamicSamplingContextToSentryBaggageHeader(%p) should return %p', (input, expectedOutput) => {
44+
])('dynamicSamplingContextToSentryBaggageHeader(%j) should return %j', (input, expectedOutput) => {
4445
expect(dynamicSamplingContextToSentryBaggageHeader(input)).toStrictEqual(expectedOutput);
4546
});
47+
48+
describe('parseBaggageHeader', () => {
49+
test.each([
50+
[undefined, undefined],
51+
[1, undefined],
52+
[true, undefined],
53+
[false, undefined],
54+
[null, undefined],
55+
[NaN, undefined],
56+
[Infinity, undefined],
57+
[0, undefined],
58+
['', undefined],
59+
['foo', {}],
60+
[
61+
'sentry-environment=production,sentry-release=10.0.2,foo=bar',
62+
{ 'sentry-environment': 'production', 'sentry-release': '10.0.2', foo: 'bar' },
63+
],
64+
[
65+
['sentry-environment=production,sentry-release=10.0.2,foo=bar', 'foo2=bar2'],
66+
{ 'sentry-environment': 'production', 'sentry-release': '10.0.2', foo: 'bar', foo2: 'bar2' },
67+
],
68+
// ignores malformed baggage entries
69+
['foo=bar,foo2=%3G', { foo: 'bar' }],
70+
])('parseBaggageHeader(%j) should return %j', (input, expectedOutput) => {
71+
const actual = parseBaggageHeader(input);
72+
expect(actual).toStrictEqual(expectedOutput);
73+
});
74+
});

0 commit comments

Comments
 (0)