Note | ||
---|---|---|
| ||
This guideline has been deprecated. It has been merged with: 06/15/2015 -- Version 1.0 |
Immutability helps to support security reasoning. It is safe to share immutable objects without risk that the recipient can modify them [Mettler 2010].
Programmers often incorrectly assume that declaring a field or variable final
makes the referenced object immutable. Declaring variables that have a primitive type to be final
does prevent changes to their values after initialization (by normal Java processing). However, when the variable has a reference type, the presence of a final
clause in the declaration only makes the reference itself immutable. The final
clause has no effect on the referenced object. Consequently, the fields of the referenced object may be mutable. For example, according to the Java Language Specification, §4.12.4, "final
Variables" [JLS 2013],
If a
final
variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.
...
Similarly, a final
method parameter obtains an immutable copy of the object reference. Again, this has no effect on the mutability of the referenced data.
...
The Point
class is declared final
to prevent subclasses from overriding the clone()
method. This enables the class to be suitably used without any inadvertent modifications of the original object.
Noncompliant Code Example (Arrays)
This noncompliant code example uses a public static final
array, items
:
...
bgColor | #ffcccc |
---|
...
.
...
Clients can trivially modify the contents of the array even though declaring the array reference to be final
prevents modification of the reference itself.
Compliant Solution (Index Getter)
This compliant solution makes the array private
and provides public methods to get individual items and array size.
Code Block | ||
---|---|---|
| ||
private static final String[] items = {/* ... */};
public static final String getItem(int index) {
return items[index];
}
public static final int getItemCount() {
return items.length;
}
|
Providing direct access to the array objects themselves is safe because String
is immutable.
Compliant Solution (Clone the Array)
This compliant solution defines a private
array and a public
method that returns a copy of the array:
Code Block | ||
---|---|---|
| ||
private static final String[] items = {/* ... */};
public static final String[] getItems() {
return items.clone();
}
|
Because a copy of the array is returned, the original array values cannot be modified by a client. Note that a manual deep copy could be required when dealing with arrays of objects. This generally happens when the objects do not export a clone()
method. Refer to OBJ06-J. Defensively copy mutable inputs and mutable internal components for more information.
As before, this method provides direct access to the array objects themselves, but this is safe because String
is immutable. If the array contained mutable objects, the getItems()
method could return an array of cloned objects instead.
Compliant Solution (Unmodifiable Wrappers)
This compliant solution declares a private
array from which a public
immutable list is constructed:
...
...
bgColor | #ccccff |
---|
...
private static final String[] items = { ... };
public static final List<String> itemsList =
Collections.unmodifiableList(Arrays.asList(items));
Neither the original array values nor the public
list can be modified by a client. For more details about unmodifiable wrappers, refer to OBJ56-J. Provide sensitive mutable classes with unmodifiable wrappers. This solution can also be used when the array contains mutable objects.
Applicability
Incorrectly assuming that final
references cause the contents of the referenced object to remain mutable can result in an attacker modifying an object believed to be immutable.
...
Item 13, "Minimize the Accessibility of Classes and Members" | |||
Chapter 6, "Interfaces and Inner Classes" | |||
[JLS 2013] | Class Properties for Security Review in an Object-Capability Subset of Java |
...