2017-10-04 13:16:10 +00:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 05:58:28 +00:00
|
|
|
|
2017-10-04 13:16:10 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: email_domain_blocks
|
|
|
|
#
|
2024-01-04 09:07:05 +00:00
|
|
|
# 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)
|
|
|
|
# allow_with_approval :boolean default(FALSE), not null
|
2017-10-04 13:16:10 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
class EmailDomainBlock < ApplicationRecord
|
2023-03-31 13:07:22 +00:00
|
|
|
self.ignored_columns += %w(
|
2022-04-29 21:27:03 +00:00
|
|
|
ips
|
|
|
|
last_refresh_at
|
|
|
|
)
|
|
|
|
|
2018-12-26 05:38:42 +00:00
|
|
|
include DomainNormalizable
|
2022-08-28 01:37:55 +00:00
|
|
|
include Paginable
|
2017-11-14 19:37:17 +00:00
|
|
|
|
2024-01-18 12:29:41 +00:00
|
|
|
with_options class_name: 'EmailDomainBlock' do
|
|
|
|
belongs_to :parent, optional: true
|
|
|
|
has_many :children, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
|
|
|
|
end
|
2020-03-12 21:35:20 +00:00
|
|
|
|
2019-08-08 21:04:19 +00:00
|
|
|
validates :domain, presence: true, uniqueness: true, domain: true
|
2017-11-14 19:37:17 +00:00
|
|
|
|
2022-02-24 16:28:23 +00:00
|
|
|
# Used for adding multiple blocks at once
|
|
|
|
attr_accessor :other_domains
|
2020-03-12 21:35:20 +00:00
|
|
|
|
2022-08-25 18:39:40 +00:00
|
|
|
def to_log_human_identifier
|
|
|
|
domain
|
|
|
|
end
|
|
|
|
|
2022-02-24 16:28:23 +00:00
|
|
|
def history
|
|
|
|
@history ||= Trends::History.new('email_domain_blocks', id)
|
2020-03-12 21:35:20 +00:00
|
|
|
end
|
|
|
|
|
2022-08-24 17:00:55 +00:00
|
|
|
class Matcher
|
|
|
|
def initialize(domain_or_domains, attempt_ip: nil)
|
|
|
|
@uris = extract_uris(domain_or_domains)
|
|
|
|
@attempt_ip = attempt_ip
|
|
|
|
end
|
2022-02-24 16:28:23 +00:00
|
|
|
|
2024-01-04 09:07:05 +00:00
|
|
|
def match?(...)
|
|
|
|
blocking?(...) || invalid_uri?
|
2022-02-24 16:28:23 +00:00
|
|
|
end
|
2020-03-12 21:35:20 +00:00
|
|
|
|
2022-08-24 17:00:55 +00:00
|
|
|
private
|
2017-11-14 19:37:17 +00:00
|
|
|
|
2022-08-24 17:00:55 +00:00
|
|
|
def invalid_uri?
|
|
|
|
@uris.any?(&:nil?)
|
|
|
|
end
|
2017-11-14 19:37:17 +00:00
|
|
|
|
2024-01-04 09:07:05 +00:00
|
|
|
def blocking?(allow_with_approval: false)
|
2024-03-12 13:09:11 +00:00
|
|
|
blocks = EmailDomainBlock.where(domain: domains_with_variants, allow_with_approval: allow_with_approval).by_domain_length
|
2022-08-24 17:00:55 +00:00
|
|
|
blocks.each { |block| block.history.add(@attempt_ip) } if @attempt_ip.present?
|
|
|
|
blocks.any?
|
2017-11-14 19:37:17 +00:00
|
|
|
end
|
|
|
|
|
2022-08-24 17:00:55 +00:00
|
|
|
def domains_with_variants
|
|
|
|
@uris.flat_map do |uri|
|
|
|
|
next if uri.nil?
|
|
|
|
|
|
|
|
segments = uri.normalized_host.split('.')
|
|
|
|
|
2023-07-12 08:03:06 +00:00
|
|
|
segments.map.with_index { |_, i| segments[i..].join('.') }
|
2022-08-24 17:00:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_uris(domain_or_domains)
|
|
|
|
Array(domain_or_domains).map do |str|
|
2023-02-18 22:09:40 +00:00
|
|
|
domain = if str.include?('@')
|
|
|
|
str.split('@', 2).last
|
|
|
|
else
|
|
|
|
str
|
|
|
|
end
|
2022-08-24 17:00:55 +00:00
|
|
|
|
|
|
|
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?
|
2017-10-04 13:16:10 +00:00
|
|
|
end
|
2024-01-04 09:07:05 +00:00
|
|
|
|
|
|
|
def self.requires_approval?(domain_or_domains, attempt_ip: nil)
|
|
|
|
Matcher.new(domain_or_domains, attempt_ip: attempt_ip).match?(allow_with_approval: true)
|
|
|
|
end
|
2017-10-04 13:16:10 +00:00
|
|
|
end
|