Versions Compared

Key

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

...

Wiki Markup
Improper use of {{public static}} fields can also result in type safety issues. For example, untrusted code can supply an unexpected subtype when the variable is defined to be of a more general type, such as {{java.lang.Object}} \[[Gong 2003|AA. Bibliography#Gong 03]\].

Noncompliant Code Example

Wiki Markup
This noncompliant code example is adopted from JDK v1.4.2 \[[FT 2008|AA. Bibliography#FT 08]\]. It declares a function table containing a {{public static}} field.

...

Note that this vulnerability was repaired in JDK v1.4.2_05.

Compliant Solution

This compliant solution declares the FuncLoader static field final and treats it like a constant.

...

It is also permissible to use a wrapper method to retrieve the value of m_functions. This has encapsulation advantages as it permits m_functions to be declared private. See guideline OBJ01-J. Declare data members as private and provide accessible wrapper methods for more information.

Noncompliant Code Example (serialVersionUID)

This noncompliant code example uses a public static non-final serialVersionUID field in a class designed for serialization.

Code Block
bgColor#FFCCCC
class DataSerializer implements Serializable {
  public static long serialVersionUID = 1973473122623778747L;
  // ...
}

Compliant Solution

This compliant solution declares the serialVersionUID field final and limits its accessibility to private.

...

The serialization mechanism uses the serialVersionUID field internally, so accessible wrapper methods are unnecessary.

Risk Assessment

Unauthorized modifications of public static variables can result in unexpected behavior and violation of class invariants. Furthermore, because static variables can be visible to code loaded by different class loaders when those class loaders are in the same delegation chain, such variables can be used as a covert communication channel between different application domains in some cases.

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ03 OBJ04-J

medium

probable

medium

P8

L2

Related Guidelines

Wiki Markup
[MITRE CWE|http://cwe.mitre.org/]: [CWE-582|http://cwe.mitre.org/data/definitions/582.html] "Array Declared Public, Final, and Static," [CWE-493|http://cwe.mitre.org/data/definitions/493.html] "Critical Public Variable Without Final Modifier," and  [CWE-500|http://cwe.mitre.org/data/definitions/500.html] "Public Static Field Not Marked Final"
\[[SCG 2007|AA. Bibliography#SCG 07]\] Guideline 3.1, Treat public static fields as constants

Bibliography

Wiki Markup
\[[FT 2008|AA. Bibliography#FT 08]\] 
\[[Gong 2003|AA. Bibliography#Gong 03]\] 9.3 Static Fields
\[[Nisewanger 2007|AA. Bibliography#Nisewanger 07]\] Antipattern 5, Misusing Public Static Variables
\[[Sterbenz 2006|AA. Bibliography#Sterbenz 06]\] Antipattern 5, Misusing Public Static Variables

...