There are the following risks for a service like an online coding platform using MongoDB as a data store:
Code injection
An attacker injects code that is then interpreted/executed by the application.
For example, JavaScript eval() method:
let str = '2 + 2';
console.log(eval(str)); //4
If str is provided by user, an attacker can pass some malicious code.
In Java it is not common to execute or evaluate scripts.
So, unless you execute code that users provide, you are safe.
While it is still possible to execute user provided code on the server side like https://ideone.com/ does. For this code must be executed in isolated sandbox, e.g. isolated Docker container.
Also, you can Gzip code provided by user and/or encode in Base64 and store it this way in the DB.
NoSQL injection
The following query is vulnerable to injection:
const query = {
username: req.body.username,
password: req.body.password
}
db.collection('users').findOne(query, function (err, user) {
console.log(user);
});
because a user can pass the following parameters:
{
"username": {"$ne": null},
"password": {"$ne": null}
}
and the query will return the first user without knowing his username or password.
Especially dangerous when JavaScript is also evaluated to allow more advanced conditions.
db.myCollection.find({
$where: function() {
return obj.credits - obj.debits < 0;
}
});
More details in https://zanon.io/posts/nosql-injection-in-mongodb
The solution is to only accept strings from users and do not accept objects or sanitize an input (for example, using mongo-sanitize). It is mostly related to Node.js. MongoDB Java Driver provides another API that by design solves some types of injections (similar to JDBC PreparedStatement):
collection.find(and(gt("i", 50), lte("i", 100))).forEach(printBlock);
Moreover, if you use Morphia, a Java object-document mapper, you are safe.
Using components with known vulnerabilities
Attacker can try to exploit a vulnerability in the platform or framework you use (JDK, Spring Framework etc.) For example, the famous Heartbleed vulnerability in OpenSSl.
So, it is very important to use the latest version of platforms and frameworks and apply all security patches.
There is a great tool for scanning Java libraries using Maven or Gradle plugin or even using CLI - OWASP Dependency Check. It automatically scans all the dependencies and prepares and alerts if vulnerabilities are found.
Insecure deserialization
It is important to use some well-known data format like JSON and use mature and stable library for deserialization (Jackson or Gson) instead of parsing input manually, especially using eval() JavaScript method or alternative in other languages.