Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Returning references to internal mutable members of a class can compromise an application's security, both by breaking encapsulation and also by providing the opportunity to corrupt the internal state of the class (whether accidentally or maliciously). Therefore, programs must not return references to internal mutable classes.

Returning a reference to a defensive copy of a reference to a mutable internal state ensures that the caller can only modify the copy and not cannot modify the original internal state, although the copy remains mutable.

Noncompliant Code Example

...

This compliant solution returns a clone of the Date object from the getDate() accessor method. This is safe because while While Date can be extended by an attacker, this is safe because the Date object returned by getDate() is controlled by MutableClass and is known not to be a malicious nonmalicious subclass.

Code Block
bgColor#ccccff
protected Date getDate() {
  return (Date)d.clone();
}

Note that defensive copies performed during execution of a constructor must avoid use of the clone() method when the class could be subclassed by untrusted code. This restriction prevents execution of a maliciously crafted overriding of the clone() method. For more details, see rule " OBJ00-J. Limit extensibility of classes and methods with invariants to trusted subclasses only."

Classes that have public setter methods must follow the related advice found in rule " OBJ06-J. Defensively copy mutable inputs and mutable internal components. " Note that setter methods can (and usually should) perform input validation and sanitization before setting internal fields.

...

In this noncompliant code example, the getDate() accessor method returns an array of Date objects. The method fails to make a defensive copy of the array before returning it. Because the array contains references to Date objects that are mutable, a shallow copy of the array would be is insufficient because an attacker can modify the Date objects in the array.

...

In this noncompliant code example, class ReturnRef contains a private Hashtable instance field. The hash table stores immutable but sensitive data (for example, social security numbers SSNs). A getter method getValues() gives the caller access to the hash table by returning a reference to it. An untrusted caller can use the getter method to gain access to the hash table; as a result, hash table entries can be maliciously added, removed, or replaced. Furthermore, multiple threads can perform these modifications can be performed by multiple threads, providing ample opportunities for race conditions.

...

Code Block
bgColor#ccccff
class ReturnRef {
  // ...
  private Hashtable<Integer,String> getValues(){
    return (Hashtable<Integer, String>)ht.clone(); // shallow copy
  }

  public static void main(String[] args) {
    ReturnRef rr = new ReturnRef();
    Hashtable<Integer,String> ht1 = rr.getValues(); // prints non-sensitive data
    Hashtable<Integer,String> ht1 = rr.removegetValues(1); 
    // untrusted caller can only modify copy
                   ht1.remove(1); 
    // untrustedprints caller can only modify copynon-sensitive data
    Hashtable<Integer,String> ht2 = rr.getValues(); // prints non-sensitive data
  }
}

When a hash table contains references to mutable data such as a series of Date objects, each of those objects must also be copied by using a copy constructor or method. For further details, refer to rules "OBJ06-J. Defensively copy mutable inputs and mutable internal components" and " OBJ04-J. Provide mutable classes with copy functionality to safely allow passing instances to untrusted code."

Note that making deep copies of the keys of a hash table do not need to be deep copiedis unncessary; shallow copying of the references suffices because a hash table's contract dictates that its keys must produce consistent results to the equals() and hashCode() functions. Mutable objects whose equals() or hashCode() method results may be modified are not suitable keys.

...

Returning references to internal object state (mutable or immutable) can render an application susceptible to information leaks and corruption of its objects' states, which , consequently , violates class invariants. Control flow can also be affected in some cases.

...

CERT C++ Secure Coding Standard

OOP35-CPP. Do not return references to private data.

MITRE CWE

CWE-375, ". Returning a Mutable Object mutable object to an Untrusted Caller" untrusted caller

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d0fd8e00f037fdc1-2ebdab83-456a43d5-b503a5ee-3156f95b1133aaafabf9d61b"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

[method 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="ca888d34696c2167-87d91c82-4d4f4ee1-bb4e9e10-cc6dd2c2a36d49cf5dd12973"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch 08]]

Item 39: . Make defensive copies when needed

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="36747461bfa5c80f-8f0efe7a-4c2148c9-981eb986-d0376ac8ef4d63daec8c33f7"><ac:plain-text-body><![CDATA[

[[Goetz 2006

AA. Bibliography#Goetz 06]]

3.2. , Publication and Escape: Allowing Internal Mutable State to Escape

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="915f45854fc039a0-6e05b64d-4fb1404b-a136ae96-3f701ba8981b18ea1ec3a569"><ac:plain-text-body><![CDATA[

[[Gong 2003

AA. Bibliography#Gong 03]]

9.4, Private Object State and Object Immutability

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="bb5df7780626fa3c-817ebfde-460b4f00-83569afe-c04ce2fb861e0d6e037a9ad7"><ac:plain-text-body><![CDATA[

[[Haggar 2000

AA. Bibliography#Haggar 00]]

[Practical Java Praxis 64: . Use clone for Immutable Objects When Passing or Receiving Object References to Mutable Objectsimmutable objects when passing or receiving object references to mutable objects

http://www.informit.com/articles/article.aspx?p=20530]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ec129c9ac0b06a65-6c2645ab-41c14b33-9aa2bd66-15db3aa8c757c7bb28e708cd"><ac:plain-text-body><![CDATA[

[[Security 2006

AA. Bibliography#Security 06]]

 

]]></ac:plain-text-body></ac:structured-macro>

...