Skip to content

Commit 2b1ed5a

Browse files
[3.15] gh-111783: Add a warning for reopening files by name to NamedTemporaryFile (GH-150793) (#162830)
gh-111783: Add a warning for reopening files by name to `NamedTemporaryFile` (GH-150793) (cherry picked from commit 31ed3ef) Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent fc41efc commit 2b1ed5a

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

‎Doc/library/tempfile.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ The module defines the following user-callable items:
106106
The latter approach is recommended as it provides assistance in automatic
107107
cleaning of the temporary file upon the context manager exit.
108108

109+
.. warning::
110+
111+
Reopening the temporary file by name is only safe when the file's
112+
directory is trusted. See :cwe:`377` for more information.
113+
109114
Opening the temporary file again by its name while it is still open works as
110115
follows:
111116

@@ -422,6 +427,7 @@ Here are some examples of typical usage of the :mod:`!tempfile` module::
422427

423428
# create a temporary file using a context manager
424429
# close the file, use the name to open the file again
430+
# this is only safe if the directory cannot be modified by untrusted users
425431
>>> with tempfile.NamedTemporaryFile(delete_on_close=False) as fp:
426432
... fp.write(b'Hello world!')
427433
... fp.close()

0 commit comments

Comments
 (0)