Skip to content

Commit

Permalink
Split domain parts before punycode conversion
Browse files Browse the repository at this point in the history
SimpleIDN.to_ascii swallows the initial period, turning ".example.com" into "example.com"

Fixes #109
  • Loading branch information
alexdunae committed Mar 12, 2024
1 parent b3e4ace commit 425251e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/validates_email_format_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions spec/validates_email_format_of_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def self.model_name
"\n[email protected]",
" [email protected]",
"[email protected] ",
"([email protected]"
"([email protected]",
"[email protected]" # 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") }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 425251e

Please sign in to comment.