...
Trusted callers can be trusted to use the provided copy functionality to make defensive copies before passing object instances to untrusted code. Untrusted callers cannot be trusted to make such defensive copies. Consequently, providing copy functionality does not obviate the need for making defensive copies of inputs received from, or outputs returned to, untrusted code.
Noncompliant Code Example
In this noncompliant code example, MutableClass
uses a mutable field date
of type Date
. Class Date
is also a mutable class. The example is noncompliant because the MutableClass
objects lack copy functionality.
...
When a trusted caller passes an instance of MutableClass
to untrusted code, and the untrusted code modifies that instance (perhaps by incrementing the month or changing the timezone), the state of the object can be inconsistent with respect to its previous state. Similar problems can arise in the presence of multiple threads, even in the absence of untrusted code.
Compliant Solution (Copy Constructor)
This compliant solution uses a copy constructor that initializes a MutableClass
instance when an argument of the same type (or subtype) is passed to it.
...
This approach is useful when the instance fields are declared final
. Callers request a copy by invoking the copy constructor with an existing MutableClass
instance as its argument.
Compliant Solution (Public Static Factory Method)
This compliant solution exports a public static factory method getInstance()
that creates and returns a copy of a given MutableClass
object instance.
...
This approach is useful when the instance fields are declared final
.
Compliant Solution (clone()
)
This compliant solution provides the needed copy functionality by declaring MutableClass
to be final, implementing the Cloneable
interface, and providing an Object.clone()
method that performs a deep copy of the object.
...
Defensive copies are unnecessary if untrusted code always invokes an object's clone()
method on a mutable state received from mutable classes and operated only on the cloned copy. Unfortunately, untrusted code has little incentive to do so, and malicious code has every incentive to misbehave. This compliant solution provides a clone()
method to trusted code and also guarantees that the state of the object cannot be compromised when the accessor methods are called directly from untrusted code.
Compliant Solution (clone()
with final
members)
When a mutable class's instance fields are declared final
and lack accessible copy methods, provide a clone()
method, as shown in this compliant solution.
...
Mutable classes that define a clone()
method must be declared final
.
Compliant Solution (unmodifiable Date wrapper)
If cloning or copying a mutable object is infeasible or expensive, one alternative is to create an unmodifiable view class. This class overrides mutable methods to throw an exception, protecting the mutable class.
Code Block | ||
---|---|---|
| ||
class UnmodifiableDateView extends Date { private Date date; public UnmodifiableDateView(Date date) { this.date = date; } public void setTime(long date) { throw new UnsupportedOperationException(); } // Override all other mutator methods to throw UnsupportedOperationException } public final class MutableClass { private Date date; public MutableClass(Date d) { this.date = d; } public void setDate(Date d) { this.date = (Date) d.clone(); } public UnmodifiableDateView getDate() { return new UnmodifiableDateView(date); } } |
Exceptions
OBJ08-EX1: Sensitive classes should not be cloneable, per rule "OBJ03-J. Sensitive classes must not let themselves be copied."
Risk Assessment
Creating a mutable class without providing copy functionality can result in the data of its instance becoming corrupted when the instance is passed to untrusted code.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
OBJ08-J | low | likely | medium | P6 | L2 |
Automated Detection
Sound automated detection appears to be infeasible in the general case. Heuristic approaches could be useful.
Related Guidelines
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ab175ce9662e5947-b0125d74-438049c2-b9ff8245-13a2d54c8ac981ebba565d94"><ac:plain-text-body><![CDATA[ | [[MITRE 2009 | AA. Bibliography#MITRE 09]] | [CWE-374 | http://cwe.mitre.org/data/definitions/374.html] "Passing Mutable Objects to an Untrusted Method" and [CWE-375 | http://cwe.mitre.org/data/definitions/375.html] "Returning a Mutable Object to an Untrusted Caller" | ]]></ac:plain-text-body></ac:structured-macro> |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a0c82069527c6e70-0163a35d-48b54cb9-af51a7b4-ecd5a8a918d8d41153aaa198"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [method clone() | http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a3183acfef3af50d-b54a0b56-42ec4166-bca5a762-5bf1a1f638a781d827680b3b"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 39: Make defensive copies when needed and Item 11: Override clone judiciously | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="557751d832357dbd-159a328f-4ee64932-a0538392-5056cc655ceb98b223c51e66"><ac:plain-text-body><![CDATA[ | [[SCG 2007 | AA. Bibliography#SCG 07]] | Guideline 2-2 Support copy functionality for a mutable class | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="592ceaef4d816749-21b45410-4f534abe-a799a98f-6f57d6b269f4b770c5642330"><ac:plain-text-body><![CDATA[ | [[SCG 2009 | AA. Bibliography#SCG 09]] | Guideline 2-3 Support copy functionality for a mutable class | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a158291285d44a9d-14f24a7e-44424e58-8adf92a4-46a2eee8385e4c3350fe52f4"><ac:plain-text-body><![CDATA[ | [[Security 2006 | AA. Bibliography#Security 06]] | ]]></ac:plain-text-body></ac:structured-macro> |
...