Function to check domains

main
bashrc 2026-04-24 12:05:35 +01:00
parent 2a53e0148c
commit 083dddea0d
3 changed files with 34 additions and 6 deletions

View File

@ -16,6 +16,7 @@ from siteactive import referer_is_active
from httpcodes import http_400
from httpcodes import http_404
from httpcodes import http_503
from utils import resembles_domain
from utils import get_json_content_from_accept
from utils import convert_domains
from utils import local_network_host
@ -113,9 +114,7 @@ def _masto_api_v1(self, path: str, calling_domain: str,
# is this a real website making the call ?
if not debug and not unit_test and referer_domain:
# Does calling_domain look like a domain?
if ' ' in referer_domain or \
';' in referer_domain or \
'.' not in referer_domain:
if resembles_domain(referer_domain):
print('mastodon api ' +
'referer does not look like a domain ' +
referer_domain)
@ -246,9 +245,7 @@ def _masto_api_v2(self, path: str, calling_domain: str,
# is this a real website making the call ?
if not debug and not unit_test and referer_domain:
# Does calling_domain look like a domain?
if ' ' in referer_domain or \
';' in referer_domain or \
'.' not in referer_domain:
if not resembles_domain(referer_domain):
print('mastodon api v2 ' +
'referer does not look like a domain ' +
referer_domain)

View File

@ -67,6 +67,7 @@ from flags import is_right_to_left_text
from status import actor_status_expired
from status import get_actor_status
from unicodetext import uninvert_text
from utils import resembles_domain
from utils import remove_domain_port
from utils import get_port_from_domain
from utils import is_yggdrasil_url
@ -9769,6 +9770,17 @@ def _test_post_collection() -> None:
"https://lemmings/activities/like/7243"
def test_domain_check() -> None:
print('test domain check')
assert not resembles_domain('')
assert not resembles_domain('abcdef')
assert resembles_domain('192.168.3.9')
assert resembles_domain('somedomain.onion')
assert not resembles_domain('somedomain.onion and something else')
assert not resembles_domain('[abc.def.abc]')
assert resembles_domain('[abc:def:abc]')
def run_all_tests():
base_dir = os.getcwd()
data_dir_testing(base_dir)

View File

@ -4030,6 +4030,25 @@ def resembles_url(text: str) -> bool:
return False
def resembles_domain(text: str) -> bool:
"""Does the given text resemble a domain?
Why not use validators? It's so that exotic, potentially p2p domains
may be used.
"""
if ' ' in text or '-' in text or '<' in text or ';' in text or \
'"' in text or '(' in text or ')' in text:
return False
# conventional domain or yggdrasil address
if '.' in text and '[' not in text and ']' not in text:
return True
if (text.startswith('[') and text.endswith(']') and ':' in text):
return True
return False
def post_summary_contains_links(message_json: {}) -> bool:
"""check if the json post summary contains links
"""