Skip to content

Commit

Permalink
Fix event auth for knocking (#431)
Browse files Browse the repository at this point in the history
Knocking is not allowed in `restricted` rooms.
  • Loading branch information
S7evinK authored Jan 16, 2024
1 parent 19c0a71 commit 14ee761
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
23 changes: 10 additions & 13 deletions eventauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,18 +1155,7 @@ func (m *membershipAllower) membershipAllowedSelf() error { // nolint: gocyclo

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

// A user that is not in the room is allowed to knock if the join
// rules are "knock" and they are not already joined to
// or banned from the room.
// Spec: https://spec.matrix.org/unstable/rooms/v7/
// MSC3787 extends this: the behaviour above is also permitted if the
// join rules are "knock_restricted"
// Spec: https://github.com/matrix-org/matrix-spec-proposals/pull/3787
func checkKnocking(m *membershipAllower) error {
supported := m.joinRule.JoinRule == spec.Knock || m.joinRule.JoinRule == spec.Restricted || m.joinRule.JoinRule == spec.KnockRestricted
// If the join_rule is anything other than knock or knock_restricted, reject.
supported := m.joinRule.JoinRule == spec.Knock || m.joinRule.JoinRule == spec.KnockRestricted
if !supported {
return m.membershipFailed(
"room version %q does not support knocking on rooms with join rule %q",
Expand Down
30 changes: 30 additions & 0 deletions eventauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,14 @@ func TestJoinRuleKnock(t *testing.T) {
"state_key": "@u4:a",
"event_id": "$e2:a",
"content": {"membership": "knock"}
},
"@u5:a": {
"type": "m.room.member",
"sender": "@u5:a",
"room_id": "!r1:a",
"state_key": "@u5:a",
"event_id": "$e2:a",
"content": {"membership": "ban"}
}
}
},
Expand Down Expand Up @@ -1873,6 +1881,28 @@ func TestJoinRuleKnock(t *testing.T) {
"unsigned": {
"not_allowed": "Sender not invited or joined"
}
},
{
"type": "m.room.member",
"sender": "@u3:a",
"room_id": "!r1:a",
"state_key": "@u3:a",
"event_id": "$e2:a",
"content": {"membership": "knock"},
"unsigned": {
"not_allowed": "Sender is already joined"
}
},
{
"type": "m.room.member",
"sender": "@u5:a",
"room_id": "!r1:a",
"state_key": "@u5:a",
"event_id": "$e2:a",
"content": {"membership": "knock"},
"unsigned": {
"not_allowed": "Sender is banned"
}
}]
}`, RoomVersionV10)
}
Expand Down

0 comments on commit 14ee761

Please sign in to comment.