Skip to content

Commit 951b3f0

Browse files
authored
Merge branch 'ossf:main' into pyDoc2GitHub_CWE-366
2 parents 3211bd7 + ca10c61 commit 951b3f0

File tree

5 files changed

+59
-42
lines changed

5 files changed

+59
-42
lines changed

docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-460/README.md

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,92 +10,101 @@ A consequence of this is that the code is left in a bad state.
1010

1111
One of the ways to mitigate this is to make sure that cleanup happens or that you should exit the program. Use throwing exceptions sparsely.
1212

13-
Another way to mitigate this is to use the ‘with’ statement. It simplifies resource management by automatically handling setup and cleanup tasks. It's commonly used with files, network connections and databases to ensure resources are properly released even if errors occur making your code cleaner.
13+
Another way to mitigate this is to use the `with` statement. It simplifies resource management by automatically handling setup and cleanup tasks. It's commonly used with files, network connections and databases to ensure resources are properly released even if errors occur making your code cleaner.
14+
15+
Not using the `with` statement requires to use `lock.aquire()` and `lock.release()` as demonstrated in the `example01.py` code.
16+
17+
*[example01.py](example01.py):*
18+
19+
```python
20+
# SPDX-FileCopyrightText: OpenSSF project contributors
21+
# SPDX-License-Identifier: MIT
22+
23+
import threading
24+
25+
lock = threading.Lock()
26+
lock.acquire()
27+
try:
28+
...
29+
finally:
30+
lock.release()
31+
32+
```
33+
34+
It is best practice to use `with` statement in such cases as it will make sure the resource gets released even if an exception occurs in the execution.
1435

1536
## Non-Compliant Code Example
1637

17-
In the noncompliant.py example, a thread gets locked, but not unlocked due to an exception being thrown before it can be closed. This might lead to the lock remaining closed and inaccessible for further use.
38+
In the `noncompliant01.py` example, a thread gets locked, but not unlocked due to an exception being thrown before it can be closed. This might lead to the lock remaining closed and inaccessible for further use.
1839

19-
noncompliant.py:
40+
*[noncompliant01.py](noncompliant01.py):*
2041

2142
```python
2243
# SPDX-FileCopyrightText: OpenSSF project contributors
2344
# SPDX-License-Identifier: MIT
24-
2545
"""Non-compliant Code Example"""
2646

2747
import threading
2848

49+
2950
lock = threading.Lock()
3051

52+
3153
def perform_critical_operation():
32-
# the lock has been acquired for performing a critical operation
3354
lock.acquire()
3455
print("Lock acquired, performing critical operation...")
35-
# simulating an error before it can be released
3656
raise ValueError("Something went wrong!")
3757
lock.release() # This line is never reached due to the exception
3858

59+
3960
try:
4061
perform_critical_operation()
4162
except ValueError as e:
4263
print(f"Caught exception: {e}")
4364

44-
# Next attempt to acquire the lock will block forever — deadlock!
65+
66+
# Next attempt to acquire the lock will block forever; as there is a deadlock!
4567
lock.acquire()
46-
print("This will never print because the lock was never released.")
68+
print("This will not print because the lock was never released.")
4769

4870
```
4971

50-
In the above code example, the acquired lock never gets released, as an error gets thrown before it can be released.
72+
In the `noncompliant01.py` code example, the acquired lock never gets released, as an error gets thrown before it can be released.
5173

5274
## Compliant Solution
5375

54-
In compliant01.py we use the with statement to ensure that the lock is released properly even if an error is to occur.
76+
The `compliant01.py` is using the `with` statement to ensure that the lock is released properly even if an error is to occur.
5577

56-
compliant01.py:
78+
*[compliant01.py](compliant01.py):*
5779

5880
## Compliant Code Example
5981

6082
```python
6183
# SPDX-FileCopyrightText: OpenSSF project contributors
6284
# SPDX-License-Identifier: MIT
85+
"""Compliant Code Example"""
6386

64-
""" Compliant Code Example """
6587
import threading
6688

6789
lock = threading.Lock()
6890

69-
def compliant_example():
91+
92+
def perform_critical_operation():
7093
with lock:
7194
# the lock has been acquired using the 'with' statement and will be released when the block exits; even if an exception occurs
7295
print("Lock acquired, performing critical operation...")
73-
# raising an exception
7496
raise ValueError("Something went wrong!")
97+
# This line will not be reached because of the exception above
7598
print("Lock released.")
7699

100+
77101
try:
78-
compliant_example()
102+
perform_critical_operation()
79103
except ValueError as e:
80104
print(f"Caught exception: {e}")
81-
```
82-
83-
### with lock: is shorthand for
84-
85-
```python
86-
# SPDX-FileCopyrightText: OpenSSF project contributors
87-
# SPDX-License-Identifier: MIT
88-
89-
lock.acquire()
90-
try:
91-
...
92-
finally:
93-
lock.release()
94105

95106
```
96107

