Fastify 相当于 express-mongo-sanitize
Fastify equivalent of express-mongo-sanitize
您好 Fastify 专家,
在 MongoDB 查询中,我可以传递各种运算符,这可能会因具有各种攻击面而带来安全方面的风险。
所以在发送有效负载之前,我想清理 query/filters/sort 等。但是我认为我不需要像这样清理请求有效负载,因为 Mongo 无论如何都会将其存储为BSON,因此更安全。
现在在 Express 世界中,我们曾经有 express-mongo-sanitize 类插件。
您为 Fastify world 推荐了哪些开源插件来实现类似的功能?
谢谢,
普拉迪普
您有两个选择:
- 使用模式驱逐:将
additionalProperties
作为标志添加到输入模式中,将从输入中删除所有您不期望的键
使用此代码,您可以提交有效负载:
{
foo: 'hello',
$where: 'cut'
}
并且 $where
密钥将被删除。
const fastify = require('fastify')({ logger: true })
fastify.post('/', {
schema: {
body: {
type: 'object',
additionalProperties: false,
properties: {
foo: { type: 'string' }
}
}
}
},
async (request, reply) => {
console.log(request.body)
return request.body
})
fastify.listen(8080)
- 您链接的框架有一个 module feature,您可以将其与挂钩集成:
const mongoSanitize = require('express-mongo-sanitize');
fastify.addHook('preHandler', function hook (request, reply, done) {
mongoSanitize.sanitize(request.body);
done(null)
})
您好 Fastify 专家,
在 MongoDB 查询中,我可以传递各种运算符,这可能会因具有各种攻击面而带来安全方面的风险。
所以在发送有效负载之前,我想清理 query/filters/sort 等。但是我认为我不需要像这样清理请求有效负载,因为 Mongo 无论如何都会将其存储为BSON,因此更安全。
现在在 Express 世界中,我们曾经有 express-mongo-sanitize 类插件。
您为 Fastify world 推荐了哪些开源插件来实现类似的功能?
谢谢, 普拉迪普
您有两个选择:
- 使用模式驱逐:将
additionalProperties
作为标志添加到输入模式中,将从输入中删除所有您不期望的键
使用此代码,您可以提交有效负载:
{
foo: 'hello',
$where: 'cut'
}
并且 $where
密钥将被删除。
const fastify = require('fastify')({ logger: true })
fastify.post('/', {
schema: {
body: {
type: 'object',
additionalProperties: false,
properties: {
foo: { type: 'string' }
}
}
}
},
async (request, reply) => {
console.log(request.body)
return request.body
})
fastify.listen(8080)
- 您链接的框架有一个 module feature,您可以将其与挂钩集成:
const mongoSanitize = require('express-mongo-sanitize');
fastify.addHook('preHandler', function hook (request, reply, done) {
mongoSanitize.sanitize(request.body);
done(null)
})