mail-parser kept every header field it read, each a struct of over a
hundred bytes for as little as four bytes of input, and mail-auth kept a
second list of the top-level ones. Anyone who can send to port 25 could
send a message whose 100 MB header block took several gigabytes to parse,
and a few at once could run the server out of memory.
In the vendored mail-parser, a part now keeps at most 10,000 header fields
and a message, with all its parts and nested messages (encoded ones
too), at most 262,144. Fields past either limit are read past and left
out; the raw message keeps them. That covers every place mail is
parsed. Because a mail client reading the raw message would still see
fields the server left out, SMTP DATA refuses with 552 5.3.4 a message
with more than 1,000 header fields in any part (real mail has tens) or
one the parser left fields out of. The headers a DKIM2-signed DSN
returns as text/rfc822-headers, which mail-auth parses on its own, are
looked at only up to 1,000 fields.
Found in a security review.
Test: in crates/email, a part of 50,000 header fields kept all of them;
now it keeps 10,000 and its body is still found, and 100 parts of 5,000
fields or 60 base64-encoded nested messages of 5,000 fields keep at most
262,144 in all. In the SMTP data suite, messages with 1,001 fields at
the top level or in a MIME part are refused with 552 5.3.4. In
crates/smtp, 1,000 fields per part, nested messages included, fit; more,
or a message the parser left fields out of, don't; and a DSN's returned
headers of 1,001 fields aren't parsed for DKIM2.
In the SMTP DMARC suite, the 1,001-field case from the DKIM limits is
now refused with 552 5.3.4 before DKIM is checked.