CWE-396 对通用异常声明Catch语句

Declaration of Catch for Generic Exception

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: unkown

基本描述

Catching overly broad exceptions promotes complex error handling code that is more likely to contain security vulnerabilities.

扩展描述

Multiple catch blocks can get ugly and repetitive, but "condensing" catch blocks by catching a high-level class like Exception can obscure exceptions that deserve special treatment or that should not be caught at this point in the program. Catching an overly broad exception essentially defeats the purpose of Java's typed exceptions, and can become particularly dangerous if the program grows and begins to throw new types of exceptions. The new exception types will not receive any attention.

相关缺陷

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

  • cwe_Nature: ChildOf cwe_CWE_ID: 755 cwe_View_ID: 1000

  • cwe_Nature: ChildOf cwe_CWE_ID: 221 cwe_View_ID: 1000

适用平台

Language: [{'cwe_Name': 'C++', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'Java', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C#', 'cwe_Prevalence': 'Undetermined'}]

常见的影响

范围 影响 注释
['Non-Repudiation', 'Other'] ['Hide Activities', 'Alter Execution Logic']

示例代码

The following code excerpt handles three types of exceptions in an identical fashion.

good Java

try {
doExchange();
}
catch (IOException e) {
logger.error("doExchange failed", e);
}
catch (InvocationTargetException e) {

logger.error("doExchange failed", e);
}
catch (SQLException e) {

logger.error("doExchange failed", e);
}

At first blush, it may seem preferable to deal with these exceptions in a single catch block, as follows:

bad

try {
doExchange();
}
catch (Exception e) {
logger.error("doExchange failed", e);
}

However, if doExchange() is modified to throw a new type of exception that should be handled in some different kind of way, the broad catch block will prevent the compiler from pointing out the situation. Further, the new catch block will now also handle exceptions derived from RuntimeException such as ClassCastException, and NullPointerException, which is not the programmer's intent.

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
7 Pernicious Kingdoms Overly-Broad Catch Block
Software Fault Patterns SFP5 Ambiguous Exception Type
OMG ASCSM ASCSM-CWE-396
OMG ASCRM ASCRM-CWE-396

引用