The vulnerability exists in the addressparser library of Nodemailer, specifically within the _handleAddress function. The function is responsible for parsing email addresses from a string of tokens. The flaw was that the function did not distinguish between regular text and text enclosed in quotes when identifying email addresses. According to email standards (RFC 5321/5322), the local part of an email address can be a quoted string, which can legally contain an '@' symbol (e.g., "local@part"@domain.com).
The vulnerable version of _handleAddress would scan the entire string for patterns matching an email address. When given a crafted input like "attacker@malicious.com"@intended.com, it would incorrectly extract attacker@malicious.com as the recipient, ignoring the intended domain. This would cause the email to be misdirected to the attacker.
The patch, identified in commit 1150d99fba77280df2cfb1885c43df23109a8626, rectifies this by introducing state tracking to identify when the parser is inside a quoted string. It adds a textWasQuoted flag to tokens that originate from a quoted section. The logic for extracting email addresses is then modified to explicitly ignore any text that was quoted, thus ensuring that only the legitimate, top-level email address is parsed. The _handleAddress function is the precise location of this flawed logic and is therefore the vulnerable function.