From d7b88ede8673c51f6f28c5bd35dd3e07e145893a Mon Sep 17 00:00:00 2001 From: cirilla-zmh Date: Tue, 11 Jun 2024 16:42:03 +0800 Subject: [PATCH] fix panic in b3 propagator --- propagators/b3/b3_propagator.go | 3 +++ propagators/b3/b3_propagator_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/propagators/b3/b3_propagator.go b/propagators/b3/b3_propagator.go index 905cd71fe8c..ea43762fe80 100644 --- a/propagators/b3/b3_propagator.go +++ b/propagators/b3/b3_propagator.go @@ -277,6 +277,9 @@ func extractSingle(ctx context.Context, contextHeader string) (context.Context, } pos += separatorWidth // {traceID}- + if headerLen < pos+spanIDWidth { + return ctx, empty, errInvalidSpanIDValue + } scc.SpanID, err = trace.SpanIDFromHex(contextHeader[pos : pos+spanIDWidth]) if err != nil { return ctx, empty, errInvalidSpanIDValue diff --git a/propagators/b3/b3_propagator_test.go b/propagators/b3/b3_propagator_test.go index bac1893f4e5..6626399f593 100644 --- a/propagators/b3/b3_propagator_test.go +++ b/propagators/b3/b3_propagator_test.go @@ -217,6 +217,16 @@ func TestExtractSingle(t *testing.T) { {"000000000000007b", trace.SpanContextConfig{}, errInvalidScope, false, false}, {"000000000000007b00000000000001c8", trace.SpanContextConfig{}, errInvalidScope, false, false}, // Support short trace IDs. + { + "invalid-000000000000007b", + trace.SpanContextConfig{}, + errInvalidTraceIDValue, false, false, + }, + { + "000000000000007b00000000000001c8-invalid", + trace.SpanContextConfig{}, + errInvalidSpanIDValue, false, false, + }, { "00000000000001c8-000000000000007b", trace.SpanContextConfig{