I have the following Fortify security issue:
JSON Injection: Ensure that all serialization is performed using a safe serialization function that delimits untrusted data within single or double quotes and escapes any special characters.
Below is my code:
public String saveJson(String json, long ID, String userId) throws SQLException, JsonParseException, JsonMappingException, IOException
{
ObjectMapper objectMapper = new ObjectMapper();
List<item> listOfNewItems = objectMapper.readValue(json, new TypeReference<List<item>>(){});
userId= userFactory.getUser().getID();
String message = saveJson(listOfNewItems,ID,userId);
return message;
}
I am trying to maybe use
org.codehaus.jackson.io.JsonStringEncoder.getInstance().quoteAsString(json);
or maybe
objectMapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
but not sure?
More details on the error:
writes unvalidated input into JSON
Any ideas?
Jackson is a powerful and efficient Java library that handles the serialization and deserialization of Java objects and their JSON representations. It's one of the most widely used libraries for this task, and runs under the hood of many other frameworks.
ObjectMapper is the main actor class of Jackson library. ObjectMapper class ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions.
The comments so far from mikaelhg
and gagan singh
are correct:
Jackson ObjectMapper
on its default settings will already "Ensure that all serialization is performed using a safe serialization function that delimits untrusted data within single or double quotes and escapes any special characters."
The code you have shown is deserialization, not serialization (and/or is broken or incorrectly copied)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With