结构: Simple
Abstraction: Base
状态: Draft
被利用可能性: unkown
Throwing overly broad exceptions promotes complex error handling code that is more likely to contain security vulnerabilities.
Declaring a method to throw Exception or Throwable makes it difficult for callers to perform proper error handling and error recovery. Java's exception mechanism, for example, is set up to make it easy for callers to anticipate what can go wrong and write code to handle each specific exceptional circumstance. Declaring that a method throws a generic form of exception defeats this system.
cwe_Nature: ChildOf cwe_CWE_ID: 705 cwe_View_ID: 1000 cwe_Ordinal: Primary
cwe_Nature: ChildOf cwe_CWE_ID: 221 cwe_View_ID: 1000
cwe_Nature: ChildOf cwe_CWE_ID: 703 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 method throws three types of exceptions.
good Java
While it might seem tidier to write
bad
doing so hampers the caller's ability to understand and handle the exceptions that occur. Further, if a later revision of doExchange() introduces a new type of exception that should be treated differently than previous exceptions, there is no easy way to enforce this requirement.
Early versions of C++ (C++98, C++03, C++11) included a feature known as Dynamic Exception Specification. This allowed functions to declare what type of exceptions it may throw. It is possible to declare a general class of exception to cover any derived exceptions that may be throw.
bad
In the example above, the code declares that myfunction() can throw an exception of type "std::exception" thus hiding details about the possible derived exceptions that could potentially be thrown.
映射的分类名 | ImNode ID | Fit | Mapped Node Name |
---|---|---|---|
7 Pernicious Kingdoms | Overly-Broad Throws Declaration | ||
The CERT Oracle Secure Coding Standard for Java (2011) | ERR07-J | Do not throw RuntimeException, Exception, or Throwable | |
Software Fault Patterns | SFP5 | Ambiguous Exception Type | |
OMG ASCSM | ASCSM-CWE-397 | ||
OMG ASCRM | ASCRM-CWE-397 |