2017-09-08 09:17:12 -05: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 12:16:53 -05:00
|
|
|
package routing
|
2017-08-23 09:13:47 -05:00
|
|
|
|
|
|
|
import (
|
2020-06-25 11:07:40 -05:00
|
|
|
"context"
|
2020-04-03 08:29:06 -05:00
|
|
|
"encoding/json"
|
2020-04-24 10:30:25 -05:00
|
|
|
"fmt"
|
2017-08-23 09:13:47 -05:00
|
|
|
"net/http"
|
|
|
|
|
2023-04-28 10:46:01 -05:00
|
|
|
"github.com/getsentry/sentry-go"
|
2017-08-23 09:13:47 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2020-06-10 06:17:54 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2023-04-27 06:54:20 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2020-12-02 11:41:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2017-08-23 09:13:47 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 03:55:01 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2017-09-04 07:14:01 -05:00
|
|
|
"github.com/matrix-org/util"
|
2017-08-23 09:13:47 -05:00
|
|
|
)
|
|
|
|
|
2020-06-25 11:07:40 -05:00
|
|
|
// InviteV2 implements /_matrix/federation/v2/invite/{roomID}/{eventID}
|
|
|
|
func InviteV2(
|
2017-09-04 07:14:01 -05:00
|
|
|
httpReq *http.Request,
|
2023-04-19 09:50:33 -05:00
|
|
|
request *fclient.FederationRequest,
|
2017-08-23 09:13:47 -05:00
|
|
|
roomID string,
|
|
|
|
eventID string,
|
2020-08-10 08:18:04 -05:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 13:30:38 -05:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2020-06-15 10:57:59 -05:00
|
|
|
keys gomatrixserverlib.JSONVerifier,
|
2017-08-23 09:13:47 -05:00
|
|
|
) util.JSONResponse {
|
2023-04-19 09:50:33 -05:00
|
|
|
inviteReq := fclient.InviteV2Request{}
|
2020-09-24 11:16:59 -05:00
|
|
|
err := json.Unmarshal(request.Content(), &inviteReq)
|
2021-07-26 04:41:58 -05: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 11:16:59 -05:00
|
|
|
case gomatrixserverlib.BadJSONError:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON(err.Error()),
|
|
|
|
}
|
|
|
|
case nil:
|
2021-07-26 04:41:58 -05:00
|
|
|
return processInvite(
|
|
|
|
httpReq.Context(), true, inviteReq.Event(), inviteReq.RoomVersion(), inviteReq.InviteRoomState(), roomID, eventID, cfg, rsAPI, keys,
|
|
|
|
)
|
2020-09-24 11:16:59 -05:00
|
|
|
default:
|
2020-03-27 11:28:22 -05:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2020-04-03 08:29:06 -05:00
|
|
|
JSON: jsonerror.NotJSON("The request body could not be decoded into an invite request. " + err.Error()),
|
2017-08-23 09:13:47 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-25 11:07:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// InviteV1 implements /_matrix/federation/v1/invite/{roomID}/{eventID}
|
|
|
|
func InviteV1(
|
|
|
|
httpReq *http.Request,
|
2023-04-19 09:50:33 -05:00
|
|
|
request *fclient.FederationRequest,
|
2020-06-25 11:07:40 -05:00
|
|
|
roomID string,
|
|
|
|
eventID string,
|
2020-08-10 08:18:04 -05:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 13:30:38 -05:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2020-06-25 11:07:40 -05:00
|
|
|
keys gomatrixserverlib.JSONVerifier,
|
|
|
|
) util.JSONResponse {
|
|
|
|
roomVer := gomatrixserverlib.RoomVersionV1
|
|
|
|
body := request.Content()
|
2023-04-24 05:50:37 -05:00
|
|
|
// roomVer is hardcoded to v1 so we know we won't panic on Must
|
2023-04-21 11:06:29 -05:00
|
|
|
event, err := gomatrixserverlib.MustGetRoomVersion(roomVer).NewEventFromTrustedJSON(body, false)
|
2020-09-24 11:16:59 -05:00
|
|
|
switch err.(type) {
|
|
|
|
case gomatrixserverlib.BadJSONError:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON(err.Error()),
|
|
|
|
}
|
|
|
|
case nil:
|
|
|
|
default:
|
2020-06-25 11:07:40 -05:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2020-09-24 11:16:59 -05:00
|
|
|
JSON: jsonerror.NotJSON("The request body could not be decoded into an invite v1 request. " + err.Error()),
|
2020-06-25 11:07:40 -05:00
|
|
|
}
|
|
|
|
}
|
2023-04-19 09:50:33 -05:00
|
|
|
var strippedState []fclient.InviteV2StrippedState
|
2020-06-25 11:07:40 -05:00
|
|
|
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 05:40:49 -05:00
|
|
|
httpReq.Context(), false, event, roomVer, strippedState, roomID, eventID, cfg, rsAPI, keys,
|
2020-06-25 11:07:40 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func processInvite(
|
|
|
|
ctx context.Context,
|
2020-08-17 05:40:49 -05:00
|
|
|
isInviteV2 bool,
|
2020-11-16 09:44:53 -06:00
|
|
|
event *gomatrixserverlib.Event,
|
2020-06-25 11:07:40 -05:00
|
|
|
roomVer gomatrixserverlib.RoomVersion,
|
2023-04-19 09:50:33 -05:00
|
|
|
strippedState []fclient.InviteV2StrippedState,
|
2020-06-25 11:07:40 -05:00
|
|
|
roomID string,
|
|
|
|
eventID string,
|
2020-08-10 08:18:04 -05:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 13:30:38 -05:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2020-06-25 11:07:40 -05:00
|
|
|
keys gomatrixserverlib.JSONVerifier,
|
|
|
|
) util.JSONResponse {
|
2017-08-23 09:13:47 -05:00
|
|
|
|
2020-04-24 10:30:25 -05:00
|
|
|
// Check that we can accept invites for this room version.
|
2023-04-21 11:06:29 -05:00
|
|
|
verImpl, err := gomatrixserverlib.GetRoomVersion(roomVer)
|
|
|
|
if err != nil {
|
2020-04-24 10:30:25 -05:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.UnsupportedRoomVersion(
|
2020-06-25 11:07:40 -05:00
|
|
|
fmt.Sprintf("Room version %q is not supported by this server.", roomVer),
|
2020-04-24 10:30:25 -05:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 09:13:47 -05:00
|
|
|
// Check that the room ID is correct.
|
|
|
|
if event.RoomID() != roomID {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2017-08-23 09:13:47 -05: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 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2017-08-23 09:13:47 -05:00
|
|
|
JSON: jsonerror.BadJSON("The event ID in the request path must match the event ID in the invite event JSON"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:05:23 -06:00
|
|
|
if event.StateKey() == nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON("The invite event has no state key"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, domain, err := cfg.Matrix.SplitLocalID('@', *event.StateKey())
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.InvalidArgumentValue(fmt.Sprintf("The user ID is invalid or domain %q does not belong to this server", domain)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 09:13:47 -05:00
|
|
|
// Check that the event is signed by the server sending the request.
|
2023-04-21 11:06:29 -05:00
|
|
|
redacted, err := verImpl.RedactEventJSON(event.JSON())
|
2022-07-11 08:31:31 -05:00
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON("The event JSON could not be redacted"),
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 11:35:35 -05: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 09:13:47 -05:00
|
|
|
verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{
|
2022-09-26 11:35:35 -05:00
|
|
|
ServerName: serverName,
|
2022-07-11 08:31:31 -05:00
|
|
|
Message: redacted,
|
2020-04-20 11:42:34 -05:00
|
|
|
AtTS: event.OriginServerTS(),
|
|
|
|
StrictValidityChecking: true,
|
2017-08-23 09:13:47 -05:00
|
|
|
}}
|
2020-06-25 11:07:40 -05:00
|
|
|
verifyResults, err := keys.VerifyJSONs(ctx, verifyRequests)
|
2017-08-23 09:13:47 -05:00
|
|
|
if err != nil {
|
2020-06-25 11:07:40 -05:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("keys.VerifyJSONs failed")
|
2020-03-02 10:20:44 -06:00
|
|
|
return jsonerror.InternalServerError()
|
2017-08-23 09:13:47 -05:00
|
|
|
}
|
|
|
|
if verifyResults[0].Error != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusForbidden,
|
2017-08-23 09:13:47 -05: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(
|
2022-11-15 09:05:23 -06:00
|
|
|
string(domain), cfg.Matrix.KeyID, cfg.Matrix.PrivateKey,
|
2017-08-23 09:13:47 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Add the invite event to the roomserver.
|
2023-04-27 06:54:20 -05:00
|
|
|
inviteEvent := &types.HeaderedEvent{Event: &signedEvent}
|
2022-05-05 07:17:38 -05:00
|
|
|
request := &api.PerformInviteRequest{
|
|
|
|
Event: inviteEvent,
|
|
|
|
InviteRoomState: strippedState,
|
2023-04-27 06:54:20 -05:00
|
|
|
RoomVersion: inviteEvent.Version(),
|
2022-05-05 07:17:38 -05:00
|
|
|
SendAsServer: string(api.DoNotSendToOtherServers),
|
|
|
|
TransactionID: nil,
|
|
|
|
}
|
2023-04-28 10:46:01 -05:00
|
|
|
|
|
|
|
if err = rsAPI.PerformInvite(ctx, request); err != nil {
|
2022-05-05 07:17:38 -05:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("PerformInvite failed")
|
2020-08-17 05:40:49 -05:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: jsonerror.InternalServerError(),
|
|
|
|
}
|
2017-08-23 09:13:47 -05:00
|
|
|
}
|
2023-04-28 10:46:01 -05:00
|
|
|
|
|
|
|
switch e := err.(type) {
|
|
|
|
case api.ErrInvalidID:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.Unknown(e.Error()),
|
|
|
|
}
|
|
|
|
case api.ErrNotAllowed:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
JSON: jsonerror.Forbidden(e.Error()),
|
|
|
|
}
|
|
|
|
case nil:
|
|
|
|
default:
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("PerformInvite failed")
|
|
|
|
sentry.CaptureException(err)
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: jsonerror.InternalServerError(),
|
|
|
|
}
|
2022-05-05 07:17:38 -05:00
|
|
|
}
|
2023-04-28 10:46:01 -05:00
|
|
|
|
2022-05-05 07:17:38 -05:00
|
|
|
// 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,
|
2023-04-06 03:55:01 -05:00
|
|
|
JSON: fclient.RespInviteV2{Event: signedEvent.JSON()},
|
2022-05-05 07:17:38 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
2023-04-06 03:55:01 -05:00
|
|
|
JSON: fclient.RespInvite{Event: signedEvent.JSON()},
|
2022-05-05 07:17:38 -05:00
|
|
|
}
|
|
|
|
}
|
2017-08-23 09:13:47 -05:00
|
|
|
}
|