From f6902e370dd0c156d66fac95160f0b9db0daf5a7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 9 Dec 2023 03:46:25 +0100 Subject: [PATCH] Continued: - check all URLs against validator --- fba/helpers/domain.py | 2 ++ fba/helpers/processing.py | 2 ++ fba/http/federation.py | 2 ++ fba/http/network.py | 2 ++ fba/models/instances.py | 2 ++ fba/utils.py | 3 +++ 6 files changed, 13 insertions(+) diff --git a/fba/helpers/domain.py b/fba/helpers/domain.py index 4141258..2df92e8 100644 --- a/fba/helpers/domain.py +++ b/fba/helpers/domain.py @@ -76,6 +76,8 @@ def is_in_url(domain: str, url: str) -> bool: raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'") elif url == "": raise ValueError("Parameter 'url' is empty") + elif not validators.url(url): + raise ValueError(f"Parameter url='{url}' is not a valid URL") elif domain + url in _cache["is_in_url"]: logger.debug("Returning cached is_in_url='%s' - EXIT!", _cache["is_in_url"][domain + url]) return _cache["is_in_url"][domain + url] diff --git a/fba/helpers/processing.py b/fba/helpers/processing.py index d8f3d12..37a4706 100644 --- a/fba/helpers/processing.py +++ b/fba/helpers/processing.py @@ -120,6 +120,8 @@ def csv_block(blocker: str, url: str, command: str): raise ValueError(f"url[]='{url}' is not of type 'str'") elif url == "": raise ValueError("Parameter 'url' is empty") + elif not validators.url(url): + raise ValueError(f"Parameter url='{url}' is not a valid URL") elif not isinstance(command, str): raise ValueError(f"command[]='{command}' is not of type 'str'") elif command == "": diff --git a/fba/http/federation.py b/fba/http/federation.py index e0bd1ae..8b1508d 100644 --- a/fba/http/federation.py +++ b/fba/http/federation.py @@ -477,6 +477,7 @@ def determine_software(domain: str, path: str = None) -> str: def find_domains(tag: bs4.element.Tag) -> list: logger.debug("tag[]='%s' - CALLED!", type(tag)) + if not isinstance(tag, bs4.element.Tag): raise ValueError(f"Parameter tag[]='{type(tag)}' is not type of bs4.element.Tag") elif len(tag.select("tr")) == 0: @@ -527,6 +528,7 @@ def find_domains(tag: bs4.element.Tag) -> list: def add_peers(rows: dict) -> list: logger.debug("rows[]='%s' - CALLED!", type(rows)) + if not isinstance(rows, dict): raise ValueError(f"Parameter rows[]='{type(rows)}' is not of type 'dict'") diff --git a/fba/http/network.py b/fba/http/network.py index a5ad140..ec548d8 100644 --- a/fba/http/network.py +++ b/fba/http/network.py @@ -125,6 +125,8 @@ def fetch_api_url(url: str, timeout: tuple) -> dict: raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'") elif url == "": raise ValueError("Parameter 'url' is empty") + elif not validators.url(url): + raise ValueError(f"Parameter url='{url}' is not a valid URL") elif not isinstance(timeout, tuple): raise ValueError(f"timeout[]='{type(timeout)}' is not of type 'tuple'") diff --git a/fba/models/instances.py b/fba/models/instances.py index 619d05b..f01dd36 100644 --- a/fba/models/instances.py +++ b/fba/models/instances.py @@ -483,6 +483,8 @@ def set_nodeinfo_url(domain: str, url: str): raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'") elif url == "": raise ValueError("Parameter 'url' is empty") + elif not validators.url(url): + raise ValueError(f"Parameter url='{url}' is not a valid URL") # Set timestamp _set_data("nodeinfo_url", domain, url) diff --git a/fba/utils.py b/fba/utils.py index 562abde..f698194 100644 --- a/fba/utils.py +++ b/fba/utils.py @@ -20,6 +20,7 @@ from urllib.parse import urlparse import bs4 import requests +import validators from fba.helpers import blacklist from fba.helpers import config @@ -52,6 +53,8 @@ def fetch_url(url: str, headers: dict, timeout: tuple) -> requests.models.Respon raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'") elif url == "": raise ValueError("Parameter 'url' is empty") + elif not validators.url(url): + raise ValueError(f"Parameter url='{url}' is not a valid URL") elif not isinstance(headers, dict): raise ValueError(f"Parameter headers[]='{type(headers)}' is not of type 'dict'") elif not isinstance(timeout, tuple): -- 2.39.5