Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Throw re.error when invalidregex is provided #134

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 25 additions & 38 deletions simplematrixbotlib/config.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
from dataclasses import dataclass, field
import toml
import re
from typing import Set, Union
from typing import Set


@dataclass
class Config:
_join_on_invite: bool = True
_allowlist: Set[re.Pattern] = field(
default_factory=set) #TODO: default to bot's homeserver
default_factory=set) # TODO: default to bot's homeserver
_blocklist: Set[re.Pattern] = field(default_factory=set)

def _check_set_regex(self,
value: Set[str]) -> Union[Set[re.Pattern], None]:
new_list = set()
@staticmethod
def _check_set_regex(value: Set[str]):
"""Checks if the patterns in value are valid or throws an error"""
for v in value:
try:
tmp = re.compile(v)
re.compile(v) # Fails for invalid regex
except re.error:
print(
f"{v} is not a valid regular expression. Ignoring your list update."
)
return None
new_list.add(tmp)
return new_list
raise re.error(f"{v} is not a valid regex.")

def _load_config_dict(self, config_dict: dict) -> None:
for key, value in config_dict.items():
Expand Down Expand Up @@ -80,34 +75,32 @@ def allowlist(self) -> Set[re.Pattern]:

@allowlist.setter
def allowlist(self, value: Set[str]) -> None:
checked = self._check_set_regex(value)
if checked is None:
return
self._allowlist = checked
self._check_set_regex(value)
self._allowlist = value

def add_allowlist(self, value: Set[str]) -> None:
def add_allowlist(self, value: Set[str]):
"""
Adds all regex to the allowlist if valid, else throws error

Parameters
----------
value : Set[str]
A set of strings which represent Matrix IDs or a regular expression matching Matrix IDs to be added to allowlist.
"""
checked = self._check_set_regex(value)
if checked is None:
return
self._allowlist = self._allowlist.union(checked)
self._check_set_regex(value)
self._allowlist = self._allowlist.union(value)

def remove_allowlist(self, value: Set[str]) -> None:
def remove_allowlist(self, value: Set[str]):
"""
Removes all in the value set from the allowlist or throws an error

Parameters
----------
value : Set[str]
A set of strings which represent Matrix IDs or a regular expression matching Matrix IDs to be removed from allowlist.
"""
checked = self._check_set_regex(value)
if checked is None:
return
self._allowlist = self._allowlist - checked
self._check_set_regex(value)
self._allowlist = self._allowlist - value

@property
def blocklist(self) -> Set[re.Pattern]:
Expand All @@ -122,10 +115,8 @@ def blocklist(self) -> Set[re.Pattern]:

@blocklist.setter
def blocklist(self, value: Set[str]) -> None:
checked = self._check_set_regex(value)
if checked is None:
return
self._blocklist = checked
self._check_set_regex(value)
self._blocklist = value

def add_blocklist(self, value: Set[str]) -> None:
"""
Expand All @@ -134,10 +125,8 @@ def add_blocklist(self, value: Set[str]) -> None:
value : Set[str]
A set of strings which represent Matrix IDs or a regular expression matching Matrix IDs to be added to blocklist.
"""
checked = self._check_set_regex(value)
if checked is None:
return
self._blocklist = self._blocklist.union(checked)
self._check_set_regex(value)
self._blocklist = self._blocklist.union(value)

def remove_blocklist(self, value: Set[str]) -> None:
"""
Expand All @@ -146,7 +135,5 @@ def remove_blocklist(self, value: Set[str]) -> None:
value : Set[str]
A set of strings which represent Matrix IDs or a regular expression matching Matrix IDs to be removed from blocklist.
"""
checked = self._check_set_regex(value)
if checked is None:
return
self._blocklist = self._blocklist - checked
self._check_set_regex(value)
self._blocklist = self._blocklist - value
3 changes: 3 additions & 0 deletions tests/config/sample_config_files/config5.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[simplematrixbotlib.config]
allowlist = ["[."]
blocklist = ["@test2:example\\.org"]
11 changes: 11 additions & 0 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,14 @@ def test_manual_set():
assert config.blocklist == set(map(re.compile, ['@test:matrix\\.org']))
config.blocklist = {'*:example\\.org'}
assert config.blocklist == set(map(re.compile, ['@test:matrix\\.org']))


def test_botlib():
config = botlib.Config()
with pytest.raises(re.error):
config._check_set_regex(set(r"[."))
# Valid patterns
config._check_set_regex(set(r"@testuser:example.org"))

with pytest.raises(re.error):
config.load_toml(os.path.join(sample_config_path, 'config5.toml'))