Versions Compared

Key

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

...

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]). The getValues() method gives the caller access to the hash table by returning a reference to it. An untrusted caller can use this 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, providing ample opportunities for race conditions.

...

OBJ05-EX0: When a method is called with only an unmodifiable immutable view of an object, that method may freely use the unmodifiable immutable view without defensive copying. This decision should be made early in the design of the API. Note that new callers of such methods must also expose only unmodifiable immutable views.

Risk Assessment

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.

...

Bibliography

[API 20062014]

Method clone()

[Bloch 2008]

Item 39, "Make Defensive Copies When Needed"

[Goetz 2006]

Section 3.2, "Publication and Escape: Allowing Internal Mutable State to Escape"

[Gong 2003]

Section 9.4, "Private Object State and Object Immutability"

[Haggar 2000]

Practical Java Praxis 64. Use clone for immutable objects when passing or receiving object references to mutable objects

[Pugh 2009] 

[Security 2006]

 

 

...