Skip to content

Commit

Permalink
filter inbox with domain blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy committed Nov 28, 2024
1 parent fd43895 commit 3794679
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/initializers/extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require "fediverse/inbox"
require "federails/moderation/filtered_inbox"
Fediverse::Inbox.send(:include, Federails::Moderation::FilteredInbox)
18 changes: 18 additions & 0 deletions lib/federails/moderation/filtered_inbox.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Federails::Moderation
module FilteredInbox
extend ActiveSupport::Concern

included do
class <<self
def filter_request(payload)
filtered_dispatch_request(payload) unless
(payload["id"] && DomainBlock.blocked?(URI.parse(payload["id"]).host)) ||
(payload["actor"] && DomainBlock.blocked?(URI.parse(payload["actor"]).host))
end

alias_method :filtered_dispatch_request, :dispatch_request
alias_method :dispatch_request, :filter_request
end
end
end
end
33 changes: 33 additions & 0 deletions spec/requests/domain_block_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
RSpec.describe Federails::Moderation::DomainBlock do
let(:actor) { FactoryBot.create(:user).federails_actor }

describe 'with a blocked domain' do
before do
described_class.create(domain: "example.com")
end

it 'allows non-matching activities' do
payload = { "id" => "https://test.com/abc1234" }
expect(Fediverse::Inbox).to receive(:filtered_dispatch_request).with(payload)
Fediverse::Inbox.dispatch_request(payload)
end

it 'rejects activities with matching IDs' do
payload = { "id" => "https://example.com/abc1234" }
expect(Fediverse::Inbox).not_to receive(:filtered_dispatch_request)
Fediverse::Inbox.dispatch_request(payload)
end

it 'rejects activities with subdomain-matching IDs' do
payload = { "id" => "https://www.example.com/abc1234" }
expect(Fediverse::Inbox).not_to receive(:filtered_dispatch_request)
Fediverse::Inbox.dispatch_request(payload)
end

it 'rejects activities with matching actors' do
payload = { "actor" => "https://example.com/users/abc1234" }
expect(Fediverse::Inbox).not_to receive(:filtered_dispatch_request)
Fediverse::Inbox.dispatch_request(payload)
end
end
end

0 comments on commit 3794679

Please sign in to comment.