Avoid declaring Inner Classes | CAST Appmarq

Avoid declaring Inner Classes


Rule Definition
Java bytecode has no concept of inner classes, so the compiler translates inner classes into ordinary classes that are accessible to any code in the same package. An inner class gets access to the fields of the enclosing outer class even if these fields are declared private and the inner class is translated into a separate class. To let this separate class access the fields of the outer class, the compiler silently changes these fields' scope from private to package. As a result, when inner classes are declared, they are exposed but also the compiler silently overrules the decision to make some fields private. This is a threat to both the stability and the security of the application.

Remediation
Make the inner class a standard class.

Violation Code Sample
public class BankAccount {

  private class AccountKey {  
    // VIOLATION
  }
}
Fixed Code Sample
public class BankAccount {

}

class AccountKey {  
    // FIXED
  }

Reference
Gary McGraw & Ed Felten, http://www.securingjava.com/preface.html, http://www.owasp.org/index.php/Java_leading_security_practice#Inner_Classes, http://www.cs.umd.edu/~pugh/java/SecureInnerClasses.pdf

Related Technologies
JEE

Health Factor

  Total Quality Index


Technical Criterion
Programming Practices - Modularity and OO Encapsulation Conformity

About CAST Appmarq

CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.

Benchmark Statistics

Global Compliance

96.14%

Total Violations
114,335
Total Opportunities
2,959,195
Average Violations / App.
81.32
The compliance score represents 1 minus the ratio between the number of times a rule has been violated compared to the number of opportunities in a set of applications that the rule could have been violated.

Industry Insights

Insurance

95.33%

Software ISV

92.47%

Financial Services

96.51%