Skip to content

Commit

Permalink
Add reject pattern to Admin setting
Browse files Browse the repository at this point in the history
  • Loading branch information
noellabo authored and 204504bySE committed Feb 18, 2024
1 parent c41639d commit c23b023
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/controllers/admin/settings/others_controller.rb
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class Admin::Settings::OthersController < Admin::SettingsController
private

def after_update_redirect_path
admin_settings_others_path
end
end
6 changes: 5 additions & 1 deletion app/lib/activitypub/activity/create.rb
Expand Up @@ -44,8 +44,12 @@ def message_franking
)
end

def reject_pattern?
Setting.reject_pattern.present? && @object['content']&.match?(Setting.reject_pattern)
end

def create_status
return reject_payload! if unsupported_object_type? || invalid_origin?(object_uri) || tombstone_exists? || !related_to_local_activity?
return reject_payload! if unsupported_object_type? || invalid_origin?(object_uri) || tombstone_exists? || !related_to_local_activity? || reject_pattern?

with_lock("create:#{object_uri}") do
return if delete_arrived_first?(object_uri) || poll_vote?
Expand Down
3 changes: 3 additions & 0 deletions app/models/form/admin_settings.rb
Expand Up @@ -34,6 +34,8 @@ class Form::AdminSettings
backups_retention_period
status_page_url
captcha_enabled
authorized_fetch
reject_pattern
).freeze

INTEGER_KEYS = %i(
Expand Down Expand Up @@ -71,6 +73,7 @@ class Form::AdminSettings
validates :show_domain_blocks_rationale, inclusion: { in: %w(disabled users all) }, if: -> { defined?(@show_domain_blocks_rationale) }
validates :media_cache_retention_period, :content_cache_retention_period, :backups_retention_period, numericality: { only_integer: true }, allow_blank: true, if: -> { defined?(@media_cache_retention_period) || defined?(@content_cache_retention_period) || defined?(@backups_retention_period) }
validates :site_short_description, length: { maximum: 200 }, if: -> { defined?(@site_short_description) }
validates :reject_pattern, regexp_syntax: true, if: -> { defined?(@reject_pattern) }
validates :status_page_url, url: true, allow_blank: true
validate :validate_site_uploads

Expand Down
13 changes: 13 additions & 0 deletions app/validators/regexp_syntax_validator.rb
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class RegexpSyntaxValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.blank?

begin
Regexp.compile(value)
rescue RegexpError => e
record.errors.add(attribute, I18n.t('applications.invalid_regexp', message: e.message))
end
end
end
19 changes: 19 additions & 0 deletions app/views/admin/settings/others/show.html.haml
@@ -0,0 +1,19 @@
- content_for :page_title do
= t('admin.settings.others.title')

- content_for :heading do
%h2= t('admin.settings.title')
= render partial: 'admin/settings/shared/links'

= simple_form_for @admin_settings, url: admin_settings_others_path, html: { method: :patch } do |f|
= render 'shared/error_messages', object: @admin_settings

%p.lead= t('admin.settings.others.preamble')

%h4= t('admin.settings.others.activitypub')

.fields-group
= f.input :reject_pattern, wrapper: :with_block_label, as: :text, label: t('admin.settings.reject_pattern.title'), hint: t('admin.settings.reject_pattern.desc_html'), input_html: { rows: 8 }

.actions
= f.button :button, t('generic.save_changes'), type: :submit
16 changes: 15 additions & 1 deletion config/locales/en.yml
Expand Up @@ -747,6 +747,10 @@ en:
all: To everyone
disabled: To no one
users: To logged-in local users
others:
activitypub: ActivityPub
preamble: Other settings, including customizing behavior
title: Other settings
registrations:
preamble: Control who can create an account on your server.
title: Registrations
Expand All @@ -755,7 +759,15 @@ en:
approved: Approval required for sign up
none: Nobody can sign up
open: Anyone can sign up
title: Server Settings
reject_pattern:
desc_html: Set a regular expression pattern to inspect Create Activity content, and refuse Activity if you match
title: Reject Pattern
security:
authorized_fetch: Require authentication from federated servers
authorized_fetch_hint: Requiring authentication from federated servers enables stricter enforcement of both user-level and server-level blocks. However, this comes at the cost of a performance penalty, reduces the reach of your replies, and may introduce compatibility issues with some federated services. In addition, this will not prevent dedicated actors from fetching your public posts and accounts.
authorized_fetch_overridden_hint: You are currently unable to change this setting because it is overridden by an environment variable.
federation_authentication: Federation authentication enforcement
title: Server settings
site_uploads:
delete: Delete uploaded file
destroyed_msg: Site upload successfully deleted!
Expand Down Expand Up @@ -967,6 +979,8 @@ en:
applications:
created: Application successfully created
destroyed: Application successfully deleted
invalid_regexp: "The provided Regexp is invalid: %{message}"
logout: Logout
regenerate_token: Regenerate access token
token_regenerated: Access token successfully regenerated
warning: Be very careful with this data. Never share it with anyone!
Expand Down
1 change: 1 addition & 0 deletions config/settings.yml
Expand Up @@ -73,6 +73,7 @@ defaults: &defaults
require_invite_text: false
backups_retention_period: 7
captcha_enabled: false
reject_pattern: ''

development:
<<: *defaults
Expand Down

0 comments on commit c23b023

Please sign in to comment.