From d43e381b3e05ff5c72de9ca0680769c29d99ee3b Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Thu, 16 May 2024 15:44:42 +1200 Subject: [PATCH] block remote actors containing any word --- app/activitypub/util.py | 4 +++- app/admin/forms.py | 1 + app/admin/routes.py | 3 +++ app/utils.py | 11 +++++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/activitypub/util.py b/app/activitypub/util.py index fbea4c06..9aad4ea4 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -30,7 +30,7 @@ from app.utils import get_request, allowlist_html, get_setting, ap_datetime, mar is_image_url, domain_from_url, gibberish, ensure_directory_exists, markdown_to_text, head_request, post_ranking, \ shorten_string, reply_already_exists, reply_is_just_link_to_gif_reaction, confidence, remove_tracking_from_link, \ blocked_phrases, microblog_content_to_title, generate_image_from_video_url, is_video_url, reply_is_stupid, \ - notification_subscribers, communities_banned_from, lemmy_markdown_to_html + notification_subscribers, communities_banned_from, lemmy_markdown_to_html, actor_contains_blocked_words def public_key(): @@ -269,6 +269,8 @@ def find_actor_or_create(actor: str, create_if_not_found=True, community_only=Fa else: if instance_blocked(server): return None + if actor_contains_blocked_words(actor): + return None user = User.query.filter(User.ap_profile_id == actor).first() # finds users formatted like https://kbin.social/u/tables if (user and user.banned) or (user and user.deleted) : return None diff --git a/app/admin/forms.py b/app/admin/forms.py index 7e168130..6c34c071 100644 --- a/app/admin/forms.py +++ b/app/admin/forms.py @@ -46,6 +46,7 @@ class FederationForm(FlaskForm): use_blocklist = BooleanField(_l('Blocklist instead of allowlist')) blocklist = TextAreaField(_l('Deny federation with these instances')) blocked_phrases = TextAreaField(_l('Discard all posts and comments with these phrases (one per line)')) + blocked_actors = TextAreaField(_l('Discard all posts and comments by users with these words in their name (one per line)')) submit = SubmitField(_l('Save')) diff --git a/app/admin/routes.py b/app/admin/routes.py index 92ba7652..324cd1bc 100644 --- a/app/admin/routes.py +++ b/app/admin/routes.py @@ -142,7 +142,9 @@ def admin_federation(): db.session.add(BannedInstances(domain=banned.strip())) cache.delete_memoized(instance_blocked, banned.strip()) site.blocked_phrases = form.blocked_phrases.data + set_setting('actor_blocked_words', form.blocked_actors.data) cache.delete_memoized(blocked_phrases) + cache.delete_memoized(get_setting, 'actor_blocked_words') db.session.commit() flash(_('Admin settings saved')) @@ -155,6 +157,7 @@ def admin_federation(): instances = AllowedInstances.query.all() form.allowlist.data = '\n'.join([instance.domain for instance in instances]) form.blocked_phrases.data = site.blocked_phrases + form.blocked_actors.data = get_setting('actor_blocked_words', '88') return render_template('admin/federation.html', title=_('Federation settings'), form=form, moderating_communities=moderating_communities(current_user.get_id()), diff --git a/app/utils.py b/app/utils.py index 89a46c0f..78a23f53 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1013,3 +1013,14 @@ def languages_for_form(): def english_language_id(): english = Language.query.filter(Language.code == 'en').first() return english.id if english else None + + +def actor_contains_blocked_words(actor): + actor = actor.lower().strip() + blocked_words = get_setting('actor_blocked_words') + if blocked_words and blocked_words.strip() != '': + for blocked_word in blocked_words.split('\n'): + blocked_word = blocked_word.lower().strip() + if blocked_word in actor: + return True + return False