From e4e694c70776f992ae725b847267c04fec865497 Mon Sep 17 00:00:00 2001 From: godara-alt Date: Fri, 20 Nov 2026 23:03:01 +0530 Subject: [PATCH 1/3] fix: normalize local store paths across fsspec versions --- pyproject.toml | 3 ++- src/datajoint/storage.py | 8 +++++--- tests/unit/test_path_confinement.py | 32 ++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 75789801c..93aa9dd2c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -251,7 +251,8 @@ skip = ".git,*.pdf,*.svg,*.csv,*.ipynb,*.drawio" # Rever -- nobody knows # numer -- numerator variable # astroid -- Python library name (not "asteroid") -ignore-words-list = "rever,numer,astroid" +# acter -- optional "acter" fragment in the character-type regex +ignore-words-list = "rever,numer,astroid,acter" [tool.pytest.ini_options] markers = [ diff --git a/src/datajoint/storage.py b/src/datajoint/storage.py index 0b8d88132..5f45a1a3f 100644 --- a/src/datajoint/storage.py +++ b/src/datajoint/storage.py @@ -505,9 +505,11 @@ def _full_path(self, path: str | PurePosixPath) -> str: # A location of "" or "." adds no prefix, and fsspec reads a leading "file:" or # "local:" as a protocol and expands "~", so compare where fsspec resolves both if self.confine_paths: - root = LocalFileSystem._strip_protocol(Path(location or ".").as_posix()).rstrip("/") - resolved = LocalFileSystem._strip_protocol(full) - if resolved != root and not resolved.startswith(root + "/"): + # Older fsspec versions can return relative paths from + # _strip_protocol(). Normalize both sides before comparing. + root = Path(os.path.abspath(location or ".")) + resolved = Path(os.path.abspath(LocalFileSystem._strip_protocol(full))) + if not resolved.is_relative_to(root): raise self._confinement_error(path) return full else: diff --git a/tests/unit/test_path_confinement.py b/tests/unit/test_path_confinement.py index efc3431b0..f7daa4cbd 100644 --- a/tests/unit/test_path_confinement.py +++ b/tests/unit/test_path_confinement.py @@ -10,6 +10,7 @@ No test reads a device or opens a pipe: a regression must fail, not hang. """ +import errno import importlib import os import sys @@ -130,6 +131,25 @@ def test_file_store_in_working_directory_rejects_protocol_and_home_paths(self, l backend.get_buffer("file://" + outside) assert backend.get_buffer("data/inside.txt") == b"inside" + @posix_only + @pytest.mark.parametrize("location", [".", ""]) + def test_file_store_with_relative_fsspec_path(self, layout, monkeypatch, location): + """Older fsspec can leave relative paths unexpanded.""" + from fsspec.implementations.local import LocalFileSystem + + _, store, _ = layout + monkeypatch.chdir(store) + original = LocalFileSystem._strip_protocol + + def old_strip_protocol(path): + if path in (".", "data/inside.txt"): + return path + return original(path) + + monkeypatch.setattr(LocalFileSystem, "_strip_protocol", staticmethod(old_strip_protocol)) + backend = StorageBackend({"protocol": "file", "location": location}) + assert backend._full_path("data/inside.txt") == "data/inside.txt" + def test_io_never_reaches_outside(self, layout): root, store, _ = layout backend = StorageBackend({"protocol": "file", "location": str(store)}) @@ -300,9 +320,15 @@ def test_download_into_directory_uses_last_component_only(self, cfg, layout, sub with pytest.raises(DataJointError, match="not a plain file name"): ref.download(downloads, subpath=subpath) else: - # Missing source file: the error comes from the store, after the name was accepted - with pytest.raises(FileNotFoundError): - ref.download(downloads, subpath=subpath) + # The source is missing. On Windows, a drive marker embedded inside + # a path is not a legal file name, so the OS rejects it before lookup. + if os.name == "nt" and subpath.startswith("sub/C:"): + with pytest.raises(OSError) as exc_info: + ref.download(downloads, subpath=subpath) + assert exc_info.value.errno == errno.EINVAL + else: + with pytest.raises(FileNotFoundError): + ref.download(downloads, subpath=subpath) assert not any(downloads.iterdir()) @pytest.fixture From de5c3d27a6c8d82239a436d1eb138d0c9d22a5c5 Mon Sep 17 00:00:00 2001 From: godara-alt Date: Fri, 20 Nov 2026 23:17:38 +0530 Subject: [PATCH 2/3] fix: enforce portable confinement for local file stores --- src/datajoint/storage.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/datajoint/storage.py b/src/datajoint/storage.py index 5f45a1a3f..627d37e40 100644 --- a/src/datajoint/storage.py +++ b/src/datajoint/storage.py @@ -505,11 +505,19 @@ def _full_path(self, path: str | PurePosixPath) -> str: # A location of "" or "." adds no prefix, and fsspec reads a leading "file:" or # "local:" as a protocol and expands "~", so compare where fsspec resolves both if self.confine_paths: - # Older fsspec versions can return relative paths from - # _strip_protocol(). Normalize both sides before comparing. - root = Path(os.path.abspath(location or ".")) - resolved = Path(os.path.abspath(LocalFileSystem._strip_protocol(full))) - if not resolved.is_relative_to(root): + # Reject URI schemes and home shortcuts before normalizing. + # fsspec may interpret them differently from the local OS. + if path.lower().startswith(("file:", "local:")) or path.startswith("~"): + raise self._confinement_error(path) + # Normalize older fsspec's relative paths, and compare with the + # OS-native path module (also when Path is mocked in tests). + root = os.path.normcase(os.path.abspath(location or ".")) + resolved = os.path.normcase(os.path.abspath(LocalFileSystem._strip_protocol(full))) + try: + confined = os.path.commonpath((root, resolved)) == root + except ValueError: + confined = False # Different Windows drives. + if not confined: raise self._confinement_error(path) return full else: From 2e51d9e5b983b71622b0034c8d5d290c02024a2c Mon Sep 17 00:00:00 2001 From: godara-alt Date: Fri, 20 Nov 2026 23:19:05 +0530 Subject: [PATCH 3/3] fix: normalize mocked Windows store locations consistently --- src/datajoint/storage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/datajoint/storage.py b/src/datajoint/storage.py index 627d37e40..4df976491 100644 --- a/src/datajoint/storage.py +++ b/src/datajoint/storage.py @@ -511,7 +511,7 @@ def _full_path(self, path: str | PurePosixPath) -> str: raise self._confinement_error(path) # Normalize older fsspec's relative paths, and compare with the # OS-native path module (also when Path is mocked in tests). - root = os.path.normcase(os.path.abspath(location or ".")) + root = os.path.normcase(os.path.abspath(Path(location or ".").as_posix())) resolved = os.path.normcase(os.path.abspath(LocalFileSystem._strip_protocol(full))) try: confined = os.path.commonpath((root, resolved)) == root