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:
My pretty personal gut feeling says: Don't bother. But then again, if you do nonetheless or even have to then here are a few suggestions for how to deal with this requirement: