我正在尝试创建一个预处理程序,在写入MongoDB之前清理所有数据,请参阅:http://mongoosejs.com/docs/middleware.html
我尝试过以下方法让每个房产都能清理它:
blogSchema.pre('save', function (next) {
var obj = this;
console.log(obj)//-> https://gist.github.com/daslicht/70e0501acd6c345df8c2
// I've tried the following to get the single items :
Object.keys(obj).forEach(function (key) {
console.log('Keys: ',obj[key]);
});
//and:
for(var key in obj) {
console.log(obj[key])
}
//and:
_.each( self , function(value, key, list){
console.log('VALUE:',key);
})
next();
})
Run Code Online (Sandbox Code Playgroud)
上述任何一种方法都会产生如下情况:
这是输出:
for(var key in obj) {
console.log(obj[key])
}
Run Code Online (Sandbox Code Playgroud)
https://gist.github.com/daslicht/cb855f53d86062570a96
有谁知道如何获得每个单一的财产,以便我可以消毒它,好吗?
〜马克
[编辑]这是一个可能的解决方法,无论如何,将它直接放在Scheme级别会更干净,因为这样会更干
var post = {
createdAt : req.body.date,
createdBy : req.user.username,
headline : req.body.headline,
content : req.body.content …Run Code Online (Sandbox Code Playgroud) 我正在为 NodeJS使用Mongoose ODM 包装器,但我担心注入攻击。假设我有以下架构:
const UserSchema = new mongoose.Schema({ userName: String, password: String });
Run Code Online (Sandbox Code Playgroud)
如果我要执行如下所示的登录请求:
router.post('/login', (request, response) => {
const userName = request.body.userName;
const password = request.body.password;
User.findOne({ userName: userName }, function (error, user) {
// ... check password, other logic
});
});
Run Code Online (Sandbox Code Playgroud)
我会接受以下 JSON 有效负载的注入攻击,它总是会找到一个用户:
{
"email": { "$gte": "" },
"password": { "$gte": "" }
}
Run Code Online (Sandbox Code Playgroud)
我不关心密码,因为如果找到阻止任何实际登录的用户,它会被散列,但我想确保我的输入被清理,这样攻击者甚至不会到达那个点。
我知道在类似的StackOverflow 帖子中引用的mongo-sanitize NPM 包似乎删除了所有以“$”开头的 JSON 键。无论如何我都打算使用它,但我永远不会允许用户提交原始的、未解析的 JSON。在这种情况下,假设我做了正确的检查,在 userName 上调用 toString() 是一种好习惯吗?null
const userName = …Run Code Online (Sandbox Code Playgroud)