2017-04-21 00:40:52 +02:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
2020-02-05 19:06:39 +01:00
|
|
|
// Copyright 2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-21 00:40:52 +02: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 18:13:15 +02:00
|
|
|
package input
|
2017-02-07 18:20:05 +01:00
|
|
|
|
|
|
|
import (
|
2020-10-20 12:42:54 +02:00
|
|
|
"bytes"
|
2017-09-13 17:30:19 +02:00
|
|
|
"context"
|
2020-07-21 16:48:21 +02:00
|
|
|
"fmt"
|
2021-01-13 18:31:46 +01:00
|
|
|
"time"
|
2017-08-21 17:37:11 +02:00
|
|
|
|
2022-01-27 15:29:14 +01:00
|
|
|
fedapi "github.com/matrix-org/dendrite/federationapi/api"
|
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-07-07 13:51:55 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2022-01-28 13:33:31 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/hooks"
|
2017-02-07 18:20:05 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-09-02 14:47:31 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
2017-03-08 16:10:26 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2017-02-09 17:48:14 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2017-02-07 18:20:05 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-10-20 12:42:54 +02:00
|
|
|
"github.com/matrix-org/util"
|
2021-01-13 18:31:46 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-04-28 12:46:47 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-02-07 18:20:05 +01:00
|
|
|
)
|
|
|
|
|
2021-01-13 18:31:46 +01:00
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(processRoomEventDuration)
|
|
|
|
}
|
|
|
|
|
2022-01-27 15:29:14 +01:00
|
|
|
// TODO: Does this value make sense?
|
2022-01-31 11:39:33 +01:00
|
|
|
const MaximumMissingProcessingTime = time.Minute * 2
|
2022-01-27 15:29:14 +01:00
|
|
|
|
2021-01-13 18:31:46 +01:00
|
|
|
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 16:22:06 +01: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 13:51:55 +02:00
|
|
|
// TODO: Break up function - we should probably do transaction ID checks before calling this.
|
|
|
|
// nolint:gocyclo
|
2020-09-02 18:13:15 +02:00
|
|
|
func (r *Inputer) processRoomEvent(
|
2022-01-31 11:39:33 +01:00
|
|
|
ctx context.Context,
|
2020-09-03 16:22:16 +02:00
|
|
|
input *api.InputRoomEvent,
|
2022-01-05 18:44:49 +01:00
|
|
|
) (err error) {
|
2022-01-27 15:29:14 +01:00
|
|
|
select {
|
2022-01-31 11:39:33 +01:00
|
|
|
case <-ctx.Done():
|
2022-01-27 15:29:14 +01:00
|
|
|
// Before we do anything, make sure the context hasn't expired for this pending task.
|
|
|
|
// If it has then we'll give up straight away — it's probably a synchronous input
|
|
|
|
// request and the caller has already given up, but the inbox task was still queued.
|
|
|
|
return context.DeadlineExceeded
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:31:46 +01: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 18:20:05 +01:00
|
|
|
// Parse and validate the event JSON
|
2020-03-27 17:28:22 +01:00
|
|
|
headered := input.Event
|
|
|
|
event := headered.Unwrap()
|
2022-01-27 15:29:14 +01:00
|
|
|
logger := util.GetLogger(ctx).WithFields(logrus.Fields{
|
|
|
|
"event_id": event.EventID(),
|
|
|
|
"room_id": event.RoomID(),
|
|
|
|
"type": event.Type(),
|
|
|
|
})
|
2017-02-07 18:20:05 +01:00
|
|
|
|
2020-10-20 12:42:54 +02: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 13:29:53 +02:00
|
|
|
evs, err2 := r.DB.EventsFromIDs(ctx, []string{event.EventID()})
|
|
|
|
if err2 == nil && len(evs) == 1 {
|
2020-10-20 12:42:54 +02:00
|
|
|
// check hash matches if we're on early room versions where the event ID was a random string
|
2020-10-20 13:29:53 +02:00
|
|
|
idFormat, err2 := headered.RoomVersion.EventIDFormat()
|
|
|
|
if err2 == nil {
|
2020-10-20 12:42:54 +02:00
|
|
|
switch idFormat {
|
|
|
|
case gomatrixserverlib.EventIDFormatV1:
|
|
|
|
if bytes.Equal(event.EventReference().EventSHA256, evs[0].EventReference().EventSHA256) {
|
2022-01-27 15:29:14 +01:00
|
|
|
logger.Debugf("Already processed event; ignoring")
|
2022-01-05 18:44:49 +01:00
|
|
|
return nil
|
2020-10-20 12:42:54 +02:00
|
|
|
}
|
|
|
|
default:
|
2022-01-27 15:29:14 +01:00
|
|
|
logger.Debugf("Already processed event; ignoring")
|
2022-01-05 18:44:49 +01:00
|
|
|
return nil
|
2020-10-20 12:42:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 15:29:14 +01:00
|
|
|
missingRes := &api.QueryMissingAuthPrevEventsResponse{}
|
|
|
|
serverRes := &fedapi.QueryJoinedHostServerNamesInRoomResponse{}
|
|
|
|
if event.Type() != gomatrixserverlib.MRoomCreate || !event.StateKeyEquals("") {
|
|
|
|
missingReq := &api.QueryMissingAuthPrevEventsRequest{
|
|
|
|
RoomID: event.RoomID(),
|
|
|
|
AuthEventIDs: event.AuthEventIDs(),
|
|
|
|
PrevEventIDs: event.PrevEventIDs(),
|
|
|
|
}
|
|
|
|
if err = r.Queryer.QueryMissingAuthPrevEvents(ctx, missingReq, missingRes); err != nil {
|
|
|
|
return fmt.Errorf("r.Queryer.QueryMissingAuthPrevEvents: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(missingRes.MissingAuthEventIDs) > 0 || len(missingRes.MissingPrevEventIDs) > 0 {
|
|
|
|
serverReq := &fedapi.QueryJoinedHostServerNamesInRoomRequest{
|
|
|
|
RoomID: event.RoomID(),
|
|
|
|
ExcludeSelf: true,
|
|
|
|
}
|
|
|
|
if err = r.FSAPI.QueryJoinedHostServerNamesInRoom(ctx, serverReq, serverRes); err != nil {
|
|
|
|
return fmt.Errorf("r.FSAPI.QueryJoinedHostServerNamesInRoom: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if input.Origin != "" {
|
|
|
|
serverRes.ServerNames = append(serverRes.ServerNames, input.Origin)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 14:00:52 +02:00
|
|
|
isRejected := false
|
2022-01-27 15:29:14 +01:00
|
|
|
authEvents := gomatrixserverlib.NewAuthEvents(nil)
|
|
|
|
knownEvents := map[string]*types.Event{}
|
|
|
|
if err = r.fetchAuthEvents(ctx, logger, headered, &authEvents, knownEvents, serverRes.ServerNames); err != nil {
|
|
|
|
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 14:00:52 +02:00
|
|
|
isRejected = true
|
2022-01-27 15:29:14 +01:00
|
|
|
logger.WithError(rejectionErr).Warnf("Event %s rejected", event.EventID())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate the auth event NIDs.
|
|
|
|
authEventIDs := event.AuthEventIDs()
|
|
|
|
authEventNIDs := make([]types.EventNID, 0, len(authEventIDs))
|
|
|
|
for _, authEventID := range authEventIDs {
|
|
|
|
if _, ok := knownEvents[authEventID]; !ok {
|
|
|
|
return fmt.Errorf("missing auth event %s", authEventID)
|
|
|
|
}
|
|
|
|
authEventNIDs = append(authEventNIDs, knownEvents[authEventID].EventNID)
|
2018-05-26 13:03:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-21 15:55:46 +02:00
|
|
|
var softfail bool
|
2020-10-07 15:05:33 +02:00
|
|
|
if input.Kind == api.KindNew {
|
2020-09-21 15:55:46 +02: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 {
|
2022-01-27 15:29:14 +01:00
|
|
|
logger.WithError(err).Info("Error authing soft-failed event")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point we are checking whether we know all of the prev events, and
|
|
|
|
// if we know the state before the prev events. This is necessary before we
|
|
|
|
// try to do `calculateAndSetState` on the event later, otherwise it will fail
|
|
|
|
// with missing event NIDs. If there's anything missing then we'll go and fetch
|
|
|
|
// the prev events and state from the federation. Note that we only do this if
|
|
|
|
// we weren't already told what the state before the event should be — if the
|
|
|
|
// HasState option was set and a state set was provided (as is the case in a
|
|
|
|
// typical federated room join) then we won't bother trying to fetch prev events
|
|
|
|
// because we may not be allowed to see them and we have no choice but to trust
|
|
|
|
// the state event IDs provided to us in the join instead.
|
|
|
|
missingPrev := !input.HasState && len(missingRes.MissingPrevEventIDs) > 0
|
|
|
|
if missingPrev && input.Kind == api.KindNew {
|
|
|
|
// Don't do this for KindOld events, otherwise old events that we fetch
|
|
|
|
// to satisfy missing prev events/state will end up recursively calling
|
|
|
|
// processRoomEvent.
|
|
|
|
if len(serverRes.ServerNames) > 0 {
|
|
|
|
missingState := missingStateReq{
|
|
|
|
origin: input.Origin,
|
|
|
|
inputer: r,
|
|
|
|
queryer: r.Queryer,
|
|
|
|
db: r.DB,
|
|
|
|
federation: r.FSAPI,
|
|
|
|
keys: r.KeyRing,
|
|
|
|
roomsMu: internal.NewMutexByRoom(),
|
|
|
|
servers: map[gomatrixserverlib.ServerName]struct{}{},
|
|
|
|
hadEvents: map[string]bool{},
|
|
|
|
haveEvents: map[string]*gomatrixserverlib.HeaderedEvent{},
|
|
|
|
}
|
|
|
|
for _, serverName := range serverRes.ServerNames {
|
|
|
|
missingState.servers[serverName] = struct{}{}
|
|
|
|
}
|
|
|
|
if err = missingState.processEventWithMissingState(ctx, event, headered.RoomVersion); err != nil {
|
|
|
|
isRejected = true
|
|
|
|
rejectionErr = fmt.Errorf("missingState.processEventWithMissingState: %w", err)
|
|
|
|
} else {
|
|
|
|
missingPrev = false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isRejected = true
|
|
|
|
rejectionErr = fmt.Errorf("missing prev events and no other servers to ask")
|
2020-09-21 15:55:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 18:49:24 +02:00
|
|
|
// Store the event.
|
2021-12-09 16:03:26 +01:00
|
|
|
_, _, stateAtEvent, redactionEvent, redactedEventID, err := r.DB.StoreEvent(ctx, event, authEventNIDs, isRejected)
|
2017-02-15 12:05:45 +01:00
|
|
|
if err != nil {
|
2022-01-05 18:44:49 +01:00
|
|
|
return fmt.Errorf("r.DB.StoreEvent: %w", err)
|
2017-02-09 17:48:14 +01:00
|
|
|
}
|
2020-09-16 14:00:52 +02:00
|
|
|
|
2020-07-07 13:51:55 +02:00
|
|
|
// if storing this event results in it being redacted then do so.
|
2020-09-16 14:00:52 +02:00
|
|
|
if !isRejected && redactedEventID == event.EventID() {
|
2020-11-16 16:44:53 +01:00
|
|
|
r, rerr := eventutil.RedactEvent(redactionEvent, event)
|
2020-07-07 13:51:55 +02:00
|
|
|
if rerr != nil {
|
2022-01-05 18:44:49 +01:00
|
|
|
return fmt.Errorf("eventutil.RedactEvent: %w", rerr)
|
2020-07-07 13:51:55 +02:00
|
|
|
}
|
2020-11-16 16:44:53 +01:00
|
|
|
event = r
|
2020-07-07 13:51:55 +02:00
|
|
|
}
|
2017-02-07 18:20:05 +01:00
|
|
|
|
2020-05-18 18:49:24 +02: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 18:20:05 +01:00
|
|
|
if input.Kind == api.KindOutlier {
|
2022-01-27 15:29:14 +01:00
|
|
|
logger.Debug("Stored outlier")
|
2022-01-28 14:11:56 +01:00
|
|
|
hooks.Run(hooks.KindNewEventPersisted, headered)
|
2022-01-05 18:44:49 +01:00
|
|
|
return nil
|
2017-02-07 18:20:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-02 11:02:48 +02:00
|
|
|
roomInfo, err := r.DB.RoomInfo(ctx, event.RoomID())
|
|
|
|
if err != nil {
|
2022-01-05 18:44:49 +01:00
|
|
|
return fmt.Errorf("r.DB.RoomInfo: %w", err)
|
2020-09-02 11:02:48 +02:00
|
|
|
}
|
|
|
|
if roomInfo == nil {
|
2022-01-05 18:44:49 +01:00
|
|
|
return fmt.Errorf("r.DB.RoomInfo missing for room %s", event.RoomID())
|
2020-09-02 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 15:29:14 +01:00
|
|
|
if !missingPrev && stateAtEvent.BeforeStateSnapshotNID == 0 {
|
2017-02-15 12:05:45 +01:00
|
|
|
// We haven't calculated a state for this event yet.
|
|
|
|
// Lets calculate one.
|
2022-01-27 15:29:14 +01:00
|
|
|
err = r.calculateAndSetState(ctx, input, roomInfo, &stateAtEvent, event, isRejected)
|
|
|
|
if err != nil {
|
2022-01-05 18:44:49 +01:00
|
|
|
return fmt.Errorf("r.calculateAndSetState: %w", err)
|
2017-09-20 11:59:19 +02:00
|
|
|
}
|
2017-02-15 12:05:45 +01:00
|
|
|
}
|
|
|
|
|
2020-09-16 14:00:52 +02: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 15:55:46 +02:00
|
|
|
if isRejected || softfail {
|
2022-01-27 15:29:14 +01:00
|
|
|
logger.WithError(rejectionErr).WithField("soft_fail", softfail).Debug("Stored rejected event")
|
2022-01-05 18:44:49 +01:00
|
|
|
return rejectionErr
|
2020-09-16 14:00:52 +02:00
|
|
|
}
|
|
|
|
|
2020-10-19 15:59:13 +02: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 {
|
2022-01-05 18:44:49 +01:00
|
|
|
return fmt.Errorf("r.updateLatestEvents: %w", err)
|
2020-10-19 15:59:13 +02:00
|
|
|
}
|
|
|
|
case api.KindOld:
|
|
|
|
err = r.WriteOutputEvents(event.RoomID(), []api.OutputEvent{
|
|
|
|
{
|
|
|
|
Type: api.OutputTypeOldRoomEvent,
|
|
|
|
OldRoomEvent: &api.OutputOldRoomEvent{
|
|
|
|
Event: headered,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-01-05 18:44:49 +01:00
|
|
|
return fmt.Errorf("r.WriteOutputEvents (old): %w", err)
|
2020-10-19 15:59:13 +02:00
|
|
|
}
|
2020-05-18 18:49:24 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 13:51:55 +02: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 {
|
2022-01-05 18:44:49 +01:00
|
|
|
return fmt.Errorf("r.WriteOutputEvents (redactions): %w", err)
|
2020-07-07 13:51:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 13:33:31 +01:00
|
|
|
// Everything was OK — the latest events updater didn't error and
|
|
|
|
// we've sent output events. Finally, generate a hook call.
|
|
|
|
hooks.Run(hooks.KindNewEventPersisted, headered)
|
2022-01-05 18:44:49 +01:00
|
|
|
return nil
|
2018-05-26 13:03:35 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 15:29:14 +01:00
|
|
|
// fetchAuthEvents will check to see if any of the
|
|
|
|
// auth events specified by the given event are unknown. If they are
|
|
|
|
// then we will go off and request them from the federation and then
|
|
|
|
// store them in the database. By the time this function ends, either
|
|
|
|
// we've failed to retrieve the auth chain altogether (in which case
|
|
|
|
// an error is returned) or we've successfully retrieved them all and
|
|
|
|
// they are now in the database.
|
|
|
|
func (r *Inputer) fetchAuthEvents(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *logrus.Entry,
|
|
|
|
event *gomatrixserverlib.HeaderedEvent,
|
|
|
|
auth *gomatrixserverlib.AuthEvents,
|
|
|
|
known map[string]*types.Event,
|
|
|
|
servers []gomatrixserverlib.ServerName,
|
|
|
|
) error {
|
2022-01-31 11:39:33 +01:00
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, MaximumMissingProcessingTime)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-01-27 15:29:14 +01:00
|
|
|
unknown := map[string]struct{}{}
|
|
|
|
authEventIDs := event.AuthEventIDs()
|
|
|
|
if len(authEventIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, authEventID := range authEventIDs {
|
|
|
|
authEvents, err := r.DB.EventsFromIDs(ctx, []string{authEventID})
|
|
|
|
if err != nil || len(authEvents) == 0 || authEvents[0].Event == nil {
|
|
|
|
unknown[authEventID] = struct{}{}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ev := authEvents[0]
|
|
|
|
known[authEventID] = &ev // don't take the pointer of the iterated event
|
|
|
|
if err = auth.AddEvent(ev.Event); err != nil {
|
|
|
|
return fmt.Errorf("auth.AddEvent: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no missing auth events then there is nothing more
|
|
|
|
// to do — we've loaded everything that we need.
|
|
|
|
if len(unknown) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var res gomatrixserverlib.RespEventAuth
|
|
|
|
var found bool
|
|
|
|
for _, serverName := range servers {
|
|
|
|
// Request the entire auth chain for the event in question. This should
|
|
|
|
// contain all of the auth events — including ones that we already know —
|
|
|
|
// so we'll need to filter through those in the next section.
|
|
|
|
res, err = r.FSAPI.GetEventAuth(ctx, serverName, event.RoomVersion, event.RoomID(), event.EventID())
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Warnf("Failed to get event auth from federation for %q: %s", event.EventID(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("no servers provided event auth for event ID %q, tried servers %v", event.EventID(), servers)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, authEvent := range gomatrixserverlib.ReverseTopologicalOrdering(
|
|
|
|
res.AuthEvents,
|
|
|
|
gomatrixserverlib.TopologicalOrderByAuthEvents,
|
|
|
|
) {
|
|
|
|
// If we already know about this event from the database then we don't
|
|
|
|
// need to store it again or do anything further with it, so just skip
|
|
|
|
// over it rather than wasting cycles.
|
|
|
|
if ev, ok := known[authEvent.EventID()]; ok && ev != nil {
|
|
|
|
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 := authEvent.VerifyEventSignatures(ctx, r.FSAPI.KeyRing()); err != nil {
|
|
|
|
return fmt.Errorf("event.VerifyEventSignatures: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to store the new auth event, we need to know its auth chain
|
|
|
|
// as NIDs for the `auth_event_nids` column. Let's see if we can find those.
|
|
|
|
authEventNIDs := make([]types.EventNID, 0, len(authEvent.AuthEventIDs()))
|
|
|
|
for _, eventID := range authEvent.AuthEventIDs() {
|
|
|
|
knownEvent, ok := known[eventID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("missing auth event %s for %s", eventID, authEvent.EventID())
|
|
|
|
}
|
|
|
|
authEventNIDs = append(authEventNIDs, knownEvent.EventNID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's take a note of the fact that we now know about this event.
|
|
|
|
if err := auth.AddEvent(authEvent); err != nil {
|
|
|
|
return fmt.Errorf("auth.AddEvent: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the auth event should be rejected.
|
|
|
|
isRejected := false
|
|
|
|
if err := gomatrixserverlib.Allowed(authEvent, auth); err != nil {
|
|
|
|
isRejected = true
|
|
|
|
logger.WithError(err).Warnf("Auth event %s rejected", authEvent.EventID())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, store the event in the database.
|
|
|
|
eventNID, _, _, _, _, err := r.DB.StoreEvent(ctx, authEvent, authEventNIDs, isRejected)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.DB.StoreEvent: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we know about this event, it was stored and the signatures were OK.
|
|
|
|
known[authEvent.EventID()] = &types.Event{
|
|
|
|
EventNID: eventNID,
|
|
|
|
Event: authEvent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:13:15 +02:00
|
|
|
func (r *Inputer) calculateAndSetState(
|
2018-05-26 13:03:35 +02:00
|
|
|
ctx context.Context,
|
2020-09-03 16:22:16 +02:00
|
|
|
input *api.InputRoomEvent,
|
2022-01-27 15:29:14 +01:00
|
|
|
roomInfo *types.RoomInfo,
|
2018-05-26 13:03:35 +02:00
|
|
|
stateAtEvent *types.StateAtEvent,
|
2020-11-16 16:44:53 +01:00
|
|
|
event *gomatrixserverlib.Event,
|
2020-09-16 14:00:52 +02:00
|
|
|
isRejected bool,
|
2018-05-26 13:03:35 +02:00
|
|
|
) error {
|
2020-03-19 19:33:04 +01:00
|
|
|
var err error
|
2020-09-02 11:02:48 +02:00
|
|
|
roomState := state.NewStateResolution(r.DB, roomInfo)
|
2020-02-05 17:25:58 +01:00
|
|
|
|
2020-09-16 14:00:52 +02:00
|
|
|
if input.HasState && !isRejected {
|
2020-05-20 19:03:06 +02:00
|
|
|
// Check here if we think we're in the room already.
|
2020-05-18 18:49:24 +02:00
|
|
|
stateAtEvent.Overwrite = true
|
2020-05-20 19:03:06 +02:00
|
|
|
var joinEventNIDs []types.EventNID
|
|
|
|
// Request join memberships only for local users only.
|
2020-09-02 11:02:48 +02:00
|
|
|
if joinEventNIDs, err = r.DB.GetMembershipEventNIDsForRoom(ctx, roomInfo.RoomNID, true, true); err == nil {
|
2020-05-20 19:03:06 +02: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 18:49:24 +02:00
|
|
|
|
2018-05-26 13:03:35 +02: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 19:03:06 +02:00
|
|
|
if entries, err = r.DB.StateEntriesForEventIDs(ctx, input.StateEventIDs); err != nil {
|
2020-09-15 12:17:46 +02:00
|
|
|
return fmt.Errorf("r.DB.StateEntriesForEventIDs: %w", err)
|
2018-05-26 13:03:35 +02:00
|
|
|
}
|
2020-09-15 12:17:46 +02:00
|
|
|
entries = types.DeduplicateStateEntries(entries)
|
2018-05-26 13:03:35 +02:00
|
|
|
|
2020-09-02 11:02:48 +02:00
|
|
|
if stateAtEvent.BeforeStateSnapshotNID, err = r.DB.AddState(ctx, roomInfo.RoomNID, nil, entries); err != nil {
|
2020-09-15 12:17:46 +02:00
|
|
|
return fmt.Errorf("r.DB.AddState: %w", err)
|
2018-05-26 13:03:35 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-18 18:49:24 +02:00
|
|
|
stateAtEvent.Overwrite = false
|
|
|
|
|
2018-05-26 13:03:35 +02: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 14:00:52 +02:00
|
|
|
if stateAtEvent.BeforeStateSnapshotNID, err = roomState.CalculateAndStoreStateBeforeEvent(ctx, event, isRejected); err != nil {
|
2020-09-15 12:17:46 +02:00
|
|
|
return fmt.Errorf("roomState.CalculateAndStoreStateBeforeEvent: %w", err)
|
2018-05-26 13:03:35 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 12:17:46 +02: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 17:37:11 +02:00
|
|
|
}
|