jackbearheart / email-addresses

An RFC 5322 email address parser
MIT License
249 stars 36 forks source link

Line feed in address string will return null #36

Open lnedry opened 6 years ago

lnedry commented 6 years ago

Lately I've been seeing emails that have a line feed separating the user name and email address. This causes email-addresses to return null, even though the email address is valid.

$ node
> var addrs = require("email-addresses")
> addrs.parseOneAddress('"Jack Bowman"\n<jack@fogcreek.com>')
parseString = "Jack Bowman"
<jack@fogcreek.com>
parsed = null
null
jackbearheart commented 6 years ago

Unfortunately, the rules for whitespace in email addresses (as laid out in rfcs) are arcane and bizarre. It is my understanding that this isn't technically a valid email address, even though to the observer's eye it is obvious how to interpret it as an email address.

The issue is that in the rfcs, only a certain kind of "folding whitespace" is allowed when CRLFs are involved. (see RFC 5322 section 3.2.2, and places where it is used.) The rule is that the whitespace has to be a CRLF, not just LF, and have spaces/tabs on each side. This is silly but there you have it.

> const addrs = require('./lib/email-addresses');
undefined
> addrs.parseOneAddress('"Foo Bar"\n<foo@bar.qux>') != null;
false
> addrs.parseOneAddress('"Foo Bar"\r\n<foo@bar.qux>') != null;
false
> addrs.parseOneAddress('"Foo Bar" \r\n <foo@bar.qux>') != null;
true

I'm sure you have resorted to some reasonable thing like stripping out what you consider whitespace first :)