Skip to content

Commit 14ee761

Browse files
authored
Fix event auth for knocking (#431)
Knocking is not allowed in `restricted` rooms.
1 parent 19c0a71 commit 14ee761

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

eventauth.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,18 +1155,7 @@ func (m *membershipAllower) membershipAllowedSelf() error { // nolint: gocyclo
11551155

11561156
switch m.newMember.Membership {
11571157
case spec.Knock:
1158-
if m.joinRule.JoinRule != spec.Knock && m.joinRule.JoinRule != spec.KnockRestricted {
1159-
return m.membershipFailed(
1160-
"join rule %q does not allow knocking", m.joinRule.JoinRule,
1161-
)
1162-
}
1163-
// A user that is not in the room is allowed to knock if the join
1164-
// rules are "knock" and they are not already joined to, invited to
1165-
// or banned from the room.
1166-
// Spec: https://spec.matrix.org/unstable/rooms/v7/
1167-
// MSC3787 extends this: the behaviour above is also permitted if the
1168-
// join rules are "knock_restricted"
1169-
// Spec: https://github.com/matrix-org/matrix-spec-proposals/pull/3787
1158+
// Check if the given roomVersionImpl allows knocking.
11701159
return m.roomVersionImpl.CheckKnockingAllowed(m)
11711160
case spec.Join:
11721161
if m.joinRule.JoinRule == spec.Restricted || m.joinRule.JoinRule == spec.KnockRestricted {
@@ -1244,8 +1233,16 @@ func disallowKnocking(m *membershipAllower) error {
12441233
)
12451234
}
12461235

1236+
// A user that is not in the room is allowed to knock if the join
1237+
// rules are "knock" and they are not already joined to
1238+
// or banned from the room.
1239+
// Spec: https://spec.matrix.org/unstable/rooms/v7/
1240+
// MSC3787 extends this: the behaviour above is also permitted if the
1241+
// join rules are "knock_restricted"
1242+
// Spec: https://github.com/matrix-org/matrix-spec-proposals/pull/3787
12471243
func checkKnocking(m *membershipAllower) error {
1248-
supported := m.joinRule.JoinRule == spec.Knock || m.joinRule.JoinRule == spec.Restricted || m.joinRule.JoinRule == spec.KnockRestricted
1244+
// If the join_rule is anything other than knock or knock_restricted, reject.
1245+
supported := m.joinRule.JoinRule == spec.Knock || m.joinRule.JoinRule == spec.KnockRestricted
12491246
if !supported {
12501247
return m.membershipFailed(
12511248
"room version %q does not support knocking on rooms with join rule %q",

eventauth_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,14 @@ func TestJoinRuleKnock(t *testing.T) {
18341834
"state_key": "@u4:a",
18351835
"event_id": "$e2:a",
18361836
"content": {"membership": "knock"}
1837+
},
1838+
"@u5:a": {
1839+
"type": "m.room.member",
1840+
"sender": "@u5:a",
1841+
"room_id": "!r1:a",
1842+
"state_key": "@u5:a",
1843+
"event_id": "$e2:a",
1844+
"content": {"membership": "ban"}
18371845
}
18381846
}
18391847
},
@@ -1873,6 +1881,28 @@ func TestJoinRuleKnock(t *testing.T) {
18731881
"unsigned": {
18741882
"not_allowed": "Sender not invited or joined"
18751883
}
1884+
},
1885+
{
1886+
"type": "m.room.member",
1887+
"sender": "@u3:a",
1888+
"room_id": "!r1:a",
1889+
"state_key": "@u3:a",
1890+
"event_id": "$e2:a",
1891+
"content": {"membership": "knock"},
1892+
"unsigned": {
1893+
"not_allowed": "Sender is already joined"
1894+
}
1895+
},
1896+
{
1897+
"type": "m.room.member",
1898+
"sender": "@u5:a",
1899+
"room_id": "!r1:a",
1900+
"state_key": "@u5:a",
1901+
"event_id": "$e2:a",
1902+
"content": {"membership": "knock"},
1903+
"unsigned": {
1904+
"not_allowed": "Sender is banned"
1905+
}
18761906
}]
18771907
}`, RoomVersionV10)
18781908
}

0 commit comments

Comments
 (0)