From 756492598d98173a2007c1ebd5aa7ae3d7f1a832 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sun, 29 Nov 2026 03:27:39 +0000 Subject: [PATCH] gh-111783: Add a warning for reopening files by name to `NamedTemporaryFile` (GH-150793) (cherry picked from commit 31ed3efd552afc14b4f3303155092070a13f97b2) Co-authored-by: Stan Ulbrych --- Doc/library/tempfile.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index 0ee2bf7f3c4db3d..874612994aad3a4 100644 --- a/Doc/library/tempfile.rst +++ b/Doc/library/tempfile.rst @@ -106,6 +106,11 @@ The module defines the following user-callable items: The latter approach is recommended as it provides assistance in automatic cleaning of the temporary file upon the context manager exit. + .. warning:: + + Reopening the temporary file by name is only safe when the file's + directory is trusted. See :cwe:`377` for more information. + Opening the temporary file again by its name while it is still open works as follows: @@ -422,6 +427,7 @@ Here are some examples of typical usage of the :mod:`!tempfile` module:: # create a temporary file using a context manager # close the file, use the name to open the file again + # this is only safe if the directory cannot be modified by untrusted users >>> with tempfile.NamedTemporaryFile(delete_on_close=False) as fp: ... fp.write(b'Hello world!') ... fp.close()