Make Match() with empty list not return a positive result

Now that we use Match() in allowAdmin() as well, it's awkward to
have it return `true` when called with an empty admin list.
No admins defined was taken to mean "everyone is an admin".

We can either have a `len(users) == 0` check in `allowAdmin` which
rejects the request, or we can change `Match()` so that it doesn't
return positive responses when called with an empty list. Doing the
latter sounds better. It's more natural that matching against an empty list
will yield "no match".
This commit is contained in:
Slavi Pantaleev
2022-08-29 14:10:52 +03:00
parent 6623251695
commit e59f5d5502
3 changed files with 6 additions and 9 deletions

View File

@@ -13,8 +13,10 @@ func (b *Bot) allowAnyone(actorID id.UserID, targetRoomID id.RoomID) bool {
}
func (b *Bot) allowOwner(actorID id.UserID, targetRoomID id.RoomID) bool {
if !utils.Match(actorID.String(), b.allowedUsers) {
return false
if len(b.allowedUsers) != 0 {
if !utils.Match(actorID.String(), b.allowedUsers) {
return false
}
}
if b.noowner {

View File

@@ -23,11 +23,6 @@ func WildcardMXIDsToRegexes(wildCardPatterns []string) ([]*regexp.Regexp, error)
// Match tells if the given user id is allowed to use the bot, according to the given whitelist
func Match(userID string, allowed []*regexp.Regexp) bool {
// No whitelisted users means everyone is whitelisted
if len(allowed) == 0 {
return true
}
for _, regex := range allowed {
if regex.MatchString(userID) {
return true

View File

@@ -127,10 +127,10 @@ func TestMatch(t *testing.T) {
tests := []testDataDefinition{
{
name: "Empty allowed users allows anyone",
name: "Empty allowed users allows no one",
checkedValue: "@someone:example.com",
allowedUsers: []string{},
expectedResult: true,
expectedResult: false,
},
{
name: "Direct full mxid match is allowed",