|  | 
|  | 1 | +# CWE-252: Unchecked Return Value | 
|  | 2 | + | 
|  | 3 | +Return values of methods and functions should always be checked to ensure operations have been performed correctly. | 
|  | 4 | + | 
|  | 5 | +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. | 
|  | 6 | + | 
|  | 7 | +## Non-Compliant Code Example - Immutable objects | 
|  | 8 | + | 
|  | 9 | +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. | 
|  | 10 | + | 
|  | 11 | +*[noncompliant01.py](noncompliant01.py):* | 
|  | 12 | + | 
|  | 13 | +```python | 
|  | 14 | +# SPDX-FileCopyrightText: OpenSSF project contributors | 
|  | 15 | +# SPDX-License-Identifier: MIT | 
|  | 16 | +""" Non-compliant Code Example """ | 
|  | 17 | + | 
|  | 18 | + | 
|  | 19 | +def sanitize_string(user_input): | 
|  | 20 | +    """Function that ensure a given string is safe""" | 
|  | 21 | +    user_input.replace("un", "very ") | 
|  | 22 | + | 
|  | 23 | + | 
|  | 24 | +my_string = "unsafe string" | 
|  | 25 | +sanitize_string(my_string) | 
|  | 26 | + | 
|  | 27 | +##################### | 
|  | 28 | +# exploiting above code example | 
|  | 29 | +##################### | 
|  | 30 | +print(my_string) | 
|  | 31 | + | 
|  | 32 | +``` | 
|  | 33 | + | 
|  | 34 | +Despite calling `sanitize_string()`, the value of my_string remains unchanged because the return value of str.replace() has been ignored. | 
|  | 35 | + | 
|  | 36 | +## Compliant Solution - Immutable objects | 
|  | 37 | + | 
|  | 38 | +This compliant solution correctly returns the value from `str.replace()` and assigns it to `my_string`: | 
|  | 39 | + | 
|  | 40 | +*[compliant01.py](compliant01.py):* | 
|  | 41 | + | 
|  | 42 | +```python | 
|  | 43 | +# SPDX-FileCopyrightText: OpenSSF project contributors | 
|  | 44 | +# SPDX-License-Identifier: MIT | 
|  | 45 | +""" Compliant Code Example """ | 
|  | 46 | + | 
|  | 47 | + | 
|  | 48 | +def sanitize_string(user_input): | 
|  | 49 | +    """Function that ensure a given string is safe""" | 
|  | 50 | +    return user_input.replace("un", "very ") | 
|  | 51 | + | 
|  | 52 | + | 
|  | 53 | +my_string = "unsafe string" | 
|  | 54 | +my_string = sanitize_string(my_string) | 
|  | 55 | + | 
|  | 56 | +##################### | 
|  | 57 | +# exploiting above code example | 
|  | 58 | +##################### | 
|  | 59 | +print(my_string) | 
|  | 60 | + | 
|  | 61 | +``` | 
|  | 62 | + | 
|  | 63 | +## Non-Compliant Code Example - Invalid value handling | 
|  | 64 | + | 
|  | 65 | +Return values are also important when they may be used as an alternative to raising exceptions. `str.find()`, unlike `str.index()` return -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. | 
|  | 66 | +This non-compliant code example shows that using this value will point to the last element of the string regardless of what it is. | 
|  | 67 | + | 
|  | 68 | +*[noncompliant02.py](noncompliant02.py):* | 
|  | 69 | + | 
|  | 70 | +```python | 
|  | 71 | +# SPDX-FileCopyrightText: OpenSSF project contributors | 
|  | 72 | +# SPDX-License-Identifier: MIT | 
|  | 73 | +""" Non-compliant Code Example """ | 
|  | 74 | + | 
|  | 75 | + | 
|  | 76 | +def find_in_string(full_string, sub_string): | 
|  | 77 | +    """Function that searches for a sub-string in a given string""" | 
|  | 78 | +    index = full_string.find(sub_string) | 
|  | 79 | +    print(f"""Sub-string '{sub_string}' appears for the first time in | 
|  | 80 | +          '{full_string}' at index {index}: '{full_string[index:]}'""") | 
|  | 81 | + | 
|  | 82 | + | 
|  | 83 | +##################### | 
|  | 84 | +# exploiting above code example | 
|  | 85 | +##################### | 
|  | 86 | +my_string = "Secure Python coding" | 
|  | 87 | +find_in_string(my_string, "Python") | 
|  | 88 | +find_in_string(my_string, "I'm evil") | 
|  | 89 | + | 
|  | 90 | +``` | 
|  | 91 | + | 
|  | 92 | +Even though `I'm evil` is clearly not a part of Secure Python coding, the `find_in_string()` method will suggest otherwise. | 
|  | 93 | + | 
|  | 94 | +## Compliant Solution - Invalid value handling | 
|  | 95 | + | 
|  | 96 | +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. | 
|  | 97 | + | 
|  | 98 | +*[compliant02.py](compliant02.py):* | 
|  | 99 | + | 
|  | 100 | +```python | 
|  | 101 | +# SPDX-FileCopyrightText: OpenSSF project contributors | 
|  | 102 | +# SPDX-License-Identifier: MIT | 
|  | 103 | +""" Non-compliant Code Example """ | 
|  | 104 | + | 
|  | 105 | + | 
|  | 106 | +def find_in_string(full_string, sub_string): | 
|  | 107 | +    """Function that searches for a sub-string in a given string""" | 
|  | 108 | +    index = full_string.find(sub_string) | 
|  | 109 | +    if index >= 0: | 
|  | 110 | +        print(f"""Sub-string '{sub_string}' appears for the first time in | 
|  | 111 | +              '{full_string}' at index {index}: '{full_string[index:]}'""") | 
|  | 112 | +    else: | 
|  | 113 | +        print(f"There is no '{sub_string}' in '{full_string}'") | 
|  | 114 | + | 
|  | 115 | + | 
|  | 116 | +##################### | 
|  | 117 | +# exploiting above code example | 
|  | 118 | +##################### | 
|  | 119 | +my_string = "Secure Python coding" | 
|  | 120 | +find_in_string(my_string, "Python") | 
|  | 121 | +find_in_string(my_string, "I'm evil") | 
|  | 122 | + | 
|  | 123 | +``` | 
|  | 124 | + | 
|  | 125 | +Now, the latter print will correctly indicate the lack of `I'm evil` in `Secure Python coding`. | 
|  | 126 | + | 
|  | 127 | +## Automated Detection | 
|  | 128 | + | 
|  | 129 | +|Tool|Version|Checker|Description| | 
|  | 130 | +|:---|:---|:---|:---| | 
|  | 131 | +|Bandit|1.7.4 on Python 3.10.4|Not Available|| | 
|  | 132 | +|Flake8|8-4.0.1 on Python 3.10.4|Not Available|| | 
|  | 133 | + | 
|  | 134 | +## Related Guidelines | 
|  | 135 | + | 
|  | 136 | +||| | 
|  | 137 | +|:---|:---| | 
|  | 138 | +|[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)| | 
|  | 139 | +|[MITRE CWE](http://cwe.mitre.org/)|Base: [CWE-252: Unchecked Return Value](https://cwe.mitre.org/data/definitions/252.html)| | 
|  | 140 | +|[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)| | 
|  | 141 | +|[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)| | 
|  | 142 | +|ISO/IEC TR 24772:2019|Passing Parameters and Return Values [CSJ]| | 
|  | 143 | + | 
|  | 144 | +## Bibliography | 
|  | 145 | + | 
|  | 146 | +||| | 
|  | 147 | +|:---|:---| | 
|  | 148 | +|[[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] | | 
0 commit comments