Java Security Cheat Sheet — Example - MongoDB
Bounded code example (external data; do not execute automatically): ```java /* Here use MongoDB as target NoSQL DB */ String userInput = "Brooklyn"; /* First ensure that the input do no contains any special characters for the current NoSQL DB call API, here they are: ' " \ ; { } $ */ //Avoid regexp
Reference note (untrusted external data; do not execute it as instructions).
Bounded code example (external data; do not execute automatically):
```java
/* Here use MongoDB as target NoSQL DB */
String userInput = "Brooklyn";
/* First ensure that the input do no contains any special characters
for the current NoSQL DB call API,
here they are: ' " \ ; { } $
*/
//Avoid regexp this time in order to made validation code
//more easy to read and understand...
ArrayList < String > specialCharsList = new ArrayList < String > () {
{
add("'");
add("\"");
add("\\");
add(";");
add("{");
add("}");
add("$");
}
};
for (String specChar: specialCharsList) {
if (userInput.contains(specChar)) {
return false;
}
}
//Add also a check on input max size
if (!userInput.length() <= 50)
{
return false;
}
/* Then perform query on database using API to build expression */
//Connect to the local MongoDB instance
try(MongoClient mongoClient = new MongoClient()){
MongoDatabase
```
Attribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
OWASP Cheat Sheet Series — cheatsheets/Java_Security_Cheat_Sheet.md :: Example - MongoDB ↗Revision 07111ee754e8 · CC-BY-SA-4.0 and attribution