Skip to content

Commit

Permalink
fix!: revert allowing scandinavian characters by making allowed multi…
Browse files Browse the repository at this point in the history
…byte characters configurable (#261)
  • Loading branch information
sasata299 authored and micke committed Nov 19, 2024
1 parent 1805945 commit cf6a1e9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ address.valid_strict_mx? => true
address.subaddressed? => false
```

If you want to allow multibyte characters, set it explicitly.

```ruby
ValidEmail2::Address.permitted_multibyte_characters_regex = /[ÆæØøÅåÄäÖöÞþÐð]/
```

### Test environment

If you are validating `mx` then your specs will fail without an internet connection.
Expand Down
17 changes: 13 additions & 4 deletions lib/valid_email2/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require "valid_email2"
require "resolv"
require "mail"
require "unicode/emoji"
require "valid_email2/dns_records_cache"

module ValidEmail2
Expand All @@ -22,6 +21,14 @@ def self.prohibited_domain_characters_regex=(val)
@prohibited_domain_characters_regex = val
end

def self.permitted_multibyte_characters_regex
@permitted_multibyte_characters_regex
end

def self.permitted_multibyte_characters_regex=(val)
@permitted_multibyte_characters_regex = val
end

def initialize(address, dns_timeout = 5, dns_nameserver = nil)
@parse_error = false
@raw_address = address
Expand All @@ -34,7 +41,7 @@ def initialize(address, dns_timeout = 5, dns_nameserver = nil)
@parse_error = true
end

@parse_error ||= address_contain_emoticons?
@parse_error ||= address_contain_multibyte_characters?
end

def valid?
Expand Down Expand Up @@ -130,10 +137,12 @@ def mx_server_is_in?(domain_list)
}
end

def address_contain_emoticons?
def address_contain_multibyte_characters?
return false if @raw_address.nil?

@raw_address.scan(Unicode::Emoji::REGEX).length >= 1
return false if @raw_address.ascii_only?

@raw_address.each_char.any? { |char| char.bytesize > 1 && char !~ self.class.permitted_multibyte_characters_regex }
end

def resolv_config
Expand Down
20 changes: 18 additions & 2 deletions spec/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,25 @@
expect(address.valid?).to be false
end

it "is valid if it contains special scandinavian characters" do
it "is invalid if it contains Japanese characters" do
address = described_class.new("あいうえお@example.com")
expect(address.valid?).to be false
end

it "is invalid if it contains special scandinavian characters" do
address = described_class.new("jørgen@email.dk")
expect(address.valid?).to eq true
expect(address.valid?).to eq false
end

context "permitted_multibyte_characters_regex is set" do
before do
described_class.permitted_multibyte_characters_regex = /[ÆæØøÅåÄäÖöÞþÐð]/
end

it "is valid if it contains special scandinavian characters" do
address = described_class.new("jørgen@email.dk")
expect(address.valid?).to eq true
end
end
end

Expand Down
1 change: 0 additions & 1 deletion valid_email2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "pry"
spec.add_runtime_dependency "mail", "~> 2.5"
spec.add_runtime_dependency "activemodel", ">= 6.0"
spec.add_runtime_dependency "unicode-emoji", "~> 3.7.0"
end

0 comments on commit cf6a1e9

Please sign in to comment.