generated from ossf/project-template
-
Notifications
You must be signed in to change notification settings - Fork 185
CWE-584 #897
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
CWE-584 #897
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4f12fd0
CWE-584
49efbca
Update docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/README.md
andrew-costello 9890c16
Update docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/compliant0…
andrew-costello a75ea87
Update docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/compliant0…
andrew-costello 7338504
Update docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/noncomplia…
andrew-costello 3838d9c
Update docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/compliant0…
andrew-costello 1ebac73
Update docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/noncomplia…
andrew-costello 8bfaf04
Update docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/README.md
andrew-costello 2385b18
Update docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/compliant0…
andrew-costello 04e8cd3
Update docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/compliant0…
andrew-costello 19bf006
Update docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/noncomplia…
andrew-costello d324ba7
Adding modified Readme
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
115 changes: 115 additions & 0 deletions
115
docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/README.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,115 @@ | ||
| # CWE-584: Return Inside Finally Block | ||
|
|
||
| Do not use `return`, `break` or `continue` statements in a try-finally block, as the exception will not be processed. The Python documentation [[Python 3.9]](https://docs.python.org/3.9/reference/compound_stmts.html#finally) notes, "If the `finally` clause executes a [`return`](https://docs.python.org/3.9/reference/simple_stmts.html#return), [`break`](https://docs.python.org/3.9/reference/simple_stmts.html#break) or [`continue`](https://docs.python.org/3.9/reference/simple_stmts.html#continue) statement, the saved exception is discarded." | ||
|
|
||
| ## Non-Compliant Code Example | ||
|
|
||
| The `return` statement is within the finally block, which means the exception will have no effect. | ||
|
|
||
| *[noncompliant01.py](noncompliant01.py):* | ||
|
|
||
| ```python | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| """ Non-compliant Code Example """ | ||
|
|
||
|
|
||
| def do_logic(): | ||
| try: | ||
| raise Exception | ||
| finally: | ||
| print("logic done") | ||
| return True | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| do_logic() | ||
| ``` | ||
|
|
||
| This code sample will print: `logic done`. | ||
|
|
||
| ## Compliant Solution | ||
|
|
||
| The `return` statement has been moved to the end of the method, so the code sample will successfully raise the exception. | ||
|
|
||
| *[compliant01.py](compliant01.py):* | ||
|
|
||
| ```python | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| """ Compliant Code Example """ | ||
|
|
||
|
|
||
| def do_logic(): | ||
| try: | ||
| raise Exception | ||
| finally: | ||
| print("logic done") | ||
| # return statement goes here | ||
| # when exception is raised conditionally | ||
| return True | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| do_logic() | ||
| ``` | ||
|
|
||
| ## Compliant Solution - `break` inside a loop | ||
|
|
||
| It is permissible to use control flow statements that lead into a finally block, as long as they do not attempt to exit from within it. For example, the following code complies with this rule because the break statement is used to exit a while loop and does not attempt to break out of the finally block itself. | ||
|
|
||
| *[compliant02.py](compliant02.py):* | ||
|
|
||
| ```python | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
| """ Compliant Code Example """ | ||
|
|
||
|
|
||
| def do_logic(): | ||
| try: | ||
| raise Exception | ||
| finally: | ||
| c = 0 | ||
| while c < 5: | ||
| print(f"c is {c}") | ||
| c += 1 | ||
| if c == 3: | ||
| break | ||
| # return statement goes here | ||
| # when exception is raised conditionally | ||
| return True | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| do_logic() | ||
| ``` | ||
|
|
||
| ## Automated Detection | ||
|
|
||
| |Tool|Version|Checker|Description| | ||
| |:----|:----|:----|:----| | ||
| |[Pylint](https://pylint.pycqa.org/)|3.3.4|W0150|return statement in finally block may swallow exception (lost-exception)| | ||
| |[Pylint](https://pylint.pycqa.org/)|3.3.4|W0134|'return' shadowed by the 'finally' clause.| | ||
|
|
||
| ## Related Guidelines | ||
|
|
||
| ||| | ||
| |:---|:---| | ||
| |[SEI CERT Oracle Coding Standard for Java](https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java?src=breadcrumbs)|[ERR04-J. Do not complete abruptly from a finally block - SEI CERT Oracle Coding Standard for Java](https://wiki.sei.cmu.edu/confluence/display/java/ERR04-J.+Do+not+complete+abruptly+from+a+finally+block)| | ||
| |[MITRE CWE](http://cwe.mitre.org/)|[CWE-459, Incomplete Cleanup](http://cwe.mitre.org/data/definitions/459.html)| | ||
| |[MITRE CWE](http://cwe.mitre.org/)|[CWE-584, Return Inside finally Block](http://cwe.mitre.org/data/definitions/584.html)| | ||
|
|
||
| ## Bibliography | ||
|
|
||
| ||| | ||
| |:---|:---| | ||
| |[[Python 3.9]](https://docs.python.org/3.9/reference/compound_stmts.html#finally)|[Compound statements — Python 3.9.13 documentation](https://docs.python.org/3.9/reference/compound_stmts.html#finally)| |
21 changes: 21 additions & 0 deletions
21
docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/compliant01.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,21 @@ | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| """Compliant Code Example""" | ||
|
|
||
|
|
||
| def do_logic(): | ||
| try: | ||
| raise Exception | ||
| finally: | ||
| print("logic done") | ||
| # return statement goes here | ||
| # when exception is raised conditionally | ||
| return True | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| do_logic() | ||
|
|
||
23 changes: 23 additions & 0 deletions
23
docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/compliant02.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,23 @@ | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| def do_logic(): | ||
| try: | ||
| raise Exception | ||
| finally: | ||
| c = 0 | ||
| while c < 5: | ||
| print(f"c is {c}") | ||
| c += 1 | ||
| if c == 3: | ||
| break | ||
| # return statement goes here | ||
| # when exception is raised conditionally | ||
| return True | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| do_logic() | ||
andrew-costello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
19 changes: 19 additions & 0 deletions
19
docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-584/noncompliant01.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,19 @@ | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| """Non-compliant Code Example""" | ||
|
|
||
|
|
||
| def do_logic(): | ||
| try: | ||
| raise Exception | ||
| finally: | ||
| print("logic done") | ||
| return True | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| do_logic() | ||
andrew-costello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
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
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.