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
This commit is contained in:
parent
eeddb1a624
commit
51ff679b9d
2 changed files with 4 additions and 2 deletions
|
@ -4,7 +4,7 @@ module DomainNormalizable
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
included do
|
included do
|
||||||
before_save :normalize_domain
|
before_validation :normalize_domain
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -50,11 +50,13 @@ class DomainBlock < ApplicationRecord
|
||||||
def rule_for(domain)
|
def rule_for(domain)
|
||||||
return if domain.blank?
|
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('.')
|
segments = uri.normalized_host.split('.')
|
||||||
variants = segments.map.with_index { |_, i| segments[i..-1].join('.') }
|
variants = segments.map.with_index { |_, i| segments[i..-1].join('.') }
|
||||||
|
|
||||||
where(domain: variants).order(Arel.sql('char_length(domain) desc')).first
|
where(domain: variants).order(Arel.sql('char_length(domain) desc')).first
|
||||||
|
rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue