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?