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" + )