Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit d3cf45e

Browse files
authored
Safely reject invalid-length span and trace ids (#1206)
1 parent 84d38db commit d3cf45e

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

plugin/ochttp/propagation/b3/b3.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func ParseTraceID(tid string) (trace.TraceID, bool) {
6868
return trace.TraceID{}, false
6969
}
7070
b, err := hex.DecodeString(tid)
71-
if err != nil {
71+
if err != nil || len(b) > 16 {
7272
return trace.TraceID{}, false
7373
}
7474
var traceID trace.TraceID
@@ -90,7 +90,7 @@ func ParseSpanID(sid string) (spanID trace.SpanID, ok bool) {
9090
return trace.SpanID{}, false
9191
}
9292
b, err := hex.DecodeString(sid)
93-
if err != nil {
93+
if err != nil || len(b) > 8 {
9494
return trace.SpanID{}, false
9595
}
9696
start := 8 - len(b)

plugin/ochttp/propagation/b3/b3_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ func TestHTTPFormat_FromRequest(t *testing.T) {
103103
wantSc: trace.SpanContext{},
104104
wantOk: false,
105105
},
106+
{
107+
name: "invalid >128-bit trace ID + 64-bit span ID; no sampling header",
108+
makeReq: func() *http.Request {
109+
req, _ := http.NewRequest("GET", "http://example.com", nil)
110+
req.Header.Set(TraceIDHeader, "0020000000000001002000000000000111")
111+
req.Header.Set(SpanIDHeader, "0020000000000001")
112+
return req
113+
},
114+
wantSc: trace.SpanContext{},
115+
wantOk: false,
116+
},
106117
{
107118
name: "128-bit trace ID; invalid span ID; no sampling header",
108119
makeReq: func() *http.Request {
@@ -114,6 +125,17 @@ func TestHTTPFormat_FromRequest(t *testing.T) {
114125
wantSc: trace.SpanContext{},
115126
wantOk: false,
116127
},
128+
{
129+
name: "128-bit trace ID; invalid >64 bit span ID; no sampling header",
130+
makeReq: func() *http.Request {
131+
req, _ := http.NewRequest("GET", "http://example.com", nil)
132+
req.Header.Set(TraceIDHeader, "463ac35c9f6413ad48485a3953bb6124")
133+
req.Header.Set(SpanIDHeader, "002000000000000111")
134+
return req
135+
},
136+
wantSc: trace.SpanContext{},
137+
wantOk: false,
138+
},
117139
{
118140
name: "128-bit trace ID + 64-bit span ID; sampled=true",
119141
makeReq: func() *http.Request {

0 commit comments

Comments
 (0)