Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions internal/messagelifecycle/reconstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,7 @@ func reconstructedID(messageID, sourceKind, sourceID, recipient string, reason R
}

func bounded(value string) string {
if len(value) <= maxDiagnosticStringBytes {
return value
}
return value[:maxDiagnosticStringBytes]
return SafeDiagnostic(value)
}

func cloneTransition(item MessageLifecycleTransition) MessageLifecycleTransition {
Expand Down
24 changes: 24 additions & 0 deletions internal/messagelifecycle/reconstruct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"
"time"
"unicode/utf8"
)

var reconstructBaseTime = time.Date(2026, 7, 21, 12, 0, 0, 0, time.UTC)
Expand Down Expand Up @@ -181,6 +182,29 @@ func TestReconstructRecipientMappingsAndIgnoredStatuses(t *testing.T) {
}
}

func TestReconstructOversizedRecipientDetailIsBoundedAtAValidUTF8Boundary(t *testing.T) {
// A 3-byte rune repeated enough times that the byte cap (2048) lands
// mid-rune: 683*3 = 2049 bytes, and 2049%3 = 0 while 2048%3 = 2, so a raw
// byte slice at maxDiagnosticStringBytes always splits the last rune.
oversized := strings.Repeat("中", 683)
s := baseSnapshot("outbound", "smtp")
s.Recipients = []RecipientSnapshot{{ID: "rcp_1", Address: "a@example.com", Status: "bounced", Detail: oversized, UpdatedAt: reconstructBaseTime.Add(time.Minute)}}
got := findReason(Reconstruct(s), ReasonDeliveryUndeterminedBounce)
if got == nil {
t.Fatalf("recipient transition disappeared: %#v", Reconstruct(s))
}
detail, ok := got.Evidence["smtp_detail"].(string)
if !ok {
t.Fatalf("smtp_detail missing or not a string: %#v", got.Evidence)
}
if len(detail) > maxDiagnosticStringBytes {
t.Fatalf("smtp_detail exceeds the byte cap: len=%d", len(detail))
}
if !utf8.ValidString(detail) {
t.Fatalf("smtp_detail split a UTF-8 rune at the byte boundary: %q", detail)
}
}

func TestReconstructCausalSuppressionsOnly(t *testing.T) {
s := baseSnapshot("outbound", "smtp")
s.Suppressions = []SuppressionSnapshot{
Expand Down
Loading