Skip to content

Commit 08552db

Browse files
dwiley258BartKaras1128s19110
authored
Apply suggestions from code review
Co-authored-by: Bartlomiej Karas <[email protected]> Co-authored-by: Hubert Daniszewski <[email protected]> Signed-off-by: dwiley258 <[email protected]>
1 parent 17e1da2 commit 08552db

File tree

1 file changed

+5
-8
lines changed
  • docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-459

1 file changed

+5
-8
lines changed

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ In Python there is two documented ways to create temporary files using the tempf
99
tempfile.mkstemp() creates a secure file in the most secure fashion allowing only read and write to the user who executed the python script. The function returns a tuple containing a file descriptor and the file path, but since this tuple is not a context manager, it does not directly integrate with the "with" statement, which automatically manages resource cleanup. This means that the user is responsible for deleting the temporary file after use.
1010

1111

12-
tempfile.NamedTemporaryFile() is more advanced than the mkstemp() method as it returns a file-like object that works well with the "with" statement, although it creates the file with the same permissions as mkstemp(). The default behaviour is to delete the file once the "with" block is finished. If the file is needed outside of the with block, the delete_on_close parameter must be set to false.
12+
tempfile.NamedTemporaryFile() is more advanced than the mkstemp() method as it returns a file-like object, which acts as a context manager, which works well with the "with" statement, although it creates the file with the same permissions as mkstemp(). The default behaviour is to delete the file once the "with" block is finished. If the file is needed outside of the with block, the delete_on_close parameter must be set to false.
13+
1314

1415

1516
## Non-Compliant Code Example
@@ -19,8 +20,6 @@ In the noncompliant01.py example, a temporary file is created but is not removed
1920
*[noncompliant01.py](noncompliant01.py):*
2021

2122
```python
22-
"""Non-compliant Code Example"""
23-
2423
# SPDX-FileCopyrightText: OpenSSF project contributors
2524
# SPDX-License-Identifier: MIT
2625
""" Non-compliant Code Example """
@@ -34,8 +33,6 @@ In noncompliant02.py, we are using the mkstemp method to generate the temporary
3433
*[noncompliant02.py](noncompliant02.py):*
3534

3635
```python
37-
"""Non-compliant Code Example"""
38-
3936
# SPDX-FileCopyrightText: OpenSSF project contributors
4037
# SPDX-License-Identifier: MIT
4138
""" Non-compliant Code Example """
@@ -49,13 +46,13 @@ with os.fdopen(fd, 'w') as f:
4946
print(path)
5047
```
5148

52-
The non-compliant02.py does not remove the file after use and leaves clean up to the user or the operating system.
49+
Neither of the code examples removes the file after use, leaving cleanup to the user or the operating system.
5350

5451
## Compliant Solution
5552

56-
In compliant01.py we use the tempFile module to generate our temporary file. When not passing in delete=false the default behaviour is the fie will be deleted after the corresponding file-like objects are closed.
53+
In compliant01.py we use the tempfile module to generate our temporary file. When not passing in delete=false the default behaviour is the file will be deleted after the corresponding file-like objects are closed.
5754

58-
Thanks to the use of the "with" statement we ensure that the file is closed after the file is written, even if an error is to occur.
55+
Thanks to the use of the "with" statement we ensure that the file is closed after writing to it, even if an error is to occur.
5956

6057
*[compliant01.py](compliant01.py):*
6158

0 commit comments

Comments
 (0)