Developers often separate program logic across multiple classes or files to modularize code and to increase reusability. 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.
...
The java.util.Calendar
class provides a compareTo()
method and an after()
method. The after()
method is documented in the Java API Reference [API 2006] as follows:
The
after()
method returns whether thisCalendar
represents a time after the time represented by the specifiedObject
. This method is equivalent to
compareTo(when) > 0
if and only ifwhen
is aCalendar
instance. Otherwise, the method returnsfalse
.
...
This compliant solution uses a design pattern called composition and forwarding (sometimes also called delegation) [Lieberman 1986], [Gamma 1995, 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.
...
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
failed to override this method to provide the necessary security manager check [SCG 2009]. This is commonly known as the fragile class hierarchy problem.
...
Secure Coding Guidelines for the Java Programming Language, Version 3.0 | Guideline 1-3. Understand how a superclass can affect subclass behavior |
Bibliography
[API 2006] | |
Item 16. Favor composition over inheritance | |
Design Patterns, Elements of Reusable Object-Oriented Software | |
Using prototypical objects to implement shared behavior in object-oriented systems |
...