97-
It is best practice to use 'with' in such cases as it will make sure the resource gets released even if an exception occurs in the execution.
98-
99108
## Automated Detection
100109

101110
|||||
@@ -106,4 +115,5 @@ It is best practice to use 'with' in such cases as it will make sure the resourc
106115

107116
|||
108117
|:---|:---|
109-
|[CWE MITRE Pillar](http://cwe.mitre.org/)|[https://cwe.mitre.org/data/definitions/460.html]|
118+
|[SEI CERT](https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java)|[ERR03-J. Restore prior object state on method failure - SEI CERT Oracle Coding Standard for Java - Confluence (cmu.edu)](https://wiki.sei.cmu.edu/confluence/display/java/ERR03-J.+Restore+prior+object+state+on+method+failure)|
119+
|[CWE MITRE Pillar](http://cwe.mitre.org/)|<http://cwe.mitre.org/data/definitions/460.html>|

docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-460/compliant.py renamed to docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-460/compliant01.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
<!--
2-
SPDX-FileCopyrightText: OpenSSF project contributors
3-
SPDX-License-Identifier: MIT
4-
-->
1+
# SPDX-FileCopyrightText: OpenSSF project contributors
2+
# SPDX-License-Identifier: MIT
3+
"""Compliant Code Example"""
54

6-
## Compliant Code Example
7-
8-
```python
95
import threading
106

117
lock = threading.Lock()
@@ -15,9 +11,8 @@ def perform_critical_operation():
1511
with lock:
1612
# the lock has been acquired using the 'with' statement and will be released when the block exits; even if an exception occurs
1713
print("Lock acquired, performing critical operation...")
18-
# raising an exception
1914
raise ValueError("Something went wrong!")
20-
# This line will not be reached because of the exception above,
15+
# This line will not be reached because of the exception above
2116
print("Lock released.")
2217

2318

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: OpenSSF project contributors
2+
# SPDX-License-Identifier: MIT
3+
4+
import threading
5+
6+
lock = threading.Lock()
7+
lock.acquire()
8+
try:
9+
...
10+
finally:
11+
lock.release()

docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-460/noncompliant.py renamed to docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-460/noncompliant01.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-FileCopyrightText: OpenSSF project contributors
22
# SPDX-License-Identifier: MIT
3-
""" Non-compliant Code Example """
3+
"""Non-compliant Code Example"""
4+
45
import threading
56

67

@@ -23,4 +24,3 @@ def perform_critical_operation():
2324
# Next attempt to acquire the lock will block forever; as there is a deadlock!
2425
lock.acquire()
2526
print("This will not print because the lock was never released.")
26-

docs/Secure-Coding-Guide-for-Python/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ It is __not production code__ and requires code-style or python best practices t
5858
|[CWE-410: Insufficient Resource Pool](CWE-664/CWE-410/README.md)||
5959
|[CWE-426: Untrusted Search Path](CWE-664/CWE-426/README.md)|[CVE-2015-1326](https://www.cvedetails.com/cve/CVE-2015-1326),<br/>CVSSv3.0: __8.8__,<br/>EPSS: __00.20__ (23.11.2023)|
6060
|[CWE-459: Incomplete Cleanup](CWE-664/CWE-459/README.md)||
61+
|[CWE-460: Improper Cleanup on Thrown Exception](CWE-664/CWE-460/README.md)|[CVE-2008-0002](https://www.cvedetails.com/cve/CVE-2008-0002),<br/>CVSSv3.1: __5.8__,<br/>EPSS: __04.10__ (04.09.2025)|
6162
|[CWE-501: Trust Boundary Violation)](CWE-664/CWE-501/README.md)|[CVE-2023-28597](https://www.cvedetails.com/cve/CVE-2023-28597),<br/>CVSSv3.0: __7.5__,<br/>EPSS: __00.11__ (05.11.2024)|
6263
|[CWE-502: Deserialization of Untrusted Data)](CWE-664/CWE-502/.)|[CVE-2018-8021](https://www.cvedetails.com/cve/CVE-2018-8021),<br/>CVSSv3.0: __9.8__,<br/>EPSS: __93.54__ (05.11.2024)|
6364
|[CWE-532: Insertion of Sensitive Information into Log File](CWE-664/CWE-532/README.md)|[CVE-2023-45585](https://www.cvedetails.com/cve/CVE-2023-45585),<br/>CVSSv3.1: __9.8__,<br/>EPSS: __0.04__ (01.11.2024)|

0 commit comments

Comments
 (0)