From 61209228b0b0fe8bd461c6ba52b5e8ae44000913 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 1 Jul 2023 01:37:25 +0200 Subject: [PATCH] Testing: - allow no redirects as normal Fediverse instances should neve redirect - send_bot_post() accepts as 2nd parameter a list, not a dictionary --- fba/commands.py | 7 ++++++- fba/csrf.py | 3 ++- fba/http/network.py | 20 +++++++++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/fba/commands.py b/fba/commands.py index 58a39bc..55be64e 100644 --- a/fba/commands.py +++ b/fba/commands.py @@ -360,7 +360,7 @@ def fetch_blocks(args: argparse.Namespace) -> int: logger.debug("Invoking cookies.clear(%s) ...", blocker) cookies.clear(blocker) - logger.debug("config[bot_enabled]='%s',blockdict()=%d'", config.get("bot_enabled"), len(blockdict)) + logger.debug("config.get(bot_enabled)='%s',blockdict()=%d'", config.get("bot_enabled"), len(blockdict)) if config.get("bot_enabled") and len(blockdict) > 0: logger.info("Sending bot POST for blocker='%s',blockdict()=%d ...", blocker, len(blockdict)) network.send_bot_post(blocker, blockdict) @@ -501,6 +501,7 @@ def fetch_todon_wiki(args: argparse.Namespace) -> int: logger.debug("Invoking commit() ...") database.connection.commit() + logger.debug("config.get(bot_enabled)='%s',blockdict()=%d", config.get("bot_enabled"), len(blockdict)) if config.get("bot_enabled") and len(blockdict) > 0: logger.info("Sending bot POST for blocker='todon.eu',blockdict()=%d ...", len(blockdict)) network.send_bot_post("todon.eu", blockdict) @@ -580,6 +581,7 @@ def fetch_cs(args: argparse.Namespace): logger.debug("Invoking commit() ...") database.connection.commit() + logger.debug("config.get(bot_enabled)='%s',blockdict()=%d", config.get("bot_enabled"), len(blockdict)) if config.get("bot_enabled") and len(blockdict) > 0: logger.info("Sending bot POST for blocker='chaos.social',blockdict()=%d ...", len(blockdict)) network.send_bot_post("chaos.social", blockdict) @@ -870,6 +872,7 @@ def fetch_oliphant(args: argparse.Namespace) -> int: logger.debug("Invoking commit() ...") database.connection.commit() + logger.debug("config.get(bot_enabled)='%s',blockdict()=%d", config.get("bot_enabled"), len(blockdict)) if config.get("bot_enabled") and len(blockdict) > 0: logger.info("Sending bot POST for blocker='%s',blockdict()=%d ...", block["blocker"], len(blockdict)) network.send_bot_post(block["blocker"], blockdict) @@ -1100,6 +1103,7 @@ def fetch_joinfediverse(args: argparse.Namespace) -> int: logger.debug("Invoking commit() ...") database.connection.commit() + logger.debug("config.get(bot_enabled)='%s',blockdict()=%d", config.get("bot_enabled"), len(blockdict)) if config.get("bot_enabled") and len(blockdict) > 0: logger.info("Sending bot POST for blocker='%s,blockdict()=%d ...", blocker, len(blockdict)) network.send_bot_post(blocker, blockdict) @@ -1200,6 +1204,7 @@ def recheck_obfuscation(args: argparse.Namespace) -> int: logger.debug("Invoking commit() ...") database.connection.commit() + logger.debug("config.get(bot_enabled)='%s',blockdict()=%d", config.get("bot_enabled"), len(blockdict)) if config.get("bot_enabled") and len(blockdict) > 0: logger.info("Sending bot POST for blocker='%s,blockdict()=%d ...", row["domain"], len(blockdict)) network.send_bot_post(row["domain"], blockdict) diff --git a/fba/csrf.py b/fba/csrf.py index e68fcb6..401b7cd 100644 --- a/fba/csrf.py +++ b/fba/csrf.py @@ -43,7 +43,8 @@ def determine(domain: str, headers: dict) -> dict: response = reqto.get( f"https://{domain}/", headers=network.web_headers, - timeout=(config.get("connection_timeout"), config.get("read_timeout")) + timeout=(config.get("connection_timeout"), config.get("read_timeout")), + allow_redirects=False ) logger.debug("response.ok='%s',response.status_code=%d,response.text()=%d", response.ok, response.status_code, len(response.text)) diff --git a/fba/http/network.py b/fba/http/network.py index f347b79..7081ba8 100644 --- a/fba/http/network.py +++ b/fba/http/network.py @@ -80,7 +80,8 @@ def post_json_api(domain: str, path: str, data: str = "", headers: dict = dict() data=data, headers={**api_headers, **headers}, timeout=(config.get("connection_timeout"), config.get("read_timeout")), - cookies=cookies.get_all(domain) if cookies.has(domain) else dict() + cookies=cookies.get_all(domain) if cookies.has(domain) else dict(), + allow_redirects=False ) logger.debug("Parsing JSON response from domain='%s',path='%s' ...", domain, path) @@ -165,7 +166,8 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: f"https://{domain}{path}", headers={**api_headers, **headers}, timeout=timeout, - cookies=cookies.get_all(domain) if cookies.has(domain) else {} + cookies=cookies.get_all(domain) if cookies.has(domain) else {}, + allow_redirects=False ) except exceptions as exception: @@ -190,12 +192,14 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: logger.debug("Returning json_reply(%d)[]='%s' - EXIT!", len(json_reply), type(json_reply)) return json_reply -def send_bot_post(domain: str, blocklist: dict): +def send_bot_post(domain: str, blocklist: list): logger.debug("domain='%s',blocklist()=%d - CALLED!", domain, len(blocklist)) domain_helper.raise_on(domain) - if not isinstance(blocklist, dict): - raise ValueError(f"Parameter blocklist[]='{type(blocklist)}' is not 'dict'") + if not isinstance(blocklist, list): + raise ValueError(f"Parameter blocklist[]='{type(blocklist)}' is not 'list'") + elif len(blocklist) == 0: + raise ValueError("Parameter 'blocklist' is empty") message = f"{domain} has blocked the following instances:\n\n" truncated = False @@ -228,7 +232,8 @@ def send_bot_post(domain: str, blocklist: dict): "content_type": "text/plain" }, headers=botheaders, - timeout=10 + timeout=(config.get("connection_timeout"), config.get("read_timeout")), + allow_redirects=False ).json() return True @@ -252,7 +257,8 @@ def fetch_response(domain: str, path: str, headers: dict, timeout: tuple) -> req f"https://{domain}{path}", headers=headers, timeout=timeout, - cookies=cookies.get_all(domain) if cookies.has(domain) else {} + cookies=cookies.get_all(domain) if cookies.has(domain) else {}, + allow_redirects=False ) except exceptions as exception: -- 2.39.5