Versions Compared

Key

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

...

Conversely, the assumption that two classes deriving from the same code base are themselves the same is error-prone. While this assumption is commonly observed to be true in desktop applications, it is typically not the case with J2EE servlet containers. The containers can use different class loader instances to deploy and recall applications at runtime, without having to restart the JVM. In such situations, two objects whose classes come from the same code base could appear to the JVM to be two different classes. Also note that the equals() method might not return true when comparing objects originating from the same code base.

Noncompliant Code Example

This noncompliant code example compares the name of the class (Auth) of object auth to the string "com.application.auth.DefaultAuthenticationHandler" and branches on the result of the comparison.

...

Comparing fully qualified class names is insufficient because distinct class loaders can load differing classes with identical fully qualified names into a single JVM.

Compliant Solution

This compliant solution compares the class object of class Auth to the class object of the class that the current class loader loads, instead of comparing just the class names.

...

The call to loadClass() returns the class with the specified name in the current name space (consisting of the class name and the defining class loader), and the comparison is correctly performed on the two class objects.

Noncompliant Code Example

This noncompliant code example compares the names of the class objects of classes x and y using the equals() method. Again, it is possible that x and y are distinct classes with the same name, if they come from different class loaders.

Code Block
bgColor#FFCCCC
// Determine whether objects x and y have the same class name
if (x.getClass().getName().equals(y.getClass().getName())) {
  // Code assumes that the objects have the same class
}

Compliant Solution

This compliant solution correctly compares the two objects' classes.

Code Block
bgColor#ccccff
// Determine whether objects x and y have the same class
if (x.getClass() == y.getClass()) {
  // Code determines whether the objects have the same class
}

Risk Assessment

Comparing classes solely using their names can allow a malicious class to bypass security checks and gain access to protected resources.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ06-J

high

unlikely

low

P9

L2

Related Guidelines

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="dffd06ecd0fd03b2-60e00068-4ee94d85-89a2bad8-a47589c7058d95700d0a61ad"><ac:plain-text-body><![CDATA[

[[MITRE 2009

AA. Bibliography#MITRE 09]]

[CWE-486

http://cwe.mitre.org/data/definitions/486.html] "Comparison of Classes by Name"

]]></ac:plain-text-body></ac:structured-macro>

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4a09aa63616ef547-8328698f-40054ee1-ab3bb1bd-d50488a926a35150cf97fef1"><ac:plain-text-body><![CDATA[

[[Christudas 2005

AA. Bibliography#Christudas 05]]

Internals of Java Class Loading

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="75b1c69a971d2e6e-5bfc7d00-4374447e-8f1e9337-dcf5010a9423c4289eff482b"><ac:plain-text-body><![CDATA[

[[JVMSpec 1999

AA. Bibliography#JVMSpec 99]]

[§2.8.1 Class Names

http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4b79f3b01a817879-c4bb1369-442d4f20-9d999835-cb98f159b663eb303f603fa5"><ac:plain-text-body><![CDATA[

[[McGraw 1998

AA. Bibliography#Mcgraw 98]]

Twelve rules for developing more secure Java code

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="beef7f4e590726d6-d5527a35-47684b0d-9b2c9f77-81c76b31443863096b6883bf"><ac:plain-text-body><![CDATA[

[[Wheeler 2003

AA. Bibliography#Wheeler 03]]

[Java

http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/java.html] Secure programming for Linux and Unix HOWTO

]]></ac:plain-text-body></ac:structured-macro>

...