Versions Compared

Key

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

It is difficult to control how data members declared public or protected are accessed. Attackers can manipulate such members in unexpected ways. Use wrapper accessor methods to expose class members beyond the package in which their class is declared. The use of wrapper methods enables appropriate monitoring and control of the modification of data members (for example, by defensive copying, validating input, and logging). The wrapper methods can preserve class invariants.

Noncompliant Code Example (Public Primitive Field)

In this noncompliant code example, the data member total keeps track of the total number of elements as they are added and removed from a container using the methods add() and remove(), respectively.

...

Wiki Markup
As a {{public}} data member, {{total}} can be altered by external code, independent of the {{add()}} and {{remove()}} methods. It is a bad practice to expose both mutable and immutable fields from a {{public}} class \[[Bloch 2008|AA. Bibliography#Bloch 08]\].

Compliant Solution (private)

This compliant solution declares total as private and provides a public accessor so that the required member can be accessed beyond the current package. The add() and remove() methods modify its value without violating any class invariants.

...

It is good practice to use methods such as add(), remove(), and getTotal(), to manipulate the private internal state, because the methods can perform additional functions, such as input validation and security manager checks, prior to manipulating the state.

Noncompliant Code Example (Public Mutable Field)

This noncompliant code example shows a static mutable hash map with public accessibility.

Code Block
bgColor#FFCCCC
public static final HashMap<Integer, String> hm = new HashMap<Integer, String>();

Compliant Solution (Provide Wrappers and Reduce Accessibility of Mutable Members)

Mutable data members that are static must be declared private.

...

Depending on the required functionality, wrapper methods may retrieve either a reference to the Hashmap, a copy of the HashMap, or a value contained by the Hashmap. This compliant solution adds a wrapper method to return the value of an element given its index in the Hashmap.

Exceptions

Wiki Markup
*OBJ00-EX0:* According to Sun's Code Conventions document \[[Conventions 2009|AA. Bibliography#Conventions 09]\]

...

Wiki Markup
*OBJ00-EX1:* "if a class is package-private or is a {{private}} nested class, there is nothing inherently wrong with exposing its data fields—assuming they do an adequate job of describing the abstraction provided by the class. This approach generates less visual clutter than the accessor-method approach, both in the class definition and in the client code that uses it" \[[Bloch 2008|AA. Bibliography#Bloch 08]\]. This exception applies to both mutable and immutable fields.

OBJ00OBJ01-EX2: Static final fields that contain mathematical constants may be declared public.

Risk Assessment

Failing to declare data members private can break encapsulation.

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ00 OBJ01-J

medium

likely

medium

P12

L1

Automated Detection

Detection of public and protected data members is trivial; heuristic detection of the presence or absence of getter and setter wrapper methods is straightforward. However, simply reporting all detected cases without suppressing those cases covered by the exceptions to this rule would produce many false positives and appears unlikely to be satisfactory. Sound detection and application of the exceptions to this guideline appears to be infeasible; heuristic techniques may be useful.

Related Vulnerabilities

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

Related Guidelines

C++ Secure Coding Standard: OOP00-CPP. Declare data members private

Wiki Markup
\[[SCG 2007|AA. Bibliography#SCG 07]\] Guideline 3-2 Define wrapper methods around modifiable internal state

Bibliography

Wiki Markup
\[[Bloch 2008|AA. Bibliography#Bloch 08]\] Items 13: Minimize the accessibility of classes and members; 14: In public classes, use accessor methods, not public fields
\[[JLS 2006|AA. Bibliography#JLS 06]\] Section 6.6, Access Control
\[[Long 2005|AA. Bibliography#Long 05]\] Section 2.2, Public Fields

...