about summary refs log tree commit diff
path: root/app/validators
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2023-04-14 19:22:47 -0500
committerStarfall <us@starfall.systems>2023-04-14 19:22:47 -0500
commit4fe1689de43f4404eb9530fcfbcbfb26d6c1c13a (patch)
tree6811b845bb7f4966b10dcefa3dea404246f161c7 /app/validators
parent65c1e53a32cabcdbb7bca57002bb0f6acdebe07e (diff)
parentbed63f6dae0879ac840066b031229e0d139089cd (diff)
Merge remote-tracking branch 'glitch/main' HEAD main
Diffstat (limited to 'app/validators')
-rw-r--r--app/validators/domain_validator.rb12
-rw-r--r--app/validators/ed25519_key_validator.rb2
-rw-r--r--app/validators/ed25519_signature_validator.rb2
-rw-r--r--app/validators/email_mx_validator.rb2
-rw-r--r--app/validators/existing_username_validator.rb14
-rw-r--r--app/validators/follow_limit_validator.rb1
-rw-r--r--app/validators/html_validator.rb20
-rw-r--r--app/validators/import_validator.rb12
-rw-r--r--app/validators/status_pin_validator.rb2
-rw-r--r--app/validators/unreserved_username_validator.rb2
-rw-r--r--app/validators/vote_validator.rb26
11 files changed, 47 insertions, 48 deletions
diff --git a/app/validators/domain_validator.rb b/app/validators/domain_validator.rb
index 6e4a854ff..3a951f9a7 100644
--- a/app/validators/domain_validator.rb
+++ b/app/validators/domain_validator.rb
@@ -4,13 +4,11 @@ class DomainValidator < ActiveModel::EachValidator
   def validate_each(record, attribute, value)
     return if value.blank?
 
-    domain = begin
-      if options[:acct]
-        value.split('@').last
-      else
-        value
-      end
-    end
+    domain = if options[:acct]
+               value.split('@').last
+             else
+               value
+             end
 
     record.errors.add(attribute, I18n.t('domain_validator.invalid_domain')) unless compliant?(domain)
   end
diff --git a/app/validators/ed25519_key_validator.rb b/app/validators/ed25519_key_validator.rb
index 00a448d5a..adf49296b 100644
--- a/app/validators/ed25519_key_validator.rb
+++ b/app/validators/ed25519_key_validator.rb
@@ -6,7 +6,7 @@ class Ed25519KeyValidator < ActiveModel::EachValidator
 
     key = Base64.decode64(value)
 
-    record.errors[attribute] << I18n.t('crypto.errors.invalid_key') unless verified?(key)
+    record.errors.add(attribute, I18n.t('crypto.errors.invalid_key')) unless verified?(key)
   end
 
   private
diff --git a/app/validators/ed25519_signature_validator.rb b/app/validators/ed25519_signature_validator.rb
index 77a21b837..0e74c231e 100644
--- a/app/validators/ed25519_signature_validator.rb
+++ b/app/validators/ed25519_signature_validator.rb
@@ -8,7 +8,7 @@ class Ed25519SignatureValidator < ActiveModel::EachValidator
     signature  = Base64.decode64(value)
     message    = option_to_value(record, :message)
 
-    record.errors[attribute] << I18n.t('crypto.errors.invalid_signature') unless verified?(verify_key, signature, message)
+    record.errors.add(attribute, I18n.t('crypto.errors.invalid_signature')) unless verified?(verify_key, signature, message)
   end
 
   private
diff --git a/app/validators/email_mx_validator.rb b/app/validators/email_mx_validator.rb
index 20f2fd37c..19c57bdf6 100644
--- a/app/validators/email_mx_validator.rb
+++ b/app/validators/email_mx_validator.rb
@@ -10,6 +10,8 @@ class EmailMxValidator < ActiveModel::Validator
 
     if domain.blank?
       user.errors.add(:email, :invalid)
