Developers often separate program logic across multiple classes or files to modularize code and to increase re-usability. When developers modify a superclass (during maintenance, for example), the developer must ensure that changes in superclasses preserve all the program invariants on which the subclasses depend. Failure to maintain all relevant invariants can cause security vulnerabilities.
Noncompliant Code Example
This noncompliant code example relies on a class Account
that stores banking-related information with no inherent security. Security is delegated to the subclass BankAccount
. The client application is required to use BankAccount
because it contains the security mechanism.
...
Code Block |
---|
public class MaliciousClient { public static void main(String[] args) { Account account = new BankAccount(); boolean result = account.overdraft(200.0); // No security check performed System.out.println("Withdrawal successful? " + result); } } |
Compliant Solution
In this compliant solution, the BankAccount
class provides an overriding version of the overdraft()
method that immediately fails, thereby preventing misuse of the overdraft feature. All other aspects of the compliant solution remain unchanged.
...
Alternately, when the intended design permits the new method in the parent class to be invoked directly from a subclass without overriding, install a security manager check directly in the new method.
Related Vulnerability: JDK 1.2 java.util.Hashtable.entrySet()
The introduction of the entrySet()
method in the java.util.Hashtable
superclass in JDK 1.2 left the java.security.Provider
subclass class vulnerable to a security attack. The Provider
class extends java.util.Properties
, which, in turn, extends Hashtable
. The Provider
class maps a cryptographic algorithm name (for example, "RSA"
) to a class that provides its implementation.
Wiki Markup |
---|
The {{Provider}} class inherits the {{put()}} and {{remove()}} methods from {{Hashtable}} and adds security manager checks to each. These checks ensure that malicious code cannot add or remove the mappings. When {{entrySet()}} was introduced, it became possible for untrusted code to remove the mappings from the {{Hashtable}} because {{Provider}} did not override this method to provide the necessary security manager check \[[SCG 2009|AA. Bibliography#SCG 09]\]. This problem is commonly known as a "fragile class hierarchy" in other object-oriented languages, such as C++. |
Noncompliant Code Example (Calendar
)
This noncompliant code example overrides the methods after()
and compareTo()
of the class java.util.Calendar
. The Calendar.after()
method returns a boolean
value that indicates whether or not the Calendar
represents a time after that represented by the specified Object
parameter. The programmer wishes to extend this functionality so that the after()
method returns true
even when the two objects represent the same date. The programmer also overrides the method compareTo()
to provide a "comparisons by day" option to clients (for example, comparing today's date with the first day of week, which differs from country to country, to check whether it is a weekday).
...
The developer of the subclass was unaware of the implementation details of Calendar.after()
and incorrectly assumed that the superclass's after()
method would invoke only its own methods without invoking overriding methods from the subclass. Rule "MET04-J. Ensure that constructors do not call overridable methods" describes similar programming errors.
Compliant Solution (Calendar
)
Wiki Markup |
---|
This compliant solution uses a design pattern called composition and forwarding (sometimes also referred to as delegation) \[[Lieberman 1986|AA. Bibliography#Lieberman 86]\] \[[Gamma 1995|AA. Bibliography#Gamma 95], p. 20\]. The compliant solution introduces a new _forwarder_ class that contains a {{private}} member field of the {{Calendar}} type; this is _composition_ rather than inheritance. In this example, the field refers to {{CalendarImplementation}}, a concrete instantiable implementation of the {{abstract}} {{Calendar}} class. The compliant solution also introduces a wrapper class called {{CompositeCalendar}} that provides the same overridden methods found in the {{CalendarSubclass}} from the preceding noncompliant code example. |
...
Note that each method of the class ForwardingCalendar
redirects to methods of the contained CalendarImplementation
class, from which it receives return values; this is the forwarding mechanism. The ForwardingCalendar
class is largely independent of the implementation of the class CalendarImplementation
. Consequently, future changes to CalendarImplementation
are unlikely to break ForwardingCalendar
and are also unlikely to break CompositeCalendar
. Invocations of the overriding after()
method of CompositeCalendar
perform the necessary comparison by using the CalendarImplementation.compareTo()
method as required. Using super.after(when)
forwards to ForwardingCalendar
, which invokes the CalendarImplementation.after()
method as required. As a result, ava.util.Calendar.after()
invokes the CalendarImplementation.compareTo()
method as required, resulting in the program correctly printing true
.
Risk Assessment
Modifying a superclass without considering the effect on subclasses can introduce vulnerabilities. Subclasses that are unaware of the superclass implementation can be subject to erratic behavior, resulting in inconsistent data state and mismanaged control flow.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
OBJ13 OBJ02-J | medium | probable | high | P4 | L3 |
Automated Detection
Sound automated detection is not currently feasible.
Related Guidelines
Secure Coding Guidelines for the Java Programming Language, Version 3.0 | Guideline 1-3 Understand how a superclass can affect subclass behavior |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="82fc6697e09fdd88-618ed747-43b947be-87409a01-56bb3d6107bace38d5dd7858"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [Class Calendar | http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="095081e3d09de39a-b2142825-42094a44-a41a8453-523ef469417b4193ce568616"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 16: "Favor composition over inheritance" | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="83687a2a89286dbe-c6d463af-41d74cf8-83f3a760-1edbae16fa204c5672a53813"><ac:plain-text-body><![CDATA[ | [[Gamma 1995 | AA. Bibliography#Gamma 95]] | Design Patterns: Elements of Reusable Object-Oriented Software | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ba7303e7d4eb7829-c45a98b9-48a84bc4-b0cfb0f1-c3f0dcfa604b60ef8d4d4e6a"><ac:plain-text-body><![CDATA[ | [[Lieberman 1986 | AA. Bibliography#Lieberman 86]] | Using prototypical objects to implement shared behavior in object-oriented systems | ]]></ac:plain-text-body></ac:structured-macro> |
...