]> cat aescling's git repositories - mastodon.git/commitdiff
Allow blocking TLDs, and fix TLD blocks not being editable (#12805)
authorThibG <thib@sitedethib.com>
Wed, 8 Jan 2020 21:42:05 +0000 (22:42 +0100)
committerEugen Rochko <eugen@zeonfederated.com>
Wed, 8 Jan 2020 21:42:05 +0000 (22:42 +0100)
Fixes #12795

It was already possible to create domain blocks for TLDs, but those
weren't enforced, nor editable. This commit changes it so that they
are enforced and editable.

app/models/domain_block.rb
spec/models/domain_block_spec.rb

index 4e865b850a970145d39ea27aaeb6033b6134d0da..f0a5bd296bcabcee05686819cad16297a9635ce2 100644 (file)
@@ -54,7 +54,7 @@ class DomainBlock < ApplicationRecord
       segments = uri.normalized_host.split('.')
       variants = segments.map.with_index { |_, i| segments[i..-1].join('.') }
 
-      where(domain: variants[0..-2]).order(Arel.sql('char_length(domain) desc')).first
+      where(domain: variants).order(Arel.sql('char_length(domain) desc')).first
     end
   end
 
index d98c5e11863030c11bbf694496b2d14db93335d5..28647dc89c62554a5cc2b62e0339e9a58e6a1bc5 100644 (file)
@@ -52,6 +52,16 @@ RSpec.describe DomainBlock, type: :model do
       block = Fabricate(:domain_block, domain: 'sub.example.com')
       expect(DomainBlock.rule_for('sub.example.com')).to eq block
     end
+
+    it 'returns a rule matching a blocked TLD' do
+      block = Fabricate(:domain_block, domain: 'google')
+      expect(DomainBlock.rule_for('google')).to eq block
+    end
+
+    it 'returns a rule matching a subdomain of a blocked TLD' do
+      block = Fabricate(:domain_block, domain: 'google')
+      expect(DomainBlock.rule_for('maps.google')).to eq block
+    end
   end
 
   describe '#stricter_than?' do