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: 5 additions & 0 deletions eventstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"fmt"
"io"
"unicode/utf8"
)

// Event is a typed message published to the event stream.
Expand Down Expand Up @@ -58,5 +59,9 @@ func ReadEvent(r io.Reader) (*Event, error) {
return nil, err
}

if !utf8.Valid(topic) {
return nil, fmt.Errorf("topic contains invalid UTF-8")
}

return &Event{Topic: string(topic), Payload: payload}, nil
}
16 changes: 16 additions & 0 deletions zz_event_wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,19 @@ func TestEventPayloadTooLarge(t *testing.T) {
t.Fatal("expected error for oversized payload, got nil")
}
}

func TestEventTopicInvalidUTF8(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
// Craft topic with invalid UTF-8: byte 0xFF is never valid
invalidTopic := []byte{0x48, 0xFF, 0x69} // H + invalid + i
binary.Write(&buf, binary.BigEndian, uint16(len(invalidTopic)))
buf.Write(invalidTopic)
// Valid payload
binary.Write(&buf, binary.BigEndian, uint32(0))

_, err := eventstream.ReadEvent(&buf)
if err == nil {
t.Fatal("expected error for invalid UTF-8 topic, got nil")
}
}
Loading