2017-09-08 16:17:12 +02:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-10-11 19:16:53 +02:00
|
|
|
package routing
|
2017-08-23 16:13:47 +02:00
|
|
|
|
|
|
|
import (
|
2020-06-25 18:07:40 +02:00
|
|
|
"context"
|
2020-04-03 15:29:06 +02:00
|
|
|
"encoding/json"
|
2020-04-24 17:30:25 +02:00
|
|
|
"fmt"
|
2017-08-23 16:13:47 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2020-06-10 13:17:54 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-04-24 17:30:25 +02:00
|
|
|
roomserverVersion "github.com/matrix-org/dendrite/roomserver/version"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2017-08-23 16:13:47 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-09-04 14:14:01 +02:00
|
|
|
"github.com/matrix-org/util"
|
2017-08-23 16:13:47 +02:00
|
|
|
)
|
|
|
|
|
2020-06-25 18:07:40 +02:00
|
|
|
// InviteV2 implements /_matrix/federation/v2/invite/{roomID}/{eventID}
|
|
|
|
func InviteV2(
|
2017-09-04 14:14:01 +02:00
|
|
|
httpReq *http.Request,
|
|
|
|
request *gomatrixserverlib.FederationRequest,
|
2017-08-23 16:13:47 +02:00
|
|
|
roomID string,
|
|
|
|
eventID string,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 20:30:38 +02:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2020-06-15 17:57:59 +02:00
|
|
|
keys gomatrixserverlib.JSONVerifier,
|
2017-08-23 16:13:47 +02:00
|
|
|
) util.JSONResponse {
|
2020-04-03 15:29:06 +02:00
|
|
|
inviteReq := gomatrixserverlib.InviteV2Request{}
|
2020-09-24 18:16:59 +02:00
|
|
|
err := json.Unmarshal(request.Content(), &inviteReq)
|
2021-07-26 11:41:58 +02:00
|
|
|
switch e := err.(type) {
|
|
|
|
case gomatrixserverlib.UnsupportedRoomVersionError:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.UnsupportedRoomVersion(
|
|
|
|
fmt.Sprintf("Room version %q is not supported by this server.", e.Version),
|
|
|
|
),
|
|
|
|
}
|
2020-09-24 18:16:59 +02:00
|
|
|
case gomatrixserverlib.BadJSONError:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON(err.Error()),
|
|
|
|
}
|
|
|
|
case nil:
|
2021-07-26 11:41:58 +02:00
|
|
|
return processInvite(
|
|
|
|
httpReq.Context(), true, inviteReq.Event(), inviteReq.RoomVersion(), inviteReq.InviteRoomState(), roomID, eventID, cfg, rsAPI, keys,
|
|
|
|
)
|
2020-09-24 18:16:59 +02:00
|
|
|
default:
|
2020-03-27 17:28:22 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2020-04-03 15:29:06 +02:00
|
|
|
JSON: jsonerror.NotJSON("The request body could not be decoded into an invite request. " + err.Error()),
|
2017-08-23 16:13:47 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-25 18:07:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// InviteV1 implements /_matrix/federation/v1/invite/{roomID}/{eventID}
|
|
|
|
func InviteV1(
|
|
|
|
httpReq *http.Request,
|
|
|
|
request *gomatrixserverlib.FederationRequest,
|
|
|
|
roomID string,
|
|
|
|
eventID string,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 20:30:38 +02:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2020-06-25 18:07:40 +02:00
|
|
|
keys gomatrixserverlib.JSONVerifier,
|
|
|
|
) util.JSONResponse {
|
|
|
|
roomVer := gomatrixserverlib.RoomVersionV1
|
|
|
|
body := request.Content()
|
|
|
|
event, err := gomatrixserverlib.NewEventFromTrustedJSON(body, false, roomVer)
|
2020-09-24 18:16:59 +02:00
|
|
|
switch err.(type) {
|
|
|
|
case gomatrixserverlib.BadJSONError:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON(err.Error()),
|
|
|
|
}
|
|
|
|
case nil:
|
|
|
|
default:
|
2020-06-25 18:07:40 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2020-09-24 18:16:59 +02:00
|
|
|
JSON: jsonerror.NotJSON("The request body could not be decoded into an invite v1 request. " + err.Error()),
|
2020-06-25 18:07:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var strippedState []gomatrixserverlib.InviteV2StrippedState
|
|
|
|
if err := json.Unmarshal(event.Unsigned(), &strippedState); err != nil {
|
|
|
|
// just warn, they may not have added any.
|
|
|
|
util.GetLogger(httpReq.Context()).Warnf("failed to extract stripped state from invite event")
|
|
|
|
}
|
|
|
|
return processInvite(
|
2020-08-17 12:40:49 +02:00
|
|
|
httpReq.Context(), false, event, roomVer, strippedState, roomID, eventID, cfg, rsAPI, keys,
|
2020-06-25 18:07:40 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func processInvite(
|
|
|
|
ctx context.Context,
|
2020-08-17 12:40:49 +02:00
|
|
|
isInviteV2 bool,
|
2020-11-16 16:44:53 +01:00
|
|
|
event *gomatrixserverlib.Event,
|
2020-06-25 18:07:40 +02:00
|
|
|
roomVer gomatrixserverlib.RoomVersion,
|
|
|
|
strippedState []gomatrixserverlib.InviteV2StrippedState,
|
|
|
|
roomID string,
|
|
|
|
eventID string,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 20:30:38 +02:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2020-06-25 18:07:40 +02:00
|
|
|
keys gomatrixserverlib.JSONVerifier,
|
|
|
|
) util.JSONResponse {
|
2017-08-23 16:13:47 +02:00
|
|
|
|
2020-04-24 17:30:25 +02:00
|
|
|
// Check that we can accept invites for this room version.
|
2020-06-25 18:07:40 +02:00
|
|
|
if _, err := roomserverVersion.SupportedRoomVersion(roomVer); err != nil {
|
2020-04-24 17:30:25 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.UnsupportedRoomVersion(
|
2020-06-25 18:07:40 +02:00
|
|
|
fmt.Sprintf("Room version %q is not supported by this server.", roomVer),
|
2020-04-24 17:30:25 +02:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 16:13:47 +02:00
|
|
|
// Check that the room ID is correct.
|
|
|
|
if event.RoomID() != roomID {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2017-08-23 16:13:47 +02:00
|
|
|
JSON: jsonerror.BadJSON("The room ID in the request path must match the room ID in the invite event JSON"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the event ID is correct.
|
|
|
|
if event.EventID() != eventID {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2017-08-23 16:13:47 +02:00
|
|
|
JSON: jsonerror.BadJSON("The event ID in the request path must match the event ID in the invite event JSON"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the event is signed by the server sending the request.
|
2022-07-11 15:31:31 +02:00
|
|
|
redacted, err := gomatrixserverlib.RedactEventJSON(event.JSON(), event.Version())
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON("The event JSON could not be redacted"),
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 18:35:35 +02:00
|
|
|
_, serverName, err := gomatrixserverlib.SplitID('@', event.Sender())
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON("The event JSON contains an invalid sender"),
|
|
|
|
}
|
|
|
|
}
|
2017-08-23 16:13:47 +02:00
|
|
|
verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{
|
2022-09-26 18:35:35 +02:00
|
|
|
ServerName: serverName,
|
2022-07-11 15:31:31 +02:00
|
|
|
Message: redacted,
|
2020-04-20 18:42:34 +02:00
|
|
|
AtTS: event.OriginServerTS(),
|
|
|
|
StrictValidityChecking: true,
|
2017-08-23 16:13:47 +02:00
|
|
|
}}
|
2020-06-25 18:07:40 +02:00
|
|
|
verifyResults, err := keys.VerifyJSONs(ctx, verifyRequests)
|
2017-08-23 16:13:47 +02:00
|
|
|
if err != nil {
|
2020-06-25 18:07:40 +02:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("keys.VerifyJSONs failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2017-08-23 16:13:47 +02:00
|
|
|
}
|
|
|
|
if verifyResults[0].Error != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusForbidden,
|
2017-08-23 16:13:47 +02:00
|
|
|
JSON: jsonerror.Forbidden("The invite must be signed by the server it originated on"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign the event so that other servers will know that we have received the invite.
|
|
|
|
signedEvent := event.Sign(
|
|
|
|
string(cfg.Matrix.ServerName), cfg.Matrix.KeyID, cfg.Matrix.PrivateKey,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add the invite event to the roomserver.
|
2022-05-05 14:17:38 +02:00
|
|
|
inviteEvent := signedEvent.Headered(roomVer)
|
|
|
|
request := &api.PerformInviteRequest{
|
|
|
|
Event: inviteEvent,
|
|
|
|
InviteRoomState: strippedState,
|
|
|
|
RoomVersion: inviteEvent.RoomVersion,
|
|
|
|
SendAsServer: string(api.DoNotSendToOtherServers),
|
|
|
|
TransactionID: nil,
|
|
|
|
}
|
|
|
|
response := &api.PerformInviteResponse{}
|
|
|
|
if err := rsAPI.PerformInvite(ctx, request, response); err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("PerformInvite failed")
|
2020-08-17 12:40:49 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: jsonerror.InternalServerError(),
|
|
|
|
}
|
2017-08-23 16:13:47 +02:00
|
|
|
}
|
2022-05-05 14:17:38 +02:00
|
|
|
if response.Error != nil {
|
|
|
|
return response.Error.JSONResponse()
|
|
|
|
}
|
|
|
|
// Return the signed event to the originating server, it should then tell
|
|
|
|
// the other servers in the room that we have been invited.
|
|
|
|
if isInviteV2 {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: gomatrixserverlib.RespInviteV2{Event: signedEvent.JSON()},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: gomatrixserverlib.RespInvite{Event: signedEvent.JSON()},
|
|
|
|
}
|
|
|
|
}
|
2017-08-23 16:13:47 +02:00
|
|
|
}
|