2017-04-21 00:40:52 +02: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.
|
|
|
|
|
2020-09-02 14:47:31 +02:00
|
|
|
package helpers
|
2017-02-10 11:44:38 +01:00
|
|
|
|
|
|
|
import (
|
2017-09-13 17:30:19 +02:00
|
|
|
"context"
|
2020-09-21 15:55:46 +02:00
|
|
|
"fmt"
|
2017-09-13 17:30:19 +02:00
|
|
|
"sort"
|
|
|
|
|
2022-10-14 09:14:54 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-10-14 09:14:54 +02:00
|
|
|
|
2020-09-21 15:55:46 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2022-02-23 16:41:32 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2017-02-10 11:44:38 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
)
|
|
|
|
|
2020-09-21 15:55:46 +02:00
|
|
|
// CheckForSoftFail returns true if the event should be soft-failed
|
|
|
|
// and false otherwise. The return error value should be checked before
|
|
|
|
// the soft-fail bool.
|
|
|
|
func CheckForSoftFail(
|
|
|
|
ctx context.Context,
|
2023-02-24 09:40:20 +01:00
|
|
|
db storage.RoomDatabase,
|
|
|
|
roomInfo *types.RoomInfo,
|
2023-04-27 13:54:20 +02:00
|
|
|
event *types.HeaderedEvent,
|
2020-09-21 15:55:46 +02:00
|
|
|
stateEventIDs []string,
|
|
|
|
) (bool, error) {
|
|
|
|
rewritesState := len(stateEventIDs) > 1
|
|
|
|
|
|
|
|
var authStateEntries []types.StateEntry
|
|
|
|
var err error
|
|
|
|
if rewritesState {
|
2022-08-18 18:06:13 +02:00
|
|
|
authStateEntries, err = db.StateEntriesForEventIDs(ctx, stateEventIDs, true)
|
2020-09-21 15:55:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return true, fmt.Errorf("StateEntriesForEventIDs failed: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Then get the state entries for the current state snapshot.
|
|
|
|
// We'll use this to check if the event is allowed right now.
|
2022-01-27 15:29:14 +01:00
|
|
|
roomState := state.NewStateResolution(db, roomInfo)
|
2022-08-01 16:29:19 +02:00
|
|
|
authStateEntries, err = roomState.LoadStateAtSnapshot(ctx, roomInfo.StateSnapshotNID())
|
2020-09-21 15:55:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return true, fmt.Errorf("roomState.LoadStateAtSnapshot: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// As a special case, it's possible that the room will have no
|
|
|
|
// state because we haven't received a m.room.create event yet.
|
|
|
|
// If we're now processing the first create event then never
|
|
|
|
// soft-fail it.
|
2023-04-19 16:50:33 +02:00
|
|
|
if len(authStateEntries) == 0 && event.Type() == spec.MRoomCreate {
|
2020-09-21 15:55:46 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Work out which of the state events we actually need.
|
2023-04-27 17:35:19 +02:00
|
|
|
stateNeeded := gomatrixserverlib.StateNeededForAuth(
|
2023-05-02 16:03:16 +02:00
|
|
|
[]gomatrixserverlib.PDU{event.PDU},
|
2023-04-27 17:35:19 +02:00
|
|
|
)
|
2020-09-21 15:55:46 +02:00
|
|
|
|
|
|
|
// Load the actual auth events from the database.
|
2023-05-31 18:33:49 +02:00
|
|
|
authEvents, err := loadAuthEvents(ctx, db, roomInfo.RoomVersion, stateNeeded, authStateEntries)
|
2020-09-21 15:55:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return true, fmt.Errorf("loadAuthEvents: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the event is allowed.
|
2023-06-07 19:14:35 +02:00
|
|
|
if err = gomatrixserverlib.Allowed(event.PDU, &authEvents, func(roomID string, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-06 22:55:18 +02:00
|
|
|
return db.GetUserIDForSender(ctx, roomID, senderID)
|
|
|
|
}); err != nil {
|
2020-09-21 15:55:46 +02:00
|
|
|
// return true, nil
|
2020-09-29 14:40:29 +02:00
|
|
|
return true, err
|
2020-09-21 15:55:46 +02:00
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2023-05-31 18:33:49 +02:00
|
|
|
// GetAuthEvents returns the numeric IDs for the auth events.
|
|
|
|
func GetAuthEvents(
|
2017-09-13 17:30:19 +02:00
|
|
|
ctx context.Context,
|
2023-02-24 09:40:20 +01:00
|
|
|
db storage.RoomDatabase,
|
2023-05-31 18:33:49 +02:00
|
|
|
roomVersion gomatrixserverlib.RoomVersion,
|
|
|
|
event gomatrixserverlib.PDU,
|
2017-09-13 17:30:19 +02:00
|
|
|
authEventIDs []string,
|
2023-05-31 18:33:49 +02:00
|
|
|
) (gomatrixserverlib.AuthEventProvider, error) {
|
2017-02-10 11:44:38 +01:00
|
|
|
// Grab the numeric IDs for the supplied auth state events from the database.
|
2022-08-18 18:06:13 +02:00
|
|
|
authStateEntries, err := db.StateEntriesForEventIDs(ctx, authEventIDs, true)
|
2017-02-10 11:44:38 +01:00
|
|
|
if err != nil {
|
2020-09-29 14:40:29 +02:00
|
|
|
return nil, fmt.Errorf("db.StateEntriesForEventIDs: %w", err)
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
2020-09-15 12:17:46 +02:00
|
|
|
authStateEntries = types.DeduplicateStateEntries(authStateEntries)
|
2017-02-10 11:44:38 +01:00
|
|
|
|
|
|
|
// Work out which of the state events we actually need.
|
2023-05-31 18:33:49 +02:00
|
|
|
stateNeeded := gomatrixserverlib.StateNeededForAuth([]gomatrixserverlib.PDU{event})
|
2017-02-10 11:44:38 +01:00
|
|
|
|
|
|
|
// Load the actual auth events from the database.
|
2023-05-31 18:33:49 +02:00
|
|
|
authEvents, err := loadAuthEvents(ctx, db, roomVersion, stateNeeded, authStateEntries)
|
2017-02-10 11:44:38 +01:00
|
|
|
if err != nil {
|
2020-09-29 14:40:29 +02:00
|
|
|
return nil, fmt.Errorf("loadAuthEvents: %w", err)
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
2023-05-31 18:33:49 +02:00
|
|
|
return &authEvents, nil
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type authEvents struct {
|
2017-02-15 12:05:45 +01:00
|
|
|
stateKeyNIDMap map[string]types.EventStateKeyNID
|
2017-02-10 11:44:38 +01:00
|
|
|
state stateEntryMap
|
2020-09-02 14:47:31 +02:00
|
|
|
events EventMap
|
2022-10-14 09:14:54 +02:00
|
|
|
valid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid verifies that all auth events are from the same room.
|
|
|
|
func (ae *authEvents) Valid() bool {
|
|
|
|
return ae.valid
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 18:54:17 +01:00
|
|
|
// Create implements gomatrixserverlib.AuthEventProvider
|
2023-04-27 17:35:19 +02:00
|
|
|
func (ae *authEvents) Create() (gomatrixserverlib.PDU, error) {
|
2017-02-10 11:44:38 +01:00
|
|
|
return ae.lookupEventWithEmptyStateKey(types.MRoomCreateNID), nil
|
|
|
|
}
|
|
|
|
|
2017-03-10 18:54:17 +01:00
|
|
|
// PowerLevels implements gomatrixserverlib.AuthEventProvider
|
2023-04-27 17:35:19 +02:00
|
|
|
func (ae *authEvents) PowerLevels() (gomatrixserverlib.PDU, error) {
|
2017-02-10 11:44:38 +01:00
|
|
|
return ae.lookupEventWithEmptyStateKey(types.MRoomPowerLevelsNID), nil
|
|
|
|
}
|
|
|
|
|
2017-03-10 18:54:17 +01:00
|
|
|
// JoinRules implements gomatrixserverlib.AuthEventProvider
|
2023-04-27 17:35:19 +02:00
|
|
|
func (ae *authEvents) JoinRules() (gomatrixserverlib.PDU, error) {
|
2017-02-10 11:44:38 +01:00
|
|
|
return ae.lookupEventWithEmptyStateKey(types.MRoomJoinRulesNID), nil
|
|
|
|
}
|
|
|
|
|
2017-03-10 18:54:17 +01:00
|
|
|
// Memmber implements gomatrixserverlib.AuthEventProvider
|
2023-06-07 19:14:35 +02:00
|
|
|
func (ae *authEvents) Member(stateKey spec.SenderID) (gomatrixserverlib.PDU, error) {
|
|
|
|
return ae.lookupEvent(types.MRoomMemberNID, string(stateKey)), nil
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 18:54:17 +01:00
|
|
|
// ThirdPartyInvite implements gomatrixserverlib.AuthEventProvider
|
2023-04-27 17:35:19 +02:00
|
|
|
func (ae *authEvents) ThirdPartyInvite(stateKey string) (gomatrixserverlib.PDU, error) {
|
2017-02-10 11:44:38 +01:00
|
|
|
return ae.lookupEvent(types.MRoomThirdPartyInviteNID, stateKey), nil
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:35:19 +02:00
|
|
|
func (ae *authEvents) lookupEventWithEmptyStateKey(typeNID types.EventTypeNID) gomatrixserverlib.PDU {
|
2017-09-13 17:30:19 +02:00
|
|
|
eventNID, ok := ae.state.lookup(types.StateKeyTuple{
|
|
|
|
EventTypeNID: typeNID,
|
|
|
|
EventStateKeyNID: types.EmptyStateKeyNID,
|
|
|
|
})
|
2017-02-10 11:44:38 +01:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-02 14:47:31 +02:00
|
|
|
event, ok := ae.events.Lookup(eventNID)
|
2017-02-10 11:44:38 +01:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-02 16:03:16 +02:00
|
|
|
return event.PDU
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:35:19 +02:00
|
|
|
func (ae *authEvents) lookupEvent(typeNID types.EventTypeNID, stateKey string) gomatrixserverlib.PDU {
|
2017-02-10 11:44:38 +01:00
|
|
|
stateKeyNID, ok := ae.stateKeyNIDMap[stateKey]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-13 17:30:19 +02:00
|
|
|
eventNID, ok := ae.state.lookup(types.StateKeyTuple{
|
|
|
|
EventTypeNID: typeNID,
|
|
|
|
EventStateKeyNID: stateKeyNID,
|
|
|
|
})
|
2017-02-10 11:44:38 +01:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-02 14:47:31 +02:00
|
|
|
event, ok := ae.events.Lookup(eventNID)
|
2017-02-10 11:44:38 +01:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-02 16:03:16 +02:00
|
|
|
return event.PDU
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadAuthEvents loads the events needed for authentication from the supplied room state.
|
|
|
|
func loadAuthEvents(
|
2017-09-13 17:30:19 +02:00
|
|
|
ctx context.Context,
|
2022-02-04 11:39:34 +01:00
|
|
|
db state.StateResolutionStorage,
|
2023-05-31 18:33:49 +02:00
|
|
|
roomVersion gomatrixserverlib.RoomVersion,
|
2017-02-10 11:44:38 +01:00
|
|
|
needed gomatrixserverlib.StateNeeded,
|
|
|
|
state []types.StateEntry,
|
|
|
|
) (result authEvents, err error) {
|
2022-10-14 09:14:54 +02:00
|
|
|
result.valid = true
|
2017-08-23 16:08:48 +02:00
|
|
|
// Look up the numeric IDs for the state keys needed for auth.
|
2017-02-10 11:44:38 +01:00
|
|
|
var neededStateKeys []string
|
|
|
|
neededStateKeys = append(neededStateKeys, needed.Member...)
|
|
|
|
neededStateKeys = append(neededStateKeys, needed.ThirdPartyInvite...)
|
2017-09-13 17:30:19 +02:00
|
|
|
if result.stateKeyNIDMap, err = db.EventStateKeyNIDs(ctx, neededStateKeys); err != nil {
|
2017-02-10 11:44:38 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the events we need.
|
|
|
|
result.state = state
|
2017-02-15 12:05:45 +01:00
|
|
|
var eventNIDs []types.EventNID
|
2017-02-10 11:44:38 +01:00
|
|
|
keyTuplesNeeded := stateKeyTuplesNeeded(result.stateKeyNIDMap, needed)
|
|
|
|
for _, keyTuple := range keyTuplesNeeded {
|
|
|
|
eventNID, ok := result.state.lookup(keyTuple)
|
|
|
|
if ok {
|
|
|
|
eventNIDs = append(eventNIDs, eventNID)
|
|
|
|
}
|
|
|
|
}
|
2023-05-08 21:25:44 +02:00
|
|
|
|
2023-05-31 18:33:49 +02:00
|
|
|
if result.events, err = db.Events(ctx, roomVersion, eventNIDs); err != nil {
|
2017-02-10 11:44:38 +01:00
|
|
|
return
|
|
|
|
}
|
2022-10-14 09:14:54 +02:00
|
|
|
roomID := ""
|
|
|
|
for _, ev := range result.events {
|
|
|
|
if roomID == "" {
|
|
|
|
roomID = ev.RoomID()
|
|
|
|
}
|
|
|
|
if ev.RoomID() != roomID {
|
|
|
|
result.valid = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 11:44:38 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// stateKeyTuplesNeeded works out which numeric state key tuples we need to authenticate some events.
|
2017-09-13 17:30:19 +02:00
|
|
|
func stateKeyTuplesNeeded(
|
|
|
|
stateKeyNIDMap map[string]types.EventStateKeyNID,
|
|
|
|
stateNeeded gomatrixserverlib.StateNeeded,
|
|
|
|
) []types.StateKeyTuple {
|
2017-02-10 11:44:38 +01:00
|
|
|
var keyTuples []types.StateKeyTuple
|
|
|
|
if stateNeeded.Create {
|
2017-09-13 17:30:19 +02:00
|
|
|
keyTuples = append(keyTuples, types.StateKeyTuple{
|
|
|
|
EventTypeNID: types.MRoomCreateNID,
|
|
|
|
EventStateKeyNID: types.EmptyStateKeyNID,
|
|
|
|
})
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
|
|
|
if stateNeeded.PowerLevels {
|
2017-09-13 17:30:19 +02:00
|
|
|
keyTuples = append(keyTuples, types.StateKeyTuple{
|
|
|
|
EventTypeNID: types.MRoomPowerLevelsNID,
|
|
|
|
EventStateKeyNID: types.EmptyStateKeyNID,
|
|
|
|
})
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
|
|
|
if stateNeeded.JoinRules {
|
2017-09-13 17:30:19 +02:00
|
|
|
keyTuples = append(keyTuples, types.StateKeyTuple{
|
|
|
|
EventTypeNID: types.MRoomJoinRulesNID,
|
|
|
|
EventStateKeyNID: types.EmptyStateKeyNID,
|
|
|
|
})
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
|
|
|
for _, member := range stateNeeded.Member {
|
|
|
|
stateKeyNID, ok := stateKeyNIDMap[member]
|
|
|
|
if ok {
|
2017-09-13 17:30:19 +02:00
|
|
|
keyTuples = append(keyTuples, types.StateKeyTuple{
|
|
|
|
EventTypeNID: types.MRoomMemberNID,
|
|
|
|
EventStateKeyNID: stateKeyNID,
|
|
|
|
})
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, token := range stateNeeded.ThirdPartyInvite {
|
|
|
|
stateKeyNID, ok := stateKeyNIDMap[token]
|
|
|
|
if ok {
|
2017-09-13 17:30:19 +02:00
|
|
|
keyTuples = append(keyTuples, types.StateKeyTuple{
|
|
|
|
EventTypeNID: types.MRoomThirdPartyInviteNID,
|
|
|
|
EventStateKeyNID: stateKeyNID,
|
|
|
|
})
|
2017-02-10 11:44:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return keyTuples
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map from event type, state key tuple to numeric event ID.
|
|
|
|
// Implemented using binary search on a sorted array.
|
|
|
|
type stateEntryMap []types.StateEntry
|
|
|
|
|
|
|
|
// lookup an entry in the event map.
|
2017-02-15 12:05:45 +01:00
|
|
|
func (m stateEntryMap) lookup(stateKey types.StateKeyTuple) (eventNID types.EventNID, ok bool) {
|
2017-02-10 11:44:38 +01:00
|
|
|
// Since the list is sorted we can implement this using binary search.
|
|
|
|
// This is faster than using a hash map.
|
|
|
|
// We don't have to worry about pathological cases because the keys are fixed
|
|
|
|
// size and are controlled by us.
|
|
|
|
list := []types.StateEntry(m)
|
|
|
|
i := sort.Search(len(list), func(i int) bool {
|
|
|
|
return !list[i].StateKeyTuple.LessThan(stateKey)
|
|
|
|
})
|
|
|
|
if i < len(list) && list[i].StateKeyTuple == stateKey {
|
|
|
|
ok = true
|
|
|
|
eventNID = list[i].EventNID
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map from numeric event ID to event.
|
|
|
|
// Implemented using binary search on a sorted array.
|
2020-09-02 14:47:31 +02:00
|
|
|
type EventMap []types.Event
|
2017-02-10 11:44:38 +01:00
|
|
|
|
|
|
|
// lookup an entry in the event map.
|
2020-09-02 14:47:31 +02:00
|
|
|
func (m EventMap) Lookup(eventNID types.EventNID) (event *types.Event, ok bool) {
|
2017-02-10 11:44:38 +01:00
|
|
|
// Since the list is sorted we can implement this using binary search.
|
|
|
|
// This is faster than using a hash map.
|
|
|
|
// We don't have to worry about pathological cases because the keys are fixed
|
|
|
|
// size are controlled by us.
|
|
|
|
list := []types.Event(m)
|
|
|
|
i := sort.Search(len(list), func(i int) bool {
|
|
|
|
return list[i].EventNID >= eventNID
|
|
|
|
})
|
|
|
|
if i < len(list) && list[i].EventNID == eventNID {
|
|
|
|
ok = true
|
|
|
|
event = &list[i]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|