block remote actors containing any word

This commit is contained in:
rimu 2024-05-16 15:44:42 +12:00
parent a77c203abf
commit d43e381b3e
4 changed files with 18 additions and 1 deletions

View file

@ -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

View file

@ -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'))

View file

@ -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()),

View file

@ -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