valid_email2 7.1.0 → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yaml +2 -7
- data/.gitignore +1 -0
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +18 -0
- data/Gemfile +2 -1
- data/README.md +26 -0
- data/config/disposable_email_domains.txt +11264 -6
- data/lib/valid_email2/address.rb +3 -3
- data/lib/valid_email2/dns.rb +26 -26
- data/lib/valid_email2/email_validator.rb +1 -1
- data/lib/valid_email2/sorted_domain_list.rb +36 -0
- data/lib/valid_email2/version.rb +1 -1
- data/lib/valid_email2.rb +27 -0
- data/pull_mailchecker_emails.rb +2 -1
- data/spec/address_spec.rb +53 -157
- data/spec/benchmark_spec.rb +11 -1
- data/spec/dns_spec.rb +44 -0
- data/spec/sorted_domain_list_spec.rb +17 -0
- data/spec/valid_email2_spec.rb +43 -0
- metadata +215 -192
data/lib/valid_email2/address.rb
CHANGED
|
@@ -85,7 +85,7 @@ module ValidEmail2
|
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
def disposable_domain?
|
|
88
|
-
valid? && domain_is_in?(address.domain, ValidEmail2.
|
|
88
|
+
valid? && domain_is_in?(address.domain, ValidEmail2.disposable_domains)
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
def allow_listed?
|
|
@@ -121,7 +121,7 @@ module ValidEmail2
|
|
|
121
121
|
end
|
|
122
122
|
|
|
123
123
|
def disposable_mx_server?
|
|
124
|
-
mx_server_is_in?(ValidEmail2.
|
|
124
|
+
mx_server_is_in?(ValidEmail2.disposable_domains)
|
|
125
125
|
end
|
|
126
126
|
|
|
127
127
|
# TODO: (PS) keep this for backward compatibility with the test setup described in the reamde
|
|
@@ -136,7 +136,7 @@ module ValidEmail2
|
|
|
136
136
|
return true if domain_list.include?(address_domain)
|
|
137
137
|
|
|
138
138
|
tokens = address_domain.split('.')
|
|
139
|
-
|
|
139
|
+
next false if tokens.length < 3
|
|
140
140
|
|
|
141
141
|
# check only 6 elements deep
|
|
142
142
|
2.upto(6).each do |depth|
|
data/lib/valid_email2/dns.rb
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
require "resolv"
|
|
2
|
+
require "logger" # Fix concurrent-ruby removing logger dependency which Rails itself does not have
|
|
3
|
+
require "active_support"
|
|
4
|
+
require "active_support/cache"
|
|
2
5
|
|
|
3
6
|
module ValidEmail2
|
|
4
7
|
class Dns
|
|
5
|
-
|
|
6
|
-
CACHE = {}
|
|
8
|
+
DEFAULT_CACHE_SIZE_BYTES = 1024 * 1024
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
class << self
|
|
11
|
+
attr_reader :cache, :cache_size
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
def cache_size=(bytes)
|
|
14
|
+
@cache_size = bytes
|
|
15
|
+
# Cached records are never mutated, so they don't need MemoryStore's defensive copies.
|
|
16
|
+
@cache = ActiveSupport::Cache::MemoryStore.new(size: bytes, coder: nil)
|
|
17
|
+
end
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
def clear_cache
|
|
20
|
+
cache.clear
|
|
21
|
+
end
|
|
18
22
|
end
|
|
19
23
|
|
|
24
|
+
self.cache_size = DEFAULT_CACHE_SIZE_BYTES
|
|
25
|
+
|
|
20
26
|
def initialize(dns_timeout = 5, dns_nameserver = nil)
|
|
21
27
|
@dns_timeout = dns_timeout
|
|
22
28
|
@dns_nameserver = dns_nameserver
|
|
@@ -32,22 +38,10 @@ module ValidEmail2
|
|
|
32
38
|
|
|
33
39
|
private
|
|
34
40
|
|
|
35
|
-
def prune_cache
|
|
36
|
-
self.class.prune_cache
|
|
37
|
-
end
|
|
38
|
-
|
|
39
41
|
def fetch(domain, type)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
cache_key = [domain, type]
|
|
44
|
-
cache_entry = CACHE[cache_key]
|
|
45
|
-
|
|
46
|
-
if cache_entry && Time.now - cache_entry.cached_at < cache_entry.ttl
|
|
47
|
-
return cache_entry.records
|
|
48
|
-
else
|
|
49
|
-
CACHE.delete(cache_key)
|
|
50
|
-
end
|
|
42
|
+
cache_key = "#{domain.downcase}/#{type}"
|
|
43
|
+
cached_records = self.class.cache.read(cache_key)
|
|
44
|
+
return cached_records if cached_records
|
|
51
45
|
|
|
52
46
|
records = Resolv::DNS.open(resolv_config) do |dns|
|
|
53
47
|
dns.timeouts = @dns_timeout
|
|
@@ -56,12 +50,18 @@ module ValidEmail2
|
|
|
56
50
|
|
|
57
51
|
if records.any?
|
|
58
52
|
ttl = records.map(&:ttl).min
|
|
59
|
-
|
|
53
|
+
cache_records(cache_key, records, ttl)
|
|
60
54
|
end
|
|
61
55
|
|
|
62
56
|
records
|
|
63
57
|
end
|
|
64
58
|
|
|
59
|
+
def cache_records(cache_key, records, ttl)
|
|
60
|
+
self.class.cache.write(cache_key, records, expires_in: ttl)
|
|
61
|
+
rescue TypeError
|
|
62
|
+
# MemoryStore sizes entries with Marshal, which can't dump test doubles
|
|
63
|
+
end
|
|
64
|
+
|
|
65
65
|
def resolv_config
|
|
66
66
|
config = Resolv::DNS::Config.default_config_hash
|
|
67
67
|
# REM: resolv gem 0.6.1 on linux can return frozen hash
|
|
@@ -44,7 +44,7 @@ module ValidEmail2
|
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
if options[:disposable_with_allow_list]
|
|
47
|
-
error(record, attribute, :disposable_with_allow_list) && return if addresses.any? { |address| address.
|
|
47
|
+
error(record, attribute, :disposable_with_allow_list) && return if addresses.any? { |address| !address.allow_listed? && address.disposable? }
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
if options[:disposable_domain_with_allow_list]
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ValidEmail2
|
|
4
|
+
# A list of domains read from a sorted file, one domain per line, and kept as a single String
|
|
5
|
+
# searched by bisection. A Set of the same domains takes several times the memory: each domain
|
|
6
|
+
# becomes a String object of its own, plus its hash table entry.
|
|
7
|
+
class SortedDomainList
|
|
8
|
+
NEWLINE = "\n".b.freeze
|
|
9
|
+
|
|
10
|
+
def initialize(path)
|
|
11
|
+
@list = File.binread(path).freeze
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def include?(domain)
|
|
15
|
+
domain = domain.to_s.b
|
|
16
|
+
low = 0
|
|
17
|
+
high = @list.bytesize
|
|
18
|
+
|
|
19
|
+
while low < high
|
|
20
|
+
middle = (low + high) / 2
|
|
21
|
+
start = (@list.rindex(NEWLINE, middle - 1) || -1) + 1
|
|
22
|
+
stop = @list.index(NEWLINE, start) || @list.bytesize
|
|
23
|
+
line = @list.byteslice(start, stop - start)
|
|
24
|
+
return true if line == domain
|
|
25
|
+
|
|
26
|
+
if line < domain
|
|
27
|
+
low = stop + 1
|
|
28
|
+
else
|
|
29
|
+
high = start
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
false
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
data/lib/valid_email2/version.rb
CHANGED
data/lib/valid_email2.rb
CHANGED
|
@@ -1,17 +1,44 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "valid_email2/email_validator"
|
|
4
|
+
require "valid_email2/sorted_domain_list"
|
|
4
5
|
|
|
5
6
|
module ValidEmail2
|
|
6
7
|
DENY_LIST_FILE = "config/deny_listed_email_domains.yml"
|
|
7
8
|
ALLOW_LIST_FILE = "config/allow_listed_email_domains.yml"
|
|
8
9
|
DISPOSABLE_FILE = File.expand_path('../config/disposable_email_domains.txt', __dir__)
|
|
10
|
+
DISPOSABLE_DOMAINS_LOOKUPS = %i[set sorted_list].freeze
|
|
9
11
|
|
|
10
12
|
class << self
|
|
13
|
+
# How disposable domains are looked up:
|
|
14
|
+
# - :set (default): the Set of disposable_emails, about 35 MB, with the fastest lookups;
|
|
15
|
+
# - :sorted_list: a SortedDomainList, about 3 MB, with lookups of a few microseconds.
|
|
16
|
+
def disposable_domains_lookup
|
|
17
|
+
@disposable_domains_lookup || :set
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def disposable_domains_lookup=(lookup)
|
|
21
|
+
unless DISPOSABLE_DOMAINS_LOOKUPS.include?(lookup)
|
|
22
|
+
raise ArgumentError, "disposable_domains_lookup must be one of #{DISPOSABLE_DOMAINS_LOOKUPS}, " \
|
|
23
|
+
"got #{lookup}"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
@disposable_domains_lookup = lookup
|
|
27
|
+
end
|
|
28
|
+
|
|
11
29
|
def disposable_emails
|
|
12
30
|
@disposable_emails ||= load_file(DISPOSABLE_FILE)
|
|
13
31
|
end
|
|
14
32
|
|
|
33
|
+
# The disposable domains lookups use, as chosen by disposable_domains_lookup.
|
|
34
|
+
def disposable_domains
|
|
35
|
+
if disposable_domains_lookup == :sorted_list
|
|
36
|
+
@sorted_disposable_domains ||= SortedDomainList.new(DISPOSABLE_FILE)
|
|
37
|
+
else
|
|
38
|
+
disposable_emails
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
15
42
|
def deny_list
|
|
16
43
|
@deny_list ||= load_if_exists(DENY_LIST_FILE) || Set.new
|
|
17
44
|
end
|
data/pull_mailchecker_emails.rb
CHANGED
|
@@ -16,7 +16,7 @@ allow_listed_emails = %w[
|
|
|
16
16
|
duck.com mozmail.com dralias.com 8alias.com 8shield.net
|
|
17
17
|
mailinblack.com anonaddy.com anonaddy.me addy.io privaterelay.appleid.com appleid.com
|
|
18
18
|
net.ua kommespaeter.de alpenjodel.de my.id web.id directbox.com embarqmail.com
|
|
19
|
-
unlimit.com reallyfast.info
|
|
19
|
+
unlimit.com reallyfast.info dreamhost.com ecmail.com google.com
|
|
20
20
|
]
|
|
21
21
|
|
|
22
22
|
existing_emails = File.open("config/disposable_email_domains.txt") { |f| f.read.split("\n") }
|
|
@@ -41,5 +41,6 @@ result_emails = result_emails.map do |line|
|
|
|
41
41
|
ts.last(2).join(".")
|
|
42
42
|
end
|
|
43
43
|
end.uniq.sort
|
|
44
|
+
result_emails -= allow_listed_emails
|
|
44
45
|
|
|
45
46
|
File.open("config/disposable_email_domains.txt", "w") { |f| f.puts(result_emails) }
|
data/spec/address_spec.rb
CHANGED
|
@@ -121,7 +121,21 @@ describe ValidEmail2::Address do
|
|
|
121
121
|
let(:email_instance) { described_class.new(email_address, dns_instance) }
|
|
122
122
|
let(:ttl) { 1_000 }
|
|
123
123
|
let(:mock_resolv_dns) { instance_double(Resolv::DNS) }
|
|
124
|
-
let(:mock_mx_records) { [
|
|
124
|
+
let(:mock_mx_records) { [mx_record("mx.ymail.com")] }
|
|
125
|
+
|
|
126
|
+
def with_ttl(record)
|
|
127
|
+
record.instance_variable_set(:@ttl, ttl)
|
|
128
|
+
record
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def mx_record(exchange)
|
|
132
|
+
with_ttl(Resolv::DNS::Resource::IN::MX.new(10, Resolv::DNS::Name.create(exchange)))
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def travel(seconds)
|
|
136
|
+
now = Time.now
|
|
137
|
+
allow(Time).to receive(:now).and_return(now + seconds)
|
|
138
|
+
end
|
|
125
139
|
|
|
126
140
|
before do
|
|
127
141
|
allow(email_instance).to receive(:null_mx?).and_return(false)
|
|
@@ -168,12 +182,32 @@ describe ValidEmail2::Address do
|
|
|
168
182
|
|
|
169
183
|
expect(disposable_email_instance.send(:disposable_mx_server?)).to be_truthy
|
|
170
184
|
end
|
|
185
|
+
|
|
186
|
+
context "when the domain has several MX servers" do
|
|
187
|
+
let(:ordinary_mx_record) { double("MX", exchange: "ordinary.example", preference: 10, ttl:) }
|
|
188
|
+
let(:disposable_mx_record) { double("MX", exchange: "emailfake.com", preference: 20, ttl:) }
|
|
189
|
+
|
|
190
|
+
def address_with_mx_records(domain, mx_records)
|
|
191
|
+
allow(mock_resolv_dns).to receive(:getresources)
|
|
192
|
+
.with(domain, Resolv::DNS::Resource::IN::MX)
|
|
193
|
+
.and_return(mx_records)
|
|
194
|
+
|
|
195
|
+
described_class.new("example@#{domain}", dns_instance)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
it "is disposable when an ordinary MX host with one dot is listed before a disposable one" do
|
|
199
|
+
address = address_with_mx_records("ordinary-first.example", [ordinary_mx_record, disposable_mx_record])
|
|
200
|
+
expect(address.disposable?).to be_truthy
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
it "is disposable when a disposable MX host is listed before an ordinary one with one dot" do
|
|
204
|
+
address = address_with_mx_records("disposable-first.example", [disposable_mx_record, ordinary_mx_record])
|
|
205
|
+
expect(address.disposable?).to be_truthy
|
|
206
|
+
end
|
|
207
|
+
end
|
|
171
208
|
end
|
|
172
209
|
|
|
173
210
|
describe "#valid_strict_mx?" do
|
|
174
|
-
let(:cached_at) { Time.now }
|
|
175
|
-
let(:mock_cache_data) { { [email_instance.address.domain, Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_mx_records, cached_at, ttl) } }
|
|
176
|
-
|
|
177
211
|
before do
|
|
178
212
|
allow(mock_resolv_dns).to receive(:getresources)
|
|
179
213
|
.with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX)
|
|
@@ -208,101 +242,31 @@ describe ValidEmail2::Address do
|
|
|
208
242
|
|
|
209
243
|
describe "ttl" do
|
|
210
244
|
before do
|
|
211
|
-
|
|
212
|
-
allow(ValidEmail2::Dns).to receive(:new).and_return(dns_instance)
|
|
213
|
-
allow(dns_instance).to receive(:fetch).with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX).and_call_original
|
|
245
|
+
email_instance.valid_strict_mx?
|
|
214
246
|
end
|
|
215
247
|
|
|
216
248
|
context "when the time since last lookup is less than the cached ttl entry" do
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
it "does not call the MX servers lookup" do
|
|
220
|
-
email_instance.valid_strict_mx?
|
|
221
|
-
|
|
222
|
-
expect(Resolv::DNS).not_to have_received(:open)
|
|
223
|
-
end
|
|
224
|
-
end
|
|
225
|
-
|
|
226
|
-
context "when the time since last lookup is greater than the cached ttl entry" do
|
|
227
|
-
let(:cached_at) { Time.now - ttl }
|
|
228
|
-
|
|
229
|
-
it "calls the MX servers lookup" do
|
|
249
|
+
it "does not call the MX servers lookup again" do
|
|
250
|
+
travel(ttl - 1)
|
|
230
251
|
email_instance.valid_strict_mx?
|
|
231
252
|
|
|
232
253
|
expect(Resolv::DNS).to have_received(:open).once
|
|
233
254
|
end
|
|
234
255
|
end
|
|
235
|
-
end
|
|
236
|
-
|
|
237
|
-
describe "cache size" do
|
|
238
|
-
before do
|
|
239
|
-
stub_const("ValidEmail2::Dns::CACHE", mock_cache_data)
|
|
240
|
-
allow(ValidEmail2::Dns).to receive(:new).and_return(dns_instance)
|
|
241
|
-
allow(dns_instance).to receive(:fetch).with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX).and_call_original
|
|
242
|
-
end
|
|
243
|
-
|
|
244
|
-
context "when the cache size is less than or equal to the max cache size" do
|
|
245
|
-
before do
|
|
246
|
-
stub_const("ValidEmail2::Dns::MAX_CACHE_SIZE", 1)
|
|
247
|
-
end
|
|
248
|
-
|
|
249
|
-
it "does not prune the cache" do
|
|
250
|
-
expect(dns_instance).not_to receive(:prune_cache)
|
|
251
|
-
|
|
252
|
-
email_instance.valid_strict_mx?
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
it "does not call the MX servers lookup" do
|
|
256
|
-
email_instance.valid_strict_mx?
|
|
257
|
-
|
|
258
|
-
expect(Resolv::DNS).not_to have_received(:open)
|
|
259
|
-
end
|
|
260
|
-
|
|
261
|
-
context "and there are older cached entries" do
|
|
262
|
-
let(:mock_cache_data) { { ["another_domain.com", Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_mx_records, cached_at - 100, ttl) } }
|
|
263
|
-
|
|
264
|
-
it "does not prune those entries" do
|
|
265
|
-
email_instance.valid_strict_mx?
|
|
266
|
-
|
|
267
|
-
expect(ValidEmail2::Dns::CACHE.keys).to match_array([[email_instance.address.domain, Resolv::DNS::Resource::IN::MX], ["another_domain.com", Resolv::DNS::Resource::IN::MX]])
|
|
268
|
-
end
|
|
269
|
-
end
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
context "when the cache size is greater than the max cache size" do
|
|
273
|
-
before do
|
|
274
|
-
stub_const("ValidEmail2::Dns::MAX_CACHE_SIZE", 0)
|
|
275
|
-
end
|
|
276
|
-
|
|
277
|
-
it "prunes the cache" do
|
|
278
|
-
expect(dns_instance).to receive(:prune_cache).once
|
|
279
|
-
|
|
280
|
-
email_instance.valid_strict_mx?
|
|
281
|
-
end
|
|
282
256
|
|
|
283
|
-
|
|
257
|
+
context "when the time since last lookup is greater than the cached ttl entry" do
|
|
258
|
+
it "calls the MX servers lookup again" do
|
|
259
|
+
travel(ttl)
|
|
284
260
|
email_instance.valid_strict_mx?
|
|
285
261
|
|
|
286
|
-
expect(Resolv::DNS).to have_received(:open).
|
|
287
|
-
end
|
|
288
|
-
|
|
289
|
-
context "and there are older cached entries" do
|
|
290
|
-
let(:mock_cache_data) { { ["another_domain.com", Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_mx_records, cached_at - 100, ttl) } }
|
|
291
|
-
|
|
292
|
-
it "prunes those entries" do
|
|
293
|
-
email_instance.valid_strict_mx?
|
|
294
|
-
|
|
295
|
-
expect(ValidEmail2::Dns::CACHE.keys).to match_array([[email_instance.address.domain, Resolv::DNS::Resource::IN::MX]])
|
|
296
|
-
end
|
|
262
|
+
expect(Resolv::DNS).to have_received(:open).twice
|
|
297
263
|
end
|
|
298
264
|
end
|
|
299
265
|
end
|
|
300
266
|
end
|
|
301
267
|
|
|
302
268
|
describe "#valid_mx?" do
|
|
303
|
-
let(:
|
|
304
|
-
let(:mock_cache_data) { { [email_instance.address.domain, Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_a_records, cached_at, ttl) } }
|
|
305
|
-
let(:mock_a_records) { [double("A", address: "192.168.1.1", ttl:)] }
|
|
269
|
+
let(:mock_a_records) { [with_ttl(Resolv::DNS::Resource::IN::A.new("192.168.1.1"))] }
|
|
306
270
|
|
|
307
271
|
before do
|
|
308
272
|
allow(email_instance).to receive(:mx_servers).and_return(mock_mx_records)
|
|
@@ -339,92 +303,24 @@ describe ValidEmail2::Address do
|
|
|
339
303
|
|
|
340
304
|
describe "ttl" do
|
|
341
305
|
before do
|
|
342
|
-
|
|
343
|
-
allow(ValidEmail2::Dns).to receive(:new).and_return(dns_instance)
|
|
344
|
-
allow(dns_instance).to receive(:fetch).with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX).and_call_original
|
|
306
|
+
email_instance.valid_mx?
|
|
345
307
|
end
|
|
346
308
|
|
|
347
309
|
context "when the time since last lookup is less than the cached ttl entry" do
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
it "does not call the MX or A servers lookup" do
|
|
351
|
-
email_instance.valid_mx?
|
|
352
|
-
|
|
353
|
-
expect(Resolv::DNS).not_to have_received(:open)
|
|
354
|
-
end
|
|
355
|
-
end
|
|
356
|
-
|
|
357
|
-
context "when the time since last lookup is greater than the cached ttl entry" do
|
|
358
|
-
let(:cached_at) { Time.now - ttl }
|
|
359
|
-
|
|
360
|
-
it "calls the MX or A servers lookup " do
|
|
310
|
+
it "does not call the MX or A servers lookup again" do
|
|
311
|
+
travel(ttl - 1)
|
|
361
312
|
email_instance.valid_mx?
|
|
362
313
|
|
|
363
314
|
expect(Resolv::DNS).to have_received(:open).once
|
|
364
315
|
end
|
|
365
316
|
end
|
|
366
|
-
end
|
|
367
|
-
|
|
368
|
-
describe "cache size" do
|
|
369
|
-
before do
|
|
370
|
-
stub_const("ValidEmail2::Dns::CACHE", mock_cache_data)
|
|
371
|
-
allow(ValidEmail2::Dns).to receive(:new).and_return(dns_instance)
|
|
372
|
-
allow(dns_instance).to receive(:fetch).with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX).and_call_original
|
|
373
|
-
end
|
|
374
|
-
|
|
375
|
-
context "when the cache size is less than or equal to the max cache size" do
|
|
376
|
-
before do
|
|
377
|
-
stub_const("ValidEmail2::Dns::MAX_CACHE_SIZE", 1)
|
|
378
|
-
end
|
|
379
|
-
|
|
380
|
-
it "does not prune the cache" do
|
|
381
|
-
expect(email_instance).not_to receive(:prune_cache)
|
|
382
317
|
|
|
318
|
+
context "when the time since last lookup is greater than the cached ttl entry" do
|
|
319
|
+
it "calls the MX or A servers lookup again" do
|
|
320
|
+
travel(ttl)
|
|
383
321
|
email_instance.valid_mx?
|
|
384
|
-
end
|
|
385
|
-
|
|
386
|
-
it "does not call the MX or A servers lookup" do
|
|
387
|
-
email_instance.valid_mx?
|
|
388
|
-
|
|
389
|
-
expect(Resolv::DNS).not_to have_received(:open)
|
|
390
|
-
end
|
|
391
|
-
|
|
392
|
-
context "and there are older cached entries" do
|
|
393
|
-
let(:mock_cache_data) { { ["another_domain.com", Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_a_records, cached_at - 100, ttl) } }
|
|
394
|
-
|
|
395
|
-
it "does not prune those entries" do
|
|
396
|
-
email_instance.valid_mx?
|
|
397
|
-
|
|
398
|
-
expect(ValidEmail2::Dns::CACHE.keys).to match_array([[email_instance.address.domain, Resolv::DNS::Resource::IN::MX], ["another_domain.com", Resolv::DNS::Resource::IN::MX]])
|
|
399
|
-
end
|
|
400
|
-
end
|
|
401
|
-
end
|
|
402
|
-
|
|
403
|
-
context "when the cache size is greater than the max cache size" do
|
|
404
|
-
before do
|
|
405
|
-
stub_const("ValidEmail2::Dns::MAX_CACHE_SIZE", 0)
|
|
406
|
-
end
|
|
407
|
-
|
|
408
|
-
it "prunes the cache" do
|
|
409
|
-
expect(dns_instance).to receive(:prune_cache).once
|
|
410
|
-
|
|
411
|
-
email_instance.valid_mx?
|
|
412
|
-
end
|
|
413
|
-
|
|
414
|
-
it "calls the MX or A servers lookup" do
|
|
415
|
-
email_instance.valid_mx?
|
|
416
|
-
|
|
417
|
-
expect(Resolv::DNS).to have_received(:open).once
|
|
418
|
-
end
|
|
419
|
-
|
|
420
|
-
context "and there are older cached entries" do
|
|
421
|
-
let(:mock_cache_data) { { ["another_domain.com", Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_a_records, cached_at - 100, ttl) } }
|
|
422
|
-
|
|
423
|
-
it "prunes those entries" do
|
|
424
|
-
email_instance.valid_mx?
|
|
425
322
|
|
|
426
|
-
|
|
427
|
-
end
|
|
323
|
+
expect(Resolv::DNS).to have_received(:open).twice
|
|
428
324
|
end
|
|
429
325
|
end
|
|
430
326
|
end
|
data/spec/benchmark_spec.rb
CHANGED
|
@@ -16,6 +16,16 @@ describe "Performance testing" do
|
|
|
16
16
|
expect { address.disposable_domain? }.to perform_under(0.0001).sec.sample(10).times
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
+
it "disposable_domain? has acceptable lookup performance with the sorted_list lookup" do
|
|
20
|
+
address = ValidEmail2::Address.new("test@example.com")
|
|
21
|
+
ValidEmail2.disposable_domains_lookup = :sorted_list
|
|
22
|
+
ValidEmail2.disposable_domains
|
|
23
|
+
|
|
24
|
+
expect { address.disposable_domain? }.to perform_under(0.0001).sec.sample(10).times
|
|
25
|
+
ensure
|
|
26
|
+
ValidEmail2.disposable_domains_lookup = :set
|
|
27
|
+
end
|
|
28
|
+
|
|
19
29
|
it "disposable_mx_server? has acceptable lookup performance" do
|
|
20
30
|
address = ValidEmail2::Address.new("test@gmail.com")
|
|
21
31
|
|
|
@@ -24,6 +34,6 @@ describe "Performance testing" do
|
|
|
24
34
|
expect(ValidEmail2.disposable_emails.count).to be > 30000
|
|
25
35
|
|
|
26
36
|
# check lookup timing
|
|
27
|
-
expect { address.send(:disposable_mx_server?) }.to perform_under(0.
|
|
37
|
+
expect { address.send(:disposable_mx_server?) }.to perform_under(0.00025).sec.sample(10).times
|
|
28
38
|
end
|
|
29
39
|
end
|
data/spec/dns_spec.rb
CHANGED
|
@@ -18,4 +18,48 @@ describe ValidEmail2::Dns do
|
|
|
18
18
|
expect(records.size).to_not be_zero
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
|
+
|
|
22
|
+
describe "cache" do
|
|
23
|
+
let(:resolv_dns) { instance_double(Resolv::DNS, :timeouts= => nil) }
|
|
24
|
+
let(:mx_record) do
|
|
25
|
+
record = Resolv::DNS::Resource::IN::MX.new(10, Resolv::DNS::Name.create("mx.example.com"))
|
|
26
|
+
record.instance_variable_set(:@ttl, 1_000)
|
|
27
|
+
record
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
before do
|
|
31
|
+
allow(Resolv::DNS).to receive(:open).and_yield(resolv_dns)
|
|
32
|
+
allow(resolv_dns).to receive(:getresources).and_return([mx_record])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
after do
|
|
36
|
+
described_class.cache_size = described_class::DEFAULT_CACHE_SIZE_BYTES
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "keeps lookups that fit into the cache" do
|
|
40
|
+
described_class.cache_size = 1024
|
|
41
|
+
|
|
42
|
+
2.times { described_class.new.mx_servers("example.com") }
|
|
43
|
+
|
|
44
|
+
expect(Resolv::DNS).to have_received(:open).once
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "evicts lookups once the cache grows past the configured size" do
|
|
48
|
+
described_class.cache_size = 1
|
|
49
|
+
|
|
50
|
+
2.times { described_class.new.mx_servers("example.com") }
|
|
51
|
+
|
|
52
|
+
expect(Resolv::DNS).to have_received(:open).twice
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "looks up records that can't be cached, such as test doubles, every time" do
|
|
56
|
+
mx_double = double("MX", exchange: "mx.example.com", preference: 10, ttl: 1_000)
|
|
57
|
+
allow(resolv_dns).to receive(:getresources).and_return([mx_double])
|
|
58
|
+
|
|
59
|
+
results = Array.new(2) { described_class.new.mx_servers("example.com") }
|
|
60
|
+
|
|
61
|
+
expect(results).to eq([[mx_double], [mx_double]])
|
|
62
|
+
expect(Resolv::DNS).to have_received(:open).twice
|
|
63
|
+
end
|
|
64
|
+
end
|
|
21
65
|
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
describe ValidEmail2::SortedDomainList do
|
|
6
|
+
let(:domains) { File.readlines(ValidEmail2::DISPOSABLE_FILE, chomp: true) }
|
|
7
|
+
let(:list) { described_class.new(ValidEmail2::DISPOSABLE_FILE) }
|
|
8
|
+
|
|
9
|
+
# The bisection relies on it; pull_mailchecker_emails.rb writes it so.
|
|
10
|
+
it "searches a sorted file without duplicates" do
|
|
11
|
+
expect(domains.each_cons(2).all? { |a, b| a.b < b.b }).to be(true)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "finds no domain missing from the file" do
|
|
15
|
+
expect(%w[gmail.com example.com 0 zzzzzzzzzz.zz].select { |domain| list.include?(domain) }).to be_empty
|
|
16
|
+
end
|
|
17
|
+
end
|
data/spec/valid_email2_spec.rb
CHANGED
|
@@ -297,6 +297,14 @@ describe ValidEmail2 do
|
|
|
297
297
|
expect(user.valid?).to be_truthy
|
|
298
298
|
end
|
|
299
299
|
|
|
300
|
+
it "does not look up MX servers if the domain is in the allow list" do
|
|
301
|
+
File.open(allow_list_file_path, "w") { |f| f.write ["gmail.com"].to_yaml }
|
|
302
|
+
set_allow_list
|
|
303
|
+
expect_any_instance_of(ValidEmail2::Dns).not_to receive(:mx_servers)
|
|
304
|
+
user = TestUserDisallowDisposableWithAllowList.new(email: "foo@gmail.com")
|
|
305
|
+
expect(user.valid?).to be_truthy
|
|
306
|
+
end
|
|
307
|
+
|
|
300
308
|
it "is invalid if the domain is a disposable_domain and not in the allow list" do
|
|
301
309
|
user = TestUserDisallowDisposableDomainWithAllowList.new(email: "foo@#{allow_list_domain}")
|
|
302
310
|
expect(user.valid?).to be_falsey
|
|
@@ -311,6 +319,41 @@ describe ValidEmail2 do
|
|
|
311
319
|
end
|
|
312
320
|
end
|
|
313
321
|
|
|
322
|
+
describe ".disposable_domains" do
|
|
323
|
+
it "uses the Set of disposable_emails by default" do
|
|
324
|
+
expect(ValidEmail2.disposable_domains).to be(ValidEmail2.disposable_emails)
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
it "rejects an unknown lookup" do
|
|
328
|
+
expect { ValidEmail2.disposable_domains_lookup = :array }.to raise_error(ArgumentError, /got array/)
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
context "with the sorted_list lookup" do
|
|
332
|
+
around do |example|
|
|
333
|
+
loaded = ValidEmail2.instance_variable_get(:@disposable_emails)
|
|
334
|
+
ValidEmail2.instance_variable_set(:@disposable_emails, nil)
|
|
335
|
+
ValidEmail2.disposable_domains_lookup = :sorted_list
|
|
336
|
+
example.run
|
|
337
|
+
ensure
|
|
338
|
+
ValidEmail2.disposable_domains_lookup = :set
|
|
339
|
+
ValidEmail2.instance_variable_set(:@disposable_emails, loaded)
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
it "checks a disposable domain without loading the Set of disposable_emails" do
|
|
343
|
+
expect(ValidEmail2::Address.new("foo@mailinator.com").disposable_domain?).to be(true)
|
|
344
|
+
expect(ValidEmail2.instance_variable_get(:@disposable_emails)).to be_nil
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
it "has all entries from the other list" do
|
|
348
|
+
disposable_domains = ValidEmail2.disposable_domains
|
|
349
|
+
disposable_emails = ValidEmail2.disposable_emails
|
|
350
|
+
expect(disposable_domains).to be_a(ValidEmail2::SortedDomainList)
|
|
351
|
+
expect(disposable_emails).to be_a(Set)
|
|
352
|
+
expect(disposable_emails.reject { |domain| disposable_domains.include?(domain) }).to be_empty
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
end
|
|
356
|
+
|
|
314
357
|
describe "with deny list validation" do
|
|
315
358
|
it "is valid if the domain is not deny-listed" do
|
|
316
359
|
user = TestUserDisallowDenyListed.new(email: "foo@gmail.com")
|