Versions Compared

Key

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

Wiki Markup
            A nested class is any class whose declaration occurs within the body of another class or interface \[[JLS 2005|AA. Bibliography#JLS 05]\]. The use of a nested class is error-prone unless the semantics are well understood. A common notion is that only the nested class may access the contents of the outer class. Not only does the nested class have access to the private fields of the outer class, the same fields can be accessed by anotherany other class within the package when the nested class is declared public or if it contains public methods or constructors. As a result the nested class must not expose the private members of the outer class to external classes or packages. 

Wiki Markup
Also,According according to the _Java Language Specification_ , [§8.3, "Field Declarations"|http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3] \[[JLS 2005|AA. Bibliography#JLS 05]\]:

Note that a private field of a superclass might be accessible to a subclass (for example, if both classes are members of the same class). Nevertheless, a private field is never inherited by a subclass.

...

This noncompliant code example exposes the sensitive private (x,y) coordinates through the getPoint() method of the inner class. Consequently, the AnotherClass class that belongs to the same package can also access the coordinates.

Code Block
bgColor#FFcccc
class Coordinates {
  private int x;
  private int y;

  public class Point {
    public void getPoint() {
      System.out.println("(" + x + "," + y + ")");
    }
  }
}

class AnotherClass {
  public static void main(String[] args) {
    Coordinates c = new Coordinates();
    Coordinates.Point p = c.new Point();
    p.getPoint();
  }
}

...

The Java language system weakens the accessibility of sensitive private entities in inner classes, private members of an outer class when a nested inner class is present which can result in a security weaknessan information leak.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ08-J

medium

probable

medium

P8

L2

...

Automated detection of nonprivate nested inner classes that define nonprivate members and constructors is straightforward. However, this rule applies only when those classes could potentially expose sensitive data or operations that leak private data from the outer class . Detection of sensitive data or operations requires programmer assistance.is straightforward.

Related Guidelines

MITRE CWE

CWE-492. Use of Inner Class Containing Sensitive Data

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e2f7808c2c4e337c-daadaee8-437f4929-bc458419-6def26859db9d82c5d8cce01"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[§8.1.3, Inner Classes and Enclosing Instances

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3] ]]></ac:plain-text-body></ac:structured-macro>

 

§8.3, Field Declarations

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1a87f66554adcab2-e08b8905-411e475b-a2b9aacf-94785eaf9580ce43b3e5ad45"><ac:plain-text-body><![CDATA[

[[Long 2005

AA. Bibliography#Long 05]]

§2.3, Inner Classes

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9cde381282e845f5-b008d6cc-4623412b-9e098468-9e9a8adda84f2b712ee8eb6c"><ac:plain-text-body><![CDATA[

[[McGraw 1999

AA. Bibliography#McGraw 99]]

Securing Java, Getting Down to Business with Mobile Code

]]></ac:plain-text-body></ac:structured-macro>

...