Skip to content

Commit 0b01d32

Browse files
tonghuarootmiss-islington
authored andcommitted
gh-127172: Honor umask and default ACL when installing venv activation scripts (GH-154850)
venv wrote the activation scripts with a fixed mode copied from the packaged templates, so they ignored the process umask and any default ACL, unlike every other file venv creates. Copy only the data and let the new files inherit the umask and ACL. (cherry picked from commit 1086205) Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
1 parent 6e7dab7 commit 0b01d32

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎Lib/test/test_venv.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,27 @@ def test_install_scripts_selinux(self):
383383
venv.create(self.env_dir)
384384
listxattr_mock.assert_not_called()
385385

386+
@unittest.skipIf(os.name == 'nt', 'POSIX-specific permission test')
387+
def test_install_scripts_honors_umask(self):
388+
"""
389+
gh-127172: Activation scripts must inherit the process umask (and any
390+
default ACL) like every other created file, rather than a fixed mode
391+
copied from the source template.
392+
"""
393+
old_umask = os.umask(0o002)
394+
try:
395+
venv.create(self.env_dir, with_pip=False)
396+
bindir = os.path.join(self.env_dir, self.bindir)
397+
control = os.path.join(bindir, 'control')
398+
with open(control, 'w'):
399+
pass
400+
expected = os.stat(control).st_mode & 0o777
401+
for name in ('activate', 'activate.csh', 'activate.fish'):
402+
mode = os.stat(os.path.join(bindir, name)).st_mode & 0o777
403+
self.assertEqual(mode, expected)
404+
finally:
405+
os.umask(old_umask)
406+
386407
def test_overwrite_existing(self):
387408
"""
388409
Test creating environment in an existing directory.

‎Lib/venv/__init__.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,10 @@ def skip_file(f):
584584
'may be binary: %s', srcfile, e)
585585
continue
586586
if new_data == data:
587-
shutil.copy(srcfile, dstfile)
587+
shutil.copyfile(srcfile, dstfile)
588588
else:
589589
with open(dstfile, 'wb') as f:
590590
f.write(new_data)
591-
shutil.copymode(srcfile, dstfile)
592591

593592
def upgrade_dependencies(self, context):
594593
logger.debug(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Creating a virtual environment with :mod:`venv` now writes the activation
2+
scripts using permissions derived from the process umask (and any default
3+
ACL), consistent with the other files it creates, instead of a fixed mode
4+
copied from the packaged templates. Patch by tonghuaroot.

0 commit comments

Comments
 (0)