diff --git a/clientapi/routing/joinroom.go b/clientapi/routing/joinroom.go index 432fabe41..09e454bca 100644 --- a/clientapi/routing/joinroom.go +++ b/clientapi/routing/joinroom.go @@ -331,7 +331,7 @@ func (r joinRoomReq) joinRoomUsingServer(roomID string, server gomatrixserverlib if respMakeJoin.RoomVersion == "" { respMakeJoin.RoomVersion = gomatrixserverlib.RoomVersionV1 } - if _, err := respMakeJoin.RoomVersion.EventFormat(); err != nil { + if _, err = respMakeJoin.RoomVersion.EventFormat(); err != nil { return &util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.UnsupportedRoomVersion( diff --git a/common/events.go b/common/events.go index 159569fdd..c0189b469 100644 --- a/common/events.go +++ b/common/events.go @@ -102,17 +102,6 @@ func AddPrevEventsToEvent( builder.Depth = queryRes.Depth - switch eventFormat { - case gomatrixserverlib.EventFormatV1: - builder.PrevEvents = queryRes.LatestEvents - case gomatrixserverlib.EventFormatV2: - v2Refs := []string{} - for _, ref := range queryRes.LatestEvents { - v2Refs = append(v2Refs, ref.EventID) - } - builder.PrevEvents = v2Refs - } - authEvents := gomatrixserverlib.NewAuthEvents(nil) for i := range queryRes.StateEvents { @@ -126,15 +115,22 @@ func AddPrevEventsToEvent( if err != nil { return err } + switch eventFormat { case gomatrixserverlib.EventFormatV1: builder.AuthEvents = refs + builder.PrevEvents = queryRes.LatestEvents case gomatrixserverlib.EventFormatV2: - v2Refs := []string{} + v2AuthRefs := []string{} + v2PrevRefs := []string{} for _, ref := range refs { - v2Refs = append(v2Refs, ref.EventID) + v2AuthRefs = append(v2AuthRefs, ref.EventID) } - builder.AuthEvents = v2Refs + for _, ref := range queryRes.LatestEvents { + v2PrevRefs = append(v2PrevRefs, ref.EventID) + } + builder.AuthEvents = v2AuthRefs + builder.PrevEvents = v2PrevRefs } return nil diff --git a/federationapi/routing/invite.go b/federationapi/routing/invite.go index 9713b7229..09c3734be 100644 --- a/federationapi/routing/invite.go +++ b/federationapi/routing/invite.go @@ -15,12 +15,13 @@ package routing import ( - "encoding/json" + "context" "net/http" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/producers" "github.com/matrix-org/dendrite/common/config" + "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" ) @@ -35,10 +36,19 @@ func Invite( producer *producers.RoomserverProducer, keys gomatrixserverlib.KeyRing, ) util.JSONResponse { + // Look up the room version for the room. + verReq := api.QueryRoomVersionForRoomRequest{RoomID: roomID} + verRes := api.QueryRoomVersionForRoomResponse{} + if err := producer.QueryAPI.QueryRoomVersionForRoom(context.Background(), &verReq, &verRes); err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.UnsupportedRoomVersion(err.Error()), + } + } // Decode the event JSON from the request. - var event gomatrixserverlib.Event - if err := json.Unmarshal(request.Content(), &event); err != nil { + event, err := gomatrixserverlib.NewEventFromUntrustedJSON(request.Content(), verRes.RoomVersion) + if err != nil { return util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.NotJSON("The request body could not be decoded into valid JSON. " + err.Error()), diff --git a/federationapi/routing/leave.go b/federationapi/routing/leave.go index 888683dd9..e0a142631 100644 --- a/federationapi/routing/leave.go +++ b/federationapi/routing/leave.go @@ -13,7 +13,6 @@ package routing import ( - "encoding/json" "net/http" "time" @@ -110,8 +109,9 @@ func SendLeave( } } - var event gomatrixserverlib.Event - if err := json.Unmarshal(request.Content(), &event); err != nil { + // Decode the event JSON from the request. + event, err := gomatrixserverlib.NewEventFromUntrustedJSON(request.Content(), verRes.RoomVersion) + if err != nil { return util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.NotJSON("The request body could not be decoded into valid JSON. " + err.Error()), diff --git a/federationapi/routing/send.go b/federationapi/routing/send.go index e829a38d7..709ac0a3b 100644 --- a/federationapi/routing/send.go +++ b/federationapi/routing/send.go @@ -48,7 +48,7 @@ func Send( } var txnEvents struct { - events []json.RawMessage `json:"events"` + Events []json.RawMessage `json:"events"` } if err := json.Unmarshal(request.Content(), &txnEvents); err != nil { diff --git a/federationapi/routing/threepid.go b/federationapi/routing/threepid.go index d17470eae..ff0473f95 100644 --- a/federationapi/routing/threepid.go +++ b/federationapi/routing/threepid.go @@ -149,7 +149,7 @@ func ExchangeThirdPartyInvite( verReq := api.QueryRoomVersionForRoomRequest{RoomID: roomID} verRes := api.QueryRoomVersionForRoomResponse{} - if err := queryAPI.QueryRoomVersionForRoom(httpReq.Context(), &verReq, &verRes); err != nil { + if err = queryAPI.QueryRoomVersionForRoom(httpReq.Context(), &verReq, &verRes); err != nil { return util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.UnsupportedRoomVersion(err.Error()),