2017-04-20 17:40:52 -05:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
2020-02-05 12:06:39 -06:00
|
|
|
// Copyright 2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-20 17:40:52 -05:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-09-02 11:13:15 -05:00
|
|
|
package input
|
2017-02-07 11:20:05 -06:00
|
|
|
|
|
|
|
import (
|
2020-10-20 05:42:54 -05:00
|
|
|
"bytes"
|
2017-09-13 10:30:19 -05:00
|
|
|
"context"
|
2020-07-21 09:48:21 -05:00
|
|
|
"fmt"
|
2021-01-13 11:31:46 -06:00
|
|
|
"time"
|
2017-08-21 10:37:11 -05:00
|
|
|
|
2021-12-14 05:38:08 -06:00
|
|
|
fedapi "github.com/matrix-org/dendrite/federationapi/api"
|
2021-12-15 07:59:58 -06:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-07-07 06:51:55 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2017-02-07 11:20:05 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-09-02 07:47:31 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
2017-03-08 09:10:26 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2017-02-09 10:48:14 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2017-02-07 11:20:05 -06:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-10-20 05:42:54 -05:00
|
|
|
"github.com/matrix-org/util"
|
2021-01-13 11:31:46 -06:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-04-28 05:46:47 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-02-07 11:20:05 -06:00
|
|
|
)
|
|
|
|
|
2021-01-13 11:31:46 -06:00
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(processRoomEventDuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
var processRoomEventDuration = prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Namespace: "dendrite",
|
|
|
|
Subsystem: "roomserver",
|
|
|
|
Name: "processroomevent_duration_millis",
|
|
|
|
Help: "How long it takes the roomserver to process an event",
|
|
|
|
Buckets: []float64{ // milliseconds
|
|
|
|
5, 10, 25, 50, 75, 100, 250, 500,
|
|
|
|
1000, 2000, 3000, 4000, 5000, 6000,
|
|
|
|
7000, 8000, 9000, 10000, 15000, 20000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"room_id"},
|
|
|
|
)
|
|
|
|
|
2017-12-15 09:22:06 -06:00
|
|
|
// processRoomEvent can only be called once at a time
|
|
|
|
//
|
|
|
|
// TODO(#375): This should be rewritten to allow concurrent calls. The
|
|
|
|
// difficulty is in ensuring that we correctly annotate events with the correct
|
|
|
|
// state deltas when sending to kafka streams
|
2020-07-07 06:51:55 -05:00
|
|
|
// TODO: Break up function - we should probably do transaction ID checks before calling this.
|
|
|
|
// nolint:gocyclo
|
2020-09-02 11:13:15 -05:00
|
|
|
func (r *Inputer) processRoomEvent(
|
2017-09-13 10:30:19 -05:00
|
|
|
ctx context.Context,
|
2020-09-03 09:22:16 -05:00
|
|
|
input *api.InputRoomEvent,
|
2018-05-26 06:03:35 -05:00
|
|
|
) (eventID string, err error) {
|
2021-01-13 11:31:46 -06:00
|
|
|
// Measure how long it takes to process this event.
|
|
|
|
started := time.Now()
|
|
|
|
defer func() {
|
|
|
|
timetaken := time.Since(started)
|
|
|
|
processRoomEventDuration.With(prometheus.Labels{
|
|
|
|
"room_id": input.Event.RoomID(),
|
|
|
|
}).Observe(float64(timetaken.Milliseconds()))
|
|
|
|
}()
|
|
|
|
|
2017-02-07 11:20:05 -06:00
|
|
|
// Parse and validate the event JSON
|
2020-03-27 11:28:22 -05:00
|
|
|
headered := input.Event
|
|
|
|
event := headered.Unwrap()
|
2021-12-15 07:59:58 -06:00
|
|
|
logger := util.GetLogger(ctx).WithFields(logrus.Fields{
|
|
|
|
"event_id": event.EventID(),
|
|
|
|
"room_id": event.RoomID(),
|
|
|
|
"type": event.Type(),
|
|
|
|
})
|
2017-02-07 11:20:05 -06:00
|
|
|
|
2020-10-20 05:42:54 -05:00
|
|
|
// if we have already got this event then do not process it again, if the input kind is an outlier.
|
|
|
|
// Outliers contain no extra information which may warrant a re-processing.
|
|
|
|
if input.Kind == api.KindOutlier {
|
2020-10-20 06:29:53 -05:00
|
|
|
evs, err2 := r.DB.EventsFromIDs(ctx, []string{event.EventID()})
|
|
|
|
if err2 == nil && len(evs) == 1 {
|
2020-10-20 05:42:54 -05:00
|
|
|
// check hash matches if we're on early room versions where the event ID was a random string
|
2020-10-20 06:29:53 -05:00
|
|
|
idFormat, err2 := headered.RoomVersion.EventIDFormat()
|
|
|
|
if err2 == nil {
|
2020-10-20 05:42:54 -05:00
|
|
|
switch idFormat {
|
|
|
|
case gomatrixserverlib.EventIDFormatV1:
|
|
|
|
if bytes.Equal(event.EventReference().EventSHA256, evs[0].EventReference().EventSHA256) {
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.Debugf("Already processed event; ignoring")
|
2020-10-20 05:42:54 -05:00
|
|
|
return event.EventID(), nil
|
|
|
|
}
|
|
|
|
default:
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.Debugf("Already processed event; ignoring")
|
2020-10-20 05:42:54 -05:00
|
|
|
return event.EventID(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 07:59:58 -06:00
|
|
|
missingReq := &api.QueryMissingAuthPrevEventsRequest{
|
|
|
|
RoomID: event.RoomID(),
|
|
|
|
AuthEventIDs: event.AuthEventIDs(),
|
|
|
|
PrevEventIDs: event.PrevEventIDs(),
|
|
|
|
}
|
|
|
|
missingRes := &api.QueryMissingAuthPrevEventsResponse{}
|
|
|
|
if event.Type() != gomatrixserverlib.MRoomCreate {
|
|
|
|
if err = r.Queryer.QueryMissingAuthPrevEvents(ctx, missingReq, missingRes); err != nil {
|
|
|
|
return "", fmt.Errorf("r.Queryer.QueryMissingAuthPrevEvents: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 05:38:08 -06:00
|
|
|
// First of all, check that the auth events of the event are known.
|
|
|
|
// If they aren't then we will ask the federation API for them.
|
2020-09-16 07:00:52 -05:00
|
|
|
isRejected := false
|
2021-12-14 05:38:08 -06:00
|
|
|
authEvents := gomatrixserverlib.NewAuthEvents(nil)
|
2021-12-15 07:59:58 -06:00
|
|
|
knownEvents := map[string]*types.Event{}
|
|
|
|
if err = r.checkForMissingAuthEvents(ctx, logger, input.Event, &authEvents, knownEvents); err != nil {
|
2021-12-14 05:38:08 -06:00
|
|
|
return "", fmt.Errorf("r.checkForMissingAuthEvents: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the event is allowed by its auth events. If it isn't then
|
|
|
|
// we consider the event to be "rejected" — it will still be persisted.
|
|
|
|
var rejectionErr error
|
|
|
|
if rejectionErr = gomatrixserverlib.Allowed(event, &authEvents); rejectionErr != nil {
|
2020-09-16 07:00:52 -05:00
|
|
|
isRejected = true
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.WithError(rejectionErr).Warnf("Event %s rejected", event.EventID())
|
2021-12-14 05:38:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate the auth event NIDs.
|
|
|
|
authEventIDs := event.AuthEventIDs()
|
|
|
|
authEventNIDs := make([]types.EventNID, 0, len(authEventIDs))
|
|
|
|
for _, authEventID := range authEventIDs {
|
2021-12-15 07:59:58 -06:00
|
|
|
authEventNIDs = append(authEventNIDs, knownEvents[authEventID].EventNID)
|
2018-05-26 06:03:35 -05:00
|
|
|
}
|
|
|
|
|
2020-09-21 08:55:46 -05:00
|
|
|
var softfail bool
|
2020-10-07 08:05:33 -05:00
|
|
|
if input.Kind == api.KindNew {
|
2020-09-21 08:55:46 -05:00
|
|
|
// Check that the event passes authentication checks based on the
|
|
|
|
// current room state.
|
|
|
|
softfail, err = helpers.CheckForSoftFail(ctx, r.DB, headered, input.StateEventIDs)
|
|
|
|
if err != nil {
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.WithError(err).Info("Error authing soft-failed event")
|
2020-09-21 08:55:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 11:49:24 -05:00
|
|
|
// Store the event.
|
2021-12-09 09:03:26 -06:00
|
|
|
_, _, stateAtEvent, redactionEvent, redactedEventID, err := r.DB.StoreEvent(ctx, event, authEventNIDs, isRejected)
|
2017-02-15 05:05:45 -06:00
|
|
|
if err != nil {
|
2020-07-21 09:48:21 -05:00
|
|
|
return "", fmt.Errorf("r.DB.StoreEvent: %w", err)
|
2017-02-09 10:48:14 -06:00
|
|
|
}
|
2020-09-16 07:00:52 -05:00
|
|
|
|
2020-07-07 06:51:55 -05:00
|
|
|
// if storing this event results in it being redacted then do so.
|
2020-09-16 07:00:52 -05:00
|
|
|
if !isRejected && redactedEventID == event.EventID() {
|
2020-11-16 09:44:53 -06:00
|
|
|
r, rerr := eventutil.RedactEvent(redactionEvent, event)
|
2020-07-07 06:51:55 -05:00
|
|
|
if rerr != nil {
|
2020-07-21 09:48:21 -05:00
|
|
|
return "", fmt.Errorf("eventutil.RedactEvent: %w", rerr)
|
2020-07-07 06:51:55 -05:00
|
|
|
}
|
2020-11-16 09:44:53 -06:00
|
|
|
event = r
|
2020-07-07 06:51:55 -05:00
|
|
|
}
|
2017-02-07 11:20:05 -06:00
|
|
|
|
2020-05-18 11:49:24 -05:00
|
|
|
// For outliers we can stop after we've stored the event itself as it
|
|
|
|
// doesn't have any associated state to store and we don't need to
|
|
|
|
// notify anyone about it.
|
2017-02-07 11:20:05 -06:00
|
|
|
if input.Kind == api.KindOutlier {
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.Debug("Stored outlier")
|
2018-05-26 06:03:35 -05:00
|
|
|
return event.EventID(), nil
|
2017-02-07 11:20:05 -06:00
|
|
|
}
|
|
|
|
|
2020-09-02 04:02:48 -05:00
|
|
|
roomInfo, err := r.DB.RoomInfo(ctx, event.RoomID())
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("r.DB.RoomInfo: %w", err)
|
|
|
|
}
|
|
|
|
if roomInfo == nil {
|
|
|
|
return "", fmt.Errorf("r.DB.RoomInfo missing for room %s", event.RoomID())
|
|
|
|
}
|
|
|
|
|
2021-12-15 07:59:58 -06:00
|
|
|
if input.Origin == "" {
|
2021-12-15 08:05:35 -06:00
|
|
|
input.Origin = event.Origin()
|
2021-12-15 07:59:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(missingRes.MissingPrevEventIDs) > 0 {
|
|
|
|
missingState := missingStateReq{
|
|
|
|
origin: input.Origin,
|
|
|
|
inputer: r,
|
|
|
|
queryer: r.Queryer,
|
|
|
|
db: r.DB,
|
|
|
|
federation: r.FSAPI,
|
|
|
|
roomsMu: internal.NewMutexByRoom(),
|
|
|
|
servers: []gomatrixserverlib.ServerName{input.Origin},
|
|
|
|
hadEvents: map[string]bool{},
|
|
|
|
haveEvents: map[string]*gomatrixserverlib.HeaderedEvent{},
|
|
|
|
}
|
|
|
|
if err = missingState.processEventWithMissingState(ctx, input.Event.Unwrap(), roomInfo.RoomVersion); err != nil {
|
|
|
|
return "", fmt.Errorf("r.checkForMissingPrevEvents: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 05:05:45 -06:00
|
|
|
if stateAtEvent.BeforeStateSnapshotNID == 0 {
|
|
|
|
// We haven't calculated a state for this event yet.
|
|
|
|
// Lets calculate one.
|
2020-09-16 07:00:52 -05:00
|
|
|
err = r.calculateAndSetState(ctx, input, *roomInfo, &stateAtEvent, event, isRejected)
|
2020-10-19 08:59:13 -05:00
|
|
|
if err != nil && input.Kind != api.KindOld {
|
2020-07-21 09:48:21 -05:00
|
|
|
return "", fmt.Errorf("r.calculateAndSetState: %w", err)
|
2017-09-20 04:59:19 -05:00
|
|
|
}
|
2017-02-15 05:05:45 -06:00
|
|
|
}
|
|
|
|
|
2020-09-16 07:00:52 -05:00
|
|
|
// We stop here if the event is rejected: We've stored it but won't update forward extremities or notify anyone about it.
|
2020-09-21 08:55:46 -05:00
|
|
|
if isRejected || softfail {
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.WithField("soft_fail", softfail).Debug("Stored rejected event")
|
2020-09-16 07:00:52 -05:00
|
|
|
return event.EventID(), rejectionErr
|
|
|
|
}
|
|
|
|
|
2020-10-19 08:59:13 -05:00
|
|
|
switch input.Kind {
|
|
|
|
case api.KindNew:
|
|
|
|
if err = r.updateLatestEvents(
|
|
|
|
ctx, // context
|
|
|
|
roomInfo, // room info for the room being updated
|
|
|
|
stateAtEvent, // state at event (below)
|
|
|
|
event, // event
|
|
|
|
input.SendAsServer, // send as server
|
|
|
|
input.TransactionID, // transaction ID
|
|
|
|
input.HasState, // rewrites state?
|
|
|
|
); err != nil {
|
|
|
|
return "", fmt.Errorf("r.updateLatestEvents: %w", err)
|
|
|
|
}
|
|
|
|
case api.KindOld:
|
|
|
|
err = r.WriteOutputEvents(event.RoomID(), []api.OutputEvent{
|
|
|
|
{
|
|
|
|
Type: api.OutputTypeOldRoomEvent,
|
|
|
|
OldRoomEvent: &api.OutputOldRoomEvent{
|
|
|
|
Event: headered,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("r.WriteOutputEvents (old): %w", err)
|
|
|
|
}
|
2020-05-18 11:49:24 -05:00
|
|
|
}
|
|
|
|
|
2020-07-07 06:51:55 -05:00
|
|
|
// processing this event resulted in an event (which may not be the one we're processing)
|
|
|
|
// being redacted. We are guaranteed to have both sides (the redaction/redacted event),
|
|
|
|
// so notify downstream components to redact this event - they should have it if they've
|
|
|
|
// been tracking our output log.
|
|
|
|
if redactedEventID != "" {
|
|
|
|
err = r.WriteOutputEvents(event.RoomID(), []api.OutputEvent{
|
|
|
|
{
|
|
|
|
Type: api.OutputTypeRedactedEvent,
|
|
|
|
RedactedEvent: &api.OutputRedactedEvent{
|
|
|
|
RedactedEventID: redactedEventID,
|
|
|
|
RedactedBecause: redactionEvent.Headered(headered.RoomVersion),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-19 08:59:13 -05:00
|
|
|
return "", fmt.Errorf("r.WriteOutputEvents (redactions): %w", err)
|
2020-07-07 06:51:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 08:50:30 -06:00
|
|
|
// Update the extremities of the event graph for the room
|
2020-05-18 11:49:24 -05:00
|
|
|
return event.EventID(), nil
|
2018-05-26 06:03:35 -05:00
|
|
|
}
|
|
|
|
|
2021-12-14 05:38:08 -06:00
|
|
|
func (r *Inputer) checkForMissingAuthEvents(
|
|
|
|
ctx context.Context,
|
2021-12-15 07:59:58 -06:00
|
|
|
logger *logrus.Entry,
|
2021-12-14 05:38:08 -06:00
|
|
|
event *gomatrixserverlib.HeaderedEvent,
|
|
|
|
auth *gomatrixserverlib.AuthEvents,
|
2021-12-15 07:59:58 -06:00
|
|
|
known map[string]*types.Event,
|
2021-12-14 05:38:08 -06:00
|
|
|
) error {
|
|
|
|
authEventIDs := event.AuthEventIDs()
|
|
|
|
if len(authEventIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
unknown := map[string]struct{}{}
|
|
|
|
|
|
|
|
authEvents, err := r.DB.EventsFromIDs(ctx, authEventIDs)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.DB.EventsFromIDs: %w", err)
|
|
|
|
}
|
|
|
|
for _, event := range authEvents {
|
|
|
|
if event.Event != nil {
|
2021-12-15 07:59:58 -06:00
|
|
|
ev := event // don't take the address of the iterated value
|
|
|
|
known[event.EventID()] = &ev
|
2021-12-14 05:38:08 -06:00
|
|
|
if err = auth.AddEvent(event.Event); err != nil {
|
|
|
|
return fmt.Errorf("auth.AddEvent: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unknown[event.EventID()] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(unknown) > 0 {
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.Printf("XXX: There are %d missing auth events", len(unknown))
|
2021-12-14 06:38:47 -06:00
|
|
|
|
2021-12-14 05:38:08 -06:00
|
|
|
serverReq := &fedapi.QueryJoinedHostServerNamesInRoomRequest{
|
|
|
|
RoomID: event.RoomID(),
|
|
|
|
}
|
|
|
|
serverRes := &fedapi.QueryJoinedHostServerNamesInRoomResponse{}
|
|
|
|
if err = r.FSAPI.QueryJoinedHostServerNamesInRoom(ctx, serverReq, serverRes); err != nil {
|
|
|
|
return fmt.Errorf("r.FSAPI.QueryJoinedHostServerNamesInRoom: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.Printf("XXX: Asking servers %+v", serverRes.ServerNames)
|
2021-12-14 06:38:47 -06:00
|
|
|
|
2021-12-14 05:38:08 -06:00
|
|
|
var res gomatrixserverlib.RespEventAuth
|
|
|
|
var found bool
|
|
|
|
for _, serverName := range serverRes.ServerNames {
|
|
|
|
res, err = r.FSAPI.GetEventAuth(ctx, serverName, event.RoomID(), event.EventID())
|
|
|
|
if err != nil {
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.WithError(err).Warnf("Failed to get event auth from federation for %q: %s", event.EventID(), err)
|
2021-12-14 05:38:08 -06:00
|
|
|
continue
|
|
|
|
}
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.Printf("XXX: Server %q provided us with %d auth events", serverName, len(res.AuthEvents))
|
2021-12-14 05:38:08 -06:00
|
|
|
found = true
|
2021-12-14 06:38:47 -06:00
|
|
|
break
|
2021-12-14 05:38:08 -06:00
|
|
|
}
|
|
|
|
if !found {
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.Printf("XXX: None of the %d servers provided us with auth events", len(serverRes.ServerNames))
|
2021-12-14 05:38:08 -06:00
|
|
|
return fmt.Errorf("no servers provided event auth")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range gomatrixserverlib.ReverseTopologicalOrdering(
|
|
|
|
res.AuthEvents,
|
|
|
|
gomatrixserverlib.TopologicalOrderByAuthEvents,
|
|
|
|
) {
|
|
|
|
// If we already know about this event then we don't need to store
|
|
|
|
// it or do anything further with it.
|
|
|
|
if _, ok := known[event.EventID()]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the signatures of the event.
|
|
|
|
// TODO: It really makes sense for the federation API to be doing this,
|
|
|
|
// because then it can attempt another server if one serves up an event
|
|
|
|
// with an invalid signature. For now this will do.
|
|
|
|
if err := event.VerifyEventSignatures(ctx, r.FSAPI.KeyRing()); err != nil {
|
|
|
|
return fmt.Errorf("event.VerifyEventSignatures: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we need to store, and that means we need to know the
|
|
|
|
// auth event NIDs. Let's see if we can find those.
|
|
|
|
authEventNIDs := make([]types.EventNID, 0, len(event.AuthEventIDs()))
|
|
|
|
for _, eventID := range event.AuthEventIDs() {
|
|
|
|
knownEvent, ok := known[eventID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("missing auth event %s for %s", eventID, event.EventID())
|
|
|
|
}
|
|
|
|
authEventNIDs = append(authEventNIDs, knownEvent.EventNID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's take a note of the fact that we now know about this event.
|
2021-12-15 07:59:58 -06:00
|
|
|
known[event.EventID()] = nil
|
2021-12-14 05:38:08 -06:00
|
|
|
if err := auth.AddEvent(event); err != nil {
|
|
|
|
return fmt.Errorf("auth.AddEvent: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the auth event should be rejected.
|
|
|
|
isRejected := false
|
|
|
|
if err := gomatrixserverlib.Allowed(event, auth); err != nil {
|
|
|
|
isRejected = true
|
2021-12-15 07:59:58 -06:00
|
|
|
logger.WithError(err).Warnf("Auth event %s rejected", event.EventID())
|
2021-12-14 05:38:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, store the event in the database.
|
|
|
|
eventNID, _, _, _, _, err := r.DB.StoreEvent(ctx, event, authEventNIDs, isRejected)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.DB.StoreEvent: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we know about this event, too.
|
2021-12-15 07:59:58 -06:00
|
|
|
known[event.EventID()] = &types.Event{
|
2021-12-14 05:38:08 -06:00
|
|
|
EventNID: eventNID,
|
|
|
|
Event: event,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-15 07:59:58 -06:00
|
|
|
/*
|
|
|
|
func (r *Inputer) checkForMissingPrevEvents(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *logrus.Entry,
|
|
|
|
event *gomatrixserverlib.HeaderedEvent,
|
|
|
|
roomInfo *types.RoomInfo,
|
|
|
|
known map[string]*types.Event,
|
|
|
|
) error {
|
|
|
|
prevStates := map[string]*types.StateAtEvent{}
|
|
|
|
prevEventIDs := event.PrevEventIDs()
|
|
|
|
if len(prevEventIDs) == 0 && event.Type() != gomatrixserverlib.MRoomCreate {
|
|
|
|
return fmt.Errorf("expected to find some prev events for event type %q", event.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, eventID := range prevEventIDs {
|
|
|
|
state, err := r.DB.StateAtEventIDs(ctx, []string{eventID})
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(types.MissingEventError); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return fmt.Errorf("r.DB.StateAtEventIDs: %w", err)
|
|
|
|
}
|
|
|
|
if len(state) == 1 {
|
|
|
|
prevStates[eventID] = &state[0]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we know all of the states of the previous events then there is nothing more to
|
|
|
|
// do here, as the state across them will be resolved later.
|
|
|
|
if len(prevStates) == len(prevEventIDs) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if r.FSAPI == nil {
|
|
|
|
return fmt.Errorf("cannot satisfy missing events without federation")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the federation API which servers we should ask. In theory the roomserver
|
|
|
|
// doesn't need the help of the federation API to do this because we already know
|
|
|
|
// all of the membership states, it's just that the federation API tracks this in
|
|
|
|
// a table for this purpose. TODO: Work out what makes most sense here.
|
|
|
|
serverReq := &fedapi.QueryJoinedHostServerNamesInRoomRequest{
|
|
|
|
RoomID: event.RoomID(),
|
|
|
|
}
|
|
|
|
serverRes := &fedapi.QueryJoinedHostServerNamesInRoomResponse{}
|
|
|
|
if err := r.FSAPI.QueryJoinedHostServerNamesInRoom(ctx, serverReq, serverRes); err != nil {
|
|
|
|
return fmt.Errorf("r.FSAPI.QueryJoinedHostServerNamesInRoom: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to fill in the gap using /get_missing_events
|
|
|
|
// This will either:
|
|
|
|
// - fill in the gap completely then process event `e` returning no backwards extremity
|
|
|
|
// - fail to fill in the gap and tell us to terminate the transaction err=not nil
|
|
|
|
// - fail to fill in the gap and tell us to fetch state at the new backwards extremity, and to not terminate the transaction
|
|
|
|
newEvents, err := r.getMissingEvents(ctx, logger, event, roomInfo, serverRes.ServerNames, known)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(newEvents) == 0 {
|
|
|
|
return fmt.Errorf("/get_missing_events returned no new events")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Inputer) getMissingEvents(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *logrus.Entry,
|
|
|
|
event *gomatrixserverlib.HeaderedEvent,
|
|
|
|
roomInfo *types.RoomInfo,
|
|
|
|
servers []gomatrixserverlib.ServerName,
|
|
|
|
known map[string]*types.Event,
|
|
|
|
) (newEvents []*gomatrixserverlib.Event, err error) {
|
|
|
|
logger.Printf("XXX: get_missing_events called")
|
|
|
|
needed := gomatrixserverlib.StateNeededForAuth([]*gomatrixserverlib.Event{event.Unwrap()})
|
|
|
|
|
|
|
|
// Ask the roomserver for our current forward extremities. These will form
|
|
|
|
// the "earliest" part of the `/get_missing_events` request.
|
|
|
|
req := &api.QueryLatestEventsAndStateRequest{
|
|
|
|
RoomID: event.RoomID(),
|
|
|
|
StateToFetch: needed.Tuples(),
|
|
|
|
}
|
|
|
|
res := &api.QueryLatestEventsAndStateResponse{}
|
|
|
|
if err = r.Queryer.QueryLatestEventsAndState(ctx, req, res); err != nil {
|
|
|
|
logger.WithError(err).Warn("Failed to query latest events")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate the event IDs of our forward extremities for use in the request.
|
|
|
|
latestEvents := make([]string, len(res.LatestEvents))
|
|
|
|
for i := range res.LatestEvents {
|
|
|
|
latestEvents[i] = res.LatestEvents[i].EventID
|
|
|
|
}
|
|
|
|
|
|
|
|
var missingResp *gomatrixserverlib.RespMissingEvents
|
|
|
|
for _, server := range servers {
|
|
|
|
logger.Printf("XXX: Calling /get_missing_events via %q", server)
|
|
|
|
var m gomatrixserverlib.RespMissingEvents
|
|
|
|
if m, err = r.FSAPI.LookupMissingEvents(ctx, server, event.RoomID(), gomatrixserverlib.MissingEvents{
|
|
|
|
Limit: 20,
|
|
|
|
EarliestEvents: latestEvents,
|
|
|
|
LatestEvents: []string{event.EventID()},
|
|
|
|
}, event.RoomVersion); err == nil {
|
|
|
|
missingResp = &m
|
|
|
|
break
|
|
|
|
} else if errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if missingResp == nil {
|
|
|
|
return nil, fmt.Errorf("/get_missing_events failed via all candidate servers")
|
|
|
|
}
|
|
|
|
if len(missingResp.Events) == 0 {
|
|
|
|
return nil, fmt.Errorf("/get_missing_events returned no events")
|
|
|
|
}
|
|
|
|
|
|
|
|
// security: how we handle failures depends on whether or not this event will become the new forward extremity for the room.
|
|
|
|
// There's 2 scenarios to consider:
|
|
|
|
// - Case A: We got pushed an event and are now fetching missing prev_events. (isInboundTxn=true)
|
|
|
|
// - Case B: We are fetching missing prev_events already and now fetching some more (isInboundTxn=false)
|
|
|
|
// In Case B, we know for sure that the event we are currently processing will not become the new forward extremity for the room,
|
|
|
|
// as it was called in response to an inbound txn which had it as a prev_event.
|
|
|
|
// In Case A, the event is a forward extremity, and could eventually become the _only_ forward extremity in the room. This is bad
|
|
|
|
// because it means we would trust the state at that event to be the state for the entire room, and allows rooms to be hijacked.
|
|
|
|
// https://github.com/matrix-org/synapse/pull/3456
|
|
|
|
// https://github.com/matrix-org/synapse/blob/229eb81498b0fe1da81e9b5b333a0285acde9446/synapse/handlers/federation.py#L335
|
|
|
|
// For now, we do not allow Case B, so reject the event.
|
|
|
|
logger.Printf("XXX: get_missing_events returned %d events", len(missingResp.Events))
|
|
|
|
|
|
|
|
newEvents = gomatrixserverlib.ReverseTopologicalOrdering(
|
|
|
|
missingResp.Events,
|
|
|
|
gomatrixserverlib.TopologicalOrderByPrevEvents,
|
|
|
|
)
|
|
|
|
for _, pe := range event.PrevEventIDs() {
|
|
|
|
hasPrevEvent := false
|
|
|
|
for _, ev := range newEvents {
|
|
|
|
if ev.EventID() == pe {
|
|
|
|
hasPrevEvent = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasPrevEvent {
|
|
|
|
logger.Errorf("Prev event %q is still missing after /get_missing_events", pe)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
backwardExtremity := newEvents[0]
|
|
|
|
fastForwardEvents := newEvents[1:]
|
|
|
|
|
|
|
|
// Do we know about the state of the backward extremity already?
|
|
|
|
if _, err := r.DB.StateAtEventIDs(ctx, []string{backwardExtremity.EventID()}); err == nil {
|
|
|
|
// Yes, we do, so we don't need to store that event.
|
|
|
|
} else {
|
|
|
|
// No, we don't, so let's go find it.
|
|
|
|
// r.FSAPI.LookupStateIDs()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ev := range fastForwardEvents {
|
|
|
|
if _, err := r.processRoomEvent(ctx, &api.InputRoomEvent{
|
|
|
|
Kind: api.KindOld,
|
|
|
|
Event: ev.Headered(event.RoomVersion),
|
|
|
|
AuthEventIDs: ev.AuthEventIDs(),
|
|
|
|
}); err != nil {
|
|
|
|
return nil, fmt.Errorf("r.processRoomEvent (prev event): %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newEvents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Inputer) lookupStateBeforeEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *logrus.Entry,
|
|
|
|
event *gomatrixserverlib.HeaderedEvent,
|
|
|
|
roomInfo *types.RoomInfo,
|
|
|
|
servers []gomatrixserverlib.ServerName,
|
|
|
|
) error {
|
|
|
|
knownPrevStates := map[string]types.StateAtEvent{}
|
|
|
|
unknownPrevStates := map[string]struct{}{}
|
|
|
|
neededStateEvents := map[string]struct{}{}
|
|
|
|
|
|
|
|
for _, prevEventID := range event.PrevEventIDs() {
|
|
|
|
if state, err := r.DB.StateAtEventIDs(ctx, []string{prevEventID}); err == nil && len(state) == 1 {
|
|
|
|
knownPrevStates[prevEventID] = state[0]
|
|
|
|
} else {
|
|
|
|
unknownPrevStates[prevEventID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for prevEventID := range unknownPrevStates {
|
|
|
|
stateIDs, err := r.FSAPI.LookupStateIDs(ctx, "TODO: SERVER", event.RoomID(), prevEventID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.FSAPI.LookupStateIDs: %w", err)
|
|
|
|
}
|
|
|
|
events, err := r.DB.EventsFromIDs(ctx, stateIDs.StateEventIDs)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.DB.EventsFromIDs: %w", err)
|
|
|
|
}
|
|
|
|
for i, eventID := range stateIDs.StateEventIDs {
|
|
|
|
if events[i].Event == nil || events[i].EventNID == 0 {
|
|
|
|
neededStateEvents[eventID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(neededStateEvents) > (len(stateIDs.StateEventIDs) / 2) {
|
|
|
|
// More than 50% of the state events are missing, so let's just
|
|
|
|
// call `/state` instead of fetching the events individually.
|
|
|
|
state, err := r.FSAPI.LookupState(ctx, "", event.RoomID(), prevEventID, roomInfo.RoomVersion)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.FSAPI.LookupState: %w", err)
|
|
|
|
}
|
|
|
|
knownPrevStates[prevEventID] = types.StateAtEvent{
|
|
|
|
StateEntry: types.StateEntry{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-09-02 11:13:15 -05:00
|
|
|
func (r *Inputer) calculateAndSetState(
|
2018-05-26 06:03:35 -05:00
|
|
|
ctx context.Context,
|
2020-09-03 09:22:16 -05:00
|
|
|
input *api.InputRoomEvent,
|
2020-09-02 04:02:48 -05:00
|
|
|
roomInfo types.RoomInfo,
|
2018-05-26 06:03:35 -05:00
|
|
|
stateAtEvent *types.StateAtEvent,
|
2020-11-16 09:44:53 -06:00
|
|
|
event *gomatrixserverlib.Event,
|
2020-09-16 07:00:52 -05:00
|
|
|
isRejected bool,
|
2018-05-26 06:03:35 -05:00
|
|
|
) error {
|
2020-03-19 13:33:04 -05:00
|
|
|
var err error
|
2020-09-02 04:02:48 -05:00
|
|
|
roomState := state.NewStateResolution(r.DB, roomInfo)
|
2020-02-05 10:25:58 -06:00
|
|
|
|
2020-09-16 07:00:52 -05:00
|
|
|
if input.HasState && !isRejected {
|
2020-05-20 12:03:06 -05:00
|
|
|
// Check here if we think we're in the room already.
|
2020-05-18 11:49:24 -05:00
|
|
|
stateAtEvent.Overwrite = true
|
2020-05-20 12:03:06 -05:00
|
|
|
var joinEventNIDs []types.EventNID
|
|
|
|
// Request join memberships only for local users only.
|
2020-09-02 04:02:48 -05:00
|
|
|
if joinEventNIDs, err = r.DB.GetMembershipEventNIDsForRoom(ctx, roomInfo.RoomNID, true, true); err == nil {
|
2020-05-20 12:03:06 -05:00
|
|
|
// If we have no local users that are joined to the room then any state about
|
|
|
|
// the room that we have is quite possibly out of date. Therefore in that case
|
|
|
|
// we should overwrite it rather than merge it.
|
|
|
|
stateAtEvent.Overwrite = len(joinEventNIDs) == 0
|
|
|
|
}
|
2020-05-18 11:49:24 -05:00
|
|
|
|
2018-05-26 06:03:35 -05:00
|
|
|
// We've been told what the state at the event is so we don't need to calculate it.
|
|
|
|
// Check that those state events are in the database and store the state.
|
|
|
|
var entries []types.StateEntry
|
2020-05-20 12:03:06 -05:00
|
|
|
if entries, err = r.DB.StateEntriesForEventIDs(ctx, input.StateEventIDs); err != nil {
|
2020-09-15 05:17:46 -05:00
|
|
|
return fmt.Errorf("r.DB.StateEntriesForEventIDs: %w", err)
|
2018-05-26 06:03:35 -05:00
|
|
|
}
|
2020-09-15 05:17:46 -05:00
|
|
|
entries = types.DeduplicateStateEntries(entries)
|
2018-05-26 06:03:35 -05:00
|
|
|
|
2020-09-02 04:02:48 -05:00
|
|
|
if stateAtEvent.BeforeStateSnapshotNID, err = r.DB.AddState(ctx, roomInfo.RoomNID, nil, entries); err != nil {
|
2020-09-15 05:17:46 -05:00
|
|
|
return fmt.Errorf("r.DB.AddState: %w", err)
|
2018-05-26 06:03:35 -05:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-18 11:49:24 -05:00
|
|
|
stateAtEvent.Overwrite = false
|
|
|
|
|
2018-05-26 06:03:35 -05:00
|
|
|
// We haven't been told what the state at the event is so we need to calculate it from the prev_events
|
2020-09-16 07:00:52 -05:00
|
|
|
if stateAtEvent.BeforeStateSnapshotNID, err = roomState.CalculateAndStoreStateBeforeEvent(ctx, event, isRejected); err != nil {
|
2020-09-15 05:17:46 -05:00
|
|
|
return fmt.Errorf("roomState.CalculateAndStoreStateBeforeEvent: %w", err)
|
2018-05-26 06:03:35 -05:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 05:17:46 -05:00
|
|
|
|
|
|
|
err = r.DB.SetState(ctx, stateAtEvent.EventNID, stateAtEvent.BeforeStateSnapshotNID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.DB.SetState: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-08-21 10:37:11 -05:00
|
|
|
}
|