Handle the case that the join rules are nonsense

This commit is contained in:
Neil Alexander 2021-11-11 14:06:13 +00:00
parent 9ea8dc71eb
commit 6123c0edca
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -244,10 +244,7 @@ func (r *Joiner) performJoinRoomByID(
// Check if the room is a restricted room. If so, update the event // Check if the room is a restricted room. If so, update the event
// builder content. // builder content.
if restricted, roomIDs, rerr := r.checkIfRestrictedJoin(ctx, req); rerr != nil { if restricted, roomIDs, rerr := r.checkIfRestrictedJoin(ctx, req); rerr != nil {
return "", "", &rsAPI.PerformError{ return "", "", err
Code: rsAPI.PerformErrorNotAllowed,
Msg: rerr.Error(),
}
} else if restricted { } else if restricted {
for _, roomID := range roomIDs { for _, roomID := range roomIDs {
if err = r.attemptRestrictedJoinUsingRoomID(ctx, req, roomID, &eb); err == nil { if err = r.attemptRestrictedJoinUsingRoomID(ctx, req, roomID, &eb); err == nil {
@ -345,13 +342,19 @@ func (r *Joiner) checkIfRestrictedJoin(
// restricted room or not. // restricted room or not.
joinRuleEvent, err := r.DB.GetStateEvent(ctx, req.RoomIDOrAlias, gomatrixserverlib.MRoomJoinRules, "") joinRuleEvent, err := r.DB.GetStateEvent(ctx, req.RoomIDOrAlias, gomatrixserverlib.MRoomJoinRules, "")
if err != nil { if err != nil {
return false, nil, fmt.Errorf("r.DB.GetStateEvent: %w", err) return false, nil, &rsAPI.PerformError{
Code: rsAPI.PerformErrorNotAllowed,
Msg: fmt.Sprintf("Unable to retrieve the join rules: %s", err),
}
} }
joinRuleContent := &gomatrixserverlib.JoinRuleContent{ joinRuleContent := &gomatrixserverlib.JoinRuleContent{
JoinRule: gomatrixserverlib.Public, JoinRule: gomatrixserverlib.Public,
} }
if err = json.Unmarshal(joinRuleEvent.Content(), &joinRuleContent); err != nil { if err = json.Unmarshal(joinRuleEvent.Content(), &joinRuleContent); err != nil {
return false, nil, fmt.Errorf("json.Unmarshal: %w", err) return false, nil, &rsAPI.PerformError{
Code: rsAPI.PerformErrorNotAllowed,
Msg: fmt.Sprintf("The room join rules are invalid: %s", err),
}
} }
roomIDs := make([]string, 0, len(joinRuleContent.Allow)) roomIDs := make([]string, 0, len(joinRuleContent.Allow))
for _, allowed := range joinRuleContent.Allow { for _, allowed := range joinRuleContent.Allow {