Move json errors over to gmsl (#3080)

This commit is contained in:
devonh 2023-05-09 22:46:49 +00:00 committed by GitHub
parent a49c9f01e2
commit 0489d16f95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
109 changed files with 808 additions and 1217 deletions

View file

@ -27,7 +27,6 @@ import (
"github.com/matrix-org/util"
"github.com/sirupsen/logrus"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/internal/eventutil"
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/roomserver/types"
@ -47,7 +46,7 @@ func MakeJoin(
if err != nil {
return util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: jsonerror.InternalServerError(),
JSON: spec.InternalServerError(),
}
}
@ -66,7 +65,7 @@ func MakeJoin(
if !remoteSupportsVersion {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.IncompatibleRoomVersion(roomVersion),
JSON: spec.IncompatibleRoomVersion(string(roomVersion)),
}
}
@ -74,13 +73,13 @@ func MakeJoin(
if err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("Invalid UserID"),
JSON: spec.BadJSON("Invalid UserID"),
}
}
if domain != request.Origin() {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("The join must be sent by the server of the user"),
JSON: spec.Forbidden("The join must be sent by the server of the user"),
}
}
@ -92,18 +91,18 @@ func MakeJoin(
inRoomRes := &api.QueryServerJoinedToRoomResponse{}
if err = rsAPI.QueryServerJoinedToRoom(httpReq.Context(), inRoomReq, inRoomRes); err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("rsAPI.QueryServerJoinedToRoom failed")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
if !inRoomRes.RoomExists {
return util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound(fmt.Sprintf("Room ID %q was not found on this server", roomID)),
JSON: spec.NotFound(fmt.Sprintf("Room ID %q was not found on this server", roomID)),
}
}
if !inRoomRes.IsInRoom {
return util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound(fmt.Sprintf("Room ID %q has no remaining users on this server", roomID)),
JSON: spec.NotFound(fmt.Sprintf("Room ID %q has no remaining users on this server", roomID)),
}
}
@ -112,7 +111,7 @@ func MakeJoin(
res, authorisedVia, err := checkRestrictedJoin(httpReq, rsAPI, roomVersion, roomID, userID)
if err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("checkRestrictedJoin failed")
return jsonerror.InternalServerError()
return spec.InternalServerError()
} else if res != nil {
return *res
}
@ -130,14 +129,14 @@ func MakeJoin(
}
if err = proto.SetContent(content); err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("builder.SetContent failed")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
identity, err := cfg.Matrix.SigningIdentityFor(request.Destination())
if err != nil {
return util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound(
JSON: spec.NotFound(
fmt.Sprintf("Server name %q does not exist", request.Destination()),
),
}
@ -150,16 +149,16 @@ func MakeJoin(
if err == eventutil.ErrRoomNoExists {
return util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound("Room does not exist"),
JSON: spec.NotFound("Room does not exist"),
}
} else if e, ok := err.(gomatrixserverlib.BadJSONError); ok {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON(e.Error()),
JSON: spec.BadJSON(e.Error()),
}
} else if err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("eventutil.BuildEvent failed")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
// Check that the join is allowed or not
@ -172,7 +171,7 @@ func MakeJoin(
if err = gomatrixserverlib.Allowed(event.PDU, &provider); err != nil {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden(err.Error()),
JSON: spec.Forbidden(err.Error()),
}
}
@ -202,14 +201,14 @@ func SendJoin(
util.GetLogger(httpReq.Context()).WithError(err).Error("rsAPI.QueryRoomVersionForRoom failed")
return util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: jsonerror.InternalServerError(),
JSON: spec.InternalServerError(),
}
}
verImpl, err := gomatrixserverlib.GetRoomVersion(roomVersion)
if err != nil {
return util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: jsonerror.UnsupportedRoomVersion(
JSON: spec.UnsupportedRoomVersion(
fmt.Sprintf("QueryRoomVersionForRoom returned unknown room version: %s", roomVersion),
),
}
@ -219,7 +218,7 @@ func SendJoin(
if err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("The request body could not be decoded into valid JSON: " + err.Error()),
JSON: spec.BadJSON("The request body could not be decoded into valid JSON: " + err.Error()),
}
}
@ -227,13 +226,13 @@ func SendJoin(
if event.StateKey() == nil || event.StateKeyEquals("") {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("No state key was provided in the join event."),
JSON: spec.BadJSON("No state key was provided in the join event."),
}
}
if !event.StateKeyEquals(event.Sender()) {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("Event state key must match the event sender."),
JSON: spec.BadJSON("Event state key must match the event sender."),
}
}
@ -244,12 +243,12 @@ func SendJoin(
if _, serverName, err = gomatrixserverlib.SplitID('@', event.Sender()); err != nil {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("The sender of the join is invalid"),
JSON: spec.Forbidden("The sender of the join is invalid"),
}
} else if serverName != request.Origin() {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("The sender does not match the server that originated the request"),
JSON: spec.Forbidden("The sender does not match the server that originated the request"),
}
}
@ -257,7 +256,7 @@ func SendJoin(
if event.RoomID() != roomID {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON(
JSON: spec.BadJSON(
fmt.Sprintf(
"The room ID in the request path (%q) must match the room ID in the join event JSON (%q)",
roomID, event.RoomID(),
@ -270,7 +269,7 @@ func SendJoin(
if event.EventID() != eventID {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON(
JSON: spec.BadJSON(
fmt.Sprintf(
"The event ID in the request path (%q) must match the event ID in the join event JSON (%q)",
eventID, event.EventID(),
@ -284,13 +283,13 @@ func SendJoin(
if err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("missing content.membership key"),
JSON: spec.BadJSON("missing content.membership key"),
}
}
if membership != spec.Join {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("membership must be 'join'"),
JSON: spec.BadJSON("membership must be 'join'"),
}
}
@ -300,7 +299,7 @@ func SendJoin(
logrus.WithError(err).Errorf("XXX: join.go")
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("The event JSON could not be redacted"),
JSON: spec.BadJSON("The event JSON could not be redacted"),
}
}
verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{
@ -312,12 +311,12 @@ func SendJoin(
verifyResults, err := keys.VerifyJSONs(httpReq.Context(), verifyRequests)
if err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("keys.VerifyJSONs failed")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
if verifyResults[0].Error != nil {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("Signature check failed: " + verifyResults[0].Error.Error()),
JSON: spec.Forbidden("Signature check failed: " + verifyResults[0].Error.Error()),
}
}
@ -332,19 +331,19 @@ func SendJoin(
}, &stateAndAuthChainResponse)
if err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("rsAPI.QueryStateAndAuthChain failed")
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
if !stateAndAuthChainResponse.RoomExists {
return util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound("Room does not exist"),
JSON: spec.NotFound("Room does not exist"),
}
}
if !stateAndAuthChainResponse.StateKnown {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("State not known"),
JSON: spec.Forbidden("State not known"),
}
}
@ -367,7 +366,7 @@ func SendJoin(
if isBanned {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("user is banned"),
JSON: spec.Forbidden("user is banned"),
}
}
@ -377,7 +376,7 @@ func SendJoin(
if err := json.Unmarshal(event.Content(), &memberContent); err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON(err.Error()),
JSON: spec.BadJSON(err.Error()),
}
}
if memberContent.AuthorisedVia != "" {
@ -385,13 +384,13 @@ func SendJoin(
if err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON(fmt.Sprintf("The authorising username %q is invalid.", memberContent.AuthorisedVia)),
JSON: spec.BadJSON(fmt.Sprintf("The authorising username %q is invalid.", memberContent.AuthorisedVia)),
}
}
if domain != cfg.Matrix.ServerName {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON(fmt.Sprintf("The authorising username %q does not belong to this server.", memberContent.AuthorisedVia)),
JSON: spec.BadJSON(fmt.Sprintf("The authorising username %q does not belong to this server.", memberContent.AuthorisedVia)),
}
}
}
@ -410,7 +409,7 @@ func SendJoin(
// the room, so set SendAsServer to cfg.Matrix.ServerName
if !alreadyJoined {
var response api.InputRoomEventsResponse
if err := rsAPI.InputRoomEvents(httpReq.Context(), &api.InputRoomEventsRequest{
rsAPI.InputRoomEvents(httpReq.Context(), &api.InputRoomEventsRequest{
InputRoomEvents: []api.InputRoomEvent{
{
Kind: api.KindNew,
@ -419,18 +418,16 @@ func SendJoin(
TransactionID: nil,
},
},
}, &response); err != nil {
return jsonerror.InternalAPIError(httpReq.Context(), err)
}
}, &response)
if response.ErrMsg != "" {
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),
JSON: spec.Forbidden(response.ErrMsg),
}
}
return jsonerror.InternalServerError()
return spec.InternalServerError()
}
}
@ -498,7 +495,7 @@ func checkRestrictedJoin(
// instead.
return &util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.UnableToAuthoriseJoin("This server cannot authorise the join."),
JSON: spec.UnableToAuthoriseJoin("This server cannot authorise the join."),
}, "", nil
case !res.Allowed:
@ -507,7 +504,7 @@ func checkRestrictedJoin(
// and therefore can't join this room.
return &util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("You are not joined to any matching rooms."),
JSON: spec.Forbidden("You are not joined to any matching rooms."),
}, "", nil
default: