Skip to main content
Menu
Advisories

CPU exhaustion from backtracking in encoded-word parsing

GHSA-2FGF-M8Q4-C4FMPublished Updated GitHub

Description

The regular expression MimeToken uses to decode an RFC 2047 encoded word backtracks quadratically when a match fails. The optional * between the charset and language groups lets both groups match the same run of characters, so a long charset that fails to match is tried in every possible split.

A single header line is too short to make this expensive, but RFC 2231 split parameters (filename*0, filename*1, ...) are joined into one value before decoding, so one header can produce a token hundreds of kilobytes long. A 245 KB Content-Disposition header took about 14 seconds to parse, at which point PCRE's backtrack limit stops the match. The cost adds up across the parts of a message: two such attachment parts took 28 seconds, and the default 8 MB limit on header size per message allows roughly thirty.

Details

/^=\?([A-Za-z\-_0-9]+)\?([A-Za-z\-_0-9]+)?\?([QBqb])\?([^\?])\?=$/

in MimeToken::decodeMime() (MimeLiteralPart in 1.x and 2.x). The patched releases require the * whenever a language is present, which removes the ambiguity without changing what valid encoded words decode to.

PoC

$pieces = str_split('=?' . str_repeat('a', 250000), 4000); $pieces[] = 'a.?Q?x?='; $params = []; foreach ($pieces as $i => $p) { $params[] = "\tfilename*$i=\"$p\""; } $raw = "From: a@example.test\r\nContent-Type: text/plain\r\n" . "Content-Disposition: attachment;\r\n" . implode(";\r\n", $params) . "\r\n\r\nbody\r\n"; $m = Message::from($raw, false); $m->getHeaderParameter('Content-Disposition', 'filename'); // ~14 s

Impact

An application that parses untrusted mail and reads header parameters such as attachment filenames can be made to spend minutes of CPU on a single message, and the sender can repeat it at will. No configuration or user interaction is required.

Patches

Fixed in 4.0.9 and 3.0.10. Versions 1.x and 2.x contain the same pattern and are no longer maintained; upgrading is recommended.

Workarounds

Reject messages whose headers contain RFC 2231 split parameters that join to an unusually long value, or limit the total header size accepted before parsing.

Affected versions

References

GitHub · zbateson/mail-mime-parser