Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot

...

Code Block
bgColor#FFCCCC
class ReturnRef {
  // Internal state, may contain sensitive data
  Hashtable&lt;Integer,String&gt;Hashtable<Integer,String> ht = new Hashtable&lt;Integer,String&gt;Hashtable<Integer,String>(); 
 
  private ReturnRef() {
    ht.put(1, &quot;"123-45-6666&quot;");
  }
 
  public Hashtable&lt;Integer,String&gt;Hashtable<Integer,String> getValues(){ 
    return ht;
  }
 
  public static void main(String[] args) {
    ReturnRef rr = new ReturnRef();
    Hashtable&lt;IntegerHashtable<Integer, String&gt;String> ht1 = rr.getValues(); // Prints sensitive data 123-45-6666
    ht1.remove(1); // Untrusted caller can remove entries
    Hashtable&lt;IntegerHashtable<Integer, String&gt;String> ht2 = rr.getValues(); // Now prints null, original entry is removed
  }	
}

...

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

public static void main(String[] args) {
  ReturnRef rr = new ReturnRef();
  Hashtable&lt;Integer,String&gt;Hashtable<Integer,String> ht1 = rr.getValues(); // prints non sensitive data
  ht1.remove(1); // untrusted caller can remove entries only from the copy
  Hashtable&lt;Integer,String&gt;Hashtable<Integer,String> ht2 = rr.getValues(); // prints non sensitive data     
}

...

Wiki Markup
\[[API 06|AA. Java References#API 06]\] [method clone()|http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()]
\[[Security 06|AA. Java References#Security 06]\]
\[[Bloch 08|AA. Java References#Bloch 08]\] Item 39: Make defensive copies when needed
\[[SCG 07|AA. Java References#SCG 07]\] Guideline 2-1 Create a copy of mutable inputs and outputs
\[[Haggar 00|AA. Java References#Haggar 00]\] [Practical Java Praxis 64: Use clone for Immutable Objects When Passing or Receiving Object References to Mutable Objects|http://www.informit.com/articles/article.aspx?p=20530]
\[[Goetz 06|AA. Java References#Goetz 06]\] 3.2. Publication and Escape: Allowing Internal Mutable State to Escape
\[[Gong 03|AA. Java References#Gong 03]\] 9.4 Private Object State and Object Immutability
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 375|http://cwe.mitre.org/data/definitions/375.html] &quot;"Passing Mutable Objects to an Untrusted Method&quot;"

...

OBJ36-J. Provide mutable classes with a clone method to allow passing instances to untrusted code safely&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      08. Object Orientation (OBJ)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      OBJ38-J. Immutable classes must prohibit extension