Sanity-check room version on RS event input

This commit is contained in:
Neil Alexander 2020-09-24 10:14:19 +01:00
parent 60524f4b99
commit 1ed1d7ae03
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
2 changed files with 39 additions and 0 deletions

View file

@ -0,0 +1,28 @@
package helpers
import (
"encoding/json"
"fmt"
"github.com/matrix-org/gomatrixserverlib"
)
// SanityCheckEvent looks for any obvious problems with the event before
// we bother to continue processing it any further.
func SanityCheckEvent(event *gomatrixserverlib.Event) error {
switch event.Type() {
case gomatrixserverlib.MRoomCreate:
var content gomatrixserverlib.CreateContent
if err := json.Unmarshal(event.Content(), &content); err != nil {
return fmt.Errorf("Failed to unmarshal content of create event %q", event.EventID())
}
// Check that the room version is supported.
if content.RoomVersion != nil {
if _, err := content.RoomVersion.EventFormat(); err != nil {
return fmt.Errorf("Room version %q is unsupported in create event %q", *content.RoomVersion, event.EventID())
}
}
}
return nil
}

View file

@ -44,6 +44,17 @@ func (r *Inputer) processRoomEvent(
headered := input.Event
event := headered.Unwrap()
// Run sanity checks against the event. This will catch any really
// obvious problems.
if err = helpers.SanityCheckEvent(&event); err != nil {
logrus.WithFields(logrus.Fields{
"event_id": event.EventID(),
"type": event.Type(),
"room": event.RoomID(),
}).WithError(err).Info("Event failed sanity-checks")
return
}
// Check that the event passes authentication checks and work out
// the numeric IDs for the auth events.
isRejected := false