@@ -86,14 +86,8 @@ class HttpTraceContext : public context::propagation::TextMapPropagator
8686 }
8787
8888private:
89- static constexpr uint8_t kInvalidVersion = 0xFF ;
90-
91- static bool IsValidVersion (nostd::string_view version_hex)
92- {
93- uint8_t version;
94- detail::HexToBinary (version_hex, &version, sizeof (version));
95- return version != kInvalidVersion ;
96- }
89+ static constexpr uint8_t kInvalidVersion = 0xFF ;
90+ static constexpr uint8_t kDefaultAssumedVersion = 0x00 ;
9791
9892 static void InjectImpl (context::propagation::TextMapCarrier &carrier,
9993 const SpanContext &span_context)
@@ -122,11 +116,6 @@ class HttpTraceContext : public context::propagation::TextMapPropagator
122116 static SpanContext ExtractContextFromTraceHeaders (nostd::string_view trace_parent,
123117 nostd::string_view trace_state)
124118 {
125- if (trace_parent.size () != kTraceParentSize )
126- {
127- return SpanContext::GetInvalid ();
128- }
129-
130119 std::array<nostd::string_view, 4 > fields{};
131120 if (detail::SplitString (trace_parent, ' -' , fields.data (), 4 ) != 4 )
132121 {
@@ -150,11 +139,33 @@ class HttpTraceContext : public context::propagation::TextMapPropagator
150139 return SpanContext::GetInvalid ();
151140 }
152141
153- if (!IsValidVersion (version_hex))
142+ // hex is valid, convert it to binary
143+ uint8_t version_binary;
144+ detail::HexToBinary (version_hex, &version_binary, sizeof (version_binary));
145+ if (version_binary == kInvalidVersion )
154146 {
147+ // invalid version encountered
155148 return SpanContext::GetInvalid ();
156149 }
157150
151+ // See https://www.w3.org/TR/trace-context/#versioning-of-traceparent
152+ if (version_binary > kDefaultAssumedVersion )
153+ {
154+ // higher than default version detected
155+ if (trace_parent.size () < kTraceParentSize )
156+ {
157+ return SpanContext::GetInvalid ();
158+ }
159+ }
160+ else
161+ {
162+ // version is either lower or same as the default version
163+ if (trace_parent.size () != kTraceParentSize )
164+ {
165+ return SpanContext::GetInvalid ();
166+ }
167+ }
168+
158169 TraceId trace_id = TraceIdFromHex (trace_id_hex);
159170 SpanId span_id = SpanIdFromHex (span_id_hex);
160171
@@ -169,7 +180,8 @@ class HttpTraceContext : public context::propagation::TextMapPropagator
169180
170181 static SpanContext ExtractImpl (const context::propagation::TextMapCarrier &carrier)
171182 {
172- nostd::string_view trace_parent = carrier.Get (kTraceParent );
183+ // Get trace_parent after trimming the leading and trailing whitespaces
184+ nostd::string_view trace_parent = common::StringUtil::Trim (carrier.Get (kTraceParent ));
173185 nostd::string_view trace_state = carrier.Get (kTraceState );
174186 if (trace_parent == " " )
175187 {
0 commit comments