2022-10-14 09:14:54 +02:00
|
|
|
package input
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_EventAuth(t *testing.T) {
|
|
|
|
alice := test.NewUser(t)
|
|
|
|
bob := test.NewUser(t)
|
|
|
|
|
|
|
|
// create two rooms, so we can craft "illegal" auth events
|
|
|
|
room1 := test.NewRoom(t, alice)
|
|
|
|
room2 := test.NewRoom(t, alice, test.RoomPreset(test.PresetPublicChat))
|
|
|
|
|
|
|
|
authEventIDs := make([]string, 0, 4)
|
2023-05-02 16:03:16 +02:00
|
|
|
authEvents := []gomatrixserverlib.PDU{}
|
2022-10-14 09:14:54 +02:00
|
|
|
|
|
|
|
// Add the legal auth events from room2
|
|
|
|
for _, x := range room2.Events() {
|
2023-04-19 16:50:33 +02:00
|
|
|
if x.Type() == spec.MRoomCreate {
|
2022-10-14 09:14:54 +02:00
|
|
|
authEventIDs = append(authEventIDs, x.EventID())
|
2023-05-02 16:03:16 +02:00
|
|
|
authEvents = append(authEvents, x.PDU)
|
2022-10-14 09:14:54 +02:00
|
|
|
}
|
2023-04-19 16:50:33 +02:00
|
|
|
if x.Type() == spec.MRoomPowerLevels {
|
2022-10-14 09:14:54 +02:00
|
|
|
authEventIDs = append(authEventIDs, x.EventID())
|
2023-05-02 16:03:16 +02:00
|
|
|
authEvents = append(authEvents, x.PDU)
|
2022-10-14 09:14:54 +02:00
|
|
|
}
|
2023-04-19 16:50:33 +02:00
|
|
|
if x.Type() == spec.MRoomJoinRules {
|
2022-10-14 09:14:54 +02:00
|
|
|
authEventIDs = append(authEventIDs, x.EventID())
|
2023-05-02 16:03:16 +02:00
|
|
|
authEvents = append(authEvents, x.PDU)
|
2022-10-14 09:14:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the illegal auth event from room1 (rooms are different)
|
|
|
|
for _, x := range room1.Events() {
|
2023-04-19 16:50:33 +02:00
|
|
|
if x.Type() == spec.MRoomMember {
|
2022-10-14 09:14:54 +02:00
|
|
|
authEventIDs = append(authEventIDs, x.EventID())
|
2023-05-02 16:03:16 +02:00
|
|
|
authEvents = append(authEvents, x.PDU)
|
2022-10-14 09:14:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Craft the illegal join event, with auth events from different rooms
|
|
|
|
ev := room2.CreateEvent(t, bob, "m.room.member", map[string]interface{}{
|
|
|
|
"membership": "join",
|
|
|
|
}, test.WithStateKey(bob.ID), test.WithAuthIDs(authEventIDs))
|
|
|
|
|
|
|
|
// Add the auth events to the allower
|
|
|
|
allower := gomatrixserverlib.NewAuthEvents(nil)
|
|
|
|
for _, a := range authEvents {
|
|
|
|
if err := allower.AddEvent(a); err != nil {
|
|
|
|
t.Fatalf("allower.AddEvent failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally check that the event is NOT allowed
|
2023-05-02 16:03:16 +02:00
|
|
|
if err := gomatrixserverlib.Allowed(ev.PDU, &allower); err == nil {
|
2022-10-14 09:14:54 +02:00
|
|
|
t.Fatalf("event should not be allowed, but it was")
|
|
|
|
}
|
|
|
|
}
|