1
0
Fork 0
mirror of https://github.com/mastodon/mastodon.git synced 2024-08-20 21:08:15 -07:00
This commit is contained in:
Fawaz Farid 2024-07-31 14:07:08 +00:00 committed by GitHub
commit b57249a641
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 87 additions and 15 deletions

View file

@ -98,7 +98,7 @@ class User < ApplicationRecord
accepts_nested_attributes_for :invite_request, reject_if: ->(attributes) { attributes['text'].blank? && !Setting.require_invite_text }
validates :invite_request, presence: true, on: :create, if: :invite_text_required?
validates :email, presence: true, email_address: true
validates :email, presence: true, email_address: true, disposable_email: true
validates_with BlacklistedEmailValidator, if: -> { ENV['EMAIL_DOMAIN_LISTS_APPLY_AFTER_CONFIRMATION'] == 'true' || !confirmed? }
validates_with EmailMxValidator, if: :validate_email_dns?

View file

@ -0,0 +1,36 @@
# frozen_string_literal: true
class DisposableEmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.blank?
return unless disposable_email?(value)
record.errors.add(
attribute,
(options[:message] || I18n.t('disposable_email_validator.invalid'))
)
end
private
def disposable_email?(email)
email_address = begin
Mail::Address.new(email.downcase)
rescue
nil
end
return false unless email_address
disposable_email_domains.include?(email_address.domain)
end
def disposable_email_domains
file_path = Rails.root.join('data', 'disposable_email_domains.txt')
return [] unless File.exist? file_path
data = File.read(file_path)
JSON.parse(data)
end
end

View file

@ -1174,6 +1174,8 @@ en-GB:
more_details_html: For more details, see the <a href="%{terms_path}">privacy policy</a>.
username_available: Your username will become available again
username_unavailable: Your username will remain unavailable
disposable_email_validator:
invalid: emails with this domain are not allowed
disputes:
strikes:
action_taken: Action taken

View file

@ -1190,6 +1190,8 @@ en:
more_details_html: For more details, see the <a href="%{terms_path}">privacy policy</a>.
username_available: Your username will become available again
username_unavailable: Your username will remain unavailable
disposable_email_validator:
invalid: emails with this domain are not allowed
disputes:
strikes:
action_taken: Action taken

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
namespace :disposable_email_domains do
desc 'Download latest list of disposable email domains'
task download: :environment do
data = HTTP.get('https://disposable.github.io/disposable-email-domains/domains.json').to_s
dir = Rails.root.join('data')
FileUtils.mkdir_p(dir)
File.write("#{dir}/disposable_email_domains.txt", data, mode: 'w')
end
end

View file

@ -246,8 +246,8 @@ describe Mastodon::CLI::Accounts do
end
context 'with --email option' do
let(:user) { Fabricate(:user, email: 'old_email@email.com') }
let(:options) { { email: 'new_email@email.com' } }
let(:user) { Fabricate(:user, email: 'old_email@example.com') }
let(:options) { { email: 'new_email@example.com' } }
it "sets the user's unconfirmed email to the provided email address" do
expect { subject }
@ -260,12 +260,12 @@ describe Mastodon::CLI::Accounts do
expect { subject }
.to output_results('OK')
expect(user.reload.email).to eq('old_email@email.com')
expect(user.reload.email).to eq('old_email@example.com')
end
context 'with --confirm option' do
let(:user) { Fabricate(:user, email: 'old_email@email.com', confirmed_at: nil) }
let(:options) { { email: 'new_email@email.com', confirm: true } }
let(:user) { Fabricate(:user, email: 'old_email@example.com', confirmed_at: nil) }
let(:options) { { email: 'new_email@example.com', confirm: true } }
it "updates the user's email address to the provided email" do
expect { subject }

View file

@ -6,7 +6,7 @@ RSpec.describe AppSignUpService do
subject { described_class.new }
let(:app) { Fabricate(:application, scopes: 'read write') }
let(:good_params) { { username: 'alice', password: '12345678', email: 'good@email.com', agreement: true } }
let(:good_params) { { username: 'alice', password: '12345678', email: 'good@example.com', agreement: true } }
let(:remote_ip) { IPAddr.new('198.0.2.1') }
describe '#call' do
@ -30,7 +30,7 @@ RSpec.describe AppSignUpService do
context 'when the email address requires approval' do
before do
Setting.registrations_mode = 'open'
Fabricate(:email_domain_block, allow_with_approval: true, domain: 'email.com')
Fabricate(:email_domain_block, allow_with_approval: true, domain: 'example.com')
end
it 'creates an unapproved user', :aggregate_failures do
@ -51,18 +51,18 @@ RSpec.describe AppSignUpService do
context 'when the email address requires approval through MX records' do
before do
Setting.registrations_mode = 'open'
Fabricate(:email_domain_block, allow_with_approval: true, domain: 'smtp.email.com')
Fabricate(:email_domain_block, allow_with_approval: true, domain: 'smtp.example.com')
allow(User).to receive(:skip_mx_check?).and_return(false)
resolver = instance_double(Resolv::DNS, :timeouts= => nil)
allow(resolver).to receive(:getresources)
.with('email.com', Resolv::DNS::Resource::IN::MX)
.and_return([instance_double(Resolv::DNS::Resource::MX, exchange: 'smtp.email.com')])
allow(resolver).to receive(:getresources).with('email.com', Resolv::DNS::Resource::IN::A).and_return([])
allow(resolver).to receive(:getresources).with('email.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
allow(resolver).to receive(:getresources).with('smtp.email.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('smtp.email.com', Resolv::DNS::Resource::IN::AAAA).and_return([instance_double(Resolv::DNS::Resource::IN::AAAA, address: 'fd00::2')])
.with('example.com', Resolv::DNS::Resource::IN::MX)
.and_return([instance_double(Resolv::DNS::Resource::MX, exchange: 'smtp.example.com')])
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('smtp.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('smtp.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([instance_double(Resolv::DNS::Resource::IN::AAAA, address: 'fd00::2')])
allow(Resolv::DNS).to receive(:open).and_yield(resolver)
end

View file

@ -0,0 +1,18 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe DisposableEmailValidator do
describe '#validate' do
it 'does not allow disposable email domains' do
user = Fabricate.build(:user, email: 'user@1994gmail.com')
expect(user).to_not be_valid
expect(user.errors.first.type).to eq I18n.t('disposable_email_validator.invalid')
end
it 'allows valid email domain' do
user = Fabricate.build(:user, email: 'user@gmail.com')
expect(user).to be_valid
end
end
end