about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2020-06-01 03:47:20 +0200
committerGitHub <noreply@github.com>2020-06-01 03:47:20 +0200
commit51ff679b9deb559ddc53cdfa6265b996f6c7fd5e (patch)
tree5e2dc5a5ba4f0073b2b0b2a9c890909b004e36e5
parenteeddb1a624c0e743e3e7b6d35104adfb10f2a747 (diff)
Fix admin page crashing when trying to block an invalid domain name (#13884)
* Fix admin page crashing when trying to block an invalid domain name

Fixes #13880

* Fix trailing and leading spaces not being properly stripped for domain blocks
-rw-r--r--app/models/concerns/domain_normalizable.rb2
-rw-r--r--app/models/domain_block.rb4
2 files changed, 4 insertions, 2 deletions
diff --git a/app/models/concerns/domain_normalizable.rb b/app/models/concerns/domain_normalizable.rb
index c00b3142f..fb84058fc 100644
--- a/app/models/concerns/domain_normalizable.rb
+++ b/app/models/concerns/domain_normalizable.rb
@@ -4,7 +4,7 @@ module DomainNormalizable
   extend ActiveSupport::Concern
 
   included do
-    before_save :normalize_domain
+    before_validation :normalize_domain
   end
 
   private
diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb
index f0a5bd296..2b18e01fa 100644
--- a/app/models/domain_block.rb
+++ b/app/models/domain_block.rb
@@ -50,11 +50,13 @@ class DomainBlock < ApplicationRecord
     def rule_for(domain)
       return if domain.blank?
 
-      uri      = Addressable::URI.new.tap { |u| u.host = domain.gsub(/[\/]/, '') }
+      uri      = Addressable::URI.new.tap { |u| u.host = domain.strip.gsub(/[\/]/, '') }
       segments = uri.normalized_host.split('.')
       variants = segments.map.with_index { |_, i| segments[i..-1].join('.') }
 
       where(domain: variants).order(Arel.sql('char_length(domain) desc')).first
+    rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
+      nil
     end
   end