Reputation: 4896
I'm new to mongodb and I'm using this query in server side (node js),
collection.find({},function(e,crimes){
collection.col.aggregate([{$match: req.body},
{$group: { _id: "$district", count: {$sum: 1}}}
],function(e,docs){
console.log(docs);
res.render('postResults', {
"output" : docs
});
});
});
in the line collection.col.aggregate([{$match: req.body}
I directly inject an object sent from the client side with req.body
.
I want to know if this approach leads to a security vulnerability like sql injection. If yes, how serious is the vulnerability? Is it possible for an attacker to do operations other than read-only ones with this?
Thank you in advance..
Upvotes: 1
Views: 3009
Reputation: 13217
You might check these out to learn the issue;
Solutions;
$where
operator if the users interact with MongoDB.Good luck.
Upvotes: 2
Reputation: 11671
Yes, you can inject a different query by changing req.body
. For example, if you were looking up a specific crime, your req.body
might look like { "_id" : "123456" }
. But I could instead send { }
. Then every document would match and the aggregation pipeline would process every document, which might be sufficient to cause performance problems. Since the aggregation pipeline never alters the original documents, you can't change the data, so it's a read-only attack but it could still be used to choke up your servers with collection scans and large-volume aggregation pipelines.
Upvotes: 1
Reputation: 7244
The answer is "No", this does not cause any Query injection issues because your Node.js server has already turned the HTTP request into a JSON object and therefore any data entered by the user is a JavaScript string that is part of this JSON object. You are not concatenating strings like you might do in other languages that make them vulnerable to SQL injection attacks.
Upvotes: 0