From 82f3a6a70a6dd9da249681cc30f65ced827e17c4 Mon Sep 17 00:00:00 2001 From: Vladislav Trotsenko Date: Tue, 4 Jan 2022 16:09:18 +0200 Subject: [PATCH] Feature/Avoid redefining Ruby code builtin implementations (#194) * Updated Truemail::Validate::Smtp::Request::Session, tests * Updated gemspec * Updated reek/rubocop configs * Updated version, changelog --- .reek.yml | 1 + .rubocop.yml | 3 +++ CHANGELOG.md | 14 +++++++++++++- lib/truemail/validate/smtp/request.rb | 20 +++++++++++++++----- lib/truemail/version.rb | 2 +- spec/truemail/validate/smtp/request_spec.rb | 18 +++++++++++++++--- truemail.gemspec | 2 +- 7 files changed, 49 insertions(+), 11 deletions(-) diff --git a/.reek.yml b/.reek.yml index 7149caf..75d6ef9 100644 --- a/.reek.yml +++ b/.reek.yml @@ -14,6 +14,7 @@ detectors: - Truemail::Validate::Mx#hosts_from_cname_records - Truemail::Configuration#logger= - Truemail::Validate::Smtp::Request#initialize + - Truemail::Validate::Smtp::Request::Session#initialize TooManyInstanceVariables: exclude: diff --git a/.rubocop.yml b/.rubocop.yml index 4633df7..64d98fc 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -374,6 +374,9 @@ Gemspec/DateAssignment: Gemspec/RequireMFA: Enabled: false +Gemspec/RubyVersionGlobalsUsage: + Enabled: false + Security/IoMethods: Enabled: true diff --git a/CHANGELOG.md b/CHANGELOG.md index c7d6348..e74e346 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.6.1] - 2022.01.04 + +### Fixed + +- Fixed redefining builtin implementations caused using stdlib as external dependencies. Thanks [@allard](https://github.com/allard) for report. + +### Updated + +- Updated `Truemail::Validate::Smtp::Request::Session#initialize`, `Truemail::Validate::Smtp::Request::Session#start`, tests +- Updated rubocop/reek configs +- Updated gem docs, version + ## [2.6.0] - 2021.12.28 ### Added @@ -33,7 +45,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Fixed -- Ruby 3.0 stdlib SMTP client SSL certificate verification issues for cases when IP address uses as MX host. Thanks [@esb](https://github.com/esb) for bug report. +- Fixed Ruby 3.0 stdlib SMTP client SSL certificate verification issues for cases when IP address uses as MX host. Thanks [@esb](https://github.com/esb) for bug report. ### Updated diff --git a/lib/truemail/validate/smtp/request.rb b/lib/truemail/validate/smtp/request.rb index 20dc7b0..ad71ce6 100644 --- a/lib/truemail/validate/smtp/request.rb +++ b/lib/truemail/validate/smtp/request.rb @@ -55,24 +55,34 @@ def initialize(configuration) class Session require 'net/smtp' - def initialize(host, port, connection_timeout, response_timeout) - @net_smtp = (old_net_smtp? ? ::Net::SMTP.new(host, port) : ::Net::SMTP.new(host, port, tls_verify: false)).tap do |settings| + UNDEFINED_VERSION = '0.0.0' + + def initialize(host, port, connection_timeout, response_timeout, net_class = ::Net::SMTP) + @net_class = net_class + @net_smtp_version = resolve_net_smtp_version + @net_smtp = (old_net_smtp? ? net_class.new(host, port) : net_class.new(host, port, tls_verify: false)).tap do |settings| settings.open_timeout = connection_timeout settings.read_timeout = response_timeout end end def start(helo_domain, &block) - return net_smtp.start(helo_domain, &block) if old_net_smtp? + return net_smtp.start(helo_domain, &block) if net_smtp_version < '0.2.0' + return net_smtp.start(helo_domain, tls_verify: false, &block) if old_net_smtp? net_smtp.start(helo: helo_domain, &block) end private - attr_reader :net_smtp + attr_reader :net_class, :net_smtp_version, :net_smtp + + def resolve_net_smtp_version + return net_class::VERSION if net_class.const_defined?(:VERSION) + Truemail::Validate::Smtp::Request::Session::UNDEFINED_VERSION + end def old_net_smtp? - !::Net::SMTP.const_defined?(:VERSION) || ::Net::SMTP::VERSION < '0.3.0' + net_smtp_version < '0.3.0' end end diff --git a/lib/truemail/version.rb b/lib/truemail/version.rb index 7ea1533..7f91383 100644 --- a/lib/truemail/version.rb +++ b/lib/truemail/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Truemail - VERSION = '2.6.0' + VERSION = '2.6.1' end diff --git a/spec/truemail/validate/smtp/request_spec.rb b/spec/truemail/validate/smtp/request_spec.rb index 382bbdc..e9761f8 100644 --- a/spec/truemail/validate/smtp/request_spec.rb +++ b/spec/truemail/validate/smtp/request_spec.rb @@ -364,7 +364,7 @@ subject(:session_start) { request_session_instance.start(helo_domain, &session_actions) } let(:helo_domain) { random_domain_name } - let(:session_actions) { ->(_) {} } + let(:session_actions) { proc {} } context 'when out of the box Net::SMTP version' do before do @@ -380,9 +380,9 @@ end end - context 'when Net::SMTP version < 0.3.0' do + context 'when Net::SMTP version in range 0.1.0...0.2.0' do before do - stub_const('Net::SMTP::VERSION', '0.2.128506') + stub_const('Net::SMTP::VERSION', '0.1.314') allow(::Net::SMTP).to receive(:new).with(host, port).and_return(net_smtp_instance) end @@ -392,6 +392,18 @@ end end + context 'when Net::SMTP version in range 0.2.0...0.3.0' do + before do + stub_const('Net::SMTP::VERSION', '0.2.128506') + allow(::Net::SMTP).to receive(:new).with(host, port).and_return(net_smtp_instance) + end + + it 'passes helo domain as position argument' do + expect(net_smtp_instance).to receive(:start).with(helo_domain, tls_verify: false, &session_actions) + session_start + end + end + context 'when Net::SMTP version >= 0.3.0' do before do stub_const('Net::SMTP::VERSION', '0.3.0') diff --git a/truemail.gemspec b/truemail.gemspec index ca74e13..b06dc94 100644 --- a/truemail.gemspec +++ b/truemail.gemspec @@ -31,7 +31,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| ::File.basename(f) } spec.require_paths = ['lib'] - spec.add_runtime_dependency 'net-smtp', '~> 0.3' + spec.add_runtime_dependency 'net-smtp', '~> 0.3' if ::RUBY_VERSION >= '3.1.0' spec.add_runtime_dependency 'simpleidn', '~> 0.2.1' spec.add_development_dependency 'bundler-audit', '~> 0.9.0.1'