2017-09-08 15:17:12 +01: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 18:16:53 +01:00
|
|
|
package routing
|
2017-06-07 14:32:53 +01:00
|
|
|
|
|
|
|
import (
|
2017-09-13 11:03:41 +01:00
|
|
|
"context"
|
2017-06-07 14:32:53 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-08-23 15:13:47 +01:00
|
|
|
"net/http"
|
|
|
|
|
2017-06-07 14:32:53 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
2017-06-19 15:21:04 +01:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2017-06-07 14:32:53 +01: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 13:14:01 +01:00
|
|
|
httpReq *http.Request,
|
|
|
|
request *gomatrixserverlib.FederationRequest,
|
2017-06-07 14:32:53 +01:00
|
|
|
txnID gomatrixserverlib.TransactionID,
|
2020-02-11 11:18:12 +00:00
|
|
|
cfg *config.Dendrite,
|
2017-06-07 14:32:53 +01:00
|
|
|
query api.RoomserverQueryAPI,
|
|
|
|
producer *producers.RoomserverProducer,
|
|
|
|
keys gomatrixserverlib.KeyRing,
|
|
|
|
federation *gomatrixserverlib.FederationClient,
|
|
|
|
) util.JSONResponse {
|
|
|
|
|
|
|
|
t := txnReq{
|
2017-09-13 11:03:41 +01:00
|
|
|
context: httpReq.Context(),
|
2017-06-07 14:32:53 +01:00
|
|
|
query: query,
|
|
|
|
producer: producer,
|
|
|
|
keys: keys,
|
|
|
|
federation: federation,
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(request.Content(), &t); err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusBadRequest,
|
2017-08-23 15:13:47 +01:00
|
|
|
JSON: jsonerror.NotJSON("The request body could not be decoded into valid JSON. " + err.Error()),
|
2017-06-07 14:32:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Origin = request.Origin()
|
|
|
|
t.TransactionID = txnID
|
2017-06-19 15:21:04 +01:00
|
|
|
t.Destination = cfg.Matrix.ServerName
|
2017-06-07 14:32:53 +01:00
|
|
|
|
|
|
|
resp, err := t.processTransaction()
|
|
|
|
if err != nil {
|
2020-03-02 16:20:44 +00:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("t.processTransaction failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-06-07 14:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusOK,
|
2017-06-07 14:32:53 +01:00
|
|
|
JSON: resp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type txnReq struct {
|
|
|
|
gomatrixserverlib.Transaction
|
2017-09-13 11:03:41 +01:00
|
|
|
context context.Context
|
2017-06-07 14:32:53 +01:00
|
|
|
query api.RoomserverQueryAPI
|
|
|
|
producer *producers.RoomserverProducer
|
|
|
|
keys gomatrixserverlib.KeyRing
|
|
|
|
federation *gomatrixserverlib.FederationClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *txnReq) processTransaction() (*gomatrixserverlib.RespSend, error) {
|
|
|
|
// Check the event signatures
|
2017-12-08 17:59:15 +00:00
|
|
|
if err := gomatrixserverlib.VerifyAllEventSignatures(t.context, t.PDUs, t.keys); err != nil {
|
2017-06-07 14:32:53 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the events.
|
|
|
|
results := map[string]gomatrixserverlib.PDUResult{}
|
|
|
|
for _, e := range t.PDUs {
|
|
|
|
err := t.processEvent(e)
|
|
|
|
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) {
|
|
|
|
case unknownRoomError:
|
|
|
|
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 11:03:41 +01:00
|
|
|
results[e.EventID()] = gomatrixserverlib.PDUResult{
|
|
|
|
Error: err.Error(),
|
|
|
|
}
|
2017-06-07 14:32:53 +01:00
|
|
|
} else {
|
|
|
|
results[e.EventID()] = gomatrixserverlib.PDUResult{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Process the EDUs.
|
|
|
|
|
|
|
|
return &gomatrixserverlib.RespSend{PDUs: results}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type unknownRoomError struct {
|
|
|
|
roomID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e unknownRoomError) Error() string { return fmt.Sprintf("unknown room %q", e.roomID) }
|
|
|
|
|
|
|
|
func (t *txnReq) processEvent(e gomatrixserverlib.Event) error {
|
2017-07-07 14:11:32 +01:00
|
|
|
prevEventIDs := e.PrevEventIDs()
|
2017-06-07 14:32:53 +01: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
|
2017-09-13 13:37:50 +01:00
|
|
|
if err := t.query.QueryStateAfterEvents(t.context, &stateReq, &stateResp); err != nil {
|
2017-06-07 14:32:53 +01: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.
|
|
|
|
return unknownRoomError{e.RoomID()}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stateResp.PrevEventsExist {
|
|
|
|
return t.processEventWithMissingState(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the event is allowed by the state at the event.
|
|
|
|
if err := checkAllowedByState(e, stateResp.StateEvents); err != nil {
|
|
|
|
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
|
2018-05-26 12:03:35 +01:00
|
|
|
_, err := t.producer.SendEvents(t.context, []gomatrixserverlib.Event{e}, api.DoNotSendToOtherServers, nil)
|
|
|
|
return err
|
2017-06-07 14:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkAllowedByState(e gomatrixserverlib.Event, stateEvents []gomatrixserverlib.Event) error {
|
|
|
|
authUsingState := gomatrixserverlib.NewAuthEvents(nil)
|
|
|
|
for i := range stateEvents {
|
2017-09-20 10:59:19 +01:00
|
|
|
err := authUsingState.AddEvent(&stateEvents[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-07 14:32:53 +01:00
|
|
|
}
|
|
|
|
return gomatrixserverlib.Allowed(e, &authUsingState)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *txnReq) processEventWithMissingState(e gomatrixserverlib.Event) error {
|
|
|
|
// 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 18:30:47 +01:00
|
|
|
// Synapse will use /state_ids if possible since usually the state
|
2017-06-07 14:32:53 +01: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
|
2017-09-13 11:03:41 +01:00
|
|
|
state, err := t.federation.LookupState(t.context, t.Origin, e.RoomID(), e.EventID())
|
2017-06-07 14:32:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Check that the returned state is valid.
|
2017-09-13 11:03:41 +01:00
|
|
|
if err := state.Check(t.context, t.keys); err != nil {
|
2017-06-07 14:32:53 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Check that the event is allowed by the state.
|
|
|
|
if err := checkAllowedByState(e, state.StateEvents); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// pass the event along with the state to the roomserver
|
2017-11-15 11:13:09 +00:00
|
|
|
return t.producer.SendEventWithState(t.context, state, e)
|
2017-06-07 14:32:53 +01:00
|
|
|
}
|