java - Malicious code vulnerability - Field should be package protected -
sonar giving me message:
malicious code vulnerability - field should package protected static array
formats.
why code considered malicious? have public class store constants.
public class constants { /* public static final constants of primitive datatypes there no sonar warning. */ public static final string[] formats = new string[] { "yyyy-mm-dd hh:mm:ss.s z", "yyyy-mm-dd hh:mm:ss.s" }
probably because piece of code execute:
constants.formats[0] = "some garbage"; and break rest of code.
in other words array constant not content.
examples of alternatives:
- you can store each format separate string constant
- you can use immutable list instead:
public static final list<string> formats = collections.unmodifiablelist(arrays.aslist("yyyy-mm-dd hh:mm:ss.s z", "yyyy-mm-dd hh:mm:ss.s")); make method:
public static string[] formats() { return new string[] { "yyyy-mm-dd hh:mm:ss.s z", "yyyy-mm-dd hh:mm:ss.s" }; }- ignore warning if confident (i) own code access class , (ii) there no way you/your colleagues think of reassigning 1 of values.
Comments
Post a Comment