Fastify uses allErrors: true ajv configuration by default which is susceptible to DoS
Fastify enables the ajv option allErrors by default, so validation continues after the first failure instead of stopping early. An attacker could send large crafted JSON bodies that make validation slow, causing a denial of service even when the schema author added length limits to prevent this.
- Fixed in 9 days
- Paid in 9 days
- Disclosed after 39 days
I would like to report a denial of service vulnerability in fastify It allows to cause a DoS with some schemas that were otherwise assumed to be secure against DoS by their authors
Module
module name: fastify
version: 2.14.1, 3.0.0-rc.4
npm page: https://www.npmjs.com/package/fastify
Module Description
An efficient server implies a lower cost of the infrastructure, a better responsiveness under load and happy users.
Module Stats
114 076 weekly downloads
Vulnerability
Vulnerability Description
See https://github.com/ajv-validator/ajv#security-risks-of-trusted-schemas:
Please note: The suggestions above to prevent slow validation would only work if you do NOT use
allErrors: truein production code (using it would continue validation after validation errors).
fastify uses allErrors: true by default which makes it susceptible to DoS attacks even when schemas are otherwise safe.
E.g. a (sub-)schema { uniqueItems: true, maxItems: 10 } is otherwise safe against DoS as maxItems is checked first and validation fails there on long arrays, but that applies to only not in allErrors: true case.
Neither https://github.com/fastify/fastify/blob/master/docs/Validation-and-Serialization.md nor https://github.com/fastify/fastify/blob/master/docs/Recommendations.md mentions this directly.
Introduced in https://github.com/fastify/fastify/pull/1398
Steps To Reproduce:
/* Client */
const fetch = require('node-fetch')
const request = body => {
const json = JSON.stringify(body)
console.log(`Payload size: ${Math.round(json.length / 1024)} KiB`)
return fetch('http://127.0.0.1:3000/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: json
})
}
const fireRequests = async () => {
await request({ string: '@'.repeat(90000) })
await request({ array: Array(20000).fill().map(() => ({x: Math.random().toString(32).slice(2)})) })
}
/* Server */
const fastify = require('fastify')({ logger: true })
const schema = {
body: {
type: 'object',
properties: {
array: { uniqueItems: true, maxItems: 10 },
string: { pattern: "^[^/]+@.+#$", maxLength: 20 },
}
},
}
fastify.post('/', { schema }, (request, reply) => {
reply.send({ hello: 'world', body: request.body })
})
fastify.listen(3000, (err, address) => {
fastify.log.info(`server listening on ${address}`)
fireRequests()
})
https://gist.github.com/ChALkeR/15e758d3fc5cbba0840b6a03a070c838
Patch
Revert https://github.com/fastify/fastify/pull/1398
Work-around
Use https://github.com/fastify/fastify/blob/master/docs/Server.md#ajv to override allErrors to false in ajv configuration.
Wrap up
- I contacted the maintainer to let them know: N
- I opened an issue in the related repository: N
Impact
Cause DoS in a presence of potentially slow pattern / format or uniqueItems in the schema, even when schema author guarded that with a length check to be otherwise immune to DoS.
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
Impact
An attacker could cause a denial of service when a schema contains a potentially slow pattern or format or uniqueItems, even if the schema author added a length check to be otherwise immune.
What the attacker needed
Server uses default ajv config; schema has slow pattern or uniqueItems; attacker can send large JSON bodies
The fix
The report suggests reverting the change that introduced the default, and as a work-around the ajv configuration can be overridden to set allErrors to false.
How it was found
- Start a Fastify server with a schema that has a uniqueItems and maxItems rule on an array and a pattern and maxLength rule on a string.
- Send a JSON body with a very long string that fails the pattern rule.
- Send a JSON body with a large array of objects that fails the uniqueItems rule.
- The large inputs are validated with allErrors enabled, which the report says makes the server susceptible to DoS.
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.
A web server checks incoming data against a schema, a set of rules describing what valid data looks like. Normally the checker stops at the first rule that fails, but Fastify tells it to keep checking every rule, so a huge input can make the server work much harder than expected. An attacker can send such inputs repeatedly and tie up the server so real users cannot be served.
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.