News for hackers. Stuff that matters.
Re:Stop Validating Email Addresses with Regex (2012) by dchest on Wednesday April 20, 2022 @01:23AM (#31093128)
RFCs for email addresses are cool, but on the web we have our own standards!

https://html.spec.whatwg.org/multipage/input.html#valid-e-ma...

"This requirement is a willful violation of RFC 5322, which defines a syntax for email addresses that is simultaneously too strict (before the "@" character), too vague (after the "@" character), and too lax (allowing comments, whitespace characters, and quoted strings in manners unfamiliar to most users) to be of practical use here."

The regex is:

  /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

Every browser implements this regex for <input type="email">.

Chromium:

https://source.chromium.org/chromium/chromium/src/+/main:thi...

WebKit:

https://github.com/WebKit/WebKit/blob/0d7afc5a45c140c44497a8...

Yes, do verify email addresses by sending a confirmation link if you bind users to their email addresses, though. Don't confuse validation with verification.

Re:Stop Validating Email Addresses with Regex (2012) by hathawsh on Wednesday April 20, 2022 @01:37AM (#31093235)
I tried using that expression for a while, but then a user with a valid email address containing upper unicode characters showed up. I switched to a simpler expression:

    ^[^@\s\x00-\x1f]+@[^@\s\x00-\x1f.]+(:?\.[^@\s\x00-\x1f.]+)*$
It requires exactly one "@", disallows whitespace and control characters, prevents repeated dots in the domain name, and ensures the domain doesn't end with a dot. It catches a few typos and I think it allows every real email address I've heard of.
Re:Stop Validating Email Addresses with Regex (2012) by easrng on Wednesday April 20, 2022 @01:43AM (#31093273)
Domains ending with a dot are valid though, and it's needed sometimes. For example, someone@ai. (ai. is a TLD) is a different email than someone@ai (ai is a local hostname)
Re:Stop Validating Email Addresses with Regex (2012) by Biganon on Wednesday April 20, 2022 @08:00AM (#31095637)
I'm pretty sure the one guy behind ai. doesn't justify that you change your regex for him. Let's not be overly pedantic.
Re:Stop Validating Email Addresses with Regex (2012) by Beltalowda on Wednesday April 20, 2022 @06:15AM (#31094779)
> it's needed sometimes

When is this needed in the context of a Rails app? You never want to send emails to local hosts, and emails directly on the TLD is possible, but very little will work with them in practice.

Re:Stop Validating Email Addresses with Regex (2012) by turminal on Wednesday April 20, 2022 @08:02AM (#31095646)
> You never want to send emails to local hosts

That's not entirely true, it depends on how your mailing infrastructure is set up.

Re:Stop Validating Email Addresses with Regex (2012) by Beltalowda on Wednesday April 20, 2022 @08:32AM (#31095912)
But in the context of a web app though? That's the key part: you're taking in email addresses from myapp.example.com. For other cases: sure, local email addresses can be useful (not often these days, but sometimes still are). But in this context I'm not really seeing any use case.
Re:Stop Validating Email Addresses with Regex (2012) by bbatha on Wednesday April 20, 2022 @09:57AM (#31097015)
At previous $work, I wrote a rails app that managed releases. On the app you could sign up for email notifications when your change was released. These went through the unix local email system. So yes there is very much a use case
Re:Stop Validating Email Addresses with Regex (2012) by lolinder on Wednesday April 20, 2022 @11:27AM (#31098326)
It's still a bit silly to tell every SaaS developer that they shouldn't validate that they have a normal-looking domain because some people's use cases call for sending emails to local addresses or to TLDs directly. If a developer needs to handle those cases, they know already, and they're in a vanishingly small minority.
Re:Stop Validating Email Addresses with Regex (2012) by hathawsh on Wednesday April 20, 2022 @01:44AM (#31093287)
Good to know. I guess I'll simplify again. Besides, I wonder whether the expression is vulnerable to catastrophic backtracking.
Re:Stop Validating Email Addresses with Regex (2012) by Supermancho on Thursday April 21, 2022 @12:35PM (#31111671)
> Domains ending with a dot are valid though,

That doesn't mean you have to support their use. I have never seen a company support an email ending in a period and the world seems to continue along.

Re:Stop Validating Email Addresses with Regex (2012) by campital on Wednesday April 20, 2022 @11:05AM (#31098003)
Re:Stop Validating Email Addresses with Regex (2012) by daurnimator on Wednesday April 20, 2022 @08:15AM (#31095759)
Re:Stop Validating Email Addresses with Regex (2012) by qwerty456127 on Wednesday April 20, 2022 @09:30AM (#31096642)
> containing upper unicode characters

Are e-mail addresses case-sensitive? I would always lowercase&trim a string meant to represent an e-mail address (or a domain name) before doing anything else with it.

Re:Stop Validating Email Addresses with Regex (2012) by electroly on Wednesday April 20, 2022 @09:45AM (#31096863)
The local-part before the @ can be case-sensitive, yes. I've never heard of a mail server that treats it as such, but the RFC says it's case sensitive.
Re:Stop Validating Email Addresses with Regex (2012) by hathawsh on Wednesday April 20, 2022 @02:53PM (#31101306)
To be clear, by "upper" unicode characters I mean non-ASCII international characters, not uppercase characters. For example, 我買@屋企.香港 is now a valid email address. [1]

1. https://en.wikipedia.org/wiki/Email_address#Internationaliza...

Re:Stop Validating Email Addresses with Regex (2012) by jeroenhd on Wednesday April 20, 2022 @02:03PM (#31100712)
Not all uppercase characters have just one lowercase representation, and not all lowercase characters have an uppercase character. If you try to make the addresses case insensitive, you're bound to run into unnecessary complexities at some point.

Email addresses aren't case insensitive in nature, but many commercial mail services (and misguided developers) assume they are.

Re:Stop Validating Email Addresses with Regex (2012) by bobbylarrybobby on Wednesday April 20, 2022 @12:23PM (#31099301)
I'm pretty sure email addresses are allowed to have multiple @'s. I believe everything after the first is (supposed to be) considered part of the domain.
Re:Stop Validating Email Addresses with Regex (2012) by chrismorgan on Wednesday April 20, 2022 @01:12PM (#31100012)
Domain labels cannot contain @. If you support comments or quoted strings (e.g. (@)."@"@example) then you can get multiple at signs in an address, but in practice their only use these days is attacks where you confuse a badly-written server, which is part of why the web actively decided to disallow them.
Re:Stop Validating Email Addresses with Regex (2012) by eadmund on Wednesday April 20, 2022 @02:44PM (#31101190)
> in practice their only use these days is attacks where you confuse a badly-written server

Or, you know, if you actually want to use an at sign in the manner allowed by the RFCs. That's the point of standards: if something is allowed, then … it is allowed.

Refusing to accept behaviour permitted by the standard is just broken.

Re:Stop Validating Email Addresses with Regex (2012) by chrismorgan on Wednesday April 20, 2022 @04:30PM (#31102587)
Usage is what matters. Standards documents only have value as long as people follow them; once enough people deviate, they cease to have meaning. This is thoroughly the case with a lot of stuff pertaining to email: if you want to parse arbitrary emails, for example, the relevant MIME RFCs are altogether insufficient, because too many people have done it incorrectly de jure, often to the point where it’s no longer incorrect de facto because everyone supports the deviation. (This is a deviation from spec in the direction of permission.)

By hearsay alone, I say: comments and quoted strings in email addresses are, de facto, more or less dead. Comments especially, which were intended just as a compatibility measure for stuff from over forty years ago (when this was being developed organically, before there was any real spec). Most software whose direct business is email (that is, MUAs and MTAs) will more or less support them for historical reasons, but newer MUAs are likely not to support them in full (again especially comments), and I suspect the considerable majority of other software that deals in email addresses won’t support one or both of them. (This is a deviation from spec in the direction of restriction.)

Re:Stop Validating Email Addresses with Regex (2012) by chrismorgan on Wednesday April 20, 2022 @01:11PM (#31099998)
One important aspect of this that I often see people forgetting is that this check is designed for working with ASCII, but the domain name at least can be non-ASCII. User interfaces should remember to support IDN in domain labels and convert it to punycode before validating, and if you store A-labels (which you probably do) then convert it back to IDN form when presenting it to users.

(Alas, <input type=email> still doesn’t support non-ASCII in the local part, which isn’t supported everywhere but is, I believe, fairly widely supported now. See https://github.com/whatwg/html/issues/4562 plus https://en.wikipedia.org/wiki/Email_address_internationaliza... for a little more background on what it is.)

Re:Stop Validating Email Addresses with Regex (2012) by 411111111111111 on Thursday April 21, 2022 @05:07AM (#31107341)
I thought they've rolled back most of this because it made phishing domains indistinguishable from the real ones?

I used an-emoji.my.domain for a while until chrome changed it back to punycode

Re:Stop Validating Email Addresses with Regex (2012) by chrismorgan on Friday April 22, 2022 @02:36AM (#31118894)
I erred slightly in what I wrote there; I said IDN form for user display, but should have said IDNA form, which is the set of restrictions to mitigate those hazards. That will allow things like उदाहरण.example, while leaving emoji as xn--n3h.example since they’re not valid in IDNA. See also https://en.wikipedia.org/wiki/Internationalized_domain_name and https://en.wikipedia.org/wiki/Emoji_domain.
Re:Stop Validating Email Addresses with Regex (2012) by chrismorgan on Friday April 22, 2022 @09:07AM (#31121129)
Actually, I’ve got to add another note because my memory and experience was insufficient here: see also https://en.wikipedia.org/wiki/IDN_homograph_attack#Client-si... for descriptions of what further restrictions browsers do, with the most notable additional filter being disallowing mixing scripts. This is something that would be nice to formalise in some way, though I don’t know of any venue suitable for the task.
Re:Stop Validating Email Addresses with Regex (2012) by goto11 on Wednesday April 20, 2022 @11:54AM (#31098775)
This is the sane approach. Nobody uses the bizarre address formats with quoted strings and embedded comments and whatnot. There are included in the RFC because of backwards compatibility with legacy email systems from before SMTP.
Re:Stop Validating Email Addresses with Regex (2012) by jeroenhd on Wednesday April 20, 2022 @02:01PM (#31100667)
Quoted strings are actually used in the real world, though they're certainly not common.

Shitty front ends are definitely putting an end to that, though.

Re:Stop Validating Email Addresses with Regex (2012) by xeromal on Wednesday April 20, 2022 @02:22PM (#31100957)
I'd say that's for the better.
Re:Stop Validating Email Addresses with Regex (2012) by omegalulw on Wednesday April 20, 2022 @01:39AM (#31093252)
What is the significance of [a-zA-Z0-9-]{0,61}? Some special limit on 61 characters?
Re:Stop Validating Email Addresses with Regex (2012) by Beltalowda on Wednesday April 20, 2022 @06:14AM (#31094768)
Domain labels are limited to 63 bytes, with the [a-z..] before and after that makes 61 + 1 + 1 = 63.
Re:Stop Validating Email Addresses with Regex (2012) by gpvos on Wednesday April 20, 2022 @07:38AM (#31095422)
Oh wow, that is awful.
Re:Stop Validating Email Addresses with Regex (2012) by seanw444 on Wednesday April 20, 2022 @01:34AM (#31093216)
I like your final sentence. Very to-the-point.
Re:Stop Validating Email Addresses with Regex (2012) by boplicity on Wednesday April 20, 2022 @09:29AM (#31096636)
If you really want to take validation seriously, why not add a human element to it, and not design one line of code to try to fix everything?

One of the biggest problems with email input is typos -- and there are some very common typos that could easily be accounted for with code. For example [email protected], [email protected], foo@comcast, etc.

It should be common, when these types of typos occur, to prompt the user to fix them. Unfortunately, this is quite rare.

Re:Stop Validating Email Addresses with Regex (2012) by tough on Wednesday April 20, 2022 @09:37AM (#31096736)
Everytime I input my .me domain in one of those well intended websites I get nagged about maybe being wrong and wantint to use .de or whatever.

No, I know my email, thx, it's bein prefilled from auto-complete. Don't fucking tell my Im typing it wrong when 1. Its my email, and I'm not even typing

Re:Stop Validating Email Addresses with Regex (2012) by alasdair_ on Wednesday April 20, 2022 @02:00PM (#31100655)
Related: I used to use me@<myname>.com but gave up because Gmail uses "Me" as the name when displaying a user's own email address and it was confusing people enormously because a thread would look like:

>>>Me wrote: >>Me wrote: >Me wrote: Me wrote:

Re:Stop Validating Email Addresses with Regex (2012) by grimjack00 on Wednesday April 20, 2022 @12:09PM (#31099033)
I have the same issues with my .name domain. Most of the reason I have a gmail address as well is for the sites that don't think "[email protected]" is a real address.
Re:Stop Validating Email Addresses with Regex (2012) by xoptions on Wednesday April 20, 2022 @10:34AM (#31097552)
OP has shared a solution for typo fix in the closing remarks of the blog: https://github.com/mailcheck/mailcheck
Re:Stop Validating Email Addresses with Regex (2012) by jancsika on Wednesday April 20, 2022 @10:54AM (#31097837)
> If you really want to take validation seriously, why not add a human element to it, and not design one line of code to try to fix everything?

human: a customer telling a clerk, "yes, it's really gmial dot com," and the two sharing a laugh and trading stories about funny email addys

human element: the customer curses their phone because the backend folks never tested the edge case of your helpful button for confirming the suspicious case of "gmial.com" and just keep rejecting it. Then, after your helpful chat bot kept autocorrecting their chat input to "gmail.com" they threw their phone on the ground so hard it broke.

Don't be a human element.

Re:Stop Validating Email Addresses with Regex (2012) by 6510 on Wednesday April 20, 2022 @12:59PM (#31099867)
I have a .con in the db
Re:Stop Validating Email Addresses with Regex (2012) by throwaway787544 on Wednesday April 20, 2022 @09:38AM (#31096763)
That would imply that programmers care about users. I've never met a single programmer who cared what the user experience was like. Today they even brag about this, saying "I only want to care about my code!" I see it here on HN all the time, and I hear it in companies.
Re:Stop Validating Email Addresses with Regex (2012) by jve on Wednesday April 20, 2022 @09:48AM (#31096907)
The other thing is overengineering. Spending a week to implement email validation? Maybe, if it is REALLY important and results in lost revenue.

Otherwise I don't know what common typos are. I would have to do research on that. Then I have popular local email providers where I may come up with typos. but then again, it is only in my region - I would have to get some list of popular email providers in every country. Come up with a way to update that list after few years when new email providers come out. Or maybe develop a solution that matches "close enough" entries in my pre-defined list.

UX effort could be spent in an area which is used more often than only once. If there is a library for that, yeah, then slap it in, UX has been upgraded in no time. And again, only if revenue is on the other side of consideration, the development effort pays off.

Re:Stop Validating Email Addresses with Regex (2012) by throwaway787544 on Wednesday April 20, 2022 @03:04PM (#31101453)
Revenue focus is not customer focus. You don't get a second chance at a first impression. The difference between a great company and a mediocre one is how much they care. Growth-obsessed companies only care about revenue and the 80% case. Long lasting companies build relationships, listen closely, and feel deeply.
Re:Stop Validating Email Addresses with Regex (2012) by Tagbert on Wednesday April 20, 2022 @10:55AM (#31097845)
Perhaps you are only looking for that kind of message. I see concern for users expresses by many devs here and hear it from other devs that I talk to. I can tell you that we have extensive discussions about the user experience whenever planning a new feature or change to and existing one. If our customers are not happy, that affects their use of our product, so of course we make this a priority. Besides, we also use it and we want it to work for anyone using it.
Re:Stop Validating Email Addresses with Regex (2012) by thrwyoilarticle on Wednesday April 20, 2022 @11:02AM (#31097950)
In fact, doesn't 'RFC' imply that it isn't a standard?

A pragmatic, domain-specific spec beats that.

edit: there's more nuance than that. Some RFCs are standards. 2822 is 'Category: Standards Track', so is a standard if referred to by its STD number? I can't find what its STD number is.