+    elsif domain.include?('..')
+      user.errors.add(:email, :invalid)
     elsif !on_allowlist?(domain)
       resolved_ips, resolved_domains = resolve_mx(domain)
 
diff --git a/app/validators/existing_username_validator.rb b/app/validators/existing_username_validator.rb
index 1c5596821..45de4f4a4 100644
--- a/app/validators/existing_username_validator.rb
+++ b/app/validators/existing_username_validator.rb
@@ -4,16 +4,14 @@ class ExistingUsernameValidator < ActiveModel::EachValidator
   def validate_each(record, attribute, value)
     return if value.blank?
 
-    usernames_and_domains = begin
-      value.split(',').map do |str|
-        username, domain = str.strip.gsub(/\A@/, '').split('@', 2)
-        domain = nil if TagManager.instance.local_domain?(domain)
+    usernames_and_domains = value.split(',').map do |str|
+      username, domain = str.strip.gsub(/\A@/, '').split('@', 2)
+      domain = nil if TagManager.instance.local_domain?(domain)
 
-        next if username.blank?
+      next if username.blank?
 
-        [str, username, domain]
-      end.compact
-    end
+      [str, username, domain]
+    end.compact
 
     usernames_with_no_accounts = usernames_and_domains.filter_map do |(str, username, domain)|
       str unless Account.find_remote(username, domain)
diff --git a/app/validators/follow_limit_validator.rb b/app/validators/follow_limit_validator.rb
index 409bf0176..c619cb9a3 100644
--- a/app/validators/follow_limit_validator.rb
+++ b/app/validators/follow_limit_validator.rb
@@ -6,6 +6,7 @@ class FollowLimitValidator < ActiveModel::Validator
 
   def validate(follow)
     return if follow.account.nil? || !follow.account.local?
+
     follow.errors.add(:base, I18n.t('users.follow_limit_reached', limit: self.class.limit_for_account(follow.account))) if limit_reached?(follow.account)
   end
 
diff --git a/app/validators/html_validator.rb b/app/validators/html_validator.rb
deleted file mode 100644
index b85b9769f..000000000
--- a/app/validators/html_validator.rb
+++ /dev/null
@@ -1,20 +0,0 @@
-# frozen_string_literal: true
-
-class HtmlValidator < ActiveModel::EachValidator
-  ERROR_RE = /Opening and ending tag mismatch|Unexpected end tag/
-
-  def validate_each(record, attribute, value)
-    return if value.blank?
-
-    errors = html_errors(value)
-
-    record.errors.add(attribute, I18n.t('html_validator.invalid_markup', error: errors.first.to_s)) unless errors.empty?
-  end
-
-  private
-
-  def html_errors(str)
-    fragment = Nokogiri::HTML.fragment(options[:wrap_with] ? "<#{options[:wrap_with]}>#{str}</#{options[:wrap_with]}>" : str)
-    fragment.errors.select { |error| ERROR_RE.match?(error.message) }
-  end
-end
diff --git a/app/validators/import_validator.rb b/app/validators/import_validator.rb
index cbad56df6..782baf5d6 100644
--- a/app/validators/import_validator.rb
+++ b/app/validators/import_validator.rb
@@ -35,13 +35,11 @@ class ImportValidator < ActiveModel::Validator
   def validate_following_import(import, row_count)
     base_limit = FollowLimitValidator.limit_for_account(import.account)
 
-    limit = begin
-      if import.overwrite?
-        base_limit
-      else
-        base_limit - import.account.following_count
-      end
-    end
+    limit = if import.overwrite?
+              base_limit
+            else
+              base_limit - import.account.following_count
+            end
 
     import.errors.add(:data, I18n.t('users.follow_limit_reached', limit: base_limit)) if row_count > limit
   end
