email_domain_block.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: email_domain_blocks
  5. #
  6. # id :bigint(8) not null, primary key
  7. # domain :string default(""), not null
  8. # created_at :datetime not null
  9. # updated_at :datetime not null
  10. # parent_id :bigint(8)
  11. #
  12. class EmailDomainBlock < ApplicationRecord
  13. self.ignored_columns += %w(
  14. ips
  15. last_refresh_at
  16. )
  17. include DomainNormalizable
  18. include Paginable
  19. belongs_to :parent, class_name: 'EmailDomainBlock', optional: true
  20. has_many :children, class_name: 'EmailDomainBlock', foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
  21. validates :domain, presence: true, uniqueness: true, domain: true
  22. # Used for adding multiple blocks at once
  23. attr_accessor :other_domains
  24. def to_log_human_identifier
  25. domain
  26. end
  27. def history
  28. @history ||= Trends::History.new('email_domain_blocks', id)
  29. end
  30. class Matcher
  31. def initialize(domain_or_domains, attempt_ip: nil)
  32. @uris = extract_uris(domain_or_domains)
  33. @attempt_ip = attempt_ip
  34. end
  35. def match?
  36. blocking? || invalid_uri?
  37. end
  38. private
  39. def invalid_uri?
  40. @uris.any?(&:nil?)
  41. end
  42. def blocking?
  43. blocks = EmailDomainBlock.where(domain: domains_with_variants).order(Arel.sql('char_length(domain) desc'))
  44. blocks.each { |block| block.history.add(@attempt_ip) } if @attempt_ip.present?
  45. blocks.any?
  46. end
  47. def domains_with_variants
  48. @uris.flat_map do |uri|
  49. next if uri.nil?
  50. segments = uri.normalized_host.split('.')
  51. segments.map.with_index { |_, i| segments[i..].join('.') }
  52. end
  53. end
  54. def extract_uris(domain_or_domains)
  55. Array(domain_or_domains).map do |str|
  56. domain = if str.include?('@')
  57. str.split('@', 2).last
  58. else
  59. str
  60. end
  61. Addressable::URI.new.tap { |u| u.host = domain.strip } if domain.present?
  62. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  63. nil
  64. end
  65. end
  66. end
  67. def self.block?(domain_or_domains, attempt_ip: nil)
  68. Matcher.new(domain_or_domains, attempt_ip: attempt_ip).match?
  69. end
  70. end