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

Open
Shrey-N wants to merge 17 commits intopython:mainfrom
Shrey-N:main
Open

gh-145417: Do not preserve SELinux context when copying venv scripts#145454
Shrey-N wants to merge 17 commits intopython:mainfrom
Shrey-N:main

Conversation

@Shrey-N
Copy link

@Shrey-NShrey-N commentedMar 3, 2026
edited by bedevere-appbot
Loading

Fixes#145417

When copying scripts invenv that don't need variables replaced (likeActivate.ps1),shutil.copy2 was incorrectly preserving extended system metadata, including thebin_t SELinux context from the system template directory.

This changesshutil.copy2 toshutil.copy, which aligns the behavior with the fallbackelse block (which usesshutil.copymode).

This ensures that only the file contents and permission mode are copied, while safely inheriting the SELinux context of the destination project directory.

@python-cla-bot
Copy link

python-cla-botbot commentedMar 3, 2026
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

Copy link
Contributor

@hroncokhroncok left a comment
edited
Loading

Choose a reason for hiding this comment

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

This way, I believe the mode is not copied, and the suggested change needs to be done as well.

@hroncok
Copy link
Contributor

OK,shutil.copy actually callsshutil.copymode, so the only change required is indeed to changeshutil.copy2 toshutil.copy. This works.

However, I was tryign to figure out how to properly test this.

@Shrey-N
Copy link
Author

Apologies@hroncok I completely missed that you were planning to handle this submission yourself, I am a new contributor, and I was eager to help out and learn about the workflow... Regarding the tests I think your idea of asserting the mtime is a nice approach, since shutil.copy should update the timestamp while copy2 would preserve it.

I am happy to either close this PR, or leave it here if it is a useful starting point. Thank you for your patience I am looking forward to this learning process!

Add a test to verify mtime behavior during script installation.
@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

@Shrey-N
Copy link
Author

I went ahead and added a test case into this PR using the mtime approach, It should cover the verification for metadata prevention now.

Address linting failures in the new test_install_scripts_mtime by removing redundant imports and fixing trailing whitespace in the docstrings.
@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

Copy link
Contributor

@hroncokhroncok left a comment

Choose a reason for hiding this comment

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

I think it might be better to actually useActivate.ps1 in the test. Something like this:

  1. check the mtime of theActivate.ps1 file in the stdlib
  2. if the current time is the same (unlikely, but possible), sleep 1 second
  3. create a virtual environment
  4. assert theActivate.ps1 file in the venv mtime differs from 1.
  5. assert the mode of bothActivate.ps1s match (asserting this has not regressed)
  6. assert theActivate.ps1 contents match, so we know we are testing with a proper file (this assert will fail if the file later becomes a template, which serves as a protection against a no longer functional test)

"""
Test that install_scripts does not preserve mtime when copying scripts.
Using mtime serves as a proxy to verify that shutil.copy2 (and thus
SELinux bin_t contexts) is not being used during script installation.
Copy link
Contributor

Choose a reason for hiding this comment

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

It's useful to link the issue here so people reading this know why it matters.

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

@Shrey-N
Copy link
Author

Thank you for the detailed feedback@hroncok ! I have updated the test case to use the Activate.ps1 from the stdlib and followed your plan, including the mode and content assertions. I also added the issue link to the docstring. Let me know if everything looks good now!

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

Comment on lines +413 to +414
self.assertNotIn(b'__VENV_PYTHON__', src_data,
"Test assumes Activate.ps1 is a static file, not a template")
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.

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

Co-authored-by: Miro Hrončok <miro@hroncok.cz>
@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

@Shrey-N
Copy link
Author

I fixed the order of the checks so the template protection happens first I think that would make sense if something fails.

I also applied your suggestion for the docstring, I have updated the branch too!

Thanks for the help and the mentorship! Let me know if everything looks good to you.@hroncok

Removed unnecessary blank lines in the test case.
@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

Added a temporal check to ensure the source file is not modified during the test. Enhanced assertions to protect against future changes in the Activate.ps1 file's structure.
@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

Co-authored-by: Miro Hrončok <miro@hroncok.cz>
@Shrey-N
Copy link
Author

Accepted! I have updated the branch and everything is passing now.

Thanks again for the help and the mentorship throughout this I really appreciate the guidance!@hroncok

incorrectly copying e.g. SELinux bin_t context.
See gh-145417.
"""
import time
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps move this to the toplevel imports. All the other unconditional imports are there, at the beginning of the file.

@hroncok
Copy link
Contributor

Thanks. I like the test, and the change fixes the reported problem.

@Shrey-N
Copy link
Author

I have moved the import time to the top level as you suggested. Everything should be good now!

Thank you so much for the detailed review and for all the guidance today it means a lot to me!@hroncok

Co-authored-by: Miro Hrončok <miro@hroncok.cz>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@vsajipvsajipAwaiting requested review from vsajipvsajip is a code owner

@FFY00FFY00Awaiting requested review from FFY00FFY00 is a code owner

1 more reviewer

@hroncokhroncokhroncok left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

python -m venv incorrectly preserves bin_t SELinux context on Activate.ps1 from system templates

2 participants

@Shrey-N@hroncok

[8]ページ先頭

©2009-2026 Movatter.jp