Skip to content

Commit

Permalink
Enforce type, sender, state_key and room_id lengths using byt…
Browse files Browse the repository at this point in the history
…es rather than codepoints (#338)

This effectively reverts the change made in
[5f66df0](5f66df0)
to bytes instead of codepoints, since Synapse will now enforce the same
after matrix-org/synapse#13710.

History here is that Synapse originally calculated bytes in Python 2.x,
started counting codepoints in Python 3.x pretty much by accident and
then the spec was ambiguous after the fact (hence
matrix-org/matrix-spec#1001).

Rationale is that bytes are probably easier for implementations to
manage and less likely to generate huge indexes for client-side
databases (especially where limits might exist like LMDB).

cc @reivilibre
  • Loading branch information
neilalexander authored Oct 21, 2022
1 parent a72a83f commit 7c772f1
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,18 +727,18 @@ func (e *Event) CheckFields() error { // nolint: gocyclo
}
}

if l := len([]rune(fields.Type)); l > maxIDLength {
if l := len(fields.Type); l > maxIDLength {
return EventValidationError{
Code: EventValidationTooLarge,
Message: fmt.Sprintf("gomatrixserverlib: event type is too long, length %d codepoints > maximum %d codepoints", l, maxIDLength),
Message: fmt.Sprintf("gomatrixserverlib: event type is too long, length %d bytes > maximum %d bytes", l, maxIDLength),
}
}

if fields.StateKey != nil {
if l := len([]rune(*fields.StateKey)); l > maxIDLength {
if l := len(*fields.StateKey); l > maxIDLength {
return EventValidationError{
Code: EventValidationTooLarge,
Message: fmt.Sprintf("gomatrixserverlib: state key is too long, length %d codepoints > maximum %d codepoints", l, maxIDLength),
Message: fmt.Sprintf("gomatrixserverlib: state key is too long, length %d bytes > maximum %d bytes", l, maxIDLength),
}
}
}
Expand All @@ -765,10 +765,10 @@ func checkID(id, kind string, sigil byte) (err error) {
)
return
}
if l := len([]rune(id)); l > maxIDLength {
if l := len(id); l > maxIDLength {
err = EventValidationError{
Code: EventValidationTooLarge,
Message: fmt.Sprintf("gomatrixserverlib: %s ID is too long, length %d codepoints > maximum %d codepoints", kind, l, maxIDLength),
Message: fmt.Sprintf("gomatrixserverlib: %s ID is too long, length %d bytes > maximum %d bytes", kind, l, maxIDLength),
}
return
}
Expand Down

0 comments on commit 7c772f1

Please sign in to comment.