Details
t.Logf("CONFIRMED: event attribute not length-truncated (%d bytes > limit %d)", len(stored), limit)
}
}
(The `-run` pattern above selects the three truncation cases plus the control; the fourth test function is included because it is part of the exact file that produced the output below - it compiles but is not selected by the pattern, and its output does not appear.)
`otelgo_log_truncate_test.go`:
```go
package otelpoc
import (
"context"
"strings"
"sync"
"testing"
"unicode/utf8"
"go.opentelemetry.io/otel/attribute"
apisdk "go.opentelemetry.io/otel/log"
sdklog "go.opentelemetry.io/otel/sdk/log"
)
type captureProcessor struct {
mu sync.Mutex
recs []*sdklog.Record
}
func (c *captureProcessor) Enabled(context.Context, sdklog.EnabledParameters) bool { return true }
func (c *captureProcessor) OnEmit(_ context.Context, r *sdklog.Record) error {
c.mu.Lock()
defer c.mu.Unlock()
clone := r.Clone()
c.recs = append(c.recs, &clone)
return nil
}
func (c *captureProcessor) Shutdown(context.Context) error { return nil }
func (c *captureProcessor) ForceFlush(context.Context) error {
return nil
}
// TestOT001_FFFDOnlyStringBypassesLogLengthLimit repeats the bypass against
// the sdk/log attribute path (sdk/log/internal/attrnorm, same template).
func TestOT001_FFFDOnlyStringBypassesLogLengthLimit(t *testing.T) {
const limit = 5
cp := &captureProcessor{}
lp := sdklog.NewLoggerProvider(
sdklog.WithProcessor(cp),
sdklog.WithAttributeValueLengthLimit(limit),
)
defer func() { _ = lp.Shutdown(context.Background()) }()
logger := lp.Logger("poc")
var r apisdk.Record
r.SetSeverity(apisdk.SeverityInfo)
r.AddAttributes(attribute.String("k", strings.Repeat("\uFFFD", 1000)))
logger.Emit(context.Background(), r)
cp.mu.Lock()
if len(cp.recs) != 1 {
t.Fatalf("expected 1 record, got %d", len(cp.recs))
}
rec := cp.recs[0]
cp.mu.Unlock()
stored := ""
rec.WalkAttributes(func(kv attribute.KeyValue) bool {
if string(kv.Key) == "k" {
stored = kv.Value.AsString()
}
return true
})
t.Logf("sdk/log input: 1000 chars / 3000 bytes; stored: %d chars / %d bytes; limit=%d",
utf8.RuneCountInString(stored), len(stored), limit)
if maxBytes := 3 * limit; len(stored) > maxBytes {
t.Fatalf("BYPASS: sdk/log WithAttributeValueLengthLimit=%d but %d bytes stored (expected <=%d)",
limit, len(stored), maxBytes)
}
}
Run:
go mod tidy
go test -count=1 -v -run "TestOT001_FFFDOnlyStringBypassesTraceLengthLimit|TestOT001_FFFDMixedStringOverRetains|TestOT001_ASCIIControl|TestOT001_FFFDOnlyStringBypassesLogLengthLimit" .
Expected output (captured against the published v1.46.0 modules; the three FAIL lines are the bypass being triggered and the PASS control proves the limit is active in the same setup; the only varying parts are the durations in parentheses and the total on the FAIL otelpoc summary line):
=== RUN TestOT001_FFFDOnlyStringBypassesLogLengthLimit
otelgo_log_truncate_test.go:66: sdk/log input: 1000 chars / 3000 bytes; stored: 1000 chars / 3000 bytes; limit=5
otelgo_log_truncate_test.go:69: BYPASS: sdk/log WithAttributeValueLengthLimit=5 but 3000 bytes stored (expected <=15)
--- FAIL: TestOT001_FFFDOnlyStringBypassesLogLengthLimit (0.00s)
=== RUN TestOT001_FFFDOnlyStringBypassesTraceLengthLimit
otelgo_truncate_test.go:61: input: 1000 chars / 3000 bytes; stored: 1000 chars / 3000 bytes; limit=5
otelgo_truncate_test.go:67: BYPASS: AttributeValueLengthLimit=5 but 3000 bytes stored (expected <=15)
--- FAIL: TestOT001_FFFDOnlyStringBypassesTraceLengthLimit (0.00s)
=== RUN TestOT001_FFFDMixedStringOverRetains
otelgo_truncate_test.go:81: input: 106 chars / 306 bytes; stored: 105 chars / 305 bytes; limit=5
otelgo_truncate_test.go:85: BYPASS: AttributeValueLengthLimit=5 but 105 chars / 305 bytes stored
--- FAIL: TestOT001_FFFDMixedStringOverRetains (0.00s)
=== RUN TestOT001_ASCIIControl
otelgo_truncate_test.go:98: input: 20 chars / 20 bytes; stored: 5 chars / 5 bytes; limit=5
--- PASS: TestOT001_ASCIIControl (0.00s)
FAIL
FAIL otelpoc 0.059s
FAIL
The mixed-string case stores 105 characters for a limit of 5: the 100 U+FFFD runes (300 uncounted bytes) are all retained, plus 5 of the 6 ASCII characters.
Impact
Applications that enable AttributeValueLengthLimit (or WithAttributeValueLengthLimit for logs) to bound attribute memory do not get that bound for values containing valid U+FFFD runes. Span and log-record attribute values are routinely derived from untrusted input (HTTP headers, request bodies, database fields), so an attacker can inflate per-span memory retention linearly, defeating the memory protection the operator explicitly enabled and accumulating increased memory usage over the span/log pipeline. Confidentiality and integrity are not affected.
Suggested fix
In the optimistic loop, count every rune of size > 1 (including the legitimately encoded U+FFFD) and only divert size-1 invalid bytes to the slow path. Add regression cases for: pure U+FFFD strings, U+FFFD-dominated mixed strings, and a truncation point landing after a U+FFFD (the current truncate_test.go has no U+FFFD case).