Skip to content

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-N Shrey-N commented Mar 3, 2026

Fixes #145417

When copying scripts in venv that don't need variables replaced (like Activate.ps1), shutil.copy2 was incorrectly preserving extended system metadata, including the bin_t SELinux context from the system template directory.

This changes shutil.copy2 to shutil.copy, which aligns the behavior with the fallback else block (which uses shutil.copymode).

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

@Shrey-N Shrey-N requested review from FFY00 and vsajip as code owners March 3, 2026 08:11
@python-cla-bot
Copy link

python-cla-bot bot commented Mar 3, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

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

Copy link
Contributor

@hroncok hroncok left a comment

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

hroncok commented Mar 3, 2026

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

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

@Shrey-N
Copy link
Author

Shrey-N commented Mar 3, 2026

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

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

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

@Shrey-N
Copy link
Author

Shrey-N commented Mar 3, 2026

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

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

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

Copy link
Contributor

@hroncok hroncok 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 use Activate.ps1 in the test. Something like this:

  1. check the mtime of the Activate.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 the Activate.ps1 file in the venv mtime differs from 1.
  5. assert the mode of both Activate.ps1s match (asserting this has not regressed)
  6. assert the Activate.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

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

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

@Shrey-N
Copy link
Author

Shrey-N commented Mar 3, 2026

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

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

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

@bedevere-app
Copy link

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip 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

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

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

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

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

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

@bedevere-app
Copy link

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

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

@bedevere-app
Copy link

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

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

@Shrey-N
Copy link
Author

Shrey-N commented Mar 3, 2026

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

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip 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

bedevere-app bot commented Mar 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

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

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

Shrey-N commented Mar 3, 2026

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

hroncok commented Mar 3, 2026

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

@Shrey-N
Copy link
Author

Shrey-N commented Mar 3, 2026

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 free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

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