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
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,18 +4,18 @@ Leftover temporary files not properly cleaned up after the completion of any scr
4
4
5
5
Temporary files can be used to store data, either between processes, or to preserve memory. Using temporary files comes with several challenges, we need to ensure that the temporary files are removed before the termination of the process. In the case of the process terminating abnormally, we can't rely on a finally block as it could fail to run. In the case that the program still fails to free up the execution, we need to ensure that the temporary files are created with the correct permissions and in the correct location so the OS can cleanup after the process finishes, while ensuring to restrict access to other processes.
6
6
7
-
In Python there is two documented ways to create temporary files using the tempfile library, tempfile.mkstemp() and tempfile.NamedTemporaryFile().
7
+
In Python there is two documented ways to create temporary files using the `tempfile` library, `tempfile.mkstemp()` and `tempfile.NamedTemporaryFile()`.
8
8
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.
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, 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.
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
16
## Non-Compliant Code Example
17
17
18
-
In the noncompliant01.py example, a temporary file is created but is not removed after completion.
18
+
In the `noncompliant01.py` example, a temporary file is created but is not removed after completion.
In noncompliant02.py, we are using the mkstemp method to generate the temporary file. This will create a more secure temporary file but doesn't do clean-up of the file after the end of execution.
31
+
In `noncompliant02.py`, we are using the `mkstemp` method to generate the temporary file. This will create a more secure temporary file but doesn't do clean-up of the file after the end of execution.
32
32
33
33
*[noncompliant02.py](noncompliant02.py):*
34
34
@@ -50,9 +50,9 @@ Neither of the code examples removes the file after use, leaving cleanup to the
50
50
51
51
## Compliant Solution
52
52
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.
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.
54
54
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.
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.
56
56
57
57
*[compliant01.py](compliant01.py):*
58
58
@@ -72,7 +72,7 @@ with open(temp_file_path, 'rb') as temp_file:
72
72
print(temp_file.read())
73
73
```
74
74
75
-
In the first with block of compliant01.py, a temporary file is created, which will be automatically deleted once the block is exited. It is expected that the second with block will return a FileNotFoundError as the file will have been successfully deleted.
75
+
In the first `with` block of `compliant01.py`, a temporary file is created, which will be automatically deleted once the block is exited. It is expected that the second `with` block will return a `FileNotFoundError` as the file will have been successfully deleted.
0 commit comments