Skip to content

Commit 39f79fa

Browse files
StanFromIrelandmiss-islington
authored andcommitted
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 0672565 commit 39f79fa

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
@@ -108,6 +108,11 @@ The module defines the following user-callable items:
108108
The latter approach is recommended as it provides assistance in automatic
109109
cleaning of the temporary file upon the context manager exit.
110110

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

@@ -424,6 +429,7 @@ Here are some examples of typical usage of the :mod:`!tempfile` module::
424429

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

0 commit comments

Comments
 (0)