You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-459/README.md
+5-8Lines changed: 5 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,8 @@ In Python there is two documented ways to create temporary files using the tempf
9
9
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.
10
10
11
11
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
+
13
14
14
15
15
16
## Non-Compliant Code Example
@@ -19,8 +20,6 @@ In the noncompliant01.py example, a temporary file is created but is not removed
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.
53
50
54
51
## Compliant Solution
55
52
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.
57
54
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.
0 commit comments