ModSecurity DoS Vulnerability in JSON Parsing (CVE-2021-42717)
ModSecurity's JSON parser lacked a depth limit, so a request with deeply nested JSON could cause excessive resource consumption and delay responses. An attacker could send many such requests to render the web server unable to handle legitimate traffic.
Published on trustwave.com
Change theme to light
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
ModSecurity is an open-source web application firewall (WAF) engine maintained by Trustwave. It sits in front of a web server and inspects HTTP requests against configured rules. Among its request body processors is a JSON parser, which lets rules inspect JSON payloads rather than treating them as opaque bytes.
The parser is normally engaged when the HTTP Content-Type request header is application/json. It can also be engaged optionally for other Content-Type values.
ModSecurity ships with a file called modsecurity.conf-recommended, which contains a small set of configuration directives and rules that are typically desirable. That file includes rule 200001, which enables the JSON request body processor, and the rule is enabled by default. The issue described here does not depend on any other ruleset being installed.

The problem is a denial of service condition in JSON parsing, tracked as CVE-2021-42717. It was reported to the ModSecurity team by Andrea "theMiddle" Menin through a responsible disclosure, and fixes landed in both the v2 and v3 branches.
Setup
The precondition is a ModSecurity installation that parses JSON request bodies. For a given installation, the report states it is likely vulnerable unless one of two things is true:
- Any rules that activate the JSON parser have been removed or commented out.
- ModSecurity was built without the JSON parser code (yajl).
No account, role or special tooling is described. The attack surface is simply an HTTP request body that the WAF will hand to its JSON parser.
Finding it
The researcher reported a use case rather than a memory-safety bug: valid JSON can be nested, and the parser's work grows with the nesting depth. The report gives a small, valid example with a maximum depth of three:
{"key1":{"key1_1":{"key1_1_1":99}}}
The concern is when the greatest depth in the JSON request body is very large. Once the depth reaches the many tens of thousands, resource consumption escalates considerably, and ModSecurity may delay a request for an excessive period while performing the JSON parsing. If enough such malicious requests run simultaneously, the web server can be rendered unable to answer legitimate requests in a timely manner.
The negative performance impact keeps increasing as the parsing depth grows. Because increasing depth requires adding characters such as {, an attacker who wants deeper parsing must also send a larger overall request body. That trade-off matters for the non-upgrade mitigations described below.
Exploitation
Step 1: Send a deeply nested JSON body
Send a request whose body is valid JSON nested to a very large depth, with a Content-Type that causes ModSecurity to invoke the JSON parser (normally application/json). The report does not include a full request, only the shape of the nesting, illustrated by the depth-three example above.
Step 2: Observe the delay
At that depth, ModSecurity may delay a request for an excessive period while performing the JSON parsing. The negative performance impact increases as depth grows, and deeper nesting requires a larger request body.
Step 3: Parallelise
Run a large enough number of such requests simultaneously. With enough concurrent deep-nesting requests, the web server can no longer respond to legitimate traffic in a timely manner.
Root cause
Deeply nested JSON caused parsing cost to grow with depth, and the report describes no limit on that depth before the fix. Parsing cost grows with depth, so a body that is valid JSON can consume considerable resources inside the WAF. The default recommended configuration enables the JSON body processor through rule 200001, so a stock installation parses such bodies without any additional ruleset being present.
Impact
A malicious actor can cause a denial of service: requests are delayed for an excessive period while JSON parsing runs, and with enough simultaneous malicious requests the web server may be unable to serve legitimate requests in a timely manner. The effect is conditional on the installation actually parsing JSON bodies, that is, on the parser-activating rules being present and on ModSecurity having been built with yajl.
Fix
ModSecurity v2.9.5 and v3.0.6 add a configurable limit on the maximum depth accepted during JSON parsing. If the maximum is exceeded, JSON parsing halts immediately and ModSecurity sets its standard REQBODY_ERROR. The default depth limit is 10,000, and it can be raised or lowered with a new configuration item:
SecRequestBodyJsonDepthLimit
Most installations should find the default sufficient. At the time of writing, v3.0.6 for Nginx and v2.9.5 for Apache were available immediately, and v2.9.5 for IIS was expected within a few days.
For users who cannot upgrade immediately, the report offers rule-based mitigations. There is no universally suitable substitute for upgrading, but some installations can reduce exposure:
- No web application accepts JSON request bodies. Disable any ModSecurity rules that activate the JSON parser.
- Some applications accept JSON bodies, but they are all small. Create a
phase:1chained rule that rejects requests which would use the JSON parser whenContent-Lengthexceeds a certain size. The report suggests 10,000 to 20,000 bytes is likely safe in most installations. Some v2 users may instead use theSecRequestBodyNoFilesLimitconfiguration item if other non-file request bodies are likewise expected to stay below similar thresholds. For ModSecurity versions before v2.9.3, the problem exists (for example in v2.9.2) but appears to require a larger request body to trigger problematic delays, so size limits can probably be larger, perhaps 40,000 to 50,000 bytes, and still provide decent protection in most installations. - A few applications expect larger JSON bodies. Use a rule as in case 2 but skip it where larger bodies are expected, either with an additional rule in the chain or by removing the rule at runtime (for example with
ctl:ruleRemoveById) once the relevant conditions are met. The report notes this is not robust protection, but it may be reasonable against less-motivated attackers.
Variants to test
- Other body processors with recursive formats. A positive result is a measurable, depth-dependent delay in request handling.
- Non-default
Content-Typevalues. Where the JSON parser is optionally engaged for other content types, send the same deep nesting with those types. A positive result is the same delay withoutapplication/json. - Depth versus size trade-off. A positive result is a curve where delay grows faster than the body size the attacker must send.
- Concurrency threshold. A positive result is legitimate traffic failing while the malicious requests are in flight.
- Version-specific thresholds.
What happened, in plain words
A security researcher found a way to make a piece of security software called ModSecurity work so hard that the website it protects can no longer answer normal visitors. The trick does not break into anything and does not steal data. It simply sends a request that is valid but extremely deeply nested, and the software spends a very long time reading it. Send enough of these at once and the website grinds to a halt. This kind of attack is called a denial of service, or DoS: the goal is to stop a service from working, not to take anything from it.
The problem was given an official tracking number, CVE-2021-42717. A CVE is a public catalogue entry for a known security flaw, so people can look it up and know they are talking about the same thing. The researcher, Andrea "theMiddle" Menin, reported it privately to the ModSecurity team, and fixes were released in v2.9.5 and v3.0.6.

The image above is the title banner of the original blog post announcing the issue.
The product and the feature involved
ModSecurity is a web application firewall, usually shortened to WAF. Think of a WAF as a security guard standing at the door of a building: every visitor (every request) is checked against a list of rules before being let in. If a request looks suspicious, the guard stops it. ModSecurity is open source, which means its code is publicly available and anyone can inspect or contribute to it, and it is maintained by a company called Trustwave.
A request is what your browser sends to a website when you click a link or submit a form. It has a body, which is the actual content being sent, for example the text you typed into a form. The body can be formatted in different ways, and one common format is JSON.
JSON is a way of writing structured data as text, using curly brackets to group things. It is very common when a web page talks to a server behind the scenes, for example to fetch new posts without reloading the page. A piece of software on a server that answers these behind-the-scenes requests is often called an API, short for application programming interface. You can think of an API as a waiter: your browser tells the waiter what it wants, the waiter goes to the kitchen (the server) and brings back the dish (the data).
ModSecurity includes a JSON parser. A parser is a component that reads a piece of text and works out its structure, the way you might read a nested set of boxes and note which box is inside which. This lets ModSecurity's rules look inside JSON content instead of treating it as a meaningless blob of characters.
The parser normally switches on when the request carries a header saying the content is JSON. A header is a small label attached to a request, and Content-Type is the header that says what format the body is in. When Content-Type is application/json, ModSecurity knows to parse the body as JSON. The report notes the parser can also be switched on optionally for other Content-Type values.
ModSecurity ships with a sample configuration file called modsecurity.conf-recommended. A configuration file is a settings file: it tells the software which rules to apply. This file contains rule 200001, which turns on the JSON body processor, and that rule is enabled by default. In other words, a freshly installed ModSecurity will parse JSON bodies without you doing anything. The issue described here does not depend on any other ruleset being installed.
Who is affected
The precondition is simple: the installation must parse JSON request bodies. For a given installation, the report states it is likely vulnerable unless one of two things is true:
- Any rules that activate the JSON parser have been removed or commented out. Commenting out means putting a marker in front of a line so the software ignores it, like crossing out a line in a recipe.
- ModSecurity was built without the JSON parser code, which is a library called yajl. A library is a reusable chunk of code that a program borrows rather than writing itself.
No account, role or special tooling is described. The attack surface, meaning the part of the system an attacker can reach, is simply an HTTP request body that the WAF will hand to its JSON parser.
Finding it
The researcher reported a use case rather than a memory-safety bug. A memory-safety bug is a flaw where a program mishandles its own internal memory, often allowing an attacker to run their own code. This is different: the software behaves as designed, but deeply nested JSON makes it work for an excessive period.
Valid JSON can be nested, meaning one set of brackets can sit inside another. The report gives a small, valid example with a maximum depth of three:
{"key1":{"key1_1":{"key1_1_1":99}}}
Line by line, in plain words:
- The outermost
{opens the first, outermost box. "key1":is a label for what is inside that box.- The next
{opens a second box inside the first. "key1_1":labels the second box.- The next
{opens a third box inside the second. "key1_1_1":99is a label and a value (the number 99) inside the third box.- The three
}characters close the three boxes, innermost first.
So the depth is three: three boxes, one inside another. This tiny example is harmless. The concern is when the greatest depth in the JSON request body is very large. Once the depth reaches the many tens of thousands, resource consumption escalates considerably, and ModSecurity may delay a request for an excessive period while performing the JSON parsing. If enough such malicious requests run simultaneously, the web server can be rendered unable to answer legitimate requests in a timely manner.
The negative performance impact keeps increasing as the parsing depth grows. Because increasing depth requires adding characters such as {, an attacker who wants deeper parsing must also send a larger overall request body. That trade-off matters for the non-upgrade mitigations described below: if you cap the size of a body, you also cap how deep it can be.
Exploitation
Step 1: Send a deeply nested JSON body
Send a request whose body is valid JSON nested to a very large depth, with a Content-Type that causes ModSecurity to invoke the JSON parser (normally application/json). The report does not include a full request, only the shape of the nesting, illustrated by the depth-three example above.
Valid but extremely deep JSON is what forces the parser to do a lot of work.
Step 2: Observe the delay
At that depth, ModSecurity may delay a request for an excessive period while performing the JSON parsing. The negative performance impact increases as depth grows, and deeper nesting requires a larger request body.
Why this step: the attacker is measuring the payoff. Each extra level of nesting is a few extra characters sent, and in return the server spends longer reading.
Step 3: Parallelise
Run a large enough number of such requests simultaneously. With enough concurrent deep-nesting requests, the web server can no longer respond to legitimate traffic in a timely manner.
Why this step: one slow request is annoying but survivable. Many slow requests at once use up the server's capacity to handle anything else, which is what turns a delay into an outage.
Root cause
Deeply nested JSON caused parsing cost to grow with depth, and the report describes no limit on that depth before the fix. Parsing cost grows with depth, so a body that is valid JSON can consume considerable resources inside the WAF. CPU time is the time the processor spends actually computing, as opposed to waiting. The default recommended configuration enables the JSON body processor through rule 200001, so a stock installation parses such bodies without any additional ruleset being present.
Impact
A malicious actor can cause a denial of service: requests are delayed for an excessive period while JSON parsing runs, and with enough simultaneous malicious requests the web server may be unable to serve legitimate requests in a timely manner. The effect is conditional on the installation actually parsing JSON bodies, that is, on the parser-activating rules being present and on ModSecurity having been built with yajl.
Fix
ModSecurity v2.9.5 and v3.0.6 add a configurable limit on the maximum depth accepted during JSON parsing. If the maximum is exceeded, JSON parsing halts immediately and ModSecurity sets its standard REQBODY_ERROR. REQBODY_ERROR is a flag the software raises to say that reading the request body failed, so rules and logs can react to it.
The default depth limit is 10,000, and it can be raised or lowered with a new configuration item:
SecRequestBodyJsonDepthLimit
This single line is the name of the setting you would put in your configuration file, followed by the number you want. The report does not show the full line with a value; it only names the setting. Most installations should find the default sufficient.
At the time of writing, v3.0.6 for Nginx and v2.9.5 for Apache were available immediately, and v2.9.5 for IIS was expected within a few days. Nginx, Apache and IIS are three common web server programs; ModSecurity plugs into each of them, and each needs its own build of the fix.
For users who cannot upgrade immediately, the report offers rule-based mitigations. There is no universally suitable substitute for upgrading, but some installations can reduce exposure:
- No web application accepts JSON request bodies. Disable any ModSecurity rules that activate the JSON parser. If nothing ever sends JSON, there is no reason to pay the cost of parsing it.
- Some applications accept JSON bodies, but they are all small. Create a
phase:1chained rule that rejects requests which would use the JSON parser whenContent-Lengthexceeds a certain size. A phase is a stage in ModSecurity's processing of a request, and phase 1 is the early stage where the request headers are examined. A chained rule is several conditions linked together, so the action only fires when all of them match.Content-Lengthis the header that states how many bytes the body contains. The report suggests 10,000 to 20,000 bytes is likely safe in most installations. Some v2 users may instead use theSecRequestBodyNoFilesLimitconfiguration item if other non-file request bodies are likewise expected to stay below similar thresholds. For ModSecurity versions before v2.9.3, the problem exists (for example in v2.9.2) but appears to require a larger request body to trigger problematic delays, so size limits can probably be larger, perhaps 40,000 to 50,000 bytes, and still provide decent protection in most installations. - A few applications expect larger JSON bodies. Use a rule as in case 2 but skip it where larger bodies are expected, either with an additional rule in the chain or by removing the rule at runtime (for example with
ctl:ruleRemoveById) once the relevant conditions have been met.ctlis a ModSecurity action that changes settings while a request is being processed, andruleRemoveByIdswitches off a rule by its number. The report notes this is not robust protection, but it may be reasonable against less-motivated attackers.
Variants to test
These are other angles a tester might explore, following the same idea:
- Other body processors with recursive formats. XML is another text format for structured data, also built from nested tags. A positive result is a measurable, depth-dependent delay in request handling.
- Non-default
Content-Typevalues. Where the JSON parser is optionally engaged for other content types, send the same deep nesting with those types. A positive result is the same delay withoutapplication/json. - Depth versus size trade-off. A positive result is a curve where delay grows faster than the body size the attacker must send.
- Concurrency threshold. A positive result is legitimate traffic failing while the malicious requests are in flight.
- Version-specific thresholds.
Glossary
- API: a way for one piece of software to ask another for data or actions, like a waiter taking an order to a kitchen.
- Body: the main content of a request, such as the text of a form or a block of JSON.
- Chained rule: several conditions in a WAF rule linked together so the action only happens when all of them match.
- Commenting out: putting a marker in front of a line of configuration so the software ignores it.
- Configuration file: a settings file that tells software which rules and options to use.
- Content-Length: a request header stating how many bytes the body contains.
- Content-Type: a request header stating what format the body is in, such as
application/json. - CPU time: the time a processor spends actually computing, as opposed to waiting.
- CVE: a public catalogue number for a known security flaw, so people can refer to the same issue.
- Denial of service (DoS): an attack that stops a service from working for legitimate users, without necessarily stealing anything.
- Header: a small label attached to a request that carries information about it.
- HTTP: the language browsers and web servers use to exchange requests and responses.
- IIS: a web server program made by Microsoft.
- JSON: a text format for structured data built from curly brackets, labels and values.
- Library: a reusable chunk of code that a program borrows instead of writing itself.
- Memory-safety bug: a flaw where a program mishandles its own memory, often allowing an attacker to run their own code.
- ModSecurity: an open-source web application firewall engine maintained by Trustwave.
- Nesting / depth: one structure placed inside another; depth is how many levels deep it goes.
- Nginx: a common web server program.
- Open source: software whose code is publicly available for anyone to inspect or contribute to.
- Parallelise: run many copies of the same action at the same time.
- Parser: a component that reads text and works out its structure.
- Phase: a stage in a WAF's processing of a request, such as examining headers early on.
- Request: what a browser sends to a website when you click a link or submit a form.
- REQBODY_ERROR: a ModSecurity flag raised when reading the request body fails.
- Rule: a condition in a WAF that decides whether a request is allowed or blocked.
- Ruleset: a collection of rules installed together.
- SecRequestBodyJsonDepthLimit: the ModSecurity setting that caps how deeply JSON may be nested.
- SecRequestBodyNoFilesLimit: a ModSecurity setting that caps the size of request bodies that are not file uploads.
- Web application firewall (WAF): software that sits in front of a web server and checks every request against rules, like a security guard at a door.
- XML: a text format for structured data built from nested tags.
- yajl: the library ModSecurity uses to parse JSON.