How I found a DoS bug in an email parser with 20M+ weekly downloads
A MIME email parser compared each line against every open boundary using a linear scan, so deeply nested messages with long shared boundary prefixes made parsing cost grow with nesting depth times input size. An attacker could send a crafted but fully valid email that consumed seconds of CPU instead of milliseconds, causing a denial of service.
Published on arjunjaincs.medium.com
Cybersecurity
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
postal-mime is a MIME email parser for Node.js, also used inside Cloudflare's Email Workers. At the time of writing it was pulling 20 million downloads a week, which puts it in production pipelines across a lot of infrastructure rather than in a niche corner. The researcher had been hunting for reportable vulnerabilities in widely used npm packages; this was his fifth CVE.
The feature under scrutiny is the parser's handling of nested multipart sections. MIME allows sections inside sections, up to 256 deep by spec, and each section is delimited by a divider line (a boundary). On every line the parser asks a simple question: does this line start with --, and if so, does it match one of the dividers currently open?
Setup
The work was done against the package source, specifically src/postal-mime.ts, and the function processLine(). No accounts, roles or special tooling are described beyond the parser itself, its own test suite, a differential fuzzer and instrumentation added to count byte comparisons. The attack relies on a crafted message with deep nesting, a long shared boundary prefix and many near-miss lines.
Finding it
The observation is about cost. Nest 250 boxes, give every divider a long shared prefix, then fill the body with lines that look almost like a divider but do not quite match any of them. Each of those near-miss lines forces the parser to walk almost the entire way through a comparison against all 250 open dividers before it can give up. Across thousands of lines, a few megabytes of email takes multiple seconds of CPU instead of milliseconds. The file stays completely valid MIME the whole time; nothing about it looks malformed.
Before settling on this, the researcher ruled out something more severe in the same codebase:
- Searched for dangerous sinks across the whole project:
eval,fs,child_process,require. Zero hits, so there is no filesystem or execution surface at all, which caps how bad anything here can get. - Checked for prototype pollution: already guarded.
- Followed a ReDoS lead in the HTML-to-text converter that looked real at first, since the source literally had comments admitting a known quadratic bug. The fix was already shipped, confirmed by throwing 80,000-repetition adversarial payloads at it directly rather than trusting the comment.
The conclusion was that this is a pure parser with no dangerous sinks, so a CPU denial-of-service bug is roughly the severity ceiling. That framing was stated plainly in the report rather than inflated.
Exploitation
Step 1: build a deeply nested message with a shared boundary prefix
The attacker constructs a valid MIME message with nesting up to depth 250, where every boundary shares a long common prefix. The body is then filled with lines that begin with -- and resemble a boundary but match none of the open ones. Each such line is a near miss that must be compared against every open boundary before failing.
Step 2: understand the loop being abused
The vulnerable code in processLine() iterated over every open boundary for each -- line, with no index and no early exit beyond a cheap length check:
for (let i = 0; i < boundaries.length; i++) { let boundary = boundaries[i]; if (line.length < boundary.value.length + 2) continue; let boundaryMatches = true; for (let j = 0; j < boundary.value.length; j++) { if (line[j + 2] !== boundary.value[j]) { boundaryMatches = false; break; } } }
The outer loop runs once per open boundary. The inner loop compares the line's bytes against the boundary's bytes and only breaks on the first differing byte. With a long shared prefix, that break happens very late, so each near-miss line costs roughly depth times shared prefix length. Total parse cost becomes depth times size instead of just size.
Step 3: measure the slowdown
The first timing pass showed roughly a 13 to 16 times slowdown at depth 250. That measurement was redone properly with JIT warmup, discarding a throwaway parse before timing, and the real number came out higher: 27 to 33 times. Without warmup, a constant startup overhead is added to both the attack and the control measurement, and the same constant added to both sides of a ratio pulls it toward 1. The corrected, larger number was the one reported.
Step 4: count the actual operation
The strongest evidence came from instrumenting the parser to count real byte comparisons rather than timing it. On a fixed workload:
| Depth | Byte comparisons |
|---|---|
| 1 | about 755,000 |
| 250 | about 181.7 million |
The scaling is clean and linear with depth, holding at a constant 62 bytes compared per boundary per line the entire way, which matches the proof-of-concept's own boundary length almost exactly. This is a direct count of the operation the bug performs too many times, not an inference from noisy timing.
Root cause
The defect is inefficient algorithmic complexity, CWE-407. For every line beginning with --, the parser scans all open boundaries and, for each, compares bytes until a mismatch. Because the boundaries share a long prefix, the mismatch is found late, so the work per line is proportional to nesting depth times prefix length. The only guard is a length check (line.length < boundary.value.length + 2), which does nothing when the line is long enough to pass it. There is no index over the open boundaries and no early exit that would make the comparison cheap. The message remains valid MIME throughout, so no validity check rejects it.
Impact
An attacker who can get a crafted message parsed can consume multiple seconds of CPU for a few megabytes of email, where milliseconds would be expected. The package is used in Node.js and in Cloudflare's Email Workers, so anything parsing untrusted mail with it is exposed. The researcher notes that the project's own README advises bounding input size before parsing untrusted mail, and named that advice directly in the submission rather than waiting for the maintainer to raise it as a reason to dispute the finding. Because the codebase has no dangerous sinks, the ceiling here is CPU denial of service rather than code execution or file access.
Fix
The fix replaces the linear scan with a trie built from the open boundaries' bytes, so matching a line costs time proportional to the line's own length rather than the number of open boundaries.
The first version of the patch cached the trie keyed by the array's identity, rebuilding only if the reference changed. That was wrong: the boundaries array is one object, mutated in place via push and splice, so the reference never changes across a parse and the cache went stale after the first build and stayed stale. Running the project's own test suite against it produced 14 failures, all in nested-multipart test cases. The clean baseline had already been confirmed at 1040 out of 1040, so the failures were attributable to the patch and not pre-existing flakiness. The fix was an explicit version counter bumped at both mutation sites instead of trusting the array reference; rerunning gave 1040 out of 1040.
An 8,000-case differential fuzzer then compared the original parser against the patched one, generating nested multipart messages with randomised boundary values and deliberately forcing exact boundary reuse between a parent and child section in about 30% of cases, since that is the scenario a separate correctness rule in the codebase exists to handle. There were zero differences across 325 distinct outputs.
The maintainer shipped 4.2.1 and CVE-2026-124016 was published as CWE-407, with CVSS 5.3 on v3.1 and 6.9 on v4.0. After the fix landed, the npm page showed the fixed version live and current, zero open issues, and close to 20 million downloads a week. The published CVSS score landed almost exactly on the number estimated before submitting.
Full advisory: https://github.com/postalsys/postal-mime/security/advisories/GHSA-q8wm-54mj-r36p
CVE record: https://www.cve.org/CVERecord?id=CVE-2026-124016
Variants to test
The general pattern is a comparison loop whose cost depends on attacker-controlled shared prefixes, so it is worth looking for elsewhere:
- Other MIME or multipart parsers that scan a list of open boundaries per line. A positive result is parse time that grows with nesting depth at fixed message size, confirmed by counting comparisons rather than only timing.
- Parsers with a cheap length guard but no index or early exit. A positive result is a near-miss line that passes the length check and then compares most of the boundary before failing.
- Anywhere a cache is keyed by object identity on a collection mutated in place with
pushorsplice. A positive result is a stale cache after the first build, visible as incorrect parsing in nested cases. - HTML-to-text or similar converters with comments admitting a quadratic bug. A positive result is a shipped fix that survives adversarial repetition payloads, which is worth confirming directly rather than trusting the comment.
- Differential fuzzing of a patch against the original, forcing exact boundary reuse between parent and child sections. A positive result is any output difference, which points at a correctness regression in the fix.
What happened, in plain words
A security researcher found a bug in a piece of software that almost every email system depends on, even if most people have never heard of it. The software is called postal-mime. It is a small program that reads email messages and takes them apart into their pieces: the text, the attachments, the formatting. That is not a niche tool; it sits inside a lot of the infrastructure that handles mail today.
A denial-of-service bug does not steal data or break into anything. It makes a program spend so much time working on crafted input that it cannot get to the other work it needs to do. Think of a shop assistant who is handed a receipt so long and so confusing that they spend the whole afternoon checking it, while the queue in front of them grows. Nobody robbed the shop; the shop just stopped serving people.
In this case, a carefully crafted email of a few megabytes could make the parser burn several seconds of processor time, where a normal email would take milliseconds. It is just built to be expensive to read.
The researcher filed it through VulnCheck, the maintainer of the project shipped a fixed version (4.2.1), and the bug was published as CVE-2026-124016. This article walks through how the bug works, how it was measured, the mistake the researcher made while writing the fix, and how the fix was checked. It is written for someone who has never done security work before, so every term is explained as it comes up.
The package and the feature
What postal-mime is
An email message is not just text. It is a structured document, and the format it follows is called MIME (Multipurpose Internet Mail Extensions). MIME is what lets one email contain a plain-text version, an HTML version, several attachments, and inline images all at once. A parser is a program that reads a document in some format and turns it into something a program can work with. postal-mime is a MIME parser: you give it the raw text of an email, and it hands back the parts.
postal-mime is distributed as an npm package. npm is the package manager for Node.js, a popular way to run JavaScript outside a browser. A package manager is like an app store for code: developers download ready-made building blocks instead of writing everything themselves. The download count (20 million a week) is a rough measure of how many projects depend on that building block.
Nested sections and boundaries
MIME allows sections inside sections, like boxes inside boxes. Each section is separated from the next by a divider line called a boundary. A boundary is just a short string of characters that marks where one part ends and the next begins. When you open an email with an attachment, the boundary lines are the invisible fences between the body text and the attachment data.
If it matches, a section starts or ends. If it does not, the line is ordinary content.
That question sounds cheap. The bug is that it is not cheap when an attacker designs the email to make it expensive.
Finding the bug
The idea: make every near-miss costly
The researcher's observation was about cost, not correctness. Imagine you nest 250 boxes, and you give every boundary a long shared prefix, meaning all the boundary strings start with the same long run of characters. Then you fill the body with lines that look almost like a boundary but do not quite match any of them.
For each of those near-miss lines, the parser has to compare the line against all 250 open boundaries. And because the boundaries share a long prefix, each comparison runs almost to the end before it finds a character that differs and gives up. So every near-miss line costs roughly the nesting depth multiplied by the length of the shared prefix. Do that across thousands of lines, and a few megabytes of email takes multiple seconds of processor time instead of milliseconds.
The important part: the file stays completely valid MIME the whole time. Nothing about it looks malformed. A validity check has nothing to complain about.
Ruling out something worse first
Before settling on this bug, the researcher wanted to know whether something more severe was hiding in the same codebase. This is good practice: if you report a small bug when a bigger one is sitting next to it, you have wasted everyone's time. So he checked, in order:
- Dangerous sinks. A sink is a place in a program where data gets used in a powerful way, such as running a command or writing a file. The classic dangerous sinks in JavaScript are
eval(which runs a string as code),fs(file system access),child_process(starting other programs) andrequire(loading code). He searched the whole project for these and found zero hits. That means there is no way for this parser to touch the file system or run anything, which caps how bad any bug in it can get. - Prototype pollution. This is a JavaScript-specific bug where an attacker sneaks extra properties into a shared object and changes how the whole program behaves. It was already guarded against.
- A ReDoS lead. ReDoS stands for Regular Expression Denial of Service: a badly written pattern-matching rule can be made to run for an extremely long time by a crafted input. The HTML-to-text converter inside the project looked like it had one, because the source code literally had comments admitting a known quadratic bug. Quadratic here means the work grows with the square of the input size, so doubling the input makes it four times slower. But the fix had already shipped. He confirmed that by throwing 80,000-repetition adversarial payloads at it directly, rather than trusting the comment.
The conclusion: this is a pure parser with no dangerous sinks, so a CPU denial-of-service bug is roughly the severity ceiling. He said that plainly in the report instead of inflating it. Being honest about the ceiling is part of a good report; it tells the maintainer you have done your homework.
The vulnerable code
The bug lives in src/postal-mime.ts, in a function called processLine(). Here is the loop as it appeared:
for (let i = 0; i < boundaries.length; i++) { let boundary = boundaries[i]; if (line.length < boundary.value.length + 2) continue; let boundaryMatches = true; for (let j = 0; j < boundary.value.length; j++) { if (line[j + 2] !== boundary.value[j]) { boundaryMatches = false; break; } } }
Line by line, in plain words:
for (let i = 0; i < boundaries.length; i++) {starts a loop that runs once for every boundary that is currently open. If 250 sections are open, this loop runs 250 times for a single line.let boundary = boundaries[i];picks the current boundary out of the list.if (line.length < boundary.value.length + 2) continue;is a cheap guard. If the line is shorter than the boundary (plus the two leading--characters), it cannot possibly match, so skip to the next boundary. This is the only shortcut in the whole loop.let boundaryMatches = true;assumes a match until proven otherwise.for (let j = 0; j < boundary.value.length; j++) {starts comparing the line against the boundary, one character at a time.if (line[j + 2] !== boundary.value[j]) {compares the character in the line (offset by two, to skip the--) with the character in the boundary. If they differ, the boundary does not match.boundaryMatches = false; break;marks it as a non-match and stops comparing this boundary.
With a long shared prefix, that difference is found very late, so the inner loop runs almost to the end every time.
Measuring the slowdown
First attempt, and why it was wrong
That is already a big number, but it was not the real one. The measurement was redone properly with JIT warmup.
JIT stands for Just-In-Time compilation. JavaScript engines do not run code the same way every time; they start by interpreting it slowly, then notice which parts run often and compile those parts into fast machine code. That means the first run of a program is slower than later runs, not because of the bug but because the engine is still warming up. If you time the first run, you are partly measuring startup overhead.
The researcher discarded a throwaway parse before timing, so the engine was already warm. The explanation is simple arithmetic. Without warmup, a constant startup overhead gets added to both the attack measurement and the control measurement. The same constant added to both sides of a ratio pulls the ratio toward 1, making the slowdown look smaller than it is. That is the honest thing to do, and it matters: a report that understates its own finding is easier to dismiss.
Counting the actual operation
Timing is noisy. A busy laptop, a background update, a garbage collector running at the wrong moment: all of these can move a timing number around. The strongest evidence came from instrumenting the parser to count real byte comparisons, rather than timing it. Instrumenting means adding a small counter to the code so it records how many times a particular operation happens.
On a fixed workload, the counts were:
| Depth | Byte comparisons |
|---|---|
| 1 | about 755,000 |
| 250 | about 181.7 million |
The scaling is clean and linear with depth. Linear means the work grows in direct proportion: 250 times the depth gives roughly 250 times the comparisons. This is not an inference from noisy timing; it is a direct count of the operation the bug performs too many times. If you want to convince someone a loop is doing too much work, count the iterations.
The fix, and the mistake in it
What the fix does
A trie (pronounced "try") is a tree-shaped lookup structure. Imagine a filing cabinet where the first drawer is chosen by the first letter, the next by the second letter, and so on. To check whether a word is in the cabinet, you follow one drawer per letter, so the cost depends on the length of the word, not on how many words are stored. With a trie, matching a line costs time proportional to the line's own length, not to the number of open boundaries. The depth multiplier disappears.
The mistake
Caching means keeping a built result around so you do not have to rebuild it. Keying by identity means deciding whether to rebuild by checking whether the array object is still the same object. Rebuild only if the reference changes, in other words.
That was wrong. push adds an item to the end of an array; splice removes or inserts items in the middle. Both change the contents of the array without creating a new array. So the reference never changes across a parse. The cache went stale after the first build and stayed stale: the parser kept using a trie built from boundaries that no longer matched the real list.
This is a classic trap. "Has the object changed?" and "is it a different object?" are not the same question. A shopping list on the fridge is still the same piece of paper after you cross something out and add something else.
Catching it with the test suite
He ran the project's own test suite against the patch. A test suite is a collection of small checks that the project's authors wrote to confirm the software behaves correctly. Running it is the cheapest way to find out whether your change broke something.
The baseline is the unmodified code: if it passes everything, then any failure after your change is yours, not pre-existing flakiness. Flaky tests are tests that sometimes pass and sometimes fail for reasons unrelated to the code, and they can waste hours if you do not establish a baseline first.
Fixing the fix
A version counter is a number that goes up every time the contents change. If the number is the same as last time, the cache is still good; if it went up, rebuild. Rerunning the suite gave 1040 out of 1040.
Checking the fix against the original
Fuzzing means feeding a program lots of automatically generated, semi-random inputs to see whether it crashes or misbehaves. Differential means running two versions side by side, the original parser and the patched parser, and comparing their outputs on the same input. If the fix is correct, the outputs should be identical.
That scenario matters because a separate correctness rule in the codebase exists specifically to handle it, so it is exactly where a fix is most likely to go wrong.
Reporting it
VulnCheck is a service that helps researchers get vulnerability reports to the right people. A README is the front page of a code project, where the authors explain how to use it. Naming that advice up front, rather than waiting for the maintainer to raise it as a reason to dispute the finding, is a deliberate move.
The outcome
A CVE, or Common Vulnerabilities and Exposures identifier, is a public catalogue number given to a specific security bug so that everyone can refer to the same thing. CWE is a catalogue of weakness types, and CWE-407 is the entry for code whose running time grows faster than it should with the size of its input.
CVSS, the Common Vulnerability Scoring System, is a standard way of turning a bug's characteristics into a number from 0 to 10, so people can compare severity across different bugs. The two numbers come from two different versions of the scoring standard.
Full advisory: https://github.com/postalsys/postal-mime/security/advisories/GHSA-q8wm-54mj-r36p
CVE record: https://www.cve.org/CVERecord?id=CVE-2026-124016
What to carry forward
The researcher's own lessons:
- Your first measurement is often wrong in a small, fixable way. Run the cheap version before trusting the expensive one.
- A patch that typechecks and looks reasonable can still be broken. Run the actual test suite, and confirm the baseline first, so you know which failures are yours.
- Counting the real operation beats timing it. Counting the real operation beats timing it.
- Naming your own weak points in the report up front works better than hoping nobody notices them. The README advice about input size is one example.
Variants to test
The general pattern here is a comparison loop whose cost depends on attacker-controlled shared prefixes. That pattern is worth looking for elsewhere:
- Other MIME or multipart parsers that scan a list of open boundaries per line. A positive result is parse time that grows with nesting depth at fixed message size, confirmed by counting comparisons rather than only timing.
- Parsers with a cheap length guard but no index or early exit. A positive result is a near-miss line that passes the length check and then compares most of the boundary before failing.
- Anywhere a cache is keyed by object identity on a collection mutated in place with
pushorsplice. A positive result is a stale cache after the first build, visible as incorrect parsing in nested cases. - HTML-to-text or similar converters with comments admitting a quadratic bug. A positive result is a shipped fix that survives adversarial repetition payloads, which is worth confirming directly rather than trusting the comment.
- Differential fuzzing of a patch against the original, forcing exact boundary reuse between parent and child sections. A positive result is any output difference, which points at a correctness regression in the fix.
Glossary
- API: Application Programming Interface; a defined way for one piece of software to talk to another, like a menu of things you can ask a program to do.
- Baseline: the result of running tests on the unmodified code, used to tell whether later failures are new.
- Boundary: in MIME, a short string that marks where one section of an email ends and the next begins.
- Byte comparison: checking whether two characters (or bytes) are the same; the basic operation the vulnerable loop performs too many times.
- Cache: a stored result kept around so it does not have to be recomputed.
- Cloudflare Email Workers: a Cloudflare service that runs code when email arrives; one of the places
postal-mimeis used. - CPU: the processor; the part of the computer that does the actual work. A CPU denial of service makes it do too much work.
- CVE: Common Vulnerabilities and Exposures; a public catalogue number for a specific security bug.
- CVSS: Common Vulnerability Scoring System; a standard 0 to 10 severity score for a bug.
- CWE: Common Weakness Enumeration; a catalogue of types of software weakness. CWE-407 is inefficient algorithmic complexity.
- Denial of service (DoS): a bug that makes a program too busy to serve legitimate requests, without stealing data or breaking in.
- Differential fuzzing: running two versions of a program on the same generated inputs and comparing their outputs.
- Dangerous sink: a place in code where data is used in a powerful way, such as running commands or touching files.
eval: a JavaScript function that runs a string as code; a classic dangerous sink.fs: the Node.js module for file system access; a classic dangerous sink.child_process: the Node.js module for starting other programs; a classic dangerous sink.require: the Node.js function for loading other code; a classic dangerous sink.- Flaky test: a test that sometimes passes and sometimes fails for reasons unrelated to the code.
- Fuzzing: feeding a program lots of automatically generated inputs to find crashes or misbehaviour.
- Instrumenting: adding counters or logging to code so you can observe what it actually does.
- JIT (Just-In-Time compilation): how JavaScript engines speed up code that runs often; the first runs are slower, which is why warmup matters when timing.
- Linear scaling: work that grows in direct proportion to input size; doubling the input doubles the work.
- MIME: Multipurpose Internet Mail Extensions; the format that lets an email contain text, HTML, attachments and inline images together.
- Near-miss line: a line that looks almost like a boundary but does not match, forcing a long comparison before failing.
- Node.js: a way to run JavaScript outside a browser, commonly used for servers.
- npm: the package manager for Node.js; an app store for code.
- Parser: a program that reads a document in some format and turns it into something a program can use.
- PoC (proof of concept): a small script that demonstrates a bug actually works.
- Prototype pollution: a JavaScript bug where an attacker adds properties to a shared object and changes how the whole program behaves.
- Quadratic: work that grows with the square of the input size; doubling the input makes it four times slower.
- ReDoS: Regular Expression Denial of Service; a pattern-matching rule that can be made to run for an extremely long time by a crafted input.
- README: the front page of a code project, where the authors explain how to use it.
- Reference (object identity): whether two variables point at the very same object in memory, as opposed to two objects with the same contents.
push/splice: JavaScript array methods that add or remove items in place, without creating a new array.- Test suite: a collection of small checks the project's authors wrote to confirm the software behaves correctly.
- Trie: a tree-shaped lookup structure where each step follows one character, so lookup cost depends on the length of the word, not the number of words stored.
- VulnCheck: a service that helps researchers get vulnerability reports to the right people.
- Warmup: running a program once before timing it, so the engine has already compiled the hot code paths.