Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: renamed

...

This also means that there is potential for some functionality having a restrictive modifier to be overridden by a less restrictive modifier.

Noncompliant Code Example

This noncompliant code example exemplifies how a malicious subclass Sub can override the doLogic method of the super class. Any user of Sub will be able to invoke the doLogic method since the base class BadScope defined it with the protected access modifier. The class Sub can allow more access than BadScope by using the public modifier.

Code Block
bgColor#FFcccc
class BadScope {
  public void doLogic() { System.out.println("Super invoked"); }
}

public class Sub extends BadScope {
  public void doLogic() { 
    System.out.println("Sub invoked");
    //do restrictive operations
  }
}

Compliant Solution

Do not override a method unless absolutely necessary. Declare all methods and fields final to avoid malicious subclassing. This is in compliance with the tenets of OBJ31-J. Misusing public static variables and OBJ00-J. Declare data members private.

Code Block
bgColor#ccccff
class BadScope {
  private final void doLogic() { System.out.println("Super invoked"); }
}

Risk Assessment

Subclassing allows access restrictions to be weakened, possibly compromising the security of a Java application.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

SCP01-J

medium

probable

medium

P8

L2

Automated Detection

TODO

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

Wiki Markup
\[[JLS 05|AA. Java References#JLS 05]\] [Section 8.4.8.3, Requirements in Overriding and Hiding|http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8.3]
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 487|http://cwe.mitre.org/data/definitions/487.html] "Reliance on Package-level Scope"

...