Versions Compared

Key

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

Classes and class members (classes, interfaces, fields and methods) are subject to access control in Java. The access is indicated by an access modifier: public, protected, private, or the absence of an access modifier (the default access; sometimes also called package-private access). A simplified view of the access control rules is summarized presented in the following table. An 'x' conveys denotes that the particular access is permitted from within that domain. For example, an x under the heading class means that the member is accessible to code present within the same class it is declared in. Likewise, the heading package denotes that the field member is accessible from any class (or subclass) defined in the same package, provided that at runtime, the class (or subclass) is loaded by the same class loader as that of the class containing the fieldmember.

Access Specifier

class

package

sub-class

world

private

x

 

 

 

none

x

x

x*

 

protected

x

x

x**

 

public

x

x

x

x

* Sub-classes within the same package can also access members that have no access specifiers (default or package-private visibility). An additional requirement for this is that, at runtime, the subclasses must be loaded by the same class loader as that of the class containing the package-private members. Sub-classes in a different package cannot access such package-private members.

** For referencing protected members, the accessing class can be a sub-class in either the same or a different package.

Classes and class members should be given the minimum access possible so that malicious code has the least chance of manipulating the systemcompromising their security. As far as possible, sensitive classes should avoid implementing interfaces. This is because only public methods are allowed to be declared within interfaces and these carry forward to the public Application Programming Interface (API) of the class. An exception is implementing an unmodifiable interface that exposes a public immutable view of a mutable object (SEC01-J. Provide sensitive mutable classes with unmodifiable wrappers). Additionally, be aware that even if a class's visibility is default, it can be susceptible to misuse if it exposes a public method.

...

In this noncompliant example, the class PublicClass is declared public. The member function method getPoint as well as the (x, y) coordinates are also declared public. This gives world-access to the class members. A real world vulnerability, for example, can arise when a malicious applet attempts to access the credit card field of another object that is declared public. Note that a non-public class is also vulnerable if its members are declared public (a violation of OBJ00-J. Declare data members private).

...

Limiting the scope of classes, interfaces, methods and fields as far as possible reduces the chance of malicious manipulation. Limit the accessibility depending on the desired implementation scope. For non-final classes, reducing the accessibility of methods also eliminates the threat of malicious overriding. This compliant solution demonstrates the most restrictive accessibility.

...

At runtime, any protected or package-private members can be called directly by a class that is maliciously inserted into the same package (package insertion attack). However, this attack is difficult to carry out in practice because in addition to the aforementioned requirement, both the target as well as the untrusted classes must be loaded by the same class loader, whereas untrusted . Untrusted code is typically deprived of such levels of access.

...