Skip to content

Commit 895aa09

Browse files
committed
gh-79459: Sanitize the prefix and suffix parameters to the tempfile functions:
- `tempfile.mkdtemp`. - `tempfile.mkstemp`. - `tempfile.NamedTemporaryFile`.
1 parent a126893 commit 895aa09

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

Doc/whatsnew/3.15.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,13 @@ New deprecations
11731173

11741174
(Contributed by Bénédikt Tran in :gh:`134978`.)
11751175

1176+
* :mod:`tempfile`:
1177+
1178+
* The ``prefix`` and ``suffix`` parameters of the tempfile functions,
1179+
:func:`tempfile.mkdtemp`, :func:`tempfile.mkstemp` and
1180+
:func:`tempfile.NamedTemporaryFile`, will be sanitized to use only the
1181+
basename of the provided values if they contain a directory separator.
1182+
11761183
* ``__version__``
11771184

11781185
* The ``__version__``, ``version`` and ``VERSION`` attributes have been

Lib/tempfile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,15 @@ def _sanitize_params(prefix, suffix, dir):
116116
output_type = _infer_return_type(prefix, suffix, dir)
117117
if suffix is None:
118118
suffix = output_type()
119+
elif _os.path.dirname(suffix):
120+
suffix = _os.path.basename(suffix)
119121
if prefix is None:
120122
if output_type is str:
121123
prefix = template
122124
else:
123125
prefix = _os.fsencode(template)
126+
elif _os.path.dirname(prefix):
127+
prefix = _os.path.basename(prefix)
124128
if dir is None:
125129
if output_type is str:
126130
dir = gettempdir()

Lib/test/test_tempfile.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,10 @@ def do_create(self, dir=None, pre=None, suf=None):
625625
dir = tempfile.gettempdirb()
626626
if pre is None:
627627
pre = output_type()
628+
pre = os.path.basename(pre)
628629
if suf is None:
629630
suf = output_type()
631+
suf = os.path.basename(suf)
630632
(fd, name) = tempfile.mkstemp(dir=dir, prefix=pre, suffix=suf)
631633
(ndir, nbase) = os.path.split(name)
632634
adir = os.path.abspath(dir)
@@ -647,6 +649,10 @@ def test_basic(self):
647649
self.do_create(pre="a", suf="b")
648650
self.do_create(pre="aa", suf=".txt")
649651
self.do_create(dir=".")
652+
self.do_create(pre=f"{os.sep}myhome")
653+
self.do_create(pre=os.fsencode(f"{os.sep}home"))
654+
self.do_create(suf=f"{os.sep}home")
655+
self.do_create(suf=os.fsencode(f"{os.sep}home"))
650656

651657
def test_basic_with_bytes_names(self):
652658
# mkstemp can create files when given name parts all
@@ -724,6 +730,8 @@ def do_create(self, dir=None, pre=None, suf=None):
724730
pre = output_type()
725731
if suf is None:
726732
suf = output_type()
733+
pre = os.path.basename(pre)
734+
suf = os.path.basename(suf)
727735
name = tempfile.mkdtemp(dir=dir, prefix=pre, suffix=suf)
728736

729737
try:
@@ -740,6 +748,10 @@ def test_basic(self):
740748
os.rmdir(self.do_create(suf="b"))
741749
os.rmdir(self.do_create(pre="a", suf="b"))
742750
os.rmdir(self.do_create(pre="aa", suf=".txt"))
751+
os.rmdir(self.do_create(pre=f"{os.sep}home"))
752+
os.rmdir(self.do_create(pre=os.fsencode(f"{os.sep}home")))
753+
os.rmdir(self.do_create(suf=f"{os.sep}home"))
754+
os.rmdir(self.do_create(suf=os.fsencode(f"{os.sep}home")))
743755

744756
def test_basic_with_bytes_names(self):
745757
# mkdtemp can create directories when given all binary parts
@@ -943,9 +955,19 @@ def test_many(self):
943955
class TestNamedTemporaryFile(BaseTestCase):
944956
"""Test NamedTemporaryFile()."""
945957

946-
def do_create(self, dir=None, pre="", suf="", delete=True):
958+
def do_create(self, dir=None, pre=None, suf=None, delete=True):
959+
output_type = tempfile._infer_return_type(dir, pre, suf)
947960
if dir is None:
948-
dir = tempfile.gettempdir()
961+
if output_type is str:
962+
dir = tempfile.gettempdir()
963+
else:
964+
dir = tempfile.gettempdirb()
965+
if pre is None:
966+
pre = output_type()
967+
if suf is None:
968+
suf = output_type()
969+
pre = os.path.basename(pre)
970+
suf = os.path.basename(suf)
949971
file = tempfile.NamedTemporaryFile(dir=dir, prefix=pre, suffix=suf,
950972
delete=delete)
951973

@@ -960,6 +982,10 @@ def test_basic(self):
960982
self.do_create(suf="b")
961983
self.do_create(pre="a", suf="b")
962984
self.do_create(pre="aa", suf=".txt")
985+
self.do_create(pre=f"{os.sep}home")
986+
self.do_create(pre=os.fsencode(f"{os.sep}home"))
987+
self.do_create(suf=f"{os.sep}home")
988+
self.do_create(suf=os.fsencode(f"{os.sep}home"))
963989

964990
def test_method_lookup(self):
965991
# Issue #18879: Looking up a temporary file method should keep it

0 commit comments

Comments
 (0)