2017-09-08 14:17:12 +00: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 17:16:53 +00:00
|
|
|
package routing
|
2017-06-07 13:32:53 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 10:03:41 +00:00
|
|
|
"context"
|
2017-06-07 13:32:53 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-08-23 14:13:47 +00:00
|
|
|
"net/http"
|
|
|
|
|
2017-06-07 13:32:53 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
2017-06-19 14:21:04 +00:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2017-06-07 13:32:53 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Send implements /_matrix/federation/v1/send/{txnID}
|
|
|
|
func Send(
|
2017-09-04 12:14:01 +00:00
|
|
|
httpReq *http.Request,
|
|
|
|
request *gomatrixserverlib.FederationRequest,
|
2017-06-07 13:32:53 +00:00
|
|
|
txnID gomatrixserverlib.TransactionID,
|
2020-02-11 11:18:12 +00:00
|
|
|
cfg *config.Dendrite,
|
2020-05-01 09:48:17 +00:00
|
|
|
rsAPI api.RoomserverInternalAPI,
|
2017-06-07 13:32:53 +00:00
|
|
|
producer *producers.RoomserverProducer,
|
2020-03-30 15:40:28 +00:00
|
|
|
eduProducer *producers.EDUServerProducer,
|
2017-06-07 13:32:53 +00:00
|
|
|
keys gomatrixserverlib.KeyRing,
|
|
|
|
federation *gomatrixserverlib.FederationClient,
|
|
|
|
) util.JSONResponse {
|
|
|
|
t := txnReq{
|
2020-03-30 15:40:28 +00:00
|
|
|
context: httpReq.Context(),
|
2020-05-01 09:48:17 +00:00
|
|
|
rsAPI: rsAPI,
|
2020-03-30 15:40:28 +00:00
|
|
|
producer: producer,
|
|
|
|
eduProducer: eduProducer,
|
|
|
|
keys: keys,
|
|
|
|
federation: federation,
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
2020-03-27 16:28:22 +00:00
|
|
|
|
|
|
|
var txnEvents struct {
|
2020-03-30 15:40:28 +00:00
|
|
|
PDUs []json.RawMessage `json:"pdus"`
|
|
|
|
EDUs []gomatrixserverlib.EDU `json:"edus"`
|
2020-03-27 16:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(request.Content(), &txnEvents); err != nil {
|
2017-06-07 13:32:53 +00:00
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusBadRequest,
|
2017-08-23 14:13:47 +00:00
|
|
|
JSON: jsonerror.NotJSON("The request body could not be decoded into valid JSON. " + err.Error()),
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:40:28 +00:00
|
|
|
// TODO: Really we should have a function to convert FederationRequest to txnReq
|
2020-03-27 16:28:22 +00:00
|
|
|
t.PDUs = txnEvents.PDUs
|
2020-03-30 15:40:28 +00:00
|
|
|
t.EDUs = txnEvents.EDUs
|
2017-06-07 13:32:53 +00:00
|
|
|
t.Origin = request.Origin()
|
|
|
|
t.TransactionID = txnID
|
2017-06-19 14:21:04 +00:00
|
|
|
t.Destination = cfg.Matrix.ServerName
|
2017-06-07 13:32:53 +00:00
|
|
|
|
2020-03-27 16:28:22 +00:00
|
|
|
util.GetLogger(httpReq.Context()).Infof("Received transaction %q containing %d PDUs, %d EDUs", txnID, len(t.PDUs), len(t.EDUs))
|
|
|
|
|
2017-06-07 13:32:53 +00:00
|
|
|
resp, err := t.processTransaction()
|
2020-04-16 16:59:55 +00:00
|
|
|
switch err.(type) {
|
|
|
|
// No error? Great! Send back a 200.
|
|
|
|
case nil:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: resp,
|
|
|
|
}
|
|
|
|
// Handle known error cases as we will return a 400 error for these.
|
|
|
|
case roomNotFoundError:
|
|
|
|
case unmarshalError:
|
|
|
|
case verifySigError:
|
|
|
|
// Handle unknown error cases. Sending 500 errors back should be a last
|
|
|
|
// resort as this can make other homeservers back off sending federation
|
|
|
|
// events.
|
|
|
|
default:
|
2020-03-02 16:20:44 +00:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("t.processTransaction failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
2020-04-16 16:59:55 +00:00
|
|
|
// Return a 400 error for bad requests as fallen through from above.
|
2017-06-07 13:32:53 +00:00
|
|
|
return util.JSONResponse{
|
2020-04-16 16:59:55 +00:00
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON(err.Error()),
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type txnReq struct {
|
|
|
|
gomatrixserverlib.Transaction
|
2020-03-30 15:40:28 +00:00
|
|
|
context context.Context
|
2020-05-01 09:48:17 +00:00
|
|
|
rsAPI api.RoomserverInternalAPI
|
2020-03-30 15:40:28 +00:00
|
|
|
producer *producers.RoomserverProducer
|
|
|
|
eduProducer *producers.EDUServerProducer
|
|
|
|
keys gomatrixserverlib.KeyRing
|
|
|
|
federation *gomatrixserverlib.FederationClient
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *txnReq) processTransaction() (*gomatrixserverlib.RespSend, error) {
|
2020-04-16 16:59:55 +00:00
|
|
|
results := make(map[string]gomatrixserverlib.PDUResult)
|
|
|
|
|
2020-03-27 16:28:22 +00:00
|
|
|
var pdus []gomatrixserverlib.HeaderedEvent
|
|
|
|
for _, pdu := range t.PDUs {
|
|
|
|
var header struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(pdu, &header); err != nil {
|
|
|
|
util.GetLogger(t.context).WithError(err).Warn("Transaction: Failed to extract room ID from event")
|
2020-04-16 16:59:55 +00:00
|
|
|
return nil, unmarshalError{err}
|
2020-03-27 16:28:22 +00:00
|
|
|
}
|
|
|
|
verReq := api.QueryRoomVersionForRoomRequest{RoomID: header.RoomID}
|
|
|
|
verRes := api.QueryRoomVersionForRoomResponse{}
|
2020-05-01 09:48:17 +00:00
|
|
|
if err := t.rsAPI.QueryRoomVersionForRoom(t.context, &verReq, &verRes); err != nil {
|
2020-03-27 16:28:22 +00:00
|
|
|
util.GetLogger(t.context).WithError(err).Warn("Transaction: Failed to query room version for room", verReq.RoomID)
|
2020-04-16 16:59:55 +00:00
|
|
|
return nil, roomNotFoundError{verReq.RoomID}
|
2020-03-27 16:28:22 +00:00
|
|
|
}
|
|
|
|
event, err := gomatrixserverlib.NewEventFromUntrustedJSON(pdu, verRes.RoomVersion)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(t.context).WithError(err).Warnf("Transaction: Failed to parse event JSON of event %q", event.EventID())
|
2020-04-16 16:59:55 +00:00
|
|
|
return nil, unmarshalError{err}
|
2020-03-27 16:28:22 +00:00
|
|
|
}
|
|
|
|
if err := gomatrixserverlib.VerifyAllEventSignatures(t.context, []gomatrixserverlib.Event{event}, t.keys); err != nil {
|
|
|
|
util.GetLogger(t.context).WithError(err).Warnf("Transaction: Couldn't validate signature of event %q", event.EventID())
|
2020-04-16 16:59:55 +00:00
|
|
|
return nil, verifySigError{event.EventID(), err}
|
2020-03-27 16:28:22 +00:00
|
|
|
}
|
|
|
|
pdus = append(pdus, event.Headered(verRes.RoomVersion))
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process the events.
|
2020-03-27 16:28:22 +00:00
|
|
|
for _, e := range pdus {
|
|
|
|
err := t.processEvent(e.Unwrap())
|
2017-06-07 13:32:53 +00:00
|
|
|
if err != nil {
|
|
|
|
// If the error is due to the event itself being bad then we skip
|
|
|
|
// it and move onto the next event. We report an error so that the
|
|
|
|
// sender knows that we have skipped processing it.
|
|
|
|
//
|
|
|
|
// However if the event is due to a temporary failure in our server
|
|
|
|
// such as a database being unavailable then we should bail, and
|
|
|
|
// hope that the sender will retry when we are feeling better.
|
|
|
|
//
|
|
|
|
// It is uncertain what we should do if an event fails because
|
|
|
|
// we failed to fetch more information from the sending server.
|
|
|
|
// For example if a request to /state fails.
|
|
|
|
// If we skip the event then we risk missing the event until we
|
|
|
|
// receive another event referencing it.
|
|
|
|
// If we bail and stop processing then we risk wedging incoming
|
|
|
|
// transactions from that server forever.
|
|
|
|
switch err.(type) {
|
2020-04-16 16:59:55 +00:00
|
|
|
case roomNotFoundError:
|
2017-06-07 13:32:53 +00:00
|
|
|
case *gomatrixserverlib.NotAllowed:
|
|
|
|
default:
|
|
|
|
// Any other error should be the result of a temporary error in
|
|
|
|
// our server so we should bail processing the transaction entirely.
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-13 10:03:41 +00:00
|
|
|
results[e.EventID()] = gomatrixserverlib.PDUResult{
|
|
|
|
Error: err.Error(),
|
|
|
|
}
|
2020-03-16 17:51:58 +00:00
|
|
|
util.GetLogger(t.context).WithError(err).WithField("event_id", e.EventID()).Warn("Failed to process incoming federation event, skipping it.")
|
2017-06-07 13:32:53 +00:00
|
|
|
} else {
|
|
|
|
results[e.EventID()] = gomatrixserverlib.PDUResult{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:40:28 +00:00
|
|
|
t.processEDUs(t.EDUs)
|
2020-03-27 16:28:22 +00:00
|
|
|
util.GetLogger(t.context).Infof("Processed %d PDUs from transaction %q", len(results), t.TransactionID)
|
2017-06-07 13:32:53 +00:00
|
|
|
return &gomatrixserverlib.RespSend{PDUs: results}, nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:59:55 +00:00
|
|
|
type roomNotFoundError struct {
|
2017-06-07 13:32:53 +00:00
|
|
|
roomID string
|
|
|
|
}
|
2020-04-16 16:59:55 +00:00
|
|
|
type unmarshalError struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
type verifySigError struct {
|
|
|
|
eventID string
|
|
|
|
err error
|
|
|
|
}
|
2017-06-07 13:32:53 +00:00
|
|
|
|
2020-04-16 16:59:55 +00:00
|
|
|
func (e roomNotFoundError) Error() string { return fmt.Sprintf("room %q not found", e.roomID) }
|
|
|
|
func (e unmarshalError) Error() string { return fmt.Sprintf("unable to parse event: %s", e.err) }
|
|
|
|
func (e verifySigError) Error() string {
|
|
|
|
return fmt.Sprintf("unable to verify signature of event %q: %s", e.eventID, e.err)
|
|
|
|
}
|
2017-06-07 13:32:53 +00:00
|
|
|
|
2020-03-30 15:40:28 +00:00
|
|
|
func (t *txnReq) processEDUs(edus []gomatrixserverlib.EDU) {
|
|
|
|
for _, e := range edus {
|
|
|
|
switch e.Type {
|
|
|
|
case gomatrixserverlib.MTyping:
|
|
|
|
// https://matrix.org/docs/spec/server_server/latest#typing-notifications
|
|
|
|
var typingPayload struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
Typing bool `json:"typing"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(e.Content, &typingPayload); err != nil {
|
|
|
|
util.GetLogger(t.context).WithError(err).Error("Failed to unmarshal typing event")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := t.eduProducer.SendTyping(t.context, typingPayload.UserID, typingPayload.RoomID, typingPayload.Typing, 30*1000); err != nil {
|
|
|
|
util.GetLogger(t.context).WithError(err).Error("Failed to send typing event to edu server")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
util.GetLogger(t.context).WithField("type", e.Type).Warn("unhandled edu")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:32:53 +00:00
|
|
|
func (t *txnReq) processEvent(e gomatrixserverlib.Event) error {
|
2017-07-07 13:11:32 +00:00
|
|
|
prevEventIDs := e.PrevEventIDs()
|
2017-06-07 13:32:53 +00:00
|
|
|
|
|
|
|
// Fetch the state needed to authenticate the event.
|
|
|
|
needed := gomatrixserverlib.StateNeededForAuth([]gomatrixserverlib.Event{e})
|
|
|
|
stateReq := api.QueryStateAfterEventsRequest{
|
|
|
|
RoomID: e.RoomID(),
|
|
|
|
PrevEventIDs: prevEventIDs,
|
|
|
|
StateToFetch: needed.Tuples(),
|
|
|
|
}
|
|
|
|
var stateResp api.QueryStateAfterEventsResponse
|
2020-05-01 09:48:17 +00:00
|
|
|
if err := t.rsAPI.QueryStateAfterEvents(t.context, &stateReq, &stateResp); err != nil {
|
2017-06-07 13:32:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stateResp.RoomExists {
|
|
|
|
// TODO: When synapse receives a message for a room it is not in it
|
|
|
|
// asks the remote server for the state of the room so that it can
|
|
|
|
// check if the remote server knows of a join "m.room.member" event
|
|
|
|
// that this server is unaware of.
|
|
|
|
// However generally speaking we should reject events for rooms we
|
|
|
|
// aren't a member of.
|
2020-04-16 16:59:55 +00:00
|
|
|
return roomNotFoundError{e.RoomID()}
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !stateResp.PrevEventsExist {
|
2020-03-27 16:28:22 +00:00
|
|
|
return t.processEventWithMissingState(e, stateResp.RoomVersion)
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the event is allowed by the state at the event.
|
2020-03-16 17:29:52 +00:00
|
|
|
var events []gomatrixserverlib.Event
|
|
|
|
for _, headeredEvent := range stateResp.StateEvents {
|
2020-03-27 16:28:22 +00:00
|
|
|
events = append(events, headeredEvent.Unwrap())
|
2020-03-16 17:29:52 +00:00
|
|
|
}
|
|
|
|
if err := checkAllowedByState(e, events); err != nil {
|
2017-06-07 13:32:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Check that the roomserver has a copy of all of the auth_events.
|
|
|
|
// TODO: Check that the event is allowed by its auth_events.
|
|
|
|
|
|
|
|
// pass the event to the roomserver
|
2020-03-27 16:28:22 +00:00
|
|
|
_, err := t.producer.SendEvents(
|
|
|
|
t.context,
|
|
|
|
[]gomatrixserverlib.HeaderedEvent{
|
|
|
|
e.Headered(stateResp.RoomVersion),
|
|
|
|
},
|
|
|
|
api.DoNotSendToOtherServers,
|
|
|
|
nil,
|
|
|
|
)
|
2018-05-26 11:03:35 +00:00
|
|
|
return err
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkAllowedByState(e gomatrixserverlib.Event, stateEvents []gomatrixserverlib.Event) error {
|
|
|
|
authUsingState := gomatrixserverlib.NewAuthEvents(nil)
|
|
|
|
for i := range stateEvents {
|
2017-09-20 09:59:19 +00:00
|
|
|
err := authUsingState.AddEvent(&stateEvents[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|
|
|
|
return gomatrixserverlib.Allowed(e, &authUsingState)
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:28:22 +00:00
|
|
|
func (t *txnReq) processEventWithMissingState(e gomatrixserverlib.Event, roomVersion gomatrixserverlib.RoomVersion) error {
|
2017-06-07 13:32:53 +00:00
|
|
|
// We are missing the previous events for this events.
|
|
|
|
// This means that there is a gap in our view of the history of the
|
|
|
|
// room. There two ways that we can handle such a gap:
|
|
|
|
// 1) We can fill in the gap using /get_missing_events
|
|
|
|
// 2) We can leave the gap and request the state of the room at
|
|
|
|
// this event from the remote server using either /state_ids
|
|
|
|
// or /state.
|
|
|
|
// Synapse will attempt to do 1 and if that fails or if the gap is
|
|
|
|
// too large then it will attempt 2.
|
2017-06-12 17:30:47 +00:00
|
|
|
// Synapse will use /state_ids if possible since usually the state
|
2017-06-07 13:32:53 +00:00
|
|
|
// is largely unchanged and it is more efficient to fetch a list of
|
|
|
|
// event ids and then use /event to fetch the individual events.
|
|
|
|
// However not all version of synapse support /state_ids so you may
|
|
|
|
// need to fallback to /state.
|
|
|
|
// TODO: Attempt to fill in the gap using /get_missing_events
|
|
|
|
// TODO: Attempt to fetch the state using /state_ids and /events
|
2020-03-27 16:28:22 +00:00
|
|
|
state, err := t.federation.LookupState(t.context, t.Origin, e.RoomID(), e.EventID(), roomVersion)
|
2017-06-07 13:32:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Check that the returned state is valid.
|
2017-09-13 10:03:41 +00:00
|
|
|
if err := state.Check(t.context, t.keys); err != nil {
|
2017-06-07 13:32:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Check that the event is allowed by the state.
|
2020-03-06 16:58:10 +00:00
|
|
|
retryAllowedState:
|
2017-06-07 13:32:53 +00:00
|
|
|
if err := checkAllowedByState(e, state.StateEvents); err != nil {
|
2020-03-06 16:58:10 +00:00
|
|
|
switch missing := err.(type) {
|
|
|
|
case gomatrixserverlib.MissingAuthEventError:
|
|
|
|
// An auth event was missing so let's look up that event over federation
|
|
|
|
for _, s := range state.StateEvents {
|
|
|
|
if s.EventID() != missing.AuthEventID {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-27 16:28:22 +00:00
|
|
|
err = t.processEventWithMissingState(s, roomVersion)
|
2020-03-06 16:58:10 +00:00
|
|
|
// If there was no error retrieving the event from federation then
|
|
|
|
// we assume that it succeeded, so retry the original state check
|
|
|
|
if err == nil {
|
|
|
|
goto retryAllowedState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
2017-06-07 13:32:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-03-27 16:28:22 +00:00
|
|
|
|
2017-06-07 13:32:53 +00:00
|
|
|
// pass the event along with the state to the roomserver
|
2020-03-27 16:28:22 +00:00
|
|
|
return t.producer.SendEventWithState(t.context, state, e.Headered(roomVersion))
|
2017-06-07 13:32:53 +00:00
|
|
|
}
|