From 23c6c8e95b1cb3eeb09f9948c9b319225de8482b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Sun, 29 Nov 2026 15:12:35 -0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix(discovery):=20probe=20ca?= =?UTF-8?q?se=20in=20private=20dir?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The case probe lower-cased the whole absolute path of its temporary file, so the lookup left the probe's directory and landed on a name in the shared temp directory. Another local user could create that name and make discovery raise PermissionError, or cache a false case-insensitive answer. The probe now runs inside a TemporaryDirectory and lower-cases only the file name, so the lookup stays in a directory only the caller can write. Reported by oss-scanner as ANT-2026-B0M9PD96. --- .../+case-probe-private-dir.security.rst | 2 + src/python_discovery/_compat.py | 7 +++- tests/python_discovery/test_compat.py | 38 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 docs/changelog/+case-probe-private-dir.security.rst create mode 100644 tests/python_discovery/test_compat.py diff --git a/docs/changelog/+case-probe-private-dir.security.rst b/docs/changelog/+case-probe-private-dir.security.rst new file mode 100644 index 0000000..35f187e --- /dev/null +++ b/docs/changelog/+case-probe-private-dir.security.rst @@ -0,0 +1,2 @@ +Discovery no longer fails with :exc:`PermissionError`, or misdetects file-system case sensitivity, when the shared +temporary directory contains entries created by another user. diff --git a/src/python_discovery/_compat.py b/src/python_discovery/_compat.py index f9764df..bd28594 100644 --- a/src/python_discovery/_compat.py +++ b/src/python_discovery/_compat.py @@ -23,8 +23,11 @@ def fs_path_id(path: str) -> str: @functools.lru_cache(maxsize=1) def fs_is_case_sensitive() -> bool: - with tempfile.NamedTemporaryFile(prefix="TmP") as tmp_file: - result = not pathlib.Path(tmp_file.name.lower()).exists() + # Probe in a private directory: the shared temp dir may hold another user's lower-cased twin of the name. + with tempfile.TemporaryDirectory() as folder: + probe = pathlib.Path(folder, "TmP") + probe.touch() + result = not probe.with_name(probe.name.lower()).exists() _LOGGER.debug("filesystem is %scase-sensitive", "" if result else "not ") return result diff --git a/tests/python_discovery/test_compat.py b/tests/python_discovery/test_compat.py new file mode 100644 index 0000000..73f84f9 --- /dev/null +++ b/tests/python_discovery/test_compat.py @@ -0,0 +1,38 @@ +from __future__ import annotations + +import tempfile +from pathlib import Path +from typing import TYPE_CHECKING + +import pytest + +from python_discovery._compat import fs_is_case_sensitive + +if TYPE_CHECKING: + from collections.abc import Generator + + from pytest_mock import MockerFixture + + +@pytest.fixture +def _fresh_case_sensitivity_probe() -> Generator[None]: + fs_is_case_sensitive.cache_clear() + yield + fs_is_case_sensitive.cache_clear() + + +@pytest.mark.usefixtures("_fresh_case_sensitivity_probe") +def test_fs_is_case_sensitive_probes_only_inside_private_directory( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture +) -> None: + shared = tmp_path / "ShaRed" + shared.mkdir() + monkeypatch.setattr(tempfile, "tempdir", str(shared)) + exists = mocker.spy(Path, "exists") + + fs_is_case_sensitive() + + exists.assert_called_once() + probed = exists.call_args.args[0] + assert probed.name == "tmp" + assert probed.parent.parent == shared From 202bd1dc1241d2a35dec9e4cdf477034c9f41404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Sun, 29 Nov 2026 15:19:45 -0800 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=85=20test(compat):=20assert=20the=20?= =?UTF-8?q?probe's=20answer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test only checked which path Path.exists received, so it still passed with the result inverted. It now plants the lower-cased twin of the probe file in the shared temp directory and asserts the probe matches the host file system. --- tests/python_discovery/test_compat.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/python_discovery/test_compat.py b/tests/python_discovery/test_compat.py index 73f84f9..f75d8cc 100644 --- a/tests/python_discovery/test_compat.py +++ b/tests/python_discovery/test_compat.py @@ -1,7 +1,6 @@ from __future__ import annotations import tempfile -from pathlib import Path from typing import TYPE_CHECKING import pytest @@ -10,6 +9,7 @@ if TYPE_CHECKING: from collections.abc import Generator + from pathlib import Path from pytest_mock import MockerFixture @@ -22,17 +22,15 @@ def _fresh_case_sensitivity_probe() -> Generator[None]: @pytest.mark.usefixtures("_fresh_case_sensitivity_probe") -def test_fs_is_case_sensitive_probes_only_inside_private_directory( +def test_fs_is_case_sensitive_ignores_lower_cased_twin_in_shared_temp_dir( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture ) -> None: - shared = tmp_path / "ShaRed" + (case_probe := tmp_path / "CaSe").touch() + expected = not case_probe.with_name("case").exists() + shared = tmp_path / "shared" shared.mkdir() + (shared / "tmpabcdef").touch() monkeypatch.setattr(tempfile, "tempdir", str(shared)) - exists = mocker.spy(Path, "exists") + mocker.patch("tempfile._get_candidate_names", autospec=True, return_value=iter(["AbCdEf", "GhIjKl"])) - fs_is_case_sensitive() - - exists.assert_called_once() - probed = exists.call_args.args[0] - assert probed.name == "tmp" - assert probed.parent.parent == shared + assert fs_is_case_sensitive() is expected