-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathv12_test.go
More file actions
1410 lines (1344 loc) · 50 KB
/
v12_test.go
File metadata and controls
1410 lines (1344 loc) · 50 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tests
import (
"encoding/json"
"fmt"
"math"
"net/url"
"slices"
"testing"
"time"
"github.com/matrix-org/complement"
"github.com/matrix-org/complement/b"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/ct"
"github.com/matrix-org/complement/federation"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/match"
"github.com/matrix-org/complement/must"
"github.com/matrix-org/complement/runtime"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/fclient"
"github.com/matrix-org/gomatrixserverlib/spec"
"github.com/matrix-org/util"
"github.com/tidwall/gjson"
)
var maxCanonicalJSONInt = math.Pow(2, 53) - 1
const roomVersion12 = "12"
var V12ServerRoom = federation.ServerRoomImplCustom{
ProtoEventCreatorFn: Protov12EventCreator,
}
// Override how Complement makes proto events so we can conditionally disable/enable the inclusion of the create event
// depending on whether we're running in combined mode or not.
// Complement also doesn't set the room version correctly on the ProtoEvent as this was a new addition to GMSL.
func Protov12EventCreator(def federation.ServerRoomImpl, room *federation.ServerRoom, ev federation.Event) (*gomatrixserverlib.ProtoEvent, error) {
var prevEvents interface{}
if ev.PrevEvents != nil {
// We deliberately want to set the prev events.
prevEvents = ev.PrevEvents
} else {
// No other prev events were supplied so we'll just
// use the forward extremities of the room, which is
// the usual behaviour.
prevEvents = room.ForwardExtremities
}
proto := gomatrixserverlib.ProtoEvent{
SenderID: ev.Sender,
Depth: int64(room.Depth + 1), // depth starts at 1
Type: ev.Type,
StateKey: ev.StateKey,
RoomID: room.RoomID,
PrevEvents: prevEvents,
AuthEvents: ev.AuthEvents,
Redacts: ev.Redacts,
Version: gomatrixserverlib.MustGetRoomVersion(room.Version),
}
if err := proto.SetContent(ev.Content); err != nil {
return nil, fmt.Errorf("EventCreator: failed to marshal event content: %s - %+v", err, ev.Content)
}
if err := proto.SetUnsigned(ev.Content); err != nil {
return nil, fmt.Errorf("EventCreator: failed to marshal event unsigned: %s - %+v", err, ev.Unsigned)
}
if proto.AuthEvents == nil {
var stateNeeded gomatrixserverlib.StateNeeded
// this does the right thing for v12
stateNeeded, err := gomatrixserverlib.StateNeededForProtoEvent(&proto)
if err != nil {
return nil, fmt.Errorf("EventCreator: failed to work out auth_events : %s", err)
}
// we never include the create event if the HS supports MSC4291
stateNeeded.Create = false
proto.AuthEvents = room.AuthEvents(stateNeeded)
}
return &proto, nil
}
// Test that the creator can kick an admin created both via
// trusted_private_chat and by explicit promotion, including beyond PL100.
// Also checks the creator isn't in the PL event.
func TestMSC4289PrivilegedRoomCreators(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "alice",
})
bob := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "bob",
})
kickBob := func(roomID string) {
t.Helper()
alice.MustDo(t,
"POST", []string{"_matrix", "client", "v3", "rooms", roomID, "kick"},
client.WithJSONBody(t, map[string]any{
"user_id": bob.UserID,
}),
)
}
t.Run("PL event is missing creator in users map", func(t *testing.T) {
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
})
content := alice.MustGetStateEventContent(t, roomID, spec.MRoomPowerLevels, "")
must.MatchGJSON(t, content, match.JSONKeyEqual("users", map[string]any{}))
})
t.Run("m.room.tombstone needs PL150 in the PL event", func(t *testing.T) {
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
})
content := alice.MustGetStateEventContent(t, roomID, spec.MRoomPowerLevels, "")
must.MatchGJSON(t, content, match.JSONKeyEqual("events."+client.GjsonEscape("m.room.tombstone"), 150))
})
t.Run("creator cannot set self in PL event", func(t *testing.T) {
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
})
resp := alice.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "state", spec.MRoomPowerLevels, ""}, client.WithJSONBody(t, map[string]any{
"users": map[string]int{
alice.UserID: 100,
},
}))
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 400,
})
})
t.Run("creator can kick admin", func(t *testing.T) {
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"invite": []string{bob.UserID},
})
bob.MustJoinRoom(t, roomID, []spec.ServerName{"hs1"})
alice.SendEventSynced(t, roomID, b.Event{
Type: spec.MRoomPowerLevels,
StateKey: b.Ptr(""),
Content: map[string]interface{}{
"users": map[string]any{
bob.UserID: 100,
},
},
})
kickBob(roomID)
})
t.Run("creator can kick admin above PL100", func(t *testing.T) {
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"invite": []string{bob.UserID},
})
bob.MustJoinRoom(t, roomID, []spec.ServerName{"hs1"})
alice.SendEventSynced(t, roomID, b.Event{
Type: spec.MRoomPowerLevels,
StateKey: b.Ptr(""),
Content: map[string]interface{}{
"users": map[string]any{
bob.UserID: 949342,
},
},
})
kickBob(roomID)
})
t.Run("creator can kick admin at JSON max value", func(t *testing.T) {
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"invite": []string{bob.UserID},
})
bob.MustJoinRoom(t, roomID, []spec.ServerName{"hs1"})
alice.SendEventSynced(t, roomID, b.Event{
Type: spec.MRoomPowerLevels,
StateKey: b.Ptr(""),
Content: map[string]interface{}{
"users": map[string]any{
bob.UserID: maxCanonicalJSONInt,
},
},
})
kickBob(roomID)
})
// technically not a MSC4289 thing but implementations may set the creator PL to be
// above the value expressible in canonical JSON to implement "infinite".
t.Run("power level cannot be set beyond max canonical JSON int", func(t *testing.T) {
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"preset": "public_chat",
"invite": []string{bob.UserID},
})
bob.MustJoinRoom(t, roomID, []spec.ServerName{"hs1"})
resp := alice.Do(
t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "state", spec.MRoomPowerLevels, ""},
client.WithJSONBody(t, map[string]interface{}{
"users": map[string]any{
bob.UserID: maxCanonicalJSONInt + 1,
},
}),
)
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 400,
})
})
t.Run("admin with >PL100 cannot kick creator", func(t *testing.T) {
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"invite": []string{bob.UserID},
})
bob.MustJoinRoom(t, roomID, []spec.ServerName{"hs1"})
alice.SendEventSynced(t, roomID, b.Event{
Type: spec.MRoomPowerLevels,
StateKey: b.Ptr(""),
Content: map[string]interface{}{
"users": map[string]any{
bob.UserID: maxCanonicalJSONInt,
},
},
})
resp := bob.Do(t,
"POST", []string{"_matrix", "client", "v3", "rooms", roomID, "kick"},
client.WithJSONBody(t, map[string]any{
"user_id": alice.UserID,
}),
)
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 403,
JSON: []match.JSON{
match.JSONKeyEqual("errcode", "M_FORBIDDEN"),
},
})
})
t.Run("admin with >PL100 sorts after the room creator for state resolution", func(t *testing.T) {
srv := federation.NewServer(t, deployment,
federation.HandleKeyRequests(),
federation.HandleMakeSendJoinRequests(),
federation.HandleTransactionRequests(nil, nil),
federation.HandleEventRequests(),
)
srv.UnexpectedRequestsAreErrors = false
cancel := srv.Listen()
defer cancel()
bob := srv.UserID("bob")
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"preset": "public_chat",
})
room := srv.MustJoinRoom(t, deployment, "hs1", roomID, bob, federation.WithRoomOpts(federation.WithImpl(&V12ServerRoom)))
plEventID := alice.SendEventSynced(t, roomID, b.Event{
Type: spec.MRoomPowerLevels,
StateKey: b.Ptr(""),
Content: map[string]interface{}{
"users": map[string]any{
bob: 9493420,
},
},
})
room.WaiterForEvent(plEventID).Waitf(t, 5*time.Second, "failed to see PL event giving bob >PL100")
alice.SendEventSynced(t, roomID, b.Event{
Type: spec.MRoomJoinRules,
StateKey: b.Ptr(""),
Content: map[string]any{
"join_rule": spec.Invite,
},
})
// Bob concurrently sets the join rule to 'knock'.
// State resolution will apply power events (join rules) from highest PL to lowest
// so ensure the end result is Bob's 'knock'.
bobJREvent := srv.MustCreateEvent(t, room, federation.Event{
Type: spec.MRoomJoinRules,
StateKey: b.Ptr(""),
Content: map[string]any{
"join_rule": spec.Knock,
},
PrevEvents: []string{plEventID},
Sender: bob,
})
room.AddEvent(bobJREvent)
srv.MustSendTransaction(t, deployment, "hs1", []json.RawMessage{bobJREvent.JSON()}, nil)
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncTimelineHasEventID(roomID, bobJREvent.EventID()))
joinRuleContent := alice.MustGetStateEventContent(t, roomID, spec.MRoomJoinRules, "")
must.MatchGJSON(t, joinRuleContent, match.JSONKeyEqual("join_rule", "knock"))
})
// Some servers may apply validation to ensure the creator appears in the power_level_content_override,
// which for v12 rooms is wrong.
t.Run("power_level_content_override can be set", func(t *testing.T) {
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"invite": []string{bob.UserID},
"power_level_content_override": map[string]any{
"users": map[string]int{
bob.UserID: 100,
},
},
})
plContent := alice.MustGetStateEventContent(t, roomID, spec.MRoomPowerLevels, "")
must.MatchGJSON(t, plContent, match.JSONKeyEqual("users", map[string]float64{
bob.UserID: 100,
}))
})
t.Run("power_level_content_override cannot set the room creator", func(t *testing.T) {
resp := alice.CreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"invite": []string{bob.UserID},
"power_level_content_override": map[string]any{
"users": map[string]int{
alice.UserID: 100,
},
},
})
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 400,
})
})
}
// Check that additional_creators works in the happy case
func TestMSC4289PrivilegedRoomCreators_Additional(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "alice",
})
bob := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "bob",
})
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"preset": "public_chat",
"creation_content": map[string]any{
"additional_creators": []string{bob.UserID},
},
})
bob.MustJoinRoom(t, roomID, []spec.ServerName{"hs1"})
// we should not be able to kick bob
res := alice.Do(t,
"POST", []string{"_matrix", "client", "v3", "rooms", roomID, "kick"},
client.WithJSONBody(t, map[string]any{
"user_id": bob.UserID,
}),
)
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: 403,
JSON: []match.JSON{
match.JSONKeyEqual("errcode", "M_FORBIDDEN"),
},
})
// Bob should be able to do privileged operations like set the room name
bob.SendEventSynced(t, roomID, b.Event{
Type: spec.MRoomName,
StateKey: b.Ptr(""),
Content: map[string]any{
"name": "Bob's room name",
},
})
// Bob should not be able to be inserted into content.users in the PL event
// because they are in additional_creators
resp := alice.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "state", spec.MRoomPowerLevels, ""}, client.WithJSONBody(t, map[string]any{
"users": map[string]int{
bob.UserID: 100,
},
}))
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 400,
})
}
func TestMSC4289PrivilegedRoomCreators_InvitedAreCreators(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "alice",
})
bob := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "bob",
})
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"preset": "trusted_private_chat",
"is_direct": true,
"invite": []string{bob.UserID},
})
createContent := alice.MustGetStateEventContent(t, roomID, spec.MRoomCreate, "")
must.MatchGJSON(t, createContent, match.JSONKeyEqual("additional_creators", []string{bob.UserID}))
}
// Ensure that trusted_private_chat handling doesn't replace additional_creators
func TestMSC4289PrivilegedRoomCreators_AdditionalCreatorsAndInvited(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "alice",
})
bob := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "bob",
})
charlie := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "charlie",
})
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"preset": "trusted_private_chat",
"is_direct": true,
"invite": []string{bob.UserID},
"creation_content": map[string]any{
"additional_creators": []string{charlie.UserID},
},
})
createContent := alice.MustGetStateEventContent(t, roomID, spec.MRoomCreate, "")
must.MatchGJSON(t, createContent,
match.JSONCheckOff("additional_creators", []interface{}{bob.UserID, charlie.UserID}, match.CheckOffMapper(func(r gjson.Result) interface{} {
return r.Str
})),
)
}
// Check that 'additional_creators' is validated correctly.
func TestMSC4289PrivilegedRoomCreators_AdditionalValidation(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "alice",
})
testCases := []struct {
Name string
AdditionalCreators any
WantSuccess bool
}{
{
Name: "additional_creators isn't an array",
AdditionalCreators: "not-an-array",
WantSuccess: false,
},
{
Name: "additional_creators elements aren't strings",
AdditionalCreators: []any{"@foo:example.com", 42},
WantSuccess: false,
},
{
Name: "additional_creators elements aren't user ID strings",
AdditionalCreators: []any{"@foo:example.com", "not-a-user-id"},
WantSuccess: false,
},
{
Name: "additional_creators elements aren't valid user ID strings (domain)",
AdditionalCreators: []any{"@invalid:dom$ain$.com"},
WantSuccess: false,
},
{
Name: "additional_creators are valid",
AdditionalCreators: []any{"@foo:example.com", "@bar:baz.code"},
WantSuccess: true,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
resp := alice.CreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"preset": "public_chat",
"creation_content": map[string]any{
"additional_creators": tc.AdditionalCreators,
},
})
if tc.WantSuccess {
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 200,
})
} else {
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 400,
})
}
})
}
}
func TestMSC4289PrivilegedRoomCreators_Upgrades(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "alice",
})
bob := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "bob",
})
charlie := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "charlie",
})
testCases := []struct {
name string
initialCreator *client.CSAPI
initialAdditionalCreators []string
initialVersion string
initialUserPLs map[string]int
entitiyDoingUpgrade *client.CSAPI
newAdditionalCreators []string
// assertions
wantAdditionalCreators []string
wantNewUsersMap map[string]int64
}{
{
name: "non-creator admins can upgrade v11 rooms to v12",
initialCreator: alice,
initialVersion: "11",
initialUserPLs: map[string]int{
bob.UserID: 100,
},
entitiyDoingUpgrade: bob,
wantAdditionalCreators: []string{},
wantNewUsersMap: map[string]int64{},
},
{
name: "non-creator admins can upgrade v11 rooms to v12 with additional moderators",
initialCreator: alice,
initialVersion: "11",
initialUserPLs: map[string]int{
bob.UserID: 100,
charlie.UserID: 100,
},
entitiyDoingUpgrade: bob,
wantAdditionalCreators: []string{},
wantNewUsersMap: map[string]int64{
charlie.UserID: 100,
},
},
{
name: "non-creator admins can upgrade v12 rooms to v12 with different creators",
initialCreator: alice,
initialVersion: roomVersion12,
initialUserPLs: map[string]int{
bob.UserID: 150, // bob has enough permission to upgrade
},
entitiyDoingUpgrade: bob,
newAdditionalCreators: []string{charlie.UserID},
wantAdditionalCreators: []string{charlie.UserID},
wantNewUsersMap: map[string]int64{},
},
{
name: "non-creator admins can upgrade v12 rooms to v12 with different creators with additional moderators",
initialCreator: alice,
initialVersion: roomVersion12,
initialUserPLs: map[string]int{
bob.UserID: 150, // bob has enough permission to upgrade
charlie.UserID: 50, // gets removed as he will become an additional creator
},
entitiyDoingUpgrade: bob,
newAdditionalCreators: []string{charlie.UserID},
wantAdditionalCreators: []string{charlie.UserID},
wantNewUsersMap: map[string]int64{},
},
{
name: "creator admins can upgrade v11 rooms to v12 with additional_creators",
initialCreator: alice,
initialVersion: "11",
initialUserPLs: map[string]int{
alice.UserID: 100,
bob.UserID: 100,
},
entitiyDoingUpgrade: alice,
newAdditionalCreators: []string{bob.UserID},
wantAdditionalCreators: []string{bob.UserID},
wantNewUsersMap: map[string]int64{}, // both alice and bob are removed as they are now creators.
},
{
name: "creator admins can upgrade v11 rooms to v12 with additional_creators and moderators",
initialCreator: alice,
initialVersion: "11",
initialUserPLs: map[string]int{
alice.UserID: 100,
bob.UserID: 100,
charlie.UserID: 50,
},
entitiyDoingUpgrade: alice,
newAdditionalCreators: []string{bob.UserID},
wantAdditionalCreators: []string{bob.UserID},
wantNewUsersMap: map[string]int64{
charlie.UserID: 50,
},
},
}
for _, tc := range testCases {
createBody := map[string]interface{}{
"room_version": tc.initialVersion,
"preset": "public_chat",
}
if tc.initialAdditionalCreators != nil {
must.Equal(t, tc.initialVersion, roomVersion12, "can only set additional_creators on v12")
createBody["additional_creators"] = tc.initialAdditionalCreators
}
roomID := tc.initialCreator.MustCreateRoom(t, createBody)
alice.JoinRoom(t, roomID, []spec.ServerName{"hs1"})
bob.JoinRoom(t, roomID, []spec.ServerName{"hs1"})
charlie.JoinRoom(t, roomID, []spec.ServerName{"hs1"})
tc.initialCreator.SendEventSynced(t, roomID, b.Event{
Type: spec.MRoomPowerLevels,
StateKey: b.Ptr(""),
Content: map[string]interface{}{
"users": tc.initialUserPLs,
},
})
upgradeBody := map[string]any{
"new_version": roomVersion12,
}
if tc.newAdditionalCreators != nil {
upgradeBody["additional_creators"] = tc.newAdditionalCreators
}
res := tc.entitiyDoingUpgrade.MustDo(t, "POST", []string{"_matrix", "client", "v3", "rooms", roomID, "upgrade"}, client.WithJSONBody(t, upgradeBody))
newRoomID := must.ParseJSON(t, res.Body).Get("replacement_room").Str
// New Create event assertions
createContent := tc.entitiyDoingUpgrade.MustGetStateEventContent(t, newRoomID, spec.MRoomCreate, "")
createAssertions := []match.JSON{
match.JSONKeyEqual("room_version", roomVersion12),
}
if tc.wantAdditionalCreators != nil {
if len(tc.wantAdditionalCreators) > 0 {
createAssertions = append(createAssertions, match.JSONKeyEqual("additional_creators", tc.wantAdditionalCreators))
} else {
createAssertions = append(createAssertions, match.JSONKeyMissing("additional_creators"))
}
}
must.MatchGJSON(
t, createContent, createAssertions...,
)
// New PL assertions
plContent := tc.entitiyDoingUpgrade.MustGetStateEventContent(t, newRoomID, spec.MRoomPowerLevels, "")
if tc.wantNewUsersMap != nil {
plContent.Get("users").ForEach(func(key, v gjson.Result) bool {
gotVal := v.Int()
wantVal, ok := tc.wantNewUsersMap[key.Str]
if !ok {
ct.Errorf(t, "%s: upgraded room PL content, user %s has PL %v but want it missing", tc.name, key.Str, gotVal)
return true
}
if gotVal != wantVal {
ct.Errorf(t, "%s: upgraded room PL content, user %s has PL %v want %v", tc.name, key.Str, gotVal, wantVal)
}
delete(tc.wantNewUsersMap, key.Str)
return true
})
if len(tc.wantNewUsersMap) > 0 {
ct.Errorf(t, "%s: upgraded room PL content missed these users %v", tc.name, tc.wantNewUsersMap)
}
}
t.Logf("OK: %v", tc.name)
}
}
// Test that the room ID is in fact the hash of the create event.
func TestMSC4291RoomIDAsHashOfCreateEvent(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
})
assertCreateEventIsRoomID(t, alice, roomID)
}
func TestMSC4291RoomIDAsHashOfCreateEvent_AuthEventsOmitsCreateEvent(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"preset": "public_chat",
})
srv := federation.NewServer(t, deployment,
federation.HandleKeyRequests(),
federation.HandleMakeSendJoinRequests(),
federation.HandleTransactionRequests(nil, nil),
federation.HandleEventRequests(),
)
srv.UnexpectedRequestsAreErrors = false
cancel := srv.Listen()
defer cancel()
bob := srv.UserID("bob")
room := srv.MustJoinRoom(t, deployment, "hs1", roomID, bob, federation.WithRoomOpts(federation.WithImpl(&V12ServerRoom)))
createEvent := room.CurrentState(spec.MRoomCreate, "")
if createEvent == nil {
ct.Fatalf(t, "missing create event from /send_join response")
}
t.Logf("Create event is %s", createEvent.EventID())
createEventID := createEvent.EventID()
must.Equal(t,
roomID, fmt.Sprintf("!%s", createEventID[1:]), // swap $ for !
"room ID was not the hash of the create event ID",
)
for _, event := range room.Timeline {
rawAuthEvents := gjson.GetBytes(event.JSON(), "auth_events")
must.Equal(t, rawAuthEvents.IsArray(), true, "auth_events key is missing / not an array")
var authEventIDs []string
for _, rawAuthEventID := range rawAuthEvents.Array() {
authEventIDs = append(authEventIDs, rawAuthEventID.Str)
}
t.Logf("create=%v authEventIDs=>%v", createEvent.EventID(), authEventIDs)
if slices.Contains(authEventIDs, createEvent.EventID()) {
ct.Fatalf(t, "Event %s (%s) contains the create event in auth_events: %v", event.EventID(), event.Type(), authEventIDs)
}
must.Equal(t, event.RoomID().String(), roomID, fmt.Sprintf("event %s room ID mismatch: got %v want %v", event.EventID(), event.RoomID(), roomID))
}
}
// Test that /upgrade also makes a room where the create event ID is the room ID
func TestMSC4291RoomIDAsHashOfCreateEvent_UpgradedRooms(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{LocalpartSuffix: "alice"})
bob := deployment.Register(t, "hs1", helpers.RegistrationOpts{LocalpartSuffix: "bob"})
testCases := []struct {
initialVersion string
}{
{
initialVersion: roomVersion12,
},
{
initialVersion: "11",
},
{
initialVersion: "10",
},
}
for _, tc := range testCases {
oldRoomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": tc.initialVersion,
"preset": "public_chat",
})
bob.MustJoinRoom(t, oldRoomID, []spec.ServerName{"hs1"})
res := alice.MustDo(t, "POST", []string{
"_matrix", "client", "v3", "rooms", oldRoomID, "upgrade",
}, client.WithJSONBody(t, map[string]any{
"new_version": roomVersion12,
}))
newRoomID := gjson.GetBytes(client.ParseJSON(t, res), "replacement_room").Str
t.Logf("upgraded from %s (%s) to %s (%s)", tc.initialVersion, oldRoomID, roomVersion12, newRoomID)
assertCreateEventIsRoomID(t, alice, newRoomID)
tombstoneContent := alice.MustGetStateEventContent(t, oldRoomID, "m.room.tombstone", "")
must.MatchGJSON(t, tombstoneContent, match.JSONKeyEqual("replacement_room", newRoomID))
createContent := alice.MustGetStateEventContent(t, newRoomID, spec.MRoomCreate, "")
must.MatchGJSON(t, createContent, match.JSONKeyEqual("predecessor.room_id", oldRoomID), match.JSONKeyMissing("predecessor.event_id"))
}
}
// Ensure that clients cannot send an m.room.create event in an existing room.
func TestMSC4291RoomIDAsHashOfCreateEvent_CannotSendCreateEvent(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
for _, version := range []string{"11", roomVersion12} {
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": version,
})
resp := alice.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "state", spec.MRoomCreate, ""}, client.WithJSONBody(t, map[string]any{
"room_version": version,
// some homeservers may not create a new event if the content exactly matches the prior state,
// so just add some entropy.
"entropy": 100,
}))
must.MatchResponse(t, resp, match.HTTPResponse{StatusCode: 400})
}
}
// Test that all CS APIs that return events include the room_id for the create event,
// with the exception of /sync as that always removes room IDs.
func TestMSC4291RoomIDAsHashOfCreateEvent_RoomIDIsOnCreateEvent(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
})
eventID := alice.SendEventSynced(t, roomID, b.Event{
Type: "m.room.message",
Content: map[string]interface{}{
"msgtype": "m.text",
"body": "Hello",
},
})
createEventID := "$" + roomID[1:]
testCases := []struct {
name string
path []string
qps url.Values
extractCreateEvent func(resp gjson.Result) *gjson.Result
}{
{
name: "/state",
path: []string{"_matrix", "client", "v3", "rooms", roomID, "state"},
extractCreateEvent: func(resp gjson.Result) *gjson.Result {
for _, ev := range resp.Array() {
if ev.Get("type").Str == spec.MRoomCreate {
return &ev
}
}
return nil
},
},
{
name: "/messages",
path: []string{"_matrix", "client", "v3", "rooms", roomID, "messages"},
qps: url.Values{
"dir": {"b"},
"limit": {"100"},
},
extractCreateEvent: func(resp gjson.Result) *gjson.Result {
for _, ev := range resp.Get("chunk").Array() {
if ev.Get("type").Str == spec.MRoomCreate {
return &ev
}
}
return nil
},
},
{
name: "/event/{eventID}",
path: []string{"_matrix", "client", "v3", "rooms", roomID, "event", createEventID},
extractCreateEvent: func(resp gjson.Result) *gjson.Result {
return &resp
},
},
{
name: "/context direct",
path: []string{"_matrix", "client", "v3", "rooms", roomID, "context", createEventID},
extractCreateEvent: func(resp gjson.Result) *gjson.Result {
ev := resp.Get("event")
return &ev
},
},
{
name: "/context indirect",
qps: url.Values{
"limit": {"100"},
},
path: []string{"_matrix", "client", "v3", "rooms", roomID, "context", eventID},
extractCreateEvent: func(resp gjson.Result) *gjson.Result {
for _, ev := range resp.Get("events_before").Array() {
if ev.Get("type").Str == spec.MRoomCreate {
return &ev
}
}
return nil
},
},
{
name: "/context state",
qps: url.Values{
"limit": {"100"},
},
path: []string{"_matrix", "client", "v3", "rooms", roomID, "context", eventID},
extractCreateEvent: func(resp gjson.Result) *gjson.Result {
for _, ev := range resp.Get("state").Array() {
if ev.Get("type").Str == spec.MRoomCreate {
return &ev
}
}
return nil
},
},
{
name: "/state?format=event",
qps: url.Values{
"format": {"event"},
},
path: []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.create", ""},
extractCreateEvent: func(resp gjson.Result) *gjson.Result {
return &resp
},
},
}
for _, tc := range testCases {
opts := []client.RequestOpt{}
if tc.qps != nil {
opts = append(opts, client.WithQueries(tc.qps))
}
resp := alice.MustDo(t, "GET", tc.path, opts...)
body := must.ParseJSON(t, resp.Body)
createEvent := tc.extractCreateEvent(body)
if createEvent == nil {
ct.Errorf(t, "%s: failed to find create event", tc.name)
continue
}
must.Equal(t, createEvent.Get("room_id").Str, roomID, fmt.Sprintf("%s: create event is missing room ID", tc.name))
}
}
func assertCreateEventIsRoomID(t ct.TestLike, client *client.CSAPI, roomID string) (createEventID string) {
t.Helper()
res := client.MustDo(t, "GET", []string{
"_matrix", "client", "v3", "rooms", roomID, "state",
})
stateEvents := must.ParseJSON(t, res.Body)
stateEvents.ForEach(func(_, value gjson.Result) bool {
if value.Get("type").Str == spec.MRoomCreate && value.Get("state_key").Str == "" {
createEventID = value.Get("event_id").Str
return false
}
return true
})
if createEventID == "" {
ct.Fatalf(t, "failed to find create event ID from /state respone: %v", stateEvents.Raw)
}
must.Equal(t,
roomID, fmt.Sprintf("!%s", createEventID[1:]),
"room ID was not the hash of the create event ID",
)
return createEventID
}
// Test that v2.1 has implemented starting from the empty set not the unconflicted set
// This test assumes a few things about the underlying server implementation:
// - It eventually gives up calling /get_missing_events for some n < 250 and hits /state or /state_ids for the historical state.
// - It does not call /event_auth but may call /event/{eventID}
// - On encountering DAG gaps, the current state is the resolution of all the forwards extremities for each section.
// In other words, the server calculates the current state as the merger of (what_we_knew_before, what_we_know_now),
// despite there being no events with >1 prev_events.
//
// The scenario in this test is similar to the one in the MSC but different in two key ways:
// - To force incorrect state, "Charlie changes display name" happens 250 times to force a /state{_ids} request.
// In the MSC this only happened once.
// - "Bob changes display name" does not exist. We rely on the server calculating the current state as the
// merging of the forwards extremitiy before the gap and the forwards extremity after the gap, so
// in other words we apply state resolution to (Alice leave, 250th Charlie display name change).
func TestMSC4297StateResolutionV2_1_starts_from_empty_set(t *testing.T) {
runtime.SkipIf(t, runtime.Dendrite) // needs additional fixes
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
srv := federation.NewServer(t, deployment,
federation.HandleKeyRequests(),
federation.HandleMakeSendJoinRequests(),
federation.HandleTransactionRequests(nil, nil),
federation.HandleEventRequests(),
)
srv.UnexpectedRequestsAreErrors = false
cancel := srv.Listen()
defer cancel()
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{LocalpartSuffix: "alice"})
bob := deployment.Register(t, "hs1", helpers.RegistrationOpts{LocalpartSuffix: "bob"})
charlie := srv.UserID("charlie")
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_version": roomVersion12,
"preset": "public_chat",
})
bob.MustJoinRoom(t, roomID, []spec.ServerName{"hs1"})
room := srv.MustJoinRoom(t, deployment, "hs1", roomID, charlie, federation.WithRoomOpts(federation.WithImpl(&V12ServerRoom)))
joinRulePublic := room.CurrentState(spec.MRoomJoinRules, "")
aliceJoin := room.CurrentState(spec.MRoomMember, alice.UserID)
synchronisationEventID := bob.SendEventSynced(t, room.RoomID, b.Event{
Type: "m.room.message",
Content: map[string]interface{}{
"msgtype": "m.text",
"body": "can you hear me charlie?",
},
})
room.WaiterForEvent(synchronisationEventID).Waitf(t, 5*time.Second, "failed to see synchronisation event, is federation working?")
// Alice makes the room invite-only then leaves
joinRuleInviteOnlyEventID := alice.SendEventSynced(t, roomID, b.Event{
Type: spec.MRoomJoinRules,
StateKey: b.Ptr(""),
Sender: alice.UserID,
Content: map[string]interface{}{
"join_rule": "invite",
},
})
room.WaiterForEvent(joinRuleInviteOnlyEventID).Waitf(t, 5*time.Second, "failed to see invite join rule event")
alice.MustLeaveRoom(t, roomID)
// Wait for Charlie to see it
time.Sleep(time.Second)
aliceLeaveEvent := room.CurrentState(spec.MRoomMember, alice.UserID)
if membership, err := aliceLeaveEvent.Membership(); err != nil || membership != spec.Leave {
ct.Fatalf(t, "failed to see Alice leave the room, alice event is %s", string(aliceLeaveEvent.JSON()))
}
// Now only Bob (server under test) and Charlie (Complement server) are left in the room.
// Charlie is going to send an event with unknown prev_event, causing /get_missing_events
// until eventually /state_ids is hit. When it is, we'll return incorrect room state, claiming
// that the current join rule is public, not invite. This will cause the join rules to get conflicted
// and replayed. V2 would base the checks off the unconflicted state, and since all servers agree
// that Alice=leave it would start like that, making the join rule transitions invalid and causing the
// room to have no join rule at all. V2.1 fixes this by loading the auth_events of the event being replayed
// which correctly has Alice joined. Alice isn't automatically re-joined to the room though because the
// last step of the algorithm is to apply the unconflicted state on top of the resolved conflicts, without
// any extra checks.
// We don't know how far back server impls will go, so let's use 250 as a large enough value.
charlieEvents := make([]gomatrixserverlib.PDU, 250)