CWE-486 使用名称来比较对象

Comparison of Classes by Name

结构: Simple

Abstraction: Variant

状态: Draft

被利用可能性: High

基本描述

The program compares classes by name, which can cause it to use the wrong class when multiple classes can have the same name.

扩展描述

If the decision to trust the methods and data of an object is based on the name of a class, it is possible for malicious users to send objects of the same name as trusted classes and thereby gain the trust afforded to known classes and types.

相关缺陷

  • cwe_Nature: ChildOf cwe_CWE_ID: 1025 cwe_View_ID: 1000 cwe_Ordinal: Primary

适用平台

Language: {'cwe_Name': 'Java', 'cwe_Prevalence': 'Undetermined'}

常见的影响

范围 影响 注释
['Integrity', 'Confidentiality', 'Availability'] Execute Unauthorized Code or Commands If a program relies solely on the name of an object to determine identity, it may execute the incorrect or unintended code.

可能的缓解方案

Implementation

策略:

Use class equivalency to determine type. Rather than use the class name to determine if an object is of a given type, use the getClass() method, and == operator.

示例代码

In this example, the expression in the if statement compares the class of the inputClass object to a trusted class by comparing the class names.

bad Java

if (inputClass.getClass().getName().equals("TrustedClassName")) {

// Do something assuming you trust inputClass

// ...
}

However, multiple classes can have the same name therefore comparing an object's class by name can allow untrusted classes of the same name as the trusted class to be use to execute unintended or incorrect code. To compare the class of an object to the intended class the getClass() method and the comparison operator "==" should be used to ensure the correct trusted class is used, as shown in the following example.

good Java

if (inputClass.getClass() == TrustedClass.class) {

// Do something assuming you trust inputClass

// ...
}

In this example, the Java class, TrustedClass, overrides the equals method of the parent class Object to determine equivalence of objects of the class. The overridden equals method first determines if the object, obj, is the same class as the TrustedClass object and then compares the object's fields to determine if the objects are equivalent.

bad Java

public class TrustedClass {
...

@Override
public boolean equals(Object obj) {
boolean isEquals = false;

// first check to see if the object is of the same class
if (obj.getClass().getName().equals(this.getClass().getName())) {

// then compare object fields
...
if (...) {
isEquals = true;
}
}

return isEquals;
}

...
}

However, the equals method compares the class names of the object, obj, and the TrustedClass object to determine if they are the same class. As with the previous example using the name of the class to compare the class of objects can lead to the execution of unintended or incorrect code if the object passed to the equals method is of another class with the same name. To compare the class of an object to the intended class, the getClass() method and the comparison operator "==" should be used to ensure the correct trusted class is used, as shown in the following example.

good Java

public boolean equals(Object obj) {
...

// first check to see if the object is of the same class
if (obj.getClass() == this.getClass()) {
...
}

...
}

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
7 Pernicious Kingdoms Comparing Classes by Name
CLASP Comparing classes by name
The CERT Oracle Secure Coding Standard for Java (2011) OBJ09-J Compare classes and not class names
Software Fault Patterns SFP1 Glitch in computation