Skip to content

Commit 5f66df0

Browse files
committed
Enforce type, sender, state_key and room_id lengths using codepoints rather than bytes (matrix-org/matrix-spec#1001)
1 parent 828d8ad commit 5f66df0

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

event.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -721,29 +721,30 @@ func (e *Event) CheckFields() error { // nolint: gocyclo
721721
panic(e.invalidFieldType())
722722
}
723723

724-
if len(e.eventJSON) > maxEventLength {
724+
if l := len(e.eventJSON); l > maxEventLength {
725725
return EventValidationError{
726726
Code: EventValidationTooLarge,
727-
Message: fmt.Sprintf("gomatrixserverlib: event is too long, length %d > maximum %d", len(e.eventJSON), maxEventLength),
727+
Message: fmt.Sprintf("gomatrixserverlib: event is too long, length %d bytes > maximum %d bytes", l, maxEventLength),
728728
}
729729
}
730730

731-
if len(fields.Type) > maxIDLength {
731+
if l := len([]rune(fields.Type)); l > maxIDLength {
732732
return EventValidationError{
733733
Code: EventValidationTooLarge,
734-
Message: fmt.Sprintf("gomatrixserverlib: event type is too long, length %d > maximum %d", len(fields.Type), maxIDLength),
734+
Message: fmt.Sprintf("gomatrixserverlib: event type is too long, length %d codepoints > maximum %d codepoints", l, maxIDLength),
735735
}
736736
}
737737

738-
if fields.StateKey != nil && len(*fields.StateKey) > maxIDLength {
739-
return EventValidationError{
740-
Code: EventValidationTooLarge,
741-
Message: fmt.Sprintf("gomatrixserverlib: state key is too long, length %d > maximum %d", len(*fields.StateKey), maxIDLength),
738+
if fields.StateKey != nil {
739+
if l := len([]rune(*fields.StateKey)); l > maxIDLength {
740+
return EventValidationError{
741+
Code: EventValidationTooLarge,
742+
Message: fmt.Sprintf("gomatrixserverlib: state key is too long, length %d codepoints > maximum %d codepoints", l, maxIDLength),
743+
}
742744
}
743745
}
744746

745-
_, err := checkID(fields.RoomID, "room", '!')
746-
if err != nil {
747+
if _, err := checkID(fields.RoomID, "room", '!'); err != nil {
747748
return err
748749
}
749750

@@ -793,11 +794,11 @@ func checkID(id, kind string, sigil byte) (domain string, err error) {
793794
)
794795
return
795796
}
796-
if len(id) > maxIDLength {
797-
err = fmt.Errorf(
798-
"gomatrixserverlib: %s ID is too long, length %d > maximum %d",
799-
kind, len(id), maxIDLength,
800-
)
797+
if l := len([]rune(id)); l > maxIDLength {
798+
err = EventValidationError{
799+
Code: EventValidationTooLarge,
800+
Message: fmt.Sprintf("gomatrixserverlib: %s ID is too long, length %d codepoints > maximum %d codepoints", kind, l, maxIDLength),
801+
}
801802
return
802803
}
803804
return

0 commit comments

Comments
 (0)