Description
Inner classes are translated into classes that are accessible at package scope and may expose code that the programmer intended to keep private to attackers.
Inner classes quietly introduce several security concerns because of the way they are translated into Java bytecode. In Java source code, it appears that an inner class can be declared to be accessible only by the enclosing class, but Java bytecode has no concept of an inner class, so the compiler must transform an inner class declaration into a peer class with package level access to the original outer class. More insidiously, since an inner class can access private fields in its enclosing class, once an inner class becomes a peer class in bytecode, the compiler converts private fields accessed by the inner class into protected fields.
Potential Impact
Confidentiality
Read Application Data
Demonstrative Examples
public final class urlTool extends Applet {private final class urlHelper {...}...}public class OuterClass {
// private member variables of OuterClass
private String memberOne;private String memberTwo;
// constructor of OuterClass
public OuterClass(String varOne, String varTwo) {this.memberOne = varOne;this.memberTwo = varTwo;}
// InnerClass is a member inner class of OuterClass
private class InnerClass {private String innerMemberOne;
public InnerClass(String innerVarOne) {this.innerMemberOne = innerVarOne;}
public String concat(String separator) {
// InnerClass has access to private member variables of OuterClass
System.out.println("Value of memberOne is: " + memberOne);return OuterClass.this.memberTwo + separator + this.innerMemberOne;}}}public class OuterClass {
// private member variables of OuterClass
private String memberOne;private static String memberTwo;
// constructor of OuterClass
public OuterClass(String varOne, String varTwo) {
this.memberOne = varOne;this.memberTwo = varTwo;}
// InnerClass is a static inner class of OuterClass
private static class InnerClass {
private String innerMemberOne;
public InnerClass(String innerVarOne) {this.innerMemberOne = innerVarOne;}public String concat(String separator) {
// InnerClass only has access to static member variables of OuterClass
return memberTwo + separator + this.innerMemberOne;
}
}
}public class BankAccount {
// private member variables of BankAccount class
private String accountOwnerName;private String accountOwnerSSN;private int accountNumber;private double balance;
// constructor for BankAccount class
public BankAccount(String accountOwnerName, String accountOwnerSSN,int accountNumber, double initialBalance, int initialRate){this.accountOwnerName = accountOwnerName;this.accountOwnerSSN = accountOwnerSSN;this.accountNumber = accountNumber;this.balance = initialBalance;this.start(initialRate);}
// start method will add interest to balance every 30 days
// creates timer object and interest adding action listener object
public void start(double rate){ActionListener adder = new InterestAdder(rate);Timer t = new Timer(1000 * 3600 * 24 * 30, adder);t.start();}
// InterestAdder is an inner class of BankAccount class
// that implements the ActionListener interface
private class InterestAdder implements ActionListener{
private double rate;
public InterestAdder(double aRate){this.rate = aRate;}
public void actionPerformed(ActionEvent event){
// update interest
double interest = BankAccount.this.balance * rate / 100;BankAccount.this.balance += interest;
}
}
}public class BankAccount {
// private member variables of BankAccount class
private String accountOwnerName;private String accountOwnerSSN;private int accountNumber;private double balance;
// constructor for BankAccount class
public BankAccount(String accountOwnerName, String accountOwnerSSN,int accountNumber, double initialBalance, int initialRate){this.accountOwnerName = accountOwnerName;this.accountOwnerSSN = accountOwnerSSN;this.accountNumber = accountNumber;this.balance = initialBalance;this.start(initialRate);}
// start method will add interest to balance every 30 days
// creates timer object and interest adding action listener object
public void start(final double rate){
// InterestAdder is a local inner class
// that implements the ActionListener interface
class InterestAdder implements ActionListener{
public void actionPerformed(ActionEvent event){
// update interest
double interest = BankAccount.this.balance * rate / 100;BankAccount.this.balance += interest;
}
}ActionListener adder = new InterestAdder();Timer t = new Timer(1000 * 3600 * 24 * 30, adder);t.start();
}
}public class BankAccount {
// private member variables of BankAccount class
private String accountOwnerName;private String accountOwnerSSN;private int accountNumber;private double balance;
// constructor for BankAccount class
public BankAccount(String accountOwnerName, String accountOwnerSSN,int accountNumber, double initialBalance, int initialRate){this.accountOwnerName = accountOwnerName;this.accountOwnerSSN = accountOwnerSSN;this.accountNumber = accountNumber;this.balance = initialBalance;this.start(initialRate);}
// start method will add interest to balance every 30 days
// creates timer object and interest adding action listener object
public void start(final double rate){
// anonymous inner class that implements the ActionListener interface
ActionListener adder = new ActionListener(){
public void actionPerformed(ActionEvent event){
double interest = BankAccount.this.balance * rate / 100;
BankAccount.this.balance += interest;
}
};
Timer t = new Timer(1000 * 3600 * 24 * 30, adder);t.start();
}
}public class UrlToolApplet extends Applet {
// private member variables for applet components
private Label enterUrlLabel;private TextField enterUrlTextField;private Button submitButton;
// init method that adds components to applet
// and creates button listener object
public void init() {setLayout(new FlowLayout());enterUrlLabel = new Label("Enter URL: ");enterUrlTextField = new TextField("", 20);submitButton = new Button("Submit");add(enterUrlLabel);add(enterUrlTextField);add(submitButton);ActionListener submitButtonListener = new SubmitButtonListener();submitButton.addActionListener(submitButtonListener);}
// button listener inner class for UrlToolApplet class
private class SubmitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == submitButton) {String urlString = enterUrlTextField.getText();URL url = null;try {url = new URL(urlString);} catch (MalformedURLException e) {System.err.println("Malformed URL: " + urlString);}if (url != null) {getAppletContext().showDocument(url);}}
}
}
}public class UrlToolApplet extends Applet implements ActionListener {
// private member variables for applet components
private Label enterUrlLabel;private TextField enterUrlTextField;private Button submitButton;
// init method that adds components to applet
public void init() {setLayout(new FlowLayout());enterUrlLabel = new Label("Enter URL: ");enterUrlTextField = new TextField("", 20);submitButton = new Button("Submit");add(enterUrlLabel);add(enterUrlTextField);add(submitButton);submitButton.addActionListener(this);}
// implementation of actionPerformed method of ActionListener interface
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == submitButton) {String urlString = enterUrlTextField.getText();URL url = null;try {url = new URL(urlString);} catch (MalformedURLException e) {System.err.println("Malformed URL: " + urlString);}if (url != null) {getAppletContext().showDocument(url);}}
}
}Mitigations & Prevention
Using sealed classes protects object-oriented encapsulation paradigms and therefore protects code from being extended in unforeseen ways.
Inner Classes do not provide security. Warning: Never reduce the security of the object from an outer class, going to an inner class. If an outer class is final or private, ensure that its inner class is private as well.
Detection Methods
- Automated Static Analysis High — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea
Related Weaknesses
Taxonomy Mappings
- 7 Pernicious Kingdoms: — Mobile Code: Use of Inner Class
- CLASP: — Publicizing of private data when using inner classes
- The CERT Oracle Secure Coding Standard for Java (2011): OBJ08-J — Do not expose private members of an outer class from within a nested class
Frequently Asked Questions
What is CWE-492?
CWE-492 (Use of Inner Class Containing Sensitive Data) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. Inner classes are translated into classes that are accessible at package scope and may expose code that the programmer intended to keep private to attackers.
How can CWE-492 be exploited?
Attackers can exploit CWE-492 (Use of Inner Class Containing Sensitive Data) to read application data. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-492?
Key mitigations include: Using sealed classes protects object-oriented encapsulation paradigms and therefore protects code from being extended in unforeseen ways.
What is the severity of CWE-492?
CWE-492 is classified as a Variant-level weakness (Low-Medium abstraction). Its actual severity depends on the specific context and how the weakness manifests in your application.