generated from ossf/project-template
-
Notifications
You must be signed in to change notification settings - Fork 185
adding CWE-252 #925
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
adding CWE-252 #925
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
00997a2
adding CWE-252
s19110 81d72da
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/README.md
s19110 fc3354c
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/README.md
s19110 ad02751
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/README.md
s19110 ed98b13
Rename noncomplaint02.py to noncompliant02.py
s19110 5991268
Update noncompliant02.py
s19110 6310c10
Update compliant02.py
s19110 ccf314d
Update README.md
s19110 43225ce
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/README.md
myteron 17485fe
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/noncomplia…
myteron 4729dae
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/compliant0…
myteron ba5aef5
Update docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/README.md
s19110 458ff87
Update README.md
s19110 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
141 changes: 141 additions & 0 deletions
141
docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/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,141 @@ | ||
| # CWE-252: Unchecked Return Value | ||
|
|
||
| Return values of methods and functions should always be checked to ensure operations have been performed correctly. | ||
|
|
||
| When immutable objects are used, methods that aim to modify them have to create a new object with the desired changed and return it. For the results of such methods to take place, the developer must remember to assign the new value to a variable, otherwise it won't be accessible. They can also be used to handle unexpected behaviors by returning specific values (such as `None` or a other default values) that may require additional safety checks. | ||
|
|
||
| ## Non-Compliant Code Example - Immutable objects | ||
|
|
||
| This non-compliant code example shows a common mistake when trying to update an immutable object. Since `str` is an immutable type, `str.replace()` creates a new `str` object with the desired change [[Python Docs - str.replace](https://docs.python.org/3.9/library/stdtypes.html#str.replace)]. This object must be then assigned, typically in place of the original string. If not, the new value remains unused. | ||
|
|
||
| *[noncompliant01.py](noncompliant01.py):* | ||
|
|
||
| ```python | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
| """ Non-compliant Code Example """ | ||
|
|
||
|
|
||
| def silly_string(user_input): | ||
| """Function that changes the content of a string""" | ||
| user_input.replace("un", "very ") | ||
| return user_input | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| print(silly_string("unsafe string")) | ||
|
|
||
| ``` | ||
|
|
||
| Despite calling `silly_string()`, "unsafe string" is printed instead of the expected "very safe string" as the return value of `str.replace()` has been ignored. | ||
|
|
||
| ## Compliant Solution - Immutable objects | ||
|
|
||
| This compliant solution correctly returns the value from `str.replace()` and then prints it: | ||
|
|
||
| *[compliant01.py](compliant01.py):* | ||
|
|
||
| ```python | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
| """ Compliant Code Example """ | ||
|
|
||
|
|
||
| def silly_string(user_input): | ||
| """Function that changes the content of a string""" | ||
| return user_input.replace("un", "very ") | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| print(silly_string("unsafe string")) | ||
|
|
||
| ``` | ||
|
|
||
| ## Non-Compliant Code Example - Invalid value handling | ||
|
|
||
| Return values are also important when they may be used as an alternative to raising exceptions. `str.find()`, unlike `str.index()` returns -1 [[Python Docs - str.find](https://docs.python.org/3/library/stdtypes.html#str.find)] instead of raising a `ValueError` [[Python Docs - str.index](https://docs.python.org/3/library/stdtypes.html#str.index)] when it cannot find the given sub-string. | ||
| This non-compliant code example shows that using this value will point to the last element of the string regardless of what it is. | ||
|
|
||
| *[noncompliant02.py](noncompliant02.py):* | ||
|
|
||
| ```python | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
| """ Non-compliant Code Example """ | ||
|
|
||
|
|
||
| def find_in_string(full_string, sub_string): | ||
| """Function that searches for a sub-string in a given string""" | ||
| index = full_string.find(sub_string) | ||
| print(f"Sub-string '{sub_string}' appears in '{full_string}' at index {index}'") | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| my_string = "Secure Python coding" | ||
| find_in_string(my_string, "Python") | ||
| find_in_string(my_string, "I'm evil") | ||
|
|
||
| ``` | ||
|
|
||
| Even though `I'm evil` is clearly not a part of "Secure Python coding", the `find_in_string()` method will suggest otherwise. | ||
|
|
||
| ## Compliant Solution - Invalid value handling | ||
|
|
||
| Since `str.find()` indicates the fact that the sub-string couldn't be found with a negative index, a simple `if` check is enough to tackle the issue from the previous code example. | ||
|
|
||
| *[compliant02.py](compliant02.py):* | ||
|
|
||
| ```python | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
| """ Non-compliant Code Example """ | ||
|
|
||
|
|
||
| def find_in_string(full_string, sub_string): | ||
| """Function that searches for a sub-string in a given string""" | ||
| index = full_string.find(sub_string) | ||
| if index >= 0: | ||
| print(f"Sub-string '{sub_string}' appears in '{full_string}' at index {index}'") | ||
| else: | ||
| print(f"There is no '{sub_string}' in '{full_string}'") | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| my_string = "Secure Python coding" | ||
| find_in_string(my_string, "Python") | ||
| find_in_string(my_string, "I'm evil") | ||
|
|
||
| ``` | ||
|
|
||
| Now, the latter print will correctly indicate the lack of `I'm evil` in `Secure Python coding`. | ||
|
|
||
| ## Automated Detection | ||
|
|
||
| |Tool|Version|Checker|Description| | ||
| |:---|:---|:---|:---| | ||
| |Bandit|1.7.4 on Python 3.10.4|Not Available|| | ||
| |Flake8|8-4.0.1 on Python 3.10.4|Not Available|| | ||
|
|
||
| ## Related Guidelines | ||
|
|
||
| ||| | ||
| |:---|:---| | ||
| |[MITRE CWE](http://cwe.mitre.org/)|Pillar: [CWE-703: Improper Check or Handling of Exceptional Conditions (4.13) (mitre.org)](https://cwe.mitre.org/data/definitions/703.html)| | ||
| |[MITRE CWE](http://cwe.mitre.org/)|Base: [CWE-252: Unchecked Return Value](https://cwe.mitre.org/data/definitions/252.html)| | ||
| |[SEI CERT Coding Standard for Java](https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java)|[EXP00-J. Do not ignore values returned by methods](https://wiki.sei.cmu.edu/confluence/display/java/EXP00-J.+Do+not+ignore+values+returned+by+methods)| | ||
| |[SEI CERT C Coding Standard](https://wiki.sei.cmu.edu/confluence/display/c/SEI+CERT+C+Coding+Standard)|[EXP12-C. Do not ignore values returned by functions](https://wiki.sei.cmu.edu/confluence/display/c/EXP12-C.+Do+not+ignore+values+returned+by+functions)| | ||
| |ISO/IEC TR 24772:2019|Passing Parameters and Return Values \[CSJ\]| | ||
|
|
||
| ## Bibliography | ||
|
|
||
| ||| | ||
| |:---|:---| | ||
| |[[Python Docs - str.replace](https://docs.python.org/3.9/library/stdtypes.html#str.replace)]<br>[[Python Docs - str.find](https://docs.python.org/3/library/stdtypes.html#str.find)]<br>[[Python Docs - str.index](https://docs.python.org/3/library/stdtypes.html#str.index)]|Python Software Foundation. (2025). Built-in Types [online]. Available from: [https://docs.python.org/3.9/library/stdtypes.html](https://docs.python.org/3.9/library/stdtypes.html) \[accessed 17 June 2025\] | | ||
14 changes: 14 additions & 0 deletions
14
docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/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,14 @@ | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
| """ Compliant Code Example """ | ||
|
|
||
|
|
||
| def silly_string(user_input): | ||
| """Function that changes the content of a string""" | ||
| return user_input.replace("un", "very ") | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| print(silly_string("unsafe string")) |
20 changes: 20 additions & 0 deletions
20
docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/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,20 @@ | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
| """ Non-compliant Code Example """ | ||
|
|
||
|
|
||
| def find_in_string(full_string, sub_string): | ||
| """Function that searches for a sub-string in a given string""" | ||
| index = full_string.find(sub_string) | ||
| if index >= 0: | ||
| print(f"Sub-string '{sub_string}' appears in '{full_string}' at index {index}'") | ||
| else: | ||
| print(f"There is no '{sub_string}' in '{full_string}'") | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| my_string = "Secure Python coding" | ||
| find_in_string(my_string, "Python") | ||
| find_in_string(my_string, "I'm evil") |
15 changes: 15 additions & 0 deletions
15
docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/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,15 @@ | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
| """ Non-compliant Code Example """ | ||
|
|
||
|
|
||
| def silly_string(user_input): | ||
| """Function that changes the content of a string""" | ||
| user_input.replace("un", "very ") | ||
| return user_input | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| print(silly_string("unsafe string")) |
17 changes: 17 additions & 0 deletions
17
docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-252/noncompliant02.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,17 @@ | ||
| # SPDX-FileCopyrightText: OpenSSF project contributors | ||
| # SPDX-License-Identifier: MIT | ||
| """ Non-compliant Code Example """ | ||
|
|
||
|
|
||
| def find_in_string(full_string, sub_string): | ||
| """Function that searches for a sub-string in a given string""" | ||
| index = full_string.find(sub_string) | ||
| print(f"Sub-string '{sub_string}' appears in '{full_string}' at index {index}'") | ||
|
|
||
|
|
||
| ##################### | ||
| # exploiting above code example | ||
| ##################### | ||
| my_string = "Secure Python coding" | ||
| find_in_string(my_string, "Python") | ||
| find_in_string(my_string, "I'm evil") |
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.