From 6ad0fb5a77dcdb12d3bf61617978dcd6e6f6fb20 Mon Sep 17 00:00:00 2001 From: Claire Date: Fri, 5 Jan 2024 12:07:57 +0100 Subject: [PATCH] Fix NULL MX handling and tighten DNS resolving specs (#28607) --- app/validators/email_mx_validator.rb | 1 + .../admin/email_domain_blocks_controller_spec.rb | 10 ++++++++++ spec/rails_helper.rb | 4 ++++ spec/validators/email_mx_validator_spec.rb | 2 +- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/validators/email_mx_validator.rb b/app/validators/email_mx_validator.rb index a30a0c820d..7943778943 100644 --- a/app/validators/email_mx_validator.rb +++ b/app/validators/email_mx_validator.rb @@ -47,6 +47,7 @@ class EmailMxValidator < ActiveModel::Validator dns.timeouts = 5 records = dns.getresources(domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s } + next if records == [''] # This domain explicitly rejects emails ([domain] + records).uniq.each do |hostname| ips.concat(dns.getresources(hostname, Resolv::DNS::Resource::IN::A).to_a.map { |e| e.address.to_s }) diff --git a/spec/controllers/admin/email_domain_blocks_controller_spec.rb b/spec/controllers/admin/email_domain_blocks_controller_spec.rb index 9379fe374a..4de3ef0f62 100644 --- a/spec/controllers/admin/email_domain_blocks_controller_spec.rb +++ b/spec/controllers/admin/email_domain_blocks_controller_spec.rb @@ -35,6 +35,16 @@ RSpec.describe Admin::EmailDomainBlocksController do describe 'POST #create' do context 'when resolve button is pressed' do before do + resolver = instance_double(Resolv::DNS) + + allow(resolver).to receive(:getresources) + .with('example.com', Resolv::DNS::Resource::IN::MX) + .and_return([]) + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([]) + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([]) + allow(resolver).to receive(:timeouts=).and_return(nil) + allow(Resolv::DNS).to receive(:open).and_yield(resolver) + post :create, params: { email_domain_block: { domain: 'example.com' } } end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index c9352a4d5d..e5d432b45f 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -128,6 +128,10 @@ RSpec.configure do |config| self.use_transactional_tests = true end + config.before do |example| + allow(Resolv::DNS).to receive(:open).and_raise('Real DNS queries are disabled, stub Resolv::DNS as needed') unless example.metadata[:type] == :system + end + config.before do |example| unless example.metadata[:paperclip_processing] allow_any_instance_of(Paperclip::Attachment).to receive(:post_process).and_return(true) # rubocop:disable RSpec/AnyInstance diff --git a/spec/validators/email_mx_validator_spec.rb b/spec/validators/email_mx_validator_spec.rb index 876d73c184..21b1ad0a11 100644 --- a/spec/validators/email_mx_validator_spec.rb +++ b/spec/validators/email_mx_validator_spec.rb @@ -111,7 +111,7 @@ describe EmailMxValidator do allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([]) allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([]) allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([instance_double(Resolv::DNS::Resource::IN::A, address: '2.3.4.5')]) - allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([instance_double(Resolv::DNS::Resource::IN::A, address: 'fd00::2')]) + allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([instance_double(Resolv::DNS::Resource::IN::AAAA, address: 'fd00::2')]) allow(resolver).to receive(:timeouts=).and_return(nil) allow(Resolv::DNS).to receive(:open).and_yield(resolver)