We have a REST API for querying records in a MongoDB. Very simple, something along the following:
GET /api/items?q=foo
During development, it was convenient to allow regular expressions as the query q
. We would simply pass the query parameter to a MongoDB $regex
operator and not do any escaping:
db.getCollection('items').find({ name: { $regex: req.query.q, $options: 'i' } });
Thus we have a very flexible and convenient way of querying our data. Now, that things are getting “serious” i.e. closer to production, I'm asking myself about the security implications. Could someone send “DoS” queries with expensive backtracking?
I’m probably not destructive enough to think of such a query, so I’ve searched the Internet and came across this very interesting read, which mentions several attacks: The Explosive Quantifier Trap.
Discarding the fact, that the mentioned queries on the above page behave far from “catastrophic” as expected (neither in a MongoDB query, nor in online tools such as regex101.com), I’d still like to know:
- Is this a real issue or am I chasing non-existent threats?
- Should we better get away from the regex parameters entirely?
- Does MongoDB have any mechanism (i.e. timeout) to prevent DoS attacks through malicious regexes? (fwiw: we’re running in a Node.js environment)
- Are there any libraries to detect such attacks before issuing a query?