Skip to content

Commit 3c2bb7c

Browse files
authored
Fix TraceContextPropagator behavior when trace parent flags contain… (#4893)
1 parent eaf1d88 commit 3c2bb7c

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

src/OpenTelemetry.Api/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
`GetTracer` from leaking memory.
1111
([#4906](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4906))
1212

13+
* Fix `TraceContextPropagator` by validating the first digit of the hex-encoded
14+
`trace-flags` field of the `traceparent` header.
15+
([#4893](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4893))
16+
1317
## 1.6.0
1418

1519
Released 2023-Sep-05

src/OpenTelemetry.Api/Context/Propagation/TraceContextPropagator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,20 @@ internal static bool TryExtractTraceparent(string traceparent, out ActivityTrace
195195
return false;
196196
}
197197

198-
byte options1;
198+
byte optionsLowByte;
199199
try
200200
{
201201
spanId = ActivitySpanId.CreateFromString(traceparent.AsSpan().Slice(VersionAndTraceIdLength, SpanIdLength));
202-
options1 = HexCharToByte(traceparent[VersionAndTraceIdAndSpanIdLength + 1]);
202+
_ = HexCharToByte(traceparent[VersionAndTraceIdAndSpanIdLength]); // to verify if there is no bad chars on options position
203+
optionsLowByte = HexCharToByte(traceparent[VersionAndTraceIdAndSpanIdLength + 1]);
203204
}
204205
catch (ArgumentOutOfRangeException)
205206
{
206207
// it's ok to still parse tracestate
207208
return false;
208209
}
209210

210-
if ((options1 & 1) == 1)
211+
if ((optionsLowByte & 1) == 1)
211212
{
212213
traceOptions |= ActivityTraceFlags.Recorded;
213214
}

test/OpenTelemetry.Instrumentation.W3cTraceContext.Tests/W3CTraceContextTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class W3CTraceContextTests : IDisposable
3535
*/
3636
private const string W3cTraceContextEnvVarName = "OTEL_W3CTRACECONTEXT";
3737
private static readonly Version AspNetCoreHostingVersion = typeof(Microsoft.AspNetCore.Hosting.Builder.IApplicationBuilderFactory).Assembly.GetName().Version;
38-
private readonly HttpClient httpClient = new HttpClient();
38+
private readonly HttpClient httpClient = new();
3939
private readonly ITestOutputHelper output;
4040

4141
public W3CTraceContextTests(ITestOutputHelper output)
@@ -107,7 +107,7 @@ public void W3CTraceContextTestSuiteAsync(string value)
107107
}
108108
else
109109
{
110-
Assert.StartsWith("FAILED (failures=3)", lastLine);
110+
Assert.StartsWith("FAILED (failures=2)", lastLine);
111111
}
112112
}
113113

test/OpenTelemetry.Tests/Trace/Propagation/TraceContextPropagatorTest.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,16 @@ public void IsBlankIfNoHeader()
103103
Assert.False(ctx.ActivityContext.IsValid());
104104
}
105105

106-
[Fact]
107-
public void IsBlankIfInvalid()
106+
[Theory]
107+
[InlineData($"00-xyz7651916cd43dd8448eb211c80319c-{SpanId}-01")]
108+
[InlineData($"00-{TraceId}-xyz7c989f97918e1-01")]
109+
[InlineData($"00-{TraceId}-{SpanId}-x1")]
110+
[InlineData($"00-{TraceId}-{SpanId}-1x")]
111+
public void IsBlankIfInvalid(string invalidTraceParent)
108112
{
109113
var headers = new Dictionary<string, string>
110114
{
111-
{ TraceParent, $"00-xyz7651916cd43dd8448eb211c80319c-{SpanId}-01" },
115+
{ TraceParent, invalidTraceParent },
112116
};
113117

114118
var f = new TraceContextPropagator();
@@ -191,8 +195,8 @@ public void DuplicateKeys()
191195
// test_tracestate_duplicated_keys
192196
Assert.Empty(CallTraceContextPropagator("foo=1,foo=1"));
193197
Assert.Empty(CallTraceContextPropagator("foo=1,foo=2"));
194-
Assert.Empty(CallTraceContextPropagator(new string[] { "foo=1", "foo=1" }));
195-
Assert.Empty(CallTraceContextPropagator(new string[] { "foo=1", "foo=2" }));
198+
Assert.Empty(CallTraceContextPropagator(new[] { "foo=1", "foo=1" }));
199+
Assert.Empty(CallTraceContextPropagator(new[] { "foo=1", "foo=2" }));
196200
}
197201

198202
[Fact]
@@ -318,7 +322,7 @@ private static string CallTraceContextPropagator(string[] tracestate)
318322
{
319323
var headers = new Dictionary<string, string[]>
320324
{
321-
{ TraceParent, new string[] { $"00-{TraceId}-{SpanId}-01" } },
325+
{ TraceParent, new[] { $"00-{TraceId}-{SpanId}-01" } },
322326
{ TraceState, tracestate },
323327
};
324328
var f = new TraceContextPropagator();

0 commit comments

Comments
 (0)