diff --git a/app/validators/status_pin_validator.rb b/app/validators/status_pin_validator.rb
index 9466a81fe..4af7bd295 100644
--- a/app/validators/status_pin_validator.rb
+++ b/app/validators/status_pin_validator.rb
@@ -7,6 +7,6 @@ class StatusPinValidator < ActiveModel::Validator
     pin.errors.add(:base, I18n.t('statuses.pin_errors.reblog')) if pin.status.reblog?
     pin.errors.add(:base, I18n.t('statuses.pin_errors.ownership')) if pin.account_id != pin.status.account_id
     pin.errors.add(:base, I18n.t('statuses.pin_errors.direct')) if pin.status.direct_visibility?
-    pin.errors.add(:base, I18n.t('statuses.pin_errors.limit')) if pin.account.status_pins.count >= MAX_PINNED  && pin.account.local?
+    pin.errors.add(:base, I18n.t('statuses.pin_errors.limit')) if pin.account.status_pins.count >= MAX_PINNED && pin.account.local?
   end
 end
diff --git a/app/validators/unreserved_username_validator.rb b/app/validators/unreserved_username_validator.rb
index 974f3ba62..f82f4b91d 100644
--- a/app/validators/unreserved_username_validator.rb
+++ b/app/validators/unreserved_username_validator.rb
@@ -13,12 +13,14 @@ class UnreservedUsernameValidator < ActiveModel::Validator
 
   def pam_controlled?
     return false unless Devise.pam_authentication && Devise.pam_controlled_service
+
     Rpam2.account(Devise.pam_controlled_service, @username).present?
   end
 
   def reserved_username?
     return true if pam_controlled?
     return false unless Setting.reserved_usernames
+
     Setting.reserved_usernames.include?(@username.downcase)
   end
 end
diff --git a/app/validators/vote_validator.rb b/app/validators/vote_validator.rb
index b1692562d..9c55f9ab6 100644
--- a/app/validators/vote_validator.rb
+++ b/app/validators/vote_validator.rb
@@ -2,13 +2,13 @@
 
 class VoteValidator < ActiveModel::Validator
   def validate(vote)
-    vote.errors.add(:base, I18n.t('polls.errors.expired')) if vote.poll.expired?
+    vote.errors.add(:base, I18n.t('polls.errors.expired')) if vote.poll_expired?
 
     vote.errors.add(:base, I18n.t('polls.errors.invalid_choice')) if invalid_choice?(vote)
 
-    if vote.poll.multiple? && vote.poll.votes.where(account: vote.account, choice: vote.choice).exists?
+    if vote.poll_multiple? && already_voted_for_same_choice_on_multiple_poll?(vote)
       vote.errors.add(:base, I18n.t('polls.errors.already_voted'))
-    elsif !vote.poll.multiple? && vote.poll.votes.where(account: vote.account).exists?
+    elsif !vote.poll_multiple? && already_voted_on_non_multiple_poll?(vote)
       vote.errors.add(:base, I18n.t('polls.errors.already_voted'))
     end
   end
@@ -18,4 +18,24 @@ class VoteValidator < ActiveModel::Validator
   def invalid_choice?(vote)
     vote.choice.negative? || vote.choice >= vote.poll.options.size
   end
+
+  def already_voted_for_same_choice_on_multiple_poll?(vote)
+    if vote.persisted?
+      account_votes_on_same_poll(vote).where(choice: vote.choice).where.not(poll_votes: { id: vote }).exists?
+    else
+      account_votes_on_same_poll(vote).where(choice: vote.choice).exists?
+    end
+  end
+
+  def already_voted_on_non_multiple_poll?(vote)
+    if vote.persisted?
+      account_votes_on_same_poll(vote).where.not(poll_votes: { id: vote }).exists?
+    else
+      account_votes_on_same_poll(vote).exists?
+    end
+  end
+
+  def account_votes_on_same_poll(vote)
+    vote.poll.votes.where(account: vote.account)
+  end
 end