generated from ossf/project-template
-
Notifications
You must be signed in to change notification settings - Fork 184
CWE-390: Detection of Error Condition without Action #805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0a589a1
CWE-390: Detection of Error Condition without Action
BartKaras1128 54ef97a
Update REAME.md
BartyBoi1128 c8931f2
fixed typo, missing code, links, formatting
myteron b5bcf0d
Removed Bloch and Goetz from bibliography
BartyBoi1128 d223193
Added new "Example Code Example" section and put it after Exceptions.
BartyBoi1128 8e86930
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-390/REAME.md
BartyBoi1128 4e8c766
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-390/REAME.md
BartyBoi1128 9e57d44
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-390/REAME.md
BartyBoi1128 a1e4cd1
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-390/REAME.md
BartyBoi1128 c1ad668
Added Exceptions section below the Example Code Example
BartyBoi1128 48dc7ac
Removed "Example Code Example" section and just have it as an extensi…
BartyBoi1128 01c71f0
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-390/REAME.md
myteron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-390/REAME.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# CWE-390: Detection of Error Condition without Action | ||
|
||
Allow exceptions to bubble up and handle exceptions at the right level in the stack. | ||
|
||
Each `except` block must ensure that the program continues only with formally specified behavior by either: | ||
|
||
* Recovering from the exceptional condition | ||
* Re-throwing the exception with additional information | ||
* Throwing custom exception only if it provides a benefit over Python's [Built-in Exceptions](https://docs.python.org/3.9/library/exceptions.html) [Python.org 2022]. | ||
|
||
Invalid reasons for suppressing exceptions cause: | ||
|
||
* Excessive complexity | ||
* Print statements from lower levels | ||
* Incomplete trace-logs | ||
* Excessive logging | ||
|
||
Printing the stack trace can reveal details and sensitive data about an application such as the components on use, existing users, and other sensitive information such as keys or passwords, as described in [CWE-209: Generation of Error Message Containing Sensitive Information](https://cwe.mitre.org/data/definitions/209.html) and will not be handled in these examples. | ||
|
||
## Non-Compliant Code Example - Bare Exception | ||
|
||
In Python `Exception` extends from `BaseException`and a bare `except` will catch everything. | ||
|
||
For instance, catching a bare `except` causes a user to be unable to stop a script via `ctrl+c`, due to the base `except` catching all exceptions. In comparison, catching `except Exception` allows a `KeyboardInterrupt` to be the Python interpreter itself or other parts of the code. This is due to `KeyboardInterrupt` extending `BaseException` and not `Exception`. | ||
BartyBoi1128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Note that using `except Exception` is still too broad as per [CWE-755: Improper Handling of Exceptional Conditions](https://github.com/ossf/wg-best-practices-os-developers/blob/main/docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-755/README.md) and that a more specific exception handling is preferred. | ||
|
||
The `noncompliant01.py` code demonstrates a bare except on a `ZeroDivisionError` and must be run on the command line in order to experience the issue. | ||
BartyBoi1128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
*[noncompliant01.py](noncompliant01.py):* | ||
|
||
```py | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
""" Non-compliant Code Example """ | ||
|
||
from time import sleep | ||
|
||
|
||
def exception_example(): | ||
"""Non-compliant Code Example using bare except""" | ||
while True: | ||
try: | ||
sleep(1) | ||
_ = 1 / 0 | ||
except: | ||
print("Don't care") | ||
|
||
|
||
##################### | ||
# exploiting above code example | ||
##################### | ||
exception_example() | ||
``` | ||
|
||
The `noncompliant01.py` will continue to run when launched via terminal even when using `CTRL+C`. | ||
|
||
The process will have to be terminated or killed in order to stop it. A programming IDE will allow stopping the `noncompliant01.py`as IDEs tend to kill the process rather than sending `CTRL+C`. | ||
|
||
## Compliant Code Example - Bare Exception | ||
|
||
The `compliant01.py` code example can be stopped via `CTRL+C` on the command line as it is catching the self created `ZeroDivisionError` instead of using a bare exception. | ||
|
||
*[compliant01.py](compliant01.py):* | ||
|
||
```py | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
""" Compliant Code Example """ | ||
from time import sleep | ||
|
||
|
||
def exception_example(): | ||
"""Compliant Code Example catching a specific exception""" | ||
while True: | ||
sleep(1) | ||
try: | ||
_ = 1 / 0 | ||
except ZeroDivisionError: | ||
print("How is it now?") | ||
|
||
|
||
##################### | ||
# exploiting above code example | ||
##################### | ||
exception_example() | ||
``` | ||
|
||
## Exceptions | ||
|
||
The following two exceptions, highlighted in [SEI Cert's Oracle Coding Standard for Java](https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java), are important to understand when to attempt to handle exceptions at the right level in the stack in Python also. | ||
|
||
We wrote a code example in Python in order to assist in the understanding of these exceptions [SEI CERT ERR00-J 2025](https://wiki.sei.cmu.edu/confluence/display/java/ERR00-J.+Do+not+suppress+or+ignore+checked+exceptions). | ||
|
||
* __ERR00-J-EX0:__ You may suppress exceptions during the release of non-reusable resources, such as closing files, network sockets, or shutting down threads, if they don't affect future program behavior. | ||
* __ERR00-J-EX1:__ Allow higher-level code to catch and attempt recovery from exceptions. If recovery is not possible, log the exception, add information if needed, and rethrow it. | ||
|
||
*[example01.py](example01.py):* | ||
myteron marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```py | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
""" Compliant Code Example """ | ||
from time import sleep | ||
|
||
|
||
def exception_example(): | ||
"""Compliant Code Example catching a specific exception""" | ||
while True: | ||
sleep(1) | ||
try: | ||
_ = 1 / 0 | ||
except ZeroDivisionError: | ||
print("How is it now?") | ||
|
||
|
||
##################### | ||
# exploiting above code example | ||
##################### | ||
exception_example() | ||
BartyBoi1128 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
``` | ||
|
||
If recovery remains impossible, wrap the checked exception in an unchecked exception and rethrow it. | ||
|
||
## Automated Detection | ||
|
||
|Tool|Version|Checker|Description| | ||
|:----|:----|:----|:----| | ||
|[Ruff](https://docs.astral.sh/ruff/)|v0.4.5|[bare-except (E722)](https://docs.astral.sh/ruff/rules/bare-except/)|Use lazy % formatting in logging functions| | ||
|[Pylint](https://pylint.pycqa.org/)|3.2.7|[W0702:bare-except](https://pylint.pycqa.org/en/latest/user_guide/messages/warning/bare-except.html)|No exception type(s) specified| | ||
|[flake8](https://www.flake8rules.com/)|7.1.1|[E722](https://www.flake8rules.com/rules/E722.html)|do not use bare 'except'| | ||
|
||
## Related Guidelines | ||
|
||
||| | ||
|:---|:---| | ||
|[MITRE](https://github.com/ossf/wg-best-practices-os-developers/tree/main/docs/Secure-Coding-Guide-for-Python)|[CWE-209: Generation of Error Message Containing Sensitive Information](https://cwe.mitre.org/data/definitions/209.html)| | ||
|[SEI CERT Oracle Coding Standard for Java](https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java)|[ERR00-J. Do not suppress or ignore checked exceptions](https://wiki.sei.cmu.edu/confluence/display/java/ERR00-J.+Do+not+suppress+or+ignore+checked+exceptions)| | ||
|[MITRE CWE Base](http://cwe.mitre.org/)|[CWE-703](https://cwe.mitre.org/data/definitions/703.html), Improper Check or Handling of Exceptional Conditions| | ||
|[MITRE CWE Pillar](http://cwe.mitre.org/)|[CWE-390](http://cwe.mitre.org/data/definitions/390.html), Detection of Error Condition without Action| | ||
|
||
## Biblography | ||
|
||
||| | ||
|:---|:---| | ||
|[[Python.org](https://docs.python.org/3.9/) 2022]| python.org. (2022). Built-in Exceptions [online]. Available from: [https://docs.python.org/3.9/library/exceptions.html](https://docs.python.org/3.9/library/exceptions.html) [accessed 08 February 2023]| | ||
|[[Bloch 2008](https://wiki.sei.cmu.edu/confluence/display/java/Rule+AA.+References#RuleAA.References-Bloch08)]|Item 62, "Document All Exceptions Thrown by Each Method"| | ||
|[[Bloch 2008](https://wiki.sei.cmu.edu/confluence/display/java/Rule+AA.+References#RuleAA.References-Bloch08)]| Item 65, "Don't Ignore Exceptions"| | ||
|[[Goetz](https://wiki.sei.cmu.edu/confluence/display/java/Rule+AA.+References#RuleAA.References-Goetz06)]|Section 5.4, "Blocking and Interruptible Methods"| | ||
myteron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|[SEI CERT ERR00-J 2025]|ERR00-J. Do not suppress or ignore checked exceptions [online]. Available from: [https://wiki.sei.cmu.edu/confluence/display/java/ERR00-J.+Do+not+suppress+or+ignore+checked+exceptions](https://wiki.sei.cmu.edu/confluence/display/java/ERR00-J.+Do+not+suppress+or+ignore+checked+exceptions) [Accessed Februrary 2025]| |
28 changes: 0 additions & 28 deletions
28
docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-390/compliant02.py
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-390/example01.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
"""Code Example""" | ||
|
||
|
||
def slice_cake(cake: int, plates: int) -> float: | ||
"""Calculates size of each slice per plate for a cake | ||
Args: | ||
cake (int) : Size of the cake | ||
guests (int): Amount of guests | ||
Returns: | ||
(float): Size of each slice | ||
""" | ||
|
||
try: | ||
return cake / plates | ||
except ZeroDivisionError as zero_division_error: | ||
raise ZeroDivisionError( | ||
"slice_cake:You got to give me plates" | ||
) from zero_division_error | ||
|
||
|
||
##################### | ||
# exploiting above code example | ||
##################### | ||
slice_cake(cake=100, plates=0) |
22 changes: 0 additions & 22 deletions
22
docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-390/noncompliant02.py
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.