Skip to content

fix(tracing): Baggage parsing fails when input is not of type string #5276

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 5 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 23 additions & 3 deletions packages/utils/src/baggage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Baggage, BaggageObj, TraceparentData } from '@sentry/types';

import { isString } from './is';
import { logger } from './logger';

export const BAGGAGE_HEADER_NAME = 'baggage';
Expand Down Expand Up @@ -89,9 +90,28 @@ export function serializeBaggage(baggage: Baggage): string {
}, baggage[1]);
}

/** Parse a baggage header from a string and return a Baggage object */
export function parseBaggageString(inputBaggageString: string): Baggage {
return inputBaggageString.split(',').reduce(
/** Parse a baggage header from a string or a string array and return a Baggage object */
export function parseBaggageString(inputBaggageValue: string | string[]): Baggage {
// Adding this check here because we got reports of this function failing due to the input value
// not being a string. This debug log might help us determine what's going on here.
if (!Array.isArray(inputBaggageValue) && !isString(inputBaggageValue)) {
__DEBUG_BUILD__ &&
logger.warn(
'[parseBaggageString] Received input value of unknown type: ',
typeof inputBaggageValue,
inputBaggageValue,
);

// Gonna early-return an empty baggage object so that we don't fail later on
return createBaggage({}, '');
}

const baggageEntries = (isString(inputBaggageValue) ? inputBaggageValue : inputBaggageValue.join(','))
.split(',')
.map(entry => entry.trim())
.filter(entry => entry !== '');
Comment on lines +110 to +113
Copy link
Contributor

Choose a reason for hiding this comment

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

I find this somehow very satisfying. :)


return baggageEntries.reduce(
([baggageObj, baggageString], curr) => {
const [key, val] = curr.split('=');
if (SENTRY_BAGGAGE_KEY_PREFIX_REGEX.test(key)) {
Expand Down
24 changes: 24 additions & 0 deletions packages/utils/test/baggage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ describe('Baggage', () => {
describe('parseBaggageString', () => {
it.each([
['parses an empty string', '', createBaggage({})],
['parses a blank string', ' ', createBaggage({})],
[
'parses sentry values into baggage',
'sentry-environment=production,sentry-release=10.0.2',
Expand All @@ -113,6 +114,29 @@ describe('Baggage', () => {
'userId=alice,serverNode=DF%2028,isProduction=false',
),
],
[
'parses arbitrary baggage headers from string with empty and blank entries',
'userId=alice, serverNode=DF%2028 , isProduction=false, ,,sentry-environment=production,,sentry-release=10.0.2',
createBaggage(
{ environment: 'production', release: '10.0.2' },
'userId=alice,serverNode=DF%2028,isProduction=false',
),
],
[
'parses a string array',
['userId=alice', 'sentry-environment=production', 'foo=bar'],
createBaggage({ environment: 'production' }, 'userId=alice,foo=bar'),
],
[
'parses a string array with items containing multiple entries',
['userId=alice, userName=bob', 'sentry-environment=production,sentry-release=1.0.1', 'foo=bar'],
createBaggage({ environment: 'production', release: '1.0.1' }, 'userId=alice,userName=bob,foo=bar'),
],
[
'parses a string array with empty/blank entries',
['', 'sentry-environment=production,sentry-release=1.0.1', ' ', 'foo=bar'],
createBaggage({ environment: 'production', release: '1.0.1' }, 'foo=bar'),
],
])('%s', (_: string, baggageString, baggage) => {
expect(parseBaggageString(baggageString)).toEqual(baggage);
});
Expand Down