r/java • u/DelayLucky • Jun 06 '26
EmailAddress Parser Improved
A few months back I had a post about the fun of using parser combinator to easily build a RFC 5322 email address parser.
Now with Dot Parse release 10.3, I'm happy to report that the EmailAddress class has been substantially improved and hardened for security.
On the feature set:
- It supports convenience accessor methods such as
user(),alias(),displayName(),domain(),hasI18nDomain(), with the values unescaped for programmatic consumption. toString()andaddress()automatically quotes and escapes for RFC-compliant output, when needed.- Supports dots in unquoted display names (
J.R.R. Tolkien <tolkien@lotr.org>). It's strictly not RFC compliant, but practically common. parseAddressList(input, logger::log)offers graceful error recovery. Useful when the address list includes one or two malformed entries.parseAddressList()is tolerant of common yet harmless human errors such as two commas in a row.
Before you ask, no. Using split(",") or regex cannot reliably pre-process an address list because the RFC allows quoted strings in the email address, and the quoted strings can include comma itself, and escapes. Splitting by , blindly or using complex and brittle regex can corrupt the email address list.
On the security front:
- Rejects dangerous characters such as control chars, formatting chars and bidi overrides.
- Rejects
<legitimate@trusted.com>attacker@evil.com - Rejects
user@good.com@evil.net. - Drops ip routing and intranet host names.
- Drops obsolete comments.
- IDN validation and canonicalization.
Overall, while RFC compliance is a goal, the library doesn't mechanically mirror RFC: it takes away obsolete and dangerous features like intranet hostnames and IP routing; and it adds support for non-RFC but practically useful features like dots in display name and helpful address list parsing.
The objective is for EmailAddress to be the trusted data model such that code operating on it can be assured that it's safe from most attack vectors.
For more details, you can check out the compliance and security breakdown.
Your feedback's welcome!
View all comments
Show parent comment
3
u/DelayLucky Jun 08 '26
I got inspiration from https://www.elttam.com/blog/jakarta-mail-primitives
Originally, this email parser was only to demonstrate using Dot Parse to declaratively build otherwise sophisticated DSL parsers. A few slight variances here and there from Jakarta
InternetAddresswouldn't be sufficient reason for Dot Parse to package it up as a serious alternative (sure, Jakarta needs to pull in a heavy dependency, but there are existing light-weight parsers out there too).But that post, and discussing security exploits with AI showed me that the niche of a secure email address parser has real value.
And I believe the approach of designing a safe-by-construction data model that all downstream code can trust hasn't been tried before.