diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2019-08-08 23:04:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-08 23:04:19 +0200 |
commit | 7a1f8a58df7edeb4f1d03c9dd3c25d5370d858a6 (patch) | |
tree | cc52064354f522e63a70494346655202b3d2ebf9 /app | |
parent | 699db454c370247684243d7497f6ebacefbb2a57 (diff) |
Fix crash when saving invalid domain name (#11528)
Fix #7629
Diffstat (limited to 'app')
-rw-r--r-- | app/models/account_domain_block.rb | 2 | ||||
-rw-r--r-- | app/models/concerns/domain_normalizable.rb | 2 | ||||
-rw-r--r-- | app/models/domain_allow.rb | 2 | ||||
-rw-r--r-- | app/models/domain_block.rb | 2 | ||||
-rw-r--r-- | app/models/email_domain_block.rb | 2 | ||||
-rw-r--r-- | app/validators/domain_validator.rb | 17 |
6 files changed, 22 insertions, 5 deletions
diff --git a/app/models/account_domain_block.rb b/app/models/account_domain_block.rb index 7c0d60379..3aaffde9a 100644 --- a/app/models/account_domain_block.rb +++ b/app/models/account_domain_block.rb @@ -15,7 +15,7 @@ class AccountDomainBlock < ApplicationRecord include DomainNormalizable belongs_to :account - validates :domain, presence: true, uniqueness: { scope: :account_id } + validates :domain, presence: true, uniqueness: { scope: :account_id }, domain: true after_commit :remove_blocking_cache after_commit :remove_relationship_cache diff --git a/app/models/concerns/domain_normalizable.rb b/app/models/concerns/domain_normalizable.rb index fb84058fc..c00b3142f 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_validation :normalize_domain + before_save :normalize_domain end private diff --git a/app/models/domain_allow.rb b/app/models/domain_allow.rb index 85018b636..5fe0e3a29 100644 --- a/app/models/domain_allow.rb +++ b/app/models/domain_allow.rb @@ -13,7 +13,7 @@ class DomainAllow < ApplicationRecord include DomainNormalizable - validates :domain, presence: true, uniqueness: true + validates :domain, presence: true, uniqueness: true, domain: true scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) } diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb index 3f5b9f23e..37b8d98c6 100644 --- a/app/models/domain_block.rb +++ b/app/models/domain_block.rb @@ -19,7 +19,7 @@ class DomainBlock < ApplicationRecord enum severity: [:silence, :suspend, :noop] - validates :domain, presence: true, uniqueness: true + validates :domain, presence: true, uniqueness: true, domain: true has_many :accounts, foreign_key: :domain, primary_key: :domain delegate :count, to: :accounts, prefix: true diff --git a/app/models/email_domain_block.rb b/app/models/email_domain_block.rb index 0fcd36477..bc70dea25 100644 --- a/app/models/email_domain_block.rb +++ b/app/models/email_domain_block.rb @@ -12,7 +12,7 @@ class EmailDomainBlock < ApplicationRecord include DomainNormalizable - validates :domain, presence: true, uniqueness: true + validates :domain, presence: true, uniqueness: true, domain: true def self.block?(email) _, domain = email.split('@', 2) diff --git a/app/validators/domain_validator.rb b/app/validators/domain_validator.rb new file mode 100644 index 000000000..ae07f1798 --- /dev/null +++ b/app/validators/domain_validator.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class DomainValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + return if value.blank? + + record.errors.add(attribute, I18n.t('domain_validator.invalid_domain')) unless compliant?(value) + end + + private + + def compliant?(value) + Addressable::URI.new.tap { |uri| uri.host = value } + rescue Addressable::URI::InvalidURIError + false + end +end |