Skip to content

fix(core): Gracefully handle invalid baggage entries #16257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/core/src/utils-hoist/baggage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,17 @@ export function parseBaggageHeader(
function baggageHeaderToObject(baggageHeader: string): Record<string, string> {
return baggageHeader
.split(',')
.map(baggageEntry => baggageEntry.split('=').map(keyOrValue => decodeURIComponent(keyOrValue.trim())))
.map(baggageEntry =>
baggageEntry.split('=').map(keyOrValue => {
try {
return decodeURIComponent(keyOrValue.trim());
} catch {
// We ignore errors here, e.g. if the value cannot be URL decoded.
// This will then be skipped in the next step
return;
Comment on lines +120 to +123
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: Do we maybe want to add a debug log in case of discarded entries?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this, but this increases bundle size, not sure if worth it? 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, alright then probably not worth the tradeoff

}
}),
)
.reduce<Record<string, string>>((acc, [key, value]) => {
if (key && value) {
acc[key] = value;
Expand Down
35 changes: 32 additions & 3 deletions packages/core/test/utils-hoist/baggage.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect, test } from 'vitest';
import { describe, expect, test } from 'vitest';
import {
baggageHeaderToDynamicSamplingContext,
dynamicSamplingContextToSentryBaggageHeader,
parseBaggageHeader,
} from '../../src/utils-hoist/baggage';

test.each([
Expand All @@ -27,7 +28,7 @@ test.each([
{ environment: 'production', release: '1.0.1' },
],
[42, undefined],
])('baggageHeaderToDynamicSamplingContext(%p) should return %p', (input, expectedOutput) => {
])('baggageHeaderToDynamicSamplingContext(%j) should return %j', (input, expectedOutput) => {
expect(baggageHeaderToDynamicSamplingContext(input)).toStrictEqual(expectedOutput);
});

Expand All @@ -40,6 +41,34 @@ test.each([
{ release: 'abcdf', environment: '1234', someRandomKey: 'foo' },
'sentry-release=abcdf,sentry-environment=1234,sentry-someRandomKey=foo',
],
])('dynamicSamplingContextToSentryBaggageHeader(%p) should return %p', (input, expectedOutput) => {
])('dynamicSamplingContextToSentryBaggageHeader(%j) should return %j', (input, expectedOutput) => {
expect(dynamicSamplingContextToSentryBaggageHeader(input)).toStrictEqual(expectedOutput);
});

describe('parseBaggageHeader', () => {
test.each([
[undefined, undefined],
[1, undefined],
[true, undefined],
[false, undefined],
[null, undefined],
[NaN, undefined],
[Infinity, undefined],
[0, undefined],
['', undefined],
['foo', {}],
[
'sentry-environment=production,sentry-release=10.0.2,foo=bar',
{ 'sentry-environment': 'production', 'sentry-release': '10.0.2', foo: 'bar' },
],
[
['sentry-environment=production,sentry-release=10.0.2,foo=bar', 'foo2=bar2'],
{ 'sentry-environment': 'production', 'sentry-release': '10.0.2', foo: 'bar', foo2: 'bar2' },
],
// ignores malformed baggage entries
['foo=bar,foo2=%3G', { foo: 'bar' }],
])('parseBaggageHeader(%j) should return %j', (input, expectedOutput) => {
const actual = parseBaggageHeader(input);
expect(actual).toStrictEqual(expectedOutput);
});
});
Loading