automatically block new user registrations based on referrer

This commit is contained in:
rimu 2024-03-22 14:35:51 +13:00
parent 081108a7c6
commit ef43e78ae5
5 changed files with 20 additions and 3 deletions

View file

@ -31,6 +31,7 @@ class SiteMiscForm(FlaskForm):
types = [('Open', _l('Open')), ('RequireApplication', _l('Require application')), ('Closed', _l('Closed'))] types = [('Open', _l('Open')), ('RequireApplication', _l('Require application')), ('Closed', _l('Closed'))]
registration_mode = SelectField(_l('Registration mode'), choices=types, default=1, coerce=str) registration_mode = SelectField(_l('Registration mode'), choices=types, default=1, coerce=str)
application_question = TextAreaField(_l('Question to ask people applying for an account')) application_question = TextAreaField(_l('Question to ask people applying for an account'))
auto_decline_referrers = TextAreaField(_l('Block registrations from these referrers (one per line)'))
log_activitypub_json = BooleanField(_l('Log ActivityPub JSON for debugging')) log_activitypub_json = BooleanField(_l('Log ActivityPub JSON for debugging'))
default_theme = SelectField(_l('Default theme'), coerce=str) default_theme = SelectField(_l('Default theme'), coerce=str)
submit = SubmitField(_l('Save')) submit = SubmitField(_l('Save'))

View file

@ -18,7 +18,7 @@ from app.community.util import save_icon_file, save_banner_file
from app.models import AllowedInstances, BannedInstances, ActivityPubLog, utcnow, Site, Community, CommunityMember, \ from app.models import AllowedInstances, BannedInstances, ActivityPubLog, utcnow, Site, Community, CommunityMember, \
User, Instance, File, Report, Topic, UserRegistration, Role, Post User, Instance, File, Report, Topic, UserRegistration, Role, Post
from app.utils import render_template, permission_required, set_setting, get_setting, gibberish, markdown_to_html, \ from app.utils import render_template, permission_required, set_setting, get_setting, gibberish, markdown_to_html, \
moderating_communities, joined_communities, finalize_user_setup, theme_list, blocked_phrases moderating_communities, joined_communities, finalize_user_setup, theme_list, blocked_phrases, blocked_referrers
from app.admin import bp from app.admin import bp
@ -80,12 +80,14 @@ def admin_misc():
site.reports_email_admins = form.reports_email_admins.data site.reports_email_admins = form.reports_email_admins.data
site.registration_mode = form.registration_mode.data site.registration_mode = form.registration_mode.data
site.application_question = form.application_question.data site.application_question = form.application_question.data
site.auto_decline_referrers = form.auto_decline_referrers.data
site.log_activitypub_json = form.log_activitypub_json.data site.log_activitypub_json = form.log_activitypub_json.data
site.updated = utcnow() site.updated = utcnow()
site.default_theme = form.default_theme.data site.default_theme = form.default_theme.data
if site.id is None: if site.id is None:
db.session.add(site) db.session.add(site)
db.session.commit() db.session.commit()
cache.delete_memoized(blocked_referrers)
flash('Settings saved.') flash('Settings saved.')
elif request.method == 'GET': elif request.method == 'GET':
form.enable_downvotes.data = site.enable_downvotes form.enable_downvotes.data = site.enable_downvotes
@ -97,6 +99,7 @@ def admin_misc():
form.reports_email_admins.data = site.reports_email_admins form.reports_email_admins.data = site.reports_email_admins
form.registration_mode.data = site.registration_mode form.registration_mode.data = site.registration_mode
form.application_question.data = site.application_question form.application_question.data = site.application_question
form.auto_decline_referrers.data = site.auto_decline_referrers
form.log_activitypub_json.data = site.log_activitypub_json form.log_activitypub_json.data = site.log_activitypub_json
form.default_theme.data = site.default_theme if site.default_theme is not None else '' form.default_theme.data = site.default_theme if site.default_theme is not None else ''
return render_template('admin/misc.html', title=_('Misc settings'), form=form, return render_template('admin/misc.html', title=_('Misc settings'), form=form,

View file

@ -12,7 +12,7 @@ from app.auth.util import random_token, normalize_utf
from app.email import send_verification_email, send_password_reset_email from app.email import send_verification_email, send_password_reset_email
from app.models import User, utcnow, IpBan, UserRegistration, Notification, Site from app.models import User, utcnow, IpBan, UserRegistration, Notification, Site
from app.utils import render_template, ip_address, user_ip_banned, user_cookie_banned, banned_ip_addresses, \ from app.utils import render_template, ip_address, user_ip_banned, user_cookie_banned, banned_ip_addresses, \
finalize_user_setup finalize_user_setup, blocked_referrers
@bp.route('/login', methods=['GET', 'POST']) @bp.route('/login', methods=['GET', 'POST'])
@ -98,6 +98,11 @@ def register():
if form.user_name.data in disallowed_usernames: if form.user_name.data in disallowed_usernames:
flash(_('Sorry, you cannot use that user name'), 'error') flash(_('Sorry, you cannot use that user name'), 'error')
else: else:
for referrer in blocked_referrers():
if referrer in session.get('Referer'):
resp = make_response(redirect(url_for('auth.please_wait')))
resp.set_cookie('sesion', '17489047567495', expires=datetime(year=2099, month=12, day=30))
return resp
verification_token = random_token(16) verification_token = random_token(16)
form.user_name.data = form.user_name.data.strip() form.user_name.data = form.user_name.data.strip()
before_normalize = form.user_name.data before_normalize = form.user_name.data

View file

@ -1171,7 +1171,7 @@ class Site(db.Model):
allowlist = db.Column(db.Text, default='') allowlist = db.Column(db.Text, default='')
blocklist = db.Column(db.Text, default='') blocklist = db.Column(db.Text, default='')
blocked_phrases = db.Column(db.Text, default='') # discard incoming content with these phrases blocked_phrases = db.Column(db.Text, default='') # discard incoming content with these phrases
auto_decline_referrers = db.Column(db.Text, default='rdrama.net') # automatically decline registration requests if the referrer is one of these auto_decline_referrers = db.Column(db.Text, default='rdrama.net\nahrefs.com') # automatically decline registration requests if the referrer is one of these
created_at = db.Column(db.DateTime, default=utcnow) created_at = db.Column(db.DateTime, default=utcnow)
updated = db.Column(db.DateTime, default=utcnow) updated = db.Column(db.DateTime, default=utcnow)
last_active = db.Column(db.DateTime, default=utcnow) last_active = db.Column(db.DateTime, default=utcnow)

View file

@ -339,6 +339,14 @@ def blocked_phrases() -> List[str]:
return [] return []
@cache.memoize(timeout=86400)
def blocked_referrers() -> List[str]:
site = Site.query.get(1)
if site.auto_decline_referrers:
return [referrer for referrer in site.auto_decline_referrers.split('\n') if referrer != '']
else:
return []
def retrieve_block_list(): def retrieve_block_list():
try: try:
response = requests.get('https://raw.githubusercontent.com/rimu/no-qanon/master/domains.txt', timeout=1) response = requests.get('https://raw.githubusercontent.com/rimu/no-qanon/master/domains.txt', timeout=1)