Breaking News

Understanding NoSQL Injection and How to Prevent it

This article starts with "once upon a time" when I was learning MongoDB and thought that the schema less feature could be more secure than SQL Databases (SQL Injections). So I migrated all my projects to MongoDB and for the past few months I have been working on NoSQL Injection and writing a series of tutorials on it.

What Is An Injection

An injection is a security vulnerability that lets attackers take control of database queries through the unsafe use of user input. It can be used by an attacker to expose unauthorized information and odify data.

Let me give you a glimpse of NoSQL Injection first. Suppose, your application is accepting JSON username and password, so it can be bypassed by:

Now if on the backend you are using:

Then your application is vulnerable to NoSQL Injection.

How? Let's substitute those values:

Now, if there is at least one document in the collection that doesn't have the same username and password as the attacker has passed, it can log in to your web application with the very first document that matches this criterion

Practical Example: https://mongoplayground.net/p/omLJSlWfR-w

Preventing NoSQL

There is only one thing you can do, "SANITIZATION" by casting the input to in specific type. Like in this case, casting username and password to String() would work. As you know String() on any object would be [object Object] so I am directly substituting the value here:

Model.findOne({
  username: "[object Object]",
  password: "[object Object]"
})

In production, this would be the rarest document in the collection.

Practical Demonstration: https://mongoplayground.net/p/XZKEXaypJjQ

ExpressJS Middle-Ware Approach

Four months ago I had created a question StackOverflow (https://stackoverflow.com/questions/59394484/expressjs-set-the-depth-of-json-parsing), to which a user named x00 posted the answer about the solution of setting up the depth of parsing nested JSON body.

Practical Demonstration:

Or if you want to use [object Object] notation to prevent application crash. I personally recommend you to use this one:

Middle-ware in action

Practical Demonstration: https://repl.it/@tbhaxor/Preventing-NoSQL-Injection-in-Express

If you have some other cool ideas, I would love to hear from you. You can either comment down here or contact me at the following

References


No comments

Please do not enter any spam link in comment box.