Skip to content

Commit c90e7fe

Browse files
committed
gh-160782: reject line breaks in the venv directory and prompt
1 parent 4a9b981 commit c90e7fe

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎Lib/test/test_venv.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,23 @@ def test_pathsep_error(self):
10751075
self.assertRaises(ValueError, venv.create, bad_itempath)
10761076
self.assertRaises(ValueError, venv.create, FakePath(bad_itempath))
10771077

1078+
def test_line_break_error(self):
1079+
"""
1080+
Test that venv creation fails when the target directory or the
1081+
prompt contains a line break.
1082+
"""
1083+
rmtree(self.env_dir)
1084+
for line_break in '\n', '\r':
1085+
with self.subTest(line_break=line_break):
1086+
bad_itempath = f'{self.env_dir}{line_break}x'
1087+
self.assertRaises(ValueError, venv.create, bad_itempath)
1088+
self.assertRaises(ValueError, venv.create,
1089+
FakePath(bad_itempath))
1090+
bad_prompt = f'x{line_break}x'
1091+
self.assertRaises(ValueError, venv.create, self.env_dir,
1092+
prompt=bad_prompt)
1093+
self.assertFalse(os.path.exists(self.env_dir))
1094+
10781095
@unittest.skipIf(os.name == 'nt', 'not relevant on Windows')
10791096
@requireVenvCreate
10801097
def test_zippath_from_non_installed_posix(self):

‎Lib/venv/__init__.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ def create_if_needed(d):
149149
if os.pathsep in os.fspath(env_dir):
150150
raise ValueError(f'Refusing to create a venv in {env_dir} because '
151151
f'it contains the PATH separator {os.pathsep}.')
152+
if '\n' in os.fspath(env_dir) or '\r' in os.fspath(env_dir):
153+
raise ValueError(f'Refusing to create a venv in {env_dir!r} '
154+
'because its path contains a line break.')
155+
if self.prompt is not None and ('\n' in self.prompt
156+
or '\r' in self.prompt):
157+
raise ValueError(f'Refusing to use the prompt {self.prompt!r} '
158+
'because it contains a line break.')
152159
if os.path.exists(env_dir) and self.clear:
153160
self.clear_directory(env_dir)
154161
context = types.SimpleNamespace()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`venv`: a line break in the prompt or the path now raises
2+
:exc:`ValueError` instead of being written into ``pyvenv.cfg``.

0 commit comments

Comments
 (0)