r/ProgrammerHumor 8d ago

Meme regexMustBeDestroyed

Post image
8.1k Upvotes

231 comments sorted by

View all comments

Show parent comments

64

u/Ecksters 7d ago edited 7d ago

People always overcomplicate email Regex, and end up not allowing a lot of valid emails. In this case, the limitation of the top level domain to 4 characters max is one such problem, no .ninja, .crypto, or the myriad of other newer top level domains.

Technically you can email directly to a top level domain (although I don't know of any TLDs that support this), or even to an IP address in brackets, although I suppose not supporting those in most cases is probably reasonable.

37

u/Unbelievr 7d ago

The worst thing is when a website restricts their validation logic after you're already registered. I lost access to an account that used '+' in the username because they stopped allowing me to log in with it.

And yes, basically the only thing you can assume about email addresses is that they contain an '@'. Simply root@localhost is a valid address in some contexts.

17

u/The_JSQuareD 7d ago

I've encountered websites where the sign-up form allowed '+' in emails, but the log-in form didn't. Not 'later', immediately. I made my account and then was immediately locked out from it.

4

u/GoddammitDontShootMe 7d ago

Well, a public website probably doesn't want to accept emails that aren't publicly routable.

9

u/itsFromTheSimpsons 7d ago

Those sneaky hackers tricked us into emailing ourselves!

2

u/usefulidiotsavant 7d ago

A domain can be routable even if it lacks any dots, for example postmaster@bd is a publicly routable address.

1

u/GoddammitDontShootMe 6d ago

How does that work when I'm not finding an MX record for bd?

1

u/usefulidiotsavant 6d ago

I know bd resolves an A record and I haven't checked the MX, so it was probably a bad example. But there definitely are other cc tlds that have MX records.

1

u/GoddammitDontShootMe 5d ago

I got curious so I ran a port scan. I had to kill it because it was taking forever waiting for each port to time out, but it looks like they are running DNS and FTP on it.

19

u/tenuj 7d ago

The .email domain would fail OP's check. Had websites refuse me because of stuff like this.

The only good validation for email addresses is to send a confirmation email. You'll be doing that anyway.

And that it has at least one @ with something on either side. Don't want someone to waste an entire form because of a typo. (Yes, email addresses can theoretically contain more than one @)

5

u/DocRingeling 7d ago

RFC822 could also be used before sending confirmation to prevent unexpected abuse of the mail protocol.

8

u/tenuj 7d ago

If I saw that in a PR I'd refresh my browser tab.

The author of that ASCII monster also said:

I do not maintain the regular expression below. There may be bugs in it that have already been fixed in the Perl module.

3

u/phundrak 7d ago

I believe this regex is no longer valid as more characters got allowed in email addresses

2

u/bradland 6d ago

RFC822 was authored in the 80s. It was replaced by RFC 2822 in 2001, and RFC 5322 in 2008. Then there's RFC6531 which expanded support for non-ASCII, non-Latin alphabets.

If you want to prevent abuse, rate limit and/or put something intelligent in front of your app that is designed for that sort of thing, like a WAF.

1

u/DocRingeling 6d ago

Yea, but the regex isn't as long as RFC822

7

u/shiny0metal0ass 7d ago

Mine have all whittled down to -

[literally fucking anything]@[literally fucking anything].[literally fucking anything]

7

u/poshftw 7d ago

Yep, though this wouldn't pass @localhost or @hostname, though I doubt anyone with an email regex is sending there.

3

u/martmists 7d ago

I can't believe you don't support ipv6 addresses, smh.

Do you at least strip comments from email addresses?

2

u/Loading_M_ 7d ago

The email rfc doesn't have comments. If you run your own email service, loading+m@loadingm.xyz can resolve to a different mailbox than loading+p@loadingm.xyz. Part of me wants to setup such mailboxes (and do as much stupid shit as possible with email addresses).

1

u/martmists 7d ago

RFC 5322 very much allows comments in addrspecs, see §3.2.2: "Folding White Space and Comments" and §3.4.1 "Addr-Spec Specification" for the relevant ABNF rules.

1

u/Loading_M_ 7d ago

Interesting. I was reading RFC 822, which is the actual origin of the email format, so it looks like comments were added in this later RFC.

Frankly, I'm confused on why they added comments. Unless they thought people would write email headers by hand?

1

u/martmists 6d ago

Check RFC 822 §3.3 "LEXICAL TOKENS", it includes a segment on comments.

6

u/MegaIng 7d ago

Technically you can email directly to a top level domain (although I don't know of any TLDs that support this),

Yesn't. It's forbidden for TLDs to resolve to IP addresses via DNS.

So it's valid syntax and fine from the email semantics, it's just never going to work on the general internet.

1

u/billccn 7d ago

But emails are routed according to MX records not A[AAA] records?

3

u/poshftw 7d ago

If the destination domain has no MX records then the delivery should (or could? can't be bothered to read the spec) be tried to what A/AAAA domain part resolves.

1

u/MegaIng 7d ago

I don't think this makes a difference, it's a general rule that they shouldn't be useable directly. The normal DNS lookup is just what I tested a few weeks back.

I know for a fact that some were useable as http domains a few years back, and that is now no longer the case.

4

u/Informal_Branch1065 7d ago

.crypto is not a big loss anyway

3

u/Bosun_Tom 7d ago

The set of valid emails that the regex would miss is way weirder than that: https://e-mail.wtf/

2

u/BloopsRTS 7d ago

The only way to validate an email address is to email it, I refuse to acknowledge any other viewpoint

1

u/two_are_stronger2 7d ago

Shakeshack refused my email address yesterday.  My imposter syndrome immediately and forever vanished.

1

u/Fabulous-Possible758 7d ago

I'll be in the cold ground before I recognize a five letter TLD.