about summary refs log tree commit diff
path: root/app/models/email_domain_block.rb
blob: 10a0e51020e628c1cdcf5106b73fae866f7d8735 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true
# == Schema Information
#
# Table name: email_domain_blocks
#
#  id         :bigint(8)        not null, primary key
#  domain     :string           default(""), not null
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  parent_id  :bigint(8)
#

class EmailDomainBlock < ApplicationRecord
  self.ignored_columns = %w(
    ips
    last_refresh_at
  )

  include DomainNormalizable
  include Paginable

  belongs_to :parent, class_name: 'EmailDomainBlock', optional: true
  has_many :children, class_name: 'EmailDomainBlock', foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy

  validates :domain, presence: true, uniqueness: true, domain: true

  # Used for adding multiple blocks at once
  attr_accessor :other_domains

  def to_log_human_identifier
    domain
  end

  def history
    @history ||= Trends::History.new('email_domain_blocks', id)
  end

  class Matcher
    def initialize(domain_or_domains, attempt_ip: nil)
      @uris       = extract_uris(domain_or_domains)
      @attempt_ip = attempt_ip
    end

    def match?
      blocking? || invalid_uri?
    end

    private

    def invalid_uri?
      @uris.any?(&:nil?)
    end

    def blocking?
      blocks = EmailDomainBlock.where(domain: domains_with_variants).order(Arel.sql('char_length(domain) desc'))
      blocks.each { |block| block.history.add(@attempt_ip) } if @attempt_ip.present?
      blocks.any?
    end

    def domains_with_variants
      @uris.flat_map do |uri|
        next if uri.nil?

        segments = uri.normalized_host.split('.')

        segments.map.with_index { |_, i| segments[i..-1].join('.') }
      end
    end

    def extract_uris(domain_or_domains)
      Array(domain_or_domains).map do |str|
        domain = begin
          if str.include?('@')
            str.split('@', 2).last
          else
            str
          end
        end

        Addressable::URI.new.tap { |u| u.host = domain.strip } if domain.present?
      rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
        nil
      end
    end
  end

  def self.block?(domain_or_domains, attempt_ip: nil)
    Matcher.new(domain_or_domains, attempt_ip: attempt_ip).match?
  end
end