Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Parasoft Jtest 2021.1

Increasing the accessibility of overridden or hidden methods permits a malicious subclass to offer wider access to the restricted method than was originally intended. Consequently, programs must override methods only when necessary and must declare methods final whenever possible to prevent malicious subclassing. When methods cannot be declared final, programs must refrain from increasing the accessibility of overridden methods.

According to JLS:

"The access modifier of an overriding or hiding method must provide at least as much access as the overridden or hidden method , or a compile-time error occurs."

The allowed accesses are:

(The Java Language Specification, §8.4.8.3, "Requirements in Overriding and Hiding" [JLS 2015]). The following table lists the allowed accesses.

Overridden/Hidden Method Modifier

Overriding/Hiding Method Modifier

Overridden/hidden method modifier

Overriding/hiding method modifier

public

public

protected

protected or public

default

default or protected or

private

public

private

anything but private

...

Cannot be overridden

Noncompliant Code Example

This noncompliant code example exemplifies demonstrates how a malicious subclass Sub can both override the doLogic() method of the superclass and increase the accessibility of the super classoverriding method. Any user of Sub will be able to can invoke the doLogic method even though because the base class BadScope defined it with the private access modifier Super defines it to be protected, consequently allowing class Sub to increase the accessibility of doLogic() by declaring its own version of the method to be public.

Code Block
bgColor#FFcccc

class BadScopeSuper {
  privateprotected void doLogic() {
    System.out.println("Super invoked");
  }
}

public class Sub extends BadScopeSuper {
  public void doLogic() {
    System.out.println("Sub invoked");
    //do Do restrictivesensitive 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 <xyz rule>This compliant solution declares the doLogic() method final to prevent malicious overriding:

Code Block
bgColor#ccccff

class BadScopeSuper {
  privateprotected final void doLogic() { // Declare as final
    System.out.println("Super invoked");
    // Do sensitive operations
  }
}

Exceptions

MET04-J-EX0: For classes that implement the java.lang.Cloneable interface, the accessibility of the Object.clone() method should be increased from protected to public [SCG 2009].

Risk Assessment

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

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

SCP01

MET04-J

medium

Medium

probable

Probable

high

Medium

P4

P8

L3

L2

Automated Detection

TODO

Related Vulnerabilities

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

References

Detecting violations of this rule is straightforward.

ToolVersionCheckerDescription
Parasoft Jtest
Include Page
Parasoft_V
Parasoft_V
CERT.MET04.OPMDo not override an instance "private" method

Related Guidelines

MITRE CWE

CWE-487, Reliance on Package-Level Scope

Secure Coding Guidelines for Java SE, Version 5.0

Guideline 4-1 / EXTEND-1: Limit the accessibility of classes, interfaces, methods, and fields

Bibliography

...

...

...

...

...


...

Image Added Image Added Image Added Hiding