From 425251e16213b51ec3bd395a7d18e7928d0e1510 Mon Sep 17 00:00:00 2001 From: Alex Dunae Date: Tue, 12 Mar 2024 13:52:21 -0700 Subject: [PATCH] Split domain parts before punycode conversion SimpleIDN.to_ascii swallows the initial period, turning ".example.com" into "example.com" Fixes https://github.com/validates-email-format-of/validates_email_format_of/issues/109 --- lib/validates_email_format_of.rb | 2 +- spec/validates_email_format_of_spec.rb | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/validates_email_format_of.rb b/lib/validates_email_format_of.rb index 8f8a7b4..2e7e6f2 100644 --- a/lib/validates_email_format_of.rb +++ b/lib/validates_email_format_of.rb @@ -269,8 +269,8 @@ def self.validate_local_part_syntax(local) end def self.validate_domain_part_syntax(domain, idn: true) - domain = SimpleIDN.to_ascii(domain) if idn parts = domain.downcase.split(".", -1) + parts.map! { |part| SimpleIDN.to_ascii(part) } if idn return false if parts.length <= 1 # Only one domain part diff --git a/spec/validates_email_format_of_spec.rb b/spec/validates_email_format_of_spec.rb index 236ea81..ceda53a 100644 --- a/spec/validates_email_format_of_spec.rb +++ b/spec/validates_email_format_of_spec.rb @@ -140,7 +140,8 @@ def self.model_name "\nnewline@example.com", " spacesbefore@example.com", "spacesafter@example.com ", - "(unbalancedcomment@example.com" + "(unbalancedcomment@example.com", + "help@.example.co.uk" # TLD can not start with a period ].each do |address| describe address do it { should have_errors_on_email.because("does not appear to be a valid email address") } @@ -203,7 +204,8 @@ def self.model_name describe "when idn support is enabled" do before(:each) do - allow(SimpleIDN).to receive(:to_ascii).once.with("exämple.com").and_return("xn--exmple-cua.com") + expect(SimpleIDN).to receive(:to_ascii).with("exämple").and_return("xn--exmple-cua") + expect(SimpleIDN).to receive(:to_ascii).with("com").and_return("com") end let(:options) { {idn: true} } describe "test@exämple.com" do