It is difficult to control how data members declared public
or protected
are accessed. Attackers can manipulate such members in unexpected ways. As a result data members must be declared private
. Use wrapper accessor methods to expose class members that are beyond to be accessed outside of the package in which their class is declared. Using 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.
...
Wiki Markup |
---|
As a {{public}} data member, {{total}} can be altered by external code, independentindependently of the {{add()}} and {{remove()}} methods. It is a bad practice to expose 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.
Note that accessor methods should be careful about care must be taken when providing references to private mutable objects from acessor methods; see rule "OBJ05-J. Defensively copy private mutable class members before returning their references" for details.
Code Block | ||
---|---|---|
| ||
public class Widget { private int total; // Declared private public int getTotal () { return total; } // definitions for add() and remove() remain the same } |
...
One example of appropriate
public
instance variables is the case where the class is essentially a data structure, with no behavior. In other words, if you would have used astruct
instead of a class (if Java supportedstruct
), then it's appropriate to make the class's instance variablespublic
.
Wiki Markup |
---|
*OBJ01-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. |
...
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 excessive false positives. Sound detection and application of the exceptions to this rule is infeasible, ; however, heuristic techniques may be useful.
...
CWE-766, "Critical Variable Declared Public" . Critical variable declared public | |
Secure Coding Guidelines for the Java Programming Language, Version 3.0 | Guideline 3-2. Define wrapper methods around modifiable internal state |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ed35cfe3af09d005-bc3acb7e-48d94830-b07bbd73-007529f5324dc89de6cfacc1"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Items Item 13: . Minimize the accessibility of classes and members; Item 14: . In public classes, use accessor methods, not public fields | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2dcfe52e78024d06-4004a89a-4ff846e5-9130b011-557d291efb9c0edfb1da360c"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [§6.6 ", Access Control" | http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d90f0f9e26158f07-28ab4511-4b2e4ea8-84c09ba3-d9120cb91a95e1924d5e37f7"><ac:plain-text-body><![CDATA[ | [[Long 2005 | AA. Bibliography#Long 05]] | §2.2, Public Fields | ]]></ac:plain-text-body></ac:structured-macro> |
...