Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-145417: Do not preserve SELinux context when copying venv scripts#145454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Shrey-N wants to merge17 commits intopython:main
base:main
Choose a base branch
Loading
fromShrey-N:main
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
17 commits
Select commitHold shift + click to select a range
de24338
Update __init__.py
Shrey-NMar 3, 2026
ec2958b
Implement test for script installation mtime
Shrey-NMar 3, 2026
1428e75
Relint test_venv.py and fix trailing whitespace
Shrey-NMar 3, 2026
8778209
Refine venv test to use Activate.ps1 and check mode (gh-145417)
Shrey-NMar 3, 2026
52ee5ef
Add news entry
Shrey-NMar 3, 2026
eec4e46
Rename News
Shrey-NMar 3, 2026
c4d32d5
Delete Misc/NEWS.d/next/Library/gh-issue-145417.shrey.rst
Shrey-NMar 3, 2026
88fae45
Apply maintainer's suggestion for docstring clarity
Shrey-NMar 3, 2026
7dc69f0
Move template protection check before content assertion
Shrey-NMar 3, 2026
878d3b8
Merge branch 'python:main' into main
Shrey-NMar 3, 2026
75bd937
Clean up blank lines in test_venv.py
Shrey-NMar 3, 2026
a76da3e
Enhance test for Activate.ps1 file integrity
Shrey-NMar 3, 2026
89c0c92
📜🤖 Added by blurb_it.
blurb-it[bot]Mar 3, 2026
431c4ac
Applying maintainer's suggestion for news wording
Shrey-NMar 3, 2026
9bcf6db
Change Location of time module
Shrey-NMar 3, 2026
ead93d7
Merge branch 'main' into main
Shrey-NMar 3, 2026
97652be
Reorder imports alphabetically
Shrey-NMar 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletionLib/test/test_venv.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,12 +11,13 @@
import os.path
import pathlib
import re
import shlex
import shutil
import subprocess
import sys
import sysconfig
import tempfile
importshlex
importtime
from test.support import (captured_stdout, captured_stderr,
skip_if_broken_multiprocessing_synchronize, verbose,
requires_subprocess, is_android, is_apple_mobile,
Expand DownExpand Up@@ -373,6 +374,51 @@ def create_contents(self, paths, filename):
with open(fn, 'wb') as f:
f.write(b'Still here?')

def test_install_scripts_mtime(self):
"""
Test that install_scripts does not preserve mtime when copying scripts.
Using mtime serves as a proxy to verify that shutil.copy2/copystat
is not used during script installation,
incorrectly copying e.g. SELinux bin_t context.
See gh-145417.
"""
venv_dir = os.path.dirname(venv.__file__)
src_path = os.path.join(venv_dir, 'scripts', 'common', 'Activate.ps1')
src_mtime = os.path.getmtime(src_path)

# Ensure a temporal difference between src and dst creation
if abs(time.time() - src_mtime) < 1.0:
time.sleep(1.1)

rmtree(self.env_dir)
venv.create(self.env_dir)

dst_path = os.path.join(self.env_dir, self.bindir, 'Activate.ps1')
self.assertTrue(os.path.exists(dst_path), "Activate.ps1 not found in venv")
dst_mtime = os.path.getmtime(dst_path)

# shutil.copy should update mtime, whereas shutil.copy2 would preserve it
self.assertNotEqual(src_mtime, dst_mtime,
"mtime was preserved, meaning shutil.copy2 was used")

# Permissions and content should still match
src_stat = os.stat(src_path)
dst_stat = os.stat(dst_path)
self.assertEqual(src_stat.st_mode, dst_stat.st_mode, "File modes do not match")

with open(src_path, 'rb') as f:
src_data = f.read()

# Protection against the file becoming a template in the future
self.assertNotIn(b'__VENV_PYTHON__', src_data,
"Test assumes Activate.ps1 is a static file, not a template")
Comment on lines +413 to +414
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggestion: Move this before the equality check. It's unlikely the files will be identical when this happens, and this assertion is more meaningful than the previous one.


with open(dst_path, 'rb') as f:
dst_data = f.read()

self.assertEqual(src_data, dst_data, "File contents do not match")


def test_overwrite_existing(self):
"""
Test creating environment in an existing directory.
Expand Down
2 changes: 1 addition & 1 deletionLib/venv/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -581,7 +581,7 @@ def skip_file(f):
'may be binary: %s',srcfile,e)
continue
ifnew_data==data:
shutil.copy2(srcfile,dstfile)
shutil.copy(srcfile,dstfile)
else:
withopen(dstfile,'wb')asf:
f.write(new_data)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Prevent incorrect preservation of SELinux context when copying scripts in :mod:`venv`.
Loading

[8]ページ先頭

©2009-2026 Movatter.jp