Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for unreasonable data in binary propagation #529

Merged
merged 2 commits into from Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions propagation.go
Expand Up @@ -215,6 +215,12 @@ func (p *BinaryPropagator) Inject(
return nil
}

// W3C limits https://github.com/w3c/baggage/blob/master/baggage/HTTP_HEADER_FORMAT.md#limits
const (
maxBinaryBaggage = 180
maxBinaryNameValueLen = 4096
)

// Extract implements Extractor of BinaryPropagator
func (p *BinaryPropagator) Extract(abstractCarrier interface{}) (SpanContext, error) {
carrier, ok := abstractCarrier.(io.Reader)
Expand Down Expand Up @@ -245,6 +251,9 @@ func (p *BinaryPropagator) Extract(abstractCarrier interface{}) (SpanContext, er
if err := binary.Read(carrier, binary.BigEndian, &numBaggage); err != nil {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
if numBaggage > maxBinaryBaggage {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
if iNumBaggage := int(numBaggage); iNumBaggage > 0 {
ctx.baggage = make(map[string]string, iNumBaggage)
buf := p.buffers.Get().(*bytes.Buffer)
Expand All @@ -265,6 +274,9 @@ func (p *BinaryPropagator) Extract(abstractCarrier interface{}) (SpanContext, er
if err := binary.Read(carrier, binary.BigEndian, &valLen); err != nil {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
if keyLen+valLen > maxBinaryNameValueLen {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
buf.Reset()
buf.Grow(int(valLen))
if n, err := io.CopyN(buf, carrier, int64(valLen)); err != nil || int32(n) != valLen {
Expand Down
22 changes: 22 additions & 0 deletions propagation_test.go
Expand Up @@ -16,7 +16,9 @@ package jaeger

import (
"bytes"
"encoding/base64"
"net/http"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -127,6 +129,26 @@ func TestSpanPropagator(t *testing.T) {
}...)
}

func TestBinaryCorruption(t *testing.T) {
const op = "test"
reporter := NewInMemoryReporter()
tracer, closer := NewTracer("x", NewConstSampler(true), reporter)

sp := tracer.StartSpan(op)
carrier := new(bytes.Buffer)
err := tracer.Inject(sp.Context(), opentracing.Binary, carrier)
assert.NoError(t, err)

// suppose we encode the data then forget to decode it
data := base64.StdEncoding.EncodeToString(carrier.Bytes())
_, err = tracer.Extract(opentracing.Binary, strings.NewReader(data))
if assert.Error(t, err) {
assert.Equal(t, opentracing.ErrSpanContextCorrupted, err)
}
sp.Finish()
closer.Close()
}

func TestSpanIntegrityAfterSerialize(t *testing.T) {
serializedString := "f6c385a2c57ed8d7:00b04a90b7723bdc:76c385a2c57ed8d7:1"

Expand Down