From 9fee4b72f727d00c931b684bb4f6ddee1e469a44 Mon Sep 17 00:00:00 2001 From: taltal-beep Date: Tue, 17 Nov 2026 08:09:42 +0200 Subject: [PATCH] fix(tracing): Strip whitespace around '=' in incoming baggage items The W3C baggage format allows optional whitespace around '=' in a list member. from_incoming_header only stripped the item as a whole, so 'sentry-sample_rand = 0.5' produced the key 'sample_rand ' and the value ' 0.5'. The incoming sample_rand was then ignored and the corrupted keys were re-sent downstream percent-encoded. --- sentry_sdk/tracing_utils.py | 1 + tests/tracing/test_baggage.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index dd91da3b4d..28c58576a3 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -786,6 +786,7 @@ def from_incoming_header( with capture_internal_exceptions(): item = item.strip() key, val = item.split("=", 1) + key, val = key.strip(), val.strip() if Baggage.SENTRY_PREFIX_REGEX.match(key): baggage_key = unquote(key.split("-")[1]) sentry_items[baggage_key] = unquote(val) diff --git a/tests/tracing/test_baggage.py b/tests/tracing/test_baggage.py index 3d70a7cfea..6acd3ff590 100644 --- a/tests/tracing/test_baggage.py +++ b/tests/tracing/test_baggage.py @@ -96,3 +96,22 @@ def test_malformed_baggage(): assert baggage.sentry_items == {} assert baggage.third_party_items == "" assert baggage.mutable + + +def test_baggage_with_optional_whitespace_around_equals(): + # W3C baggage allows optional whitespace around "=" in list members + header = ( + "sentry-trace_id = 771a43a4192642f0b136d5159a501700, " + "sentry-sample_rand= 0.5, other-vendor = foo" + ) + + baggage = Baggage.from_incoming_header(header) + + assert baggage.sentry_items == { + "trace_id": "771a43a4192642f0b136d5159a501700", + "sample_rand": "0.5", + } + assert baggage._sample_rand() == 0.5 + assert baggage.serialize() == ( + "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rand=0.5" + )