Fix failing Complement tests (#1931)

* Check for missing state keys to avoid panicking

* Check for not allowed errors on send_leave

* More logging

* handle send_join errors too

* Additional send_join checks

* s/join/gmsl.json/
This commit is contained in:
kegsay 2021-07-19 13:15:19 +01:00 committed by GitHub
parent b954343d73
commit af64e648d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 19 deletions

View File

@ -26,6 +26,7 @@ import (
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
"github.com/sirupsen/logrus"
) )
// MakeJoin implements the /make_join API // MakeJoin implements the /make_join API
@ -228,6 +229,21 @@ func SendJoin(
} }
} }
// Check that this is in fact a join event
membership, err := event.Membership()
if err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("missing content.membership key"),
}
}
if membership != gomatrixserverlib.Join {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("membership must be 'join'"),
}
}
// Check that the event is signed by the server sending the request. // Check that the event is signed by the server sending the request.
redacted := event.Redact() redacted := event.Redact()
verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{ verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{
@ -296,16 +312,26 @@ func SendJoin(
// We are responsible for notifying other servers that the user has joined // We are responsible for notifying other servers that the user has joined
// the room, so set SendAsServer to cfg.Matrix.ServerName // the room, so set SendAsServer to cfg.Matrix.ServerName
if !alreadyJoined { if !alreadyJoined {
if err = api.SendEvents( var response api.InputRoomEventsResponse
httpReq.Context(), rsAPI, rsAPI.InputRoomEvents(httpReq.Context(), &api.InputRoomEventsRequest{
api.KindNew, InputRoomEvents: []api.InputRoomEvent{
[]*gomatrixserverlib.HeaderedEvent{ {
event.Headered(stateAndAuthChainResponse.RoomVersion), Kind: api.KindNew,
Event: event.Headered(stateAndAuthChainResponse.RoomVersion),
AuthEventIDs: event.AuthEventIDs(),
SendAsServer: string(cfg.Matrix.ServerName),
TransactionID: nil,
}, },
cfg.Matrix.ServerName, },
nil, }, &response)
); err != nil { if response.ErrMsg != "" {
util.GetLogger(httpReq.Context()).WithError(err).Error("SendEvents failed") util.GetLogger(httpReq.Context()).WithField(logrus.ErrorKey, response.ErrMsg).Error("SendEvents failed")
if response.NotAllowed {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.Forbidden(response.ErrMsg),
}
}
return jsonerror.InternalServerError() return jsonerror.InternalServerError()
} }
} }

View File

@ -22,6 +22,7 @@ import (
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
"github.com/sirupsen/logrus"
) )
// MakeLeave implements the /make_leave API // MakeLeave implements the /make_leave API
@ -174,6 +175,13 @@ func SendLeave(
} }
} }
if event.StateKey() == nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.InvalidArgumentValue("missing state_key"),
}
}
// Check if the user has already left. If so, no-op! // Check if the user has already left. If so, no-op!
queryReq := &api.QueryLatestEventsAndStateRequest{ queryReq := &api.QueryLatestEventsAndStateRequest{
RoomID: roomID, RoomID: roomID,
@ -240,7 +248,10 @@ func SendLeave(
mem, err := event.Membership() mem, err := event.Membership()
if err != nil { if err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("event.Membership failed") util.GetLogger(httpReq.Context()).WithError(err).Error("event.Membership failed")
return jsonerror.InternalServerError() return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("missing content.membership key"),
}
} }
if mem != gomatrixserverlib.Leave { if mem != gomatrixserverlib.Leave {
return util.JSONResponse{ return util.JSONResponse{
@ -252,16 +263,27 @@ func SendLeave(
// Send the events to the room server. // Send the events to the room server.
// We are responsible for notifying other servers that the user has left // We are responsible for notifying other servers that the user has left
// the room, so set SendAsServer to cfg.Matrix.ServerName // the room, so set SendAsServer to cfg.Matrix.ServerName
if err = api.SendEvents( var response api.InputRoomEventsResponse
httpReq.Context(), rsAPI, rsAPI.InputRoomEvents(httpReq.Context(), &api.InputRoomEventsRequest{
api.KindNew, InputRoomEvents: []api.InputRoomEvent{
[]*gomatrixserverlib.HeaderedEvent{ {
event.Headered(verRes.RoomVersion), Kind: api.KindNew,
Event: event.Headered(verRes.RoomVersion),
AuthEventIDs: event.AuthEventIDs(),
SendAsServer: string(cfg.Matrix.ServerName),
TransactionID: nil,
}, },
cfg.Matrix.ServerName, },
nil, }, &response)
); err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("producer.SendEvents failed") if response.ErrMsg != "" {
util.GetLogger(httpReq.Context()).WithField(logrus.ErrorKey, response.ErrMsg).WithField("not_allowed", response.NotAllowed).Error("producer.SendEvents failed")
if response.NotAllowed {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.Forbidden(response.ErrMsg),
}
}
return jsonerror.InternalServerError() return jsonerror.InternalServerError()
} }