...
Code Block | ||
---|---|---|
| ||
class BadScope { private final void doLogic() { System.out.println("Super invoked"); } } |
Noncompliant Code Example
This noncompliant code example overrides the finalize()
method of the superclass Base
, and changes its accessibility from protected
to public
.
According to Sun's Secure Coding Guidelines [[SCG 07]]:
In addition, refrain from increasing the accessibility of an inherited method, as doing so may break assumptions made by the superclass. A class that overrides the protected java.lang.Object.finalize method and declares that method public, for example, enables hostile callers to finalize an instance of that class, and to call methods on that instance after it has been finalized. A superclass implementation unprepared to handle such a call sequence could throw runtime exceptions that leak private information, or that leave the object in an invalid state that compromises security. One noteworthy exception to this guideline pertains to classes that implement the java.lang.Cloneable interface. In these cases, the accessibility of the Object.clone method should be increased from protected to public.
Code Block | ||
---|---|---|
| ||
final class SubClass extends Base {
public void finalize() {
// ...
}
}
|
Compliant SOlution
This compliant solution correctly declares the finalize()
method protected
. It is not possible to further limit the accessibility as Object
's finalize
method itself is declared protected
.
Code Block | ||
---|---|---|
| ||
final class SubClass extends Base {
protected void finalize() {
// ...
}
}
|
Risk Assessment
Subclassing allows access restrictions to be weakened, possibly compromising the security of a Java application.
...