The code contains a class with sensitive data, but the class is cloneable. The data can then be accessed by cloning the class.
Cloneable classes are effectively open classes, since data cannot be hidden in them. Classes that do not explicitly deny cloning can be cloned by any other class without running the constructor.
Threat Mapped score: 0.0
Industry: Finiancial
Threat priority: Unclassified
N/A
N/A
Phase | Note |
---|---|
Implementation | N/A |
Intro: The following example demonstrates the weakness.
Body: Make classes uncloneable by defining a clone function like:
public class CloneClient { public CloneClient() //throws java.lang.CloneNotSupportedException { Teacher t1 = new Teacher("guddu","22,nagar road"); //... // Do some stuff to remove the teacher. Teacher t2 = (Teacher)t1.clone(); System.out.println(t2.name); } public static void main(String args[]) { new CloneClient(); } } class Teacher implements Cloneable { public Object clone() { try { return super.clone(); } catch (java.lang.CloneNotSupportedException e) { throw new RuntimeException(e.toString()); } } public String name; public String clas; public Teacher(String name,String clas) { this.name = name; this.clas = clas; } }