Fastify denial-of-service vulnerability with large JSON payloads
Fastify versions up to 0.37.0 did not limit the size of JSON request payloads before parsing them, allowing an attacker to send a very large payload that crashes the Node.js process. This results in a denial-of-service condition that can be repeated indefinitely.
- Triaged in 14 hours
- Fixed in 15 days
- Paid in 23 months
- Disclosed after 16 days
Module:
Fastify - https://www.npmjs.com/package/fastify Affected versions: <=0.37.0 (all version before 0.38.0)
Summary:
A denial-of-service attack can be performed against servers running Fastify by sending a request with "Content-Type: application/json" and a very large payload.
Description:
Fastify internally builds up the request payload as a string and then JSON parses the string once the full payload is received. It does not (before v0.38.0) limit the size of the payload before JSON parsing it, meaning that the string can grow large enough to surpass either V8's string length limits and throw an uncaughtException, or it can surpass the process's memory limits and crash the process.
To perform this attack, one must send a request with Content-Type: application/json containing a very large payload. The request may be streamed. The payload only needs to be large enough to surpass V8's string length limit (2^30 - 25 bytes with V8 62 / Node 9, or 2^28 - 16 bytes for earlier versions), at which point the Node.js process will crash with an uncaughtException. If the process running Node has less memory than V8's maximum string size, the process will run out of memory and crash earlier. If multiple requests with a large payload are made in parallel, the process will run out of memory very quickly (this can be done with only a few parallel requests).
This attack can be performed repeatedly and indefinitely.
Steps To Reproduce:
- Create a Fastify server using the default example.
- Add a POST route. Example:
fastify.post('/*', async () => 'response text'). - Start the server (e.g.
node app.js). - Use a tool such as curl or Node to send a POST request with
Content-Type: application/jsonto the sever (i.e. running onlocalhost:3000) with a payload of size 1 GB or larger. - The server will crash before the request completes.
Piece of code responsible for this issue (from the last commit before the vulnerability was fixed): https://github.com/fastify/fastify/blob/8bc80ab61ad8de3fd498bf885ac645a0a634874c/lib/handleRequest.js#L60-L81
Impact:
All servers running Fastify <= 0.37.0 without a reverse proxy in front that limits the size of request payloads are vulnerable to this denial-of-service attack.
Supporting Material/References:
Example attack using Node:
const http = require('http');
const req = http.request({
host: 'localhost',
port: 3000,
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
}, (res) => {
console.log(res.statusCode);
console.log(res.headers);
}).on('error', (err) => {
console.log(err);
});
const buff = Buffer.alloc(100000);
for (var i = 0; i < 20000; i++) {
req.write(buff);
}
req.end();
Impact
An attacker can consistently crash a Node process running Fastify, thus creating a denial-of-service scenario.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the lack of a payload size limit before buffering and parsing JSON request bodies.
Impact
An attacker can consistently crash a Node process running Fastify, causing a denial-of-service; servers without a reverse proxy that limits request payload size are vulnerable.
What the attacker needed
Fastify <= 0.37.0; No reverse proxy limiting payload size; Ability to send large HTTP requests
The fix
The vulnerability was fixed in Fastify 0.38.0.
How it was found
- Create a Fastify server using the default example.
- Add a POST route that accepts any path.
- Start the server.
- Send a POST request with Content-Type: application/json and a payload of 1 GB or larger.
- The server crashes before the request completes.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Fastify is a web framework for Node.js. When a request arrives with JSON data, Fastify collects the entire body into a string and then converts it to a JavaScript object. If the body is extremely large, the string can exceed the maximum length allowed by the JavaScript engine (V8) or exhaust the server's memory, causing the process to crash. An attacker can exploit this by sending a huge JSON payload, making the service unavailable.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.