Skip to content

Commit 3dca058

Browse files
committed
fix _sentryTraceToTraceParentHeader potentially returning invalid traceparent header
1 parent af4a72d commit 3dca058

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/core/src/utils/traceData.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export function getTraceData(
5858
};
5959

6060
if (options.propagateTraceparent) {
61-
traceData.traceparent = _sentryTraceToTraceParentHeader(sentryTrace);
61+
const traceparent = _sentryTraceToTraceParentHeader(sentryTrace);
62+
if (traceparent) {
63+
traceData.traceparent = traceparent;
64+
}
6265
}
6366

6467
return traceData;
@@ -84,7 +87,10 @@ function scopeToTraceHeader(scope: Scope): string {
8487
*
8588
* Exported for testing
8689
*/
87-
export function _sentryTraceToTraceParentHeader(sentryTrace: string): string {
90+
export function _sentryTraceToTraceParentHeader(sentryTrace: string): string | undefined {
8891
const { traceId, parentSpanId, parentSampled } = extractTraceparentData(sentryTrace) || {};
92+
if (!traceId || !parentSpanId) {
93+
return undefined;
94+
}
8995
return `00-${traceId}-${parentSpanId}-${parentSampled ? '01' : '00'}`;
9096
}

packages/core/test/lib/utils/traceData.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,18 @@ describe('_sentryTraceToTraceParentHeader', () => {
365365
const traceparent = _sentryTraceToTraceParentHeader('12345678901234567890123456789012-1234567890123456');
366366
expect(traceparent).toBe('00-12345678901234567890123456789012-1234567890123456-00');
367367
});
368+
369+
it.each([
370+
'12345678901234567890123456789012--0',
371+
'-12345678901234567890123456789012-0',
372+
'--1',
373+
'0',
374+
'1',
375+
'',
376+
'00-12345678901234567890123456789012-1234567890123456-01',
377+
'00-12345678901234567890123456789012-1234567890123456-00',
378+
])('returns undefined if the sentry-trace header is invalid (%s)', sentryTrace => {
379+
const traceparent = _sentryTraceToTraceParentHeader(sentryTrace);
380+
expect(traceparent).toBeUndefined();
381+
});
368382
});

0 commit comments

Comments
 (0)