...
Classes and class members should be given minimum possible access so that malicious code has the least chance of compromising their security. As far as possible, sensitive classes should avoid exposing internal functionality through interfaces because interfaces allow only public methods, and such methods carry forward to the public Application Programming Interface (API) of the class. An One exception to this is implementing an unmodifiable interface that exposes a public immutable view of a mutable object. (See guideline SEC14-J. Provide sensitive mutable classes with unmodifiable wrappers.) . Additionally, note that even if a non-final class's visibility is default, it can be susceptible to misuse if it contains public methods.
...
This example complies with guideline OBJ00-J. Declare data members as private and provide accessible wrapper methods.
...
A nested class may be declared private even though the compiler changes its accessibility to package-private.
Compliant Solution (Non-final Classes With Non-
...
Public Methods)
This compliant solution declares the Point class and its getPoint() method as package-private. This allows the Point class to be non-final and getPoint() to be invoked by classes present within the same package and loaded by a common class loader.
...
| Code Block | ||
|---|---|---|
| ||
public final class Point {
private static final int x = 1;
private static final int y = 2;
private Point(int x, int y) {}
public static void getPoint() {
System.out.println("(" + x + "," + y + ")");
}
}
|
Compliant Solution (Package-
...
Private Class)
This compliant solution reduces the accessibility of the class to package-private.
...
Granting unnecessary access breaks encapsulation and weakens the security of Java applications.
Rule Guideline | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
SEC01- J | medium | likely | medium | P12 | L1 |
...