...
Some objects appear to be immutable because they have no mutator methods. For example, the java.lang.CharacterSequence
interface describes an immutable sequence of characters. It should be noted that if the underlying implementation on which the CharacterSequence
is based changes, the value of the CharacterSequence
also changes. Such objects must be defensively copied before use. It is also permissible to use the toString()
method to make them immutable before passing them as parameters.
To avoid exposing mutable fields by storing them in static
variables, creating defensive copies of the fields is highly recommended.
Risk Assessment
Failing to create a copy of a mutable input may enable an attacker to exploit a TOCTOU vulnerability and at other times, expose internal mutable components to untrusted code.
...