tmpdir: prevent using a non-private root temp directory
pytest uses a root temp directory named `/tmp/pytest-of-<username>`. The name is predictable, and the directory might already exists from a previous run, so that's allowed. This makes it possible for my_user to pre-create `/tmp/pytest-of-another_user`, thus giving my_user control of another_user's tempdir. Prevent this scenario by adding a couple of safety checks. I believe they are sufficient. Testing the first check requires changing the owner, which requires root permissions, so can't be unit-tested easily, but I checked it manually.
This commit is contained in:
@@ -461,3 +461,28 @@ def test_tmp_path_factory_create_directory_with_safe_permissions(
|
||||
assert (basetemp.stat().st_mode & 0o077) == 0
|
||||
# Parent too (pytest-of-foo).
|
||||
assert (basetemp.parent.stat().st_mode & 0o077) == 0
|
||||
|
||||
|
||||
@pytest.mark.skipif(not hasattr(os, "getuid"), reason="checks unix permissions")
|
||||
def test_tmp_path_factory_fixes_up_world_readable_permissions(
|
||||
tmp_path: Path, monkeypatch,
|
||||
) -> None:
|
||||
"""Verify that if a /tmp/pytest-of-foo directory already exists with
|
||||
world-readable permissions, it is fixed.
|
||||
|
||||
pytest used to mkdir with such permissions, that's why we fix it up.
|
||||
"""
|
||||
# Use the test's tmp_path as the system temproot (/tmp).
|
||||
monkeypatch.setenv("PYTEST_DEBUG_TEMPROOT", str(tmp_path))
|
||||
tmp_factory = TempPathFactory(None, lambda *args: None, _ispytest=True)
|
||||
basetemp = tmp_factory.getbasetemp()
|
||||
|
||||
# Before - simulate bad perms.
|
||||
os.chmod(basetemp.parent, 0o777)
|
||||
assert (basetemp.parent.stat().st_mode & 0o077) != 0
|
||||
|
||||
tmp_factory = TempPathFactory(None, lambda *args: None, _ispytest=True)
|
||||
basetemp = tmp_factory.getbasetemp()
|
||||
|
||||
# After - fixed.
|
||||
assert (basetemp.parent.stat().st_mode & 0o077) == 0
|
||||
|
||||
Reference in New Issue
Block a user