2020-09-03 11:20:54 -05:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package acls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2024-02-21 07:10:22 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2020-09-03 11:20:54 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 09:50:33 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-09-03 11:20:54 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-11-22 08:38:04 -06:00
|
|
|
const MRoomServerACL = "m.room.server_acl"
|
|
|
|
|
2020-09-03 11:20:54 -05:00
|
|
|
type ServerACLDatabase interface {
|
|
|
|
// GetKnownRooms returns a list of all rooms we know about.
|
|
|
|
GetKnownRooms(ctx context.Context) ([]string, error)
|
2024-02-21 07:10:22 -06:00
|
|
|
|
|
|
|
// GetBulkStateContent returns all state events which match a given room ID and a given state key tuple. Both must be satisfied for a match.
|
|
|
|
// If a tuple has the StateKey of '*' and allowWildcards=true then all state events with the EventType should be returned.
|
|
|
|
GetBulkStateContent(ctx context.Context, roomIDs []string, tuples []gomatrixserverlib.StateKeyTuple, allowWildcards bool) ([]tables.StrippedEvent, error)
|
2020-09-03 11:20:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type ServerACLs struct {
|
2024-02-28 13:58:56 -06:00
|
|
|
acls map[string]*serverACL // room ID -> ACL
|
|
|
|
aclsMutex sync.RWMutex // protects the above
|
|
|
|
aclRegexCache map[string]**regexp.Regexp // Cache from "serverName" -> pointer to a regex
|
|
|
|
aclRegexCacheMutex sync.RWMutex // protects the above
|
2020-09-03 11:20:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewServerACLs(db ServerACLDatabase) *ServerACLs {
|
|
|
|
ctx := context.TODO()
|
|
|
|
acls := &ServerACLs{
|
|
|
|
acls: make(map[string]*serverACL),
|
2024-02-28 13:58:56 -06:00
|
|
|
// Be generous when creating the cache, as in reality
|
|
|
|
// there are hundreds of servers in an ACL.
|
|
|
|
aclRegexCache: make(map[string]**regexp.Regexp, 100),
|
2020-09-03 11:20:54 -05:00
|
|
|
}
|
2024-02-28 13:58:56 -06:00
|
|
|
|
2020-09-03 11:20:54 -05:00
|
|
|
// Look up all of the rooms that the current state server knows about.
|
|
|
|
rooms, err := db.GetKnownRooms(ctx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Fatalf("Failed to get known rooms")
|
|
|
|
}
|
|
|
|
// For each room, let's see if we have a server ACL state event. If we
|
|
|
|
// do then we'll process it into memory so that we have the regexes to
|
|
|
|
// hand.
|
2024-02-21 07:10:22 -06:00
|
|
|
|
|
|
|
events, err := db.GetBulkStateContent(ctx, rooms, []gomatrixserverlib.StateKeyTuple{{EventType: MRoomServerACL, StateKey: ""}}, false)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Errorf("Failed to get server ACLs for all rooms: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range events {
|
|
|
|
acls.OnServerACLUpdate(event)
|
2020-09-03 11:20:54 -05:00
|
|
|
}
|
2024-02-28 13:58:56 -06:00
|
|
|
|
2020-09-03 11:20:54 -05:00
|
|
|
return acls
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerACL struct {
|
|
|
|
Allowed []string `json:"allow"`
|
|
|
|
Denied []string `json:"deny"`
|
|
|
|
AllowIPLiterals bool `json:"allow_ip_literals"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverACL struct {
|
|
|
|
ServerACL
|
2024-02-28 13:58:56 -06:00
|
|
|
allowedRegexes []**regexp.Regexp
|
|
|
|
deniedRegexes []**regexp.Regexp
|
2020-09-03 11:20:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func compileACLRegex(orig string) (*regexp.Regexp, error) {
|
|
|
|
escaped := regexp.QuoteMeta(orig)
|
|
|
|
escaped = strings.Replace(escaped, "\\?", ".", -1)
|
|
|
|
escaped = strings.Replace(escaped, "\\*", ".*", -1)
|
|
|
|
return regexp.Compile(escaped)
|
|
|
|
}
|
|
|
|
|
2024-02-28 13:58:56 -06:00
|
|
|
// cachedCompileACLRegex is a wrapper around compileACLRegex with added caching
|
|
|
|
func (s *ServerACLs) cachedCompileACLRegex(orig string) (**regexp.Regexp, error) {
|
|
|
|
s.aclRegexCacheMutex.RLock()
|
|
|
|
re, ok := s.aclRegexCache[orig]
|
|
|
|
if ok {
|
|
|
|
s.aclRegexCacheMutex.RUnlock()
|
|
|
|
return re, nil
|
|
|
|
}
|
|
|
|
s.aclRegexCacheMutex.RUnlock()
|
|
|
|
compiled, err := compileACLRegex(orig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.aclRegexCacheMutex.Lock()
|
|
|
|
defer s.aclRegexCacheMutex.Unlock()
|
|
|
|
s.aclRegexCache[orig] = &compiled
|
|
|
|
return &compiled, nil
|
|
|
|
}
|
|
|
|
|
2024-02-21 07:10:22 -06:00
|
|
|
func (s *ServerACLs) OnServerACLUpdate(strippedEvent tables.StrippedEvent) {
|
2020-09-03 11:20:54 -05:00
|
|
|
acls := &serverACL{}
|
2024-02-21 07:10:22 -06:00
|
|
|
if err := json.Unmarshal([]byte(strippedEvent.ContentValue), &acls.ServerACL); err != nil {
|
2020-09-03 11:20:54 -05:00
|
|
|
logrus.WithError(err).Errorf("Failed to unmarshal state content for server ACLs")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// The spec calls only for * (zero or more chars) and ? (exactly one char)
|
|
|
|
// to be supported as wildcard components, so we will escape all of the regex
|
|
|
|
// special characters and then replace * and ? with their regex counterparts.
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.6.1#m-room-server-acl
|
|
|
|
for _, orig := range acls.Allowed {
|
2024-02-28 13:58:56 -06:00
|
|
|
if expr, err := s.cachedCompileACLRegex(orig); err != nil {
|
2020-09-03 11:20:54 -05:00
|
|
|
logrus.WithError(err).Errorf("Failed to compile allowed regex")
|
|
|
|
} else {
|
|
|
|
acls.allowedRegexes = append(acls.allowedRegexes, expr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, orig := range acls.Denied {
|
2024-02-28 13:58:56 -06:00
|
|
|
if expr, err := s.cachedCompileACLRegex(orig); err != nil {
|
2020-09-03 11:20:54 -05:00
|
|
|
logrus.WithError(err).Errorf("Failed to compile denied regex")
|
|
|
|
} else {
|
|
|
|
acls.deniedRegexes = append(acls.deniedRegexes, expr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"allow_ip_literals": acls.AllowIPLiterals,
|
|
|
|
"num_allowed": len(acls.allowedRegexes),
|
|
|
|
"num_denied": len(acls.deniedRegexes),
|
2024-02-21 07:10:22 -06:00
|
|
|
}).Debugf("Updating server ACLs for %q", strippedEvent.RoomID)
|
2024-02-28 13:58:56 -06:00
|
|
|
|
|
|
|
// Clear out Denied and Allowed, now that we have the compiled regexes.
|
|
|
|
// They are not needed anymore from this point on.
|
|
|
|
acls.Denied = nil
|
|
|
|
acls.Allowed = nil
|
2020-09-03 11:20:54 -05:00
|
|
|
s.aclsMutex.Lock()
|
|
|
|
defer s.aclsMutex.Unlock()
|
2024-02-21 07:10:22 -06:00
|
|
|
s.acls[strippedEvent.RoomID] = acls
|
2020-09-03 11:20:54 -05:00
|
|
|
}
|
|
|
|
|
2023-04-19 09:50:33 -05:00
|
|
|
func (s *ServerACLs) IsServerBannedFromRoom(serverName spec.ServerName, roomID string) bool {
|
2020-09-03 11:20:54 -05:00
|
|
|
s.aclsMutex.RLock()
|
|
|
|
// First of all check if we have an ACL for this room. If we don't then
|
|
|
|
// no servers are banned from the room.
|
|
|
|
acls, ok := s.acls[roomID]
|
|
|
|
if !ok {
|
|
|
|
s.aclsMutex.RUnlock()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
s.aclsMutex.RUnlock()
|
|
|
|
// Split the host and port apart. This is because the spec calls on us to
|
|
|
|
// validate the hostname only in cases where the port is also present.
|
|
|
|
if serverNameOnly, _, err := net.SplitHostPort(string(serverName)); err == nil {
|
2023-04-19 09:50:33 -05:00
|
|
|
serverName = spec.ServerName(serverNameOnly)
|
2020-09-03 11:20:54 -05:00
|
|
|
}
|
|
|
|
// Check if the hostname is an IPv4 or IPv6 literal. We cheat here by adding
|
|
|
|
// a /0 prefix length just to trick ParseCIDR into working. If we find that
|
|
|
|
// the server is an IP literal and we don't allow those then stop straight
|
|
|
|
// away.
|
|
|
|
if _, _, err := net.ParseCIDR(fmt.Sprintf("%s/0", serverName)); err == nil {
|
|
|
|
if !acls.AllowIPLiterals {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if the hostname matches one of the denied regexes. If it does then
|
|
|
|
// the server is banned from the room.
|
|
|
|
for _, expr := range acls.deniedRegexes {
|
2024-02-28 13:58:56 -06:00
|
|
|
if (*expr).MatchString(string(serverName)) {
|
2020-09-03 11:20:54 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if the hostname matches one of the allowed regexes. If it does then
|
|
|
|
// the server is NOT banned from the room.
|
|
|
|
for _, expr := range acls.allowedRegexes {
|
2024-02-28 13:58:56 -06:00
|
|
|
if (*expr).MatchString(string(serverName)) {
|
2020-09-03 11:20:54 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we've got to this point then we haven't matched any regexes or an IP
|
|
|
|
// hostname if disallowed. The spec calls for default-deny here.
|
|
|
|
return true
|
|
|
|
}
|