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.
2.9 KiB
mail-parser, vendored
The published mail-parser 0.11.9,
taken from crates.io (.crate sha256
4ec00bda90c6e645a54506c630c2820cd6b1890cfd2b0a169b50f74b2b8c7c86, the
checksum the server's Cargo.lock had for it) under Apache-2.0 OR MIT.
INBUXA takes it under MIT. The root Cargo.toml patches it in with
[patch.crates-io], so every crate in the build that parses mail (the
server's own and its dependencies, such as sieve-rs and mail-auth) uses this
copy.
Left out: the top-level resources/ (5 MB of sample messages), tests/,
examples/, .github/ and flamegraph.sh, which the server never builds,
and Cargo.lock, Cargo.toml.orig, .cargo_vcs_info.json and .cargo-ok.
Cargo.toml drops the [[example]] and [[test]] targets that pointed at
them. The crate's own tests need resources/ and dev-dependencies the server
doesn't otherwise use, so they aren't run here; the server's tests cover the
changes below.
Changes
Every changed file carries a Coffey Labs notice in its header.
src/parsers/message.rs: message/rfc822 (and message/global) parts without a Content-Transfer-Encoding nest at mostMAX_NESTED_MESSAGES(20) deep in one parse. A part deeper than that is kept as an attachment (PartType::Binary, its raw bytes) and not parsed. Upstream limited only encoded nested messages (MAX_NESTED_ENCODED, 3); unencoded ones nested to any depth, each aMessageinside its parent, and dropping the tree took a stack frame per level. A 1.6 MB message nested 50,000 deep overflowed a 2 MiB thread stack in a release build and aborted the server, once when SMTP finished with it and again on every attempt to deliver it to a mailbox with a Sieve script. With both limits no tree is more than 80 deep. Thedeeply_nested_messages_drop_on_a_small_stacktest incrates/emailcovers it.src/parsers/message.rsandsrc/parsers/header.rs: a part keeps at mostMAX_HEADERS_PER_PART(10,000) header fields and a message, with all its parts and nested messages (encoded ones too), at mostMAX_HEADERS_PER_MESSAGE(262,144). Fields past either limit are read past and left out. Upstream kept every field, each a struct of over a hundred bytes for as little as four bytes of input, so a 100 MB header block took gigabytes. SMTP DATA refuses a message the parser left fields out of. Theheader_fields_are_cappedtest incrates/emailcovers it.
Updating
When the server's Cargo.lock moves mail-parser to a new version, Cargo
stops using this copy and only warns that the patch went unused, so check
Cargo.lock has no mail-parser entry with a source line; the tests
above also fail once a version without the limit is in use. Re-vendor the new
version from ~/.cargo/registry/src/*/mail-parser-*, leaving out what's
listed above, repeat the changes, and update the version here and in the root
Cargo.toml comment.