1
0
mirror of https://github.com/mastodon/mastodon synced 2024-11-30 15:58:14 +09:00

Add DomainResource class to wrap MX lookup/normalize (#32864)

This commit is contained in:
Matt Jankowski 2024-11-14 08:47:29 -05:00 committed by GitHub
parent e8b6607ece
commit 62d65504f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 46 additions and 19 deletions

View File

@ -58,10 +58,7 @@ module Admin
private
def set_resolved_records
Resolv::DNS.open do |dns|
dns.timeouts = 5
@resolved_records = dns.getresources(@email_domain_block.domain, Resolv::DNS::Resource::IN::MX).to_a
end
@resolved_records = DomainResource.new(@email_domain_block.domain).mx
end
def resource_params

View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
class DomainResource
attr_reader :domain
RESOLVE_TIMEOUT = 5
def initialize(domain)
@domain = domain
end
def mx
Resolv::DNS.open do |dns|
dns.timeouts = RESOLVE_TIMEOUT
dns
.getresources(domain, Resolv::DNS::Resource::IN::MX)
.to_a
.map { |mx| mx.exchange.to_s }
.compact_blank
end
end
end

View File

@ -457,13 +457,7 @@ class User < ApplicationRecord
# Doing this conditionally is not very satisfying, but this is consistent
# with the MX records validations we do and keeps the specs tractable.
unless self.class.skip_mx_check?
Resolv::DNS.open do |dns|
dns.timeouts = 5
records = dns.getresources(domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }.compact_blank
end
end
records = DomainResource.new(domain).mx unless self.class.skip_mx_check?
EmailDomainBlock.requires_approval?(records + [domain], attempt_ip: sign_up_ip)
end

View File

@ -30,12 +30,12 @@
%label.batch-table__row__select.batch-table__row__select--aligned.batch-checkbox
= f.input_field :other_domains,
as: :boolean,
checked_value: record.exchange.to_s,
checked_value: record,
include_hidden: false,
multiple: true
.batch-table__row__content.pending-account
.pending-account__header
%samp= record.exchange.to_s
%samp= record
%br
= t('admin.email_domain_blocks.dns.types.mx')

View File

@ -45,12 +45,7 @@ module Mastodon::CLI
end
other_domains = []
if options[:with_dns_records]
Resolv::DNS.open do |dns|
dns.timeouts = 5
other_domains = dns.getresources(domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }.compact_blank
end
end
other_domains = DomainResource.new(domain).mx if options[:with_dns_records]
email_domain_block = EmailDomainBlock.new(domain: domain, other_domains: other_domains)
email_domain_block.save!

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe DomainResource do
describe '#mx' do
subject { described_class.new(domain) }
let(:domain) { 'example.host' }
let(:exchange) { 'mx.host' }
before { configure_mx(domain: domain, exchange: exchange) }
it 'returns array of hostnames' do
expect(subject.mx)
.to eq([exchange])
end
end
end