From 3174713f7fa8aaf69468c64c85d7e64f5eb23502 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 29 Aug 2023 11:23:39 +0200 Subject: [PATCH] Continued: - added checks if instance is registered when update function are invoked - some empty lines added --- fba/commands.py | 1 + fba/helpers/cache.py | 3 ++ fba/helpers/config.py | 1 + fba/helpers/dicts.py | 1 + fba/helpers/domain.py | 2 ++ fba/helpers/json.py | 1 + fba/helpers/software.py | 3 ++ fba/helpers/tidyup.py | 2 ++ fba/helpers/version.py | 1 + fba/http/federation.py | 3 ++ fba/http/network.py | 1 + fba/models/blocks.py | 68 ++++++++++++++++----------------------- fba/models/instances.py | 2 ++ fba/networks/friendica.py | 3 ++ fba/networks/lemmy.py | 4 +++ fba/networks/mastodon.py | 6 ++++ fba/networks/misskey.py | 3 ++ fba/networks/pleroma.py | 7 ++++ fba/utils.py | 2 ++ 19 files changed, 74 insertions(+), 40 deletions(-) diff --git a/fba/commands.py b/fba/commands.py index d64c649..c6199ef 100644 --- a/fba/commands.py +++ b/fba/commands.py @@ -63,6 +63,7 @@ logger = logging.getLogger(__name__) def check_instance(args: argparse.Namespace) -> int: logger.debug("args.domain='%s' - CALLED!", args.domain) + status = 0 if not validators.domain(args.domain): logger.warning("args.domain='%s' is not valid", args.domain) diff --git a/fba/helpers/cache.py b/fba/helpers/cache.py index 8800bca..f15d56f 100644 --- a/fba/helpers/cache.py +++ b/fba/helpers/cache.py @@ -30,6 +30,7 @@ def key_exists(key: str) -> bool: def set_all(key: str, rows: list, value: any): logger.debug("key='%s',rows()=%d,value[]='%s' - CALLED!", key, len(rows), type(value)) + if not isinstance(key, str): raise ValueError(f"Parameter key[]='{type(key)}' is not of type 'str'") elif not key_exists(key): @@ -48,6 +49,7 @@ def set_all(key: str, rows: list, value: any): def set_sub_key(key: str, sub: str, value: any): logger.debug("key='%s',sub='%s',value[]='%s' - CALLED!", key, sub, type(value)) + if not isinstance(key, str): raise ValueError(f"Parameter key[]='{type(key)}' is not of type 'str'") elif not isinstance(sub, str): @@ -62,6 +64,7 @@ def set_sub_key(key: str, sub: str, value: any): def sub_key_exists(key: str, sub: str) -> bool: logger.debug("key='%s',sub='%s' - CALLED!", key, sub) + if not isinstance(key, str): raise ValueError(f"Parameter key[]='{type(key)}' is not of type 'str'") elif not isinstance(sub, str): diff --git a/fba/helpers/config.py b/fba/helpers/config.py index 6bece6d..85d1518 100644 --- a/fba/helpers/config.py +++ b/fba/helpers/config.py @@ -28,6 +28,7 @@ with open("config.json") as f: def get(key: str) -> any: logger.debug("key[%s]='%s' - CALLED!", type(key), key) + if not isinstance(key, str): raise ValueError(f"Parameter key[]='{type(key)}' is not of type 'str'") elif key == "": diff --git a/fba/helpers/dicts.py b/fba/helpers/dicts.py index b3f2632..0d6b8be 100644 --- a/fba/helpers/dicts.py +++ b/fba/helpers/dicts.py @@ -20,6 +20,7 @@ logger = logging.getLogger(__name__) def has_key(lists: list, key: str, value: any) -> bool: logger.debug("lists()=%d,key='%s',value[]='%s' - CALLED!", len(lists), key, type(value)) + if not isinstance(lists, list): raise ValueError(f"Parameter lists[]='{type(lists)}' is not of type 'list'") elif not isinstance(key, str): diff --git a/fba/helpers/domain.py b/fba/helpers/domain.py index 9f8c578..84c9bcb 100644 --- a/fba/helpers/domain.py +++ b/fba/helpers/domain.py @@ -29,6 +29,7 @@ logger = logging.getLogger(__name__) def raise_on(domain: str): logger.debug("domain='%s' - CALLED!", domain) + if not isinstance(domain, str): raise ValueError(f"Parameter domain[]='{type(domain)}' is not of type 'str'") elif domain == "": @@ -67,6 +68,7 @@ def is_in_url(domain: str, url: str) -> bool: def is_wanted(domain: str) -> bool: logger.debug("domain='%s' - CALLED!", domain) + if not isinstance(domain, str): raise ValueError(f"Parameter domain[]='{type(domain)}' is not of type 'str'") elif domain == "": diff --git a/fba/helpers/json.py b/fba/helpers/json.py index d20a2c7..a3e3fe5 100644 --- a/fba/helpers/json.py +++ b/fba/helpers/json.py @@ -24,6 +24,7 @@ logger = logging.getLogger(__name__) def from_response(response: requests.models.Response) -> any: logger.debug("response[]='%s' - CALLED!", type(response)) + if not isinstance(response, requests.models.Response): raise ValueError(f"Parameter response[]='{type(response)}' is not type of 'Response'") elif not response.ok or response.status_code > 200: diff --git a/fba/helpers/software.py b/fba/helpers/software.py index 432aead..d259147 100644 --- a/fba/helpers/software.py +++ b/fba/helpers/software.py @@ -23,6 +23,7 @@ logger = logging.getLogger(__name__) def alias(software: str) -> str: logger.debug("software='%s'- CALLED!", software) + if not isinstance(software, str) and software is not None: raise ValueError(f"software[]='{type(software)}' is not type 'str'") @@ -114,6 +115,7 @@ def alias(software: str) -> str: def strip_hosted_on(software: str) -> str: logger.debug("software='%s' - CALLED!", software) + if not isinstance(software, str): raise ValueError(f"Parameter software[]='{type(software)}' is not of type 'str'") elif software == "": @@ -160,6 +162,7 @@ def strip_powered_by(software: str) -> str: def strip_until(software: str, until: str) -> str: logger.debug("software='%s',until='%s' - CALLED!", software, until) + if not isinstance(software, str): raise ValueError(f"Parameter software[]='{type(software)}' is not of type 'str'") elif software == "": diff --git a/fba/helpers/tidyup.py b/fba/helpers/tidyup.py index 4b14ae7..580cb22 100644 --- a/fba/helpers/tidyup.py +++ b/fba/helpers/tidyup.py @@ -21,6 +21,7 @@ logger = logging.getLogger(__name__) def reason(string: str) -> str: logger.debug("string='%s' - CALLED!", string) + if not isinstance(string, str): raise ValueError(f"Parameter string[]='{type(string)}' is not of type 'str'") @@ -32,6 +33,7 @@ def reason(string: str) -> str: def domain(string: str) -> str: logger.debug("string='%s' - CALLED!", string) + if not isinstance(string, str): raise ValueError(f"Parameter string[]='{type(string)}' is not of type 'str'") diff --git a/fba/helpers/version.py b/fba/helpers/version.py index 5dda159..2f54ab3 100644 --- a/fba/helpers/version.py +++ b/fba/helpers/version.py @@ -35,6 +35,7 @@ patterns = [ def remove(software: str) -> str: logger.debug("software='%s' - CALLED!", software) + if "." not in software and " " not in software: logger.warning("software='%s' does not contain a version number.", software) return software diff --git a/fba/http/federation.py b/fba/http/federation.py index a5f5fdc..b22b278 100644 --- a/fba/http/federation.py +++ b/fba/http/federation.py @@ -533,6 +533,9 @@ def fetch_blocks(domain: str) -> list: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) + if not instances.is_registered(domain): + raise Exception(f"domain='{domain}' is not registered but function is invoked.") + # Init block list blocklist = list() diff --git a/fba/http/network.py b/fba/http/network.py index 6834de0..2f307fe 100644 --- a/fba/http/network.py +++ b/fba/http/network.py @@ -109,6 +109,7 @@ def post_json_api(domain: str, path: str, data: str = "", headers: dict = dict() def fetch_api_url(url: str, timeout: tuple) -> dict: logger.debug("url='%s',timeout()=%d - CALLED!", url, len(timeout)) + if not isinstance(url, str): raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'") elif url == "": diff --git a/fba/models/blocks.py b/fba/models/blocks.py index 0e445ea..f4af845 100644 --- a/fba/models/blocks.py +++ b/fba/models/blocks.py @@ -29,26 +29,19 @@ logger = logging.getLogger(__name__) def update_reason(reason: str, blocker: str, blocked: str, block_level: str): logger.debug("reason='%s',blocker='%s',blocked='%s',block_level='%s' - CALLED!", reason, blocker, blocked, block_level) + domain_helper.raise_on(blocker) + domain_helper.raise_on(blocked) + if not isinstance(reason, str) and reason is not None: raise ValueError(f"Parameter reason[]='{type(reason)}' is not of type 'str'") - elif not isinstance(blocker, str): - raise ValueError(f"Parameter blocker[]='{type(blocker)}' is not of type 'str'") - elif blocker == "": - raise ValueError("Parameter 'blocker' is empty") - elif blocker.lower() != blocker: - raise ValueError(f"Parameter blocker='{blocker}' must be all lower-case") - elif not isinstance(blocked, str): - raise ValueError(f"Parameter blocked[]='{type(blocked)}' is not of type 'str'") - elif blocked == "": - raise ValueError("Parameter 'blocked' is empty") - elif blocked.lower() != blocked: - raise ValueError(f"Parameter blocked='{blocked}' must be all lower-case") elif not isinstance(block_level, str): raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not of type 'str'") elif block_level == "": raise ValueError("Parameter 'block_level' is empty") elif block_level in ["accept", "suspend", "silence", "nsfw", "quarantined_instances"]: raise ValueError(f"block_level='{block_level}' is not wanted.") + elif not is_instance_blocked(blocker, blocked, block_level): + raise Exception(f"blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' is not registered but function is invoked") logger.debug("Updating block reason='%s',blocker='%s',blocked='%s',block_level='%s'", reason, blocker, blocked, block_level) database.cursor.execute( @@ -65,24 +58,21 @@ def update_reason(reason: str, blocker: str, blocked: str, block_level: str): def update_last_seen(blocker: str, blocked: str, block_level: str): logger.debug("blocker='%s',blocked='%s',block_level='%s' - CALLED!", blocker, blocked, block_level) - if not isinstance(blocker, str): - raise ValueError(f"Parameter blocker[]='{type(blocker)}' is not of type 'str'") - elif blocker == "": - raise ValueError("Parameter 'blocker' is empty") - elif blocker.lower() != blocker: - raise ValueError(f"Parameter blocker='{blocker}' must be all lower-case") - elif not isinstance(blocked, str): - raise ValueError(f"Parameter blocked[]='{type(blocked)}' is not of type 'str'") - elif blocked == "": - raise ValueError("Parameter 'blocked' is empty") - elif blocked.lower() != blocked: - raise ValueError(f"Parameter blocked='{blocked}' must be all lower-case") - elif not isinstance(block_level, str): + domain_helper.raise_on(blocker) + domain_helper.raise_on(blocked) + + if not isinstance(block_level, str): raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not of type 'str'") elif block_level == "": raise ValueError("Parameter 'block_level' is empty") elif block_level in ["accept", "suspend", "silence", "nsfw", "quarantined_instances"]: raise ValueError(f"blocked='{blocked}' has unwanted block_level='{block_level}'") + elif blacklist.is_blacklisted(blocker): + raise Exception(f"blocker='{blocker}' is blacklisted but function invoked") + elif blacklist.is_blacklisted(blocked): + raise Exception(f"blocked='{blocked}' is blacklisted but function invoked") + elif not is_instance_blocked(blocker, blocked, block_level): + raise Exception(f"blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' is not registered but function is invoked") database.cursor.execute( "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1", @@ -97,24 +87,19 @@ def update_last_seen(blocker: str, blocked: str, block_level: str): def is_instance_blocked(blocker: str, blocked: str, block_level: str = None) -> bool: logger.debug("blocker='%s',blocked='%s',block_level='%s' - CALLED!", blocker, blocked, block_level) - if not isinstance(blocker, str): - raise ValueError(f"Parameter blocker[]='{type(blocker)}' is not of type 'str'") - elif blocker == "": - raise ValueError("Parameter 'blocker' is empty") - elif blocker.lower() != blocker: - raise ValueError(f"Parameter blocker='{blocker}' must be all lower-case") - elif not isinstance(blocked, str): - raise ValueError(f"Parameter blocked[]='{type(blocked)}' is not of type 'str'") - elif blocked == "": - raise ValueError("Parameter 'blocked' is empty") - elif blocked.lower() != blocked: - raise ValueError(f"Parameter blocked='{blocked}' must be all lower-case") - elif not isinstance(block_level, str) and block_level is not None: + domain_helper.raise_on(blocker) + domain_helper.raise_on(blocked) + + if not isinstance(block_level, str) and block_level is not None: raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not of type 'str'") elif block_level == "": raise ValueError("Parameter 'block_level' is empty") elif block_level in ["accept", "suspend", "silence", "nsfw", "quarantined_instances"]: raise ValueError(f"blocked='{blocked}' has unwanted block_level='{block_level}'") + elif blacklist.is_blacklisted(blocker): + raise Exception(f"blocker='{blocker}' is blacklisted but function invoked") + elif blacklist.is_blacklisted(blocked): + raise Exception(f"blocked='{blocked}' is blacklisted but function invoked") if block_level is None: database.cursor.execute( @@ -148,12 +133,12 @@ def add(blocker: str, blocked: str, reason: str, block_level: str): raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not of type 'str'") elif block_level == "": raise ValueError("Parameter 'block_level' is empty") + elif block_level in ["accept", "suspend", "silence", "nsfw", "quarantined_instances"]: + raise ValueError(f"blocked='{blocked}' has unwanted block_level='{block_level}'") elif blacklist.is_blacklisted(blocker): raise Exception(f"blocker='{blocker}' is blacklisted but function invoked") elif blacklist.is_blacklisted(blocked): raise Exception(f"blocked='{blocked}' is blacklisted but function invoked") - elif block_level in ["accept", "suspend", "silence", "nsfw", "quarantined_instances"]: - raise ValueError(f"blocked='{blocked}' has unwanted block_level='{block_level}'") if reason is not None: # Maybe needs cleaning @@ -176,6 +161,7 @@ def add(blocker: str, blocked: str, reason: str, block_level: str): def valid(value: str, column: str) -> bool: logger.debug("value='%s' - CALLED!", value) + if not isinstance(value, str): raise ValueError(f"Parameter value[]='{type(value)}' is not of type 'str'") elif value == "": @@ -197,6 +183,7 @@ def valid(value: str, column: str) -> bool: def translate_idnas(rows: list, column: str): logger.debug("rows[]='%s' - CALLED!", type(rows)) + if not isinstance(rows, list): raise ValueError(f"rows[]='{type(rows)}' is not of type 'list'") elif len(rows) == 0: @@ -226,6 +213,7 @@ def translate_idnas(rows: list, column: str): def alias_block_level(block_level: str) -> str: logger.debug("block_level='%s' - CALLED!", block_level) + if not isinstance(block_level, str): raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not of type 'str'") elif block_level == "": diff --git a/fba/models/instances.py b/fba/models/instances.py index 36191bb..fe72a66 100644 --- a/fba/models/instances.py +++ b/fba/models/instances.py @@ -75,6 +75,7 @@ _pending = { def _set_data(key: str, domain: str, value: any): logger.debug("key='%s',domain='%s',value[]='%s' - CALLED!", key, domain, type(value)) domain_helper.raise_on(domain) + if not isinstance(key, str): raise ValueError(f"Parameter key[]='{type(key)}' is not of type 'str'") elif key == "": @@ -505,6 +506,7 @@ def valid(value: str, column: str) -> bool: def translate_idnas(rows: list, column: str): logger.debug("rows[]='%s' - CALLED!", type(rows)) + if not isinstance(rows, list): raise ValueError("rows[]='{type(rows)}' is not of type 'list'") elif len(rows) == 0: diff --git a/fba/networks/friendica.py b/fba/networks/friendica.py index 17fc28b..046bf90 100644 --- a/fba/networks/friendica.py +++ b/fba/networks/friendica.py @@ -34,6 +34,9 @@ def fetch_blocks(domain: str) -> list: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) + if not instances.is_registered(domain): + raise Exception(f"domain='{domain}' is not registered but function is invoked.") + blocklist = list() block_tag = None diff --git a/fba/networks/lemmy.py b/fba/networks/lemmy.py index c0b6afe..07a854a 100644 --- a/fba/networks/lemmy.py +++ b/fba/networks/lemmy.py @@ -87,6 +87,9 @@ def fetch_blocks(domain: str) -> list: logger.debug("domain='%s - CALLED!", domain) domain_helper.raise_on(domain) + if not instances.is_registered(domain): + raise Exception(f"domain='{domain}' is not registered but function is invoked.") + translations = [ "Blocked Instances".lower(), "Instàncies bloquejades".lower(), @@ -291,6 +294,7 @@ def fetch_instances(domain: str, origin: str) -> list: def parse_script(doc: bs4.BeautifulSoup, only: str = None) -> list: logger.debug("doc[]='%s',only='%s' - CALLED!") + if not isinstance(doc, bs4.BeautifulSoup): raise ValueError(f"Parameter doc[]='{type(only)}' is not of type 'bs4.BeautifulSoup'") elif not isinstance(only, str) and only is not None: diff --git a/fba/networks/mastodon.py b/fba/networks/mastodon.py index 679d2b0..bea34c9 100644 --- a/fba/networks/mastodon.py +++ b/fba/networks/mastodon.py @@ -63,6 +63,9 @@ def fetch_blocks_from_about(domain: str) -> dict: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) + if not instances.is_registered(domain): + raise Exception(f"domain='{domain}' is not registered but function is invoked.") + logger.debug("Fetching mastodon blocks from domain='%s'", domain) doc = None for path in ["/about/more", "/about"]: @@ -131,6 +134,9 @@ def fetch_blocks(domain: str) -> list: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) + if not instances.is_registered(domain): + raise Exception(f"domain='{domain}' is not registered but function is invoked.") + blocklist = list() logger.debug("Invoking federation.fetch_blocks(%s) ...", domain) diff --git a/fba/networks/misskey.py b/fba/networks/misskey.py index fd8b234..e9750da 100644 --- a/fba/networks/misskey.py +++ b/fba/networks/misskey.py @@ -125,6 +125,9 @@ def fetch_blocks(domain: str) -> list: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) + if not instances.is_registered(domain): + raise Exception(f"domain='{domain}' is not registered but function is invoked.") + # No CSRF by default, you don't have to add network.api_headers by yourself here headers = tuple() diff --git a/fba/networks/pleroma.py b/fba/networks/pleroma.py index 68ca874..73720f7 100644 --- a/fba/networks/pleroma.py +++ b/fba/networks/pleroma.py @@ -54,8 +54,12 @@ def fetch_blocks(domain: str) -> list: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) + if not instances.is_registered(domain): + raise Exception(f"domain='{domain}' is not registered but function is invoked.") + blockdict = list() rows = None + try: logger.debug("Fetching nodeinfo: domain='%s'", domain) rows = nodeinfo.fetch(domain, update_mode=False) @@ -276,6 +280,9 @@ def fetch_blocks_from_about(domain: str) -> dict: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) + if not instances.is_registered(domain): + raise Exception(f"domain='{domain}' is not registered but function is invoked.") + logger.debug("Fetching mastodon blocks from domain='%s'", domain) doc = None for path in ["/instance/about/index.html"]: diff --git a/fba/utils.py b/fba/utils.py index 44c8310..18e4712 100644 --- a/fba/utils.py +++ b/fba/utils.py @@ -46,6 +46,7 @@ def get_hash(domain: str) -> str: def fetch_url(url: str, headers: dict, timeout: tuple) -> requests.models.Response: logger.debug("url='%s',headers()=%d,timeout(%d)='%s' - CALLED!", url, len(headers), len(timeout), timeout) + if not isinstance(url, str): raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'") elif url == "": @@ -70,6 +71,7 @@ def fetch_url(url: str, headers: dict, timeout: tuple) -> requests.models.Respon def find_domains(tags: bs4.element.ResultSet, search: str) -> list: logger.debug("tags[%s]()=%d,search='%s' - CALLED!", type(tags), len(tags), search) + if not isinstance(tags, bs4.element.ResultSet): raise ValueError(f"Parameter tags[]='{type(tags)}' is not of type 'ResultSet'") elif not isinstance(search, str): -- 2.39.5