K-and-R / email_validator

An email validator for Rails.
MIT License
644 stars 63 forks source link

ACE representation of email address with IDN considered invalid #87

Closed manuelmeurer closed 1 year ago

manuelmeurer commented 1 year ago

When I enter an email with an IDN like "test@umläut.com" in a form in my Rails app and submit it, the backend receives the ACE representation "test@xn--umlut-ira.com", which is considered invalid by email_validator in strict mode:

EmailValidator.default_options[:mode] = :strict

> EmailValidator.valid?("test@xn--umlut-ira.com")
false

> EmailValidator.valid?("test@umläut.com")
true

Is this a bug?

karlwilbur commented 1 year ago

Yeah, I see what you are saying. International Domain Names (IDN), when ASCII-Compatible Encoded (ACE), are getting double dashes in them. I think that is why they are getting rejected in strict mode.

karlwilbur commented 1 year ago

https://github.com/K-and-R/email_validator/blob/develop/lib/email_validator.rb#L126-L128

I expected GitHub this to include the lines of code (below) just by adding in the link to them (above). It did not, so here they are:

    def label_contains_no_more_than_one_consecutive_hyphen
      '(?!.*?--.*$)'
    end
karlwilbur commented 1 year ago

So, this behavior is very much intentional, but may be a mistake. Let me look at the RFC again.

karlwilbur commented 1 year ago

So, I am thinking that rejecting consecutive hyphens was my mistake. I cannot see where they are not allowed in domain names.

I do see that India's Domain Registration Service does say that:

Two hyphens together is usually not permitted and also hyphens cannot appear at both third and fourth positions.

In fact, RFC 3696 clearly indicates that double hyphens preceded by "xn" should be expected. (https://www.rfc-editor.org/rfc/rfc3696#section-5)

So, definitely a bug.

Thank you for bringing it to my attention.

karlwilbur commented 1 year ago

@manuelmeurer Fixed in version 2.2.4

manuelmeurer commented 1 year ago

Brilliant, thanks so much for the quick fix! ❤️