diff --git a/plugins/workswarm/inferrsi/skills/inferrsi/scripts/inferrsi.py b/plugins/workswarm/inferrsi/skills/inferrsi/scripts/inferrsi.py index 95775914b..aa17796ec 100644 --- a/plugins/workswarm/inferrsi/skills/inferrsi/scripts/inferrsi.py +++ b/plugins/workswarm/inferrsi/skills/inferrsi/scripts/inferrsi.py @@ -73,7 +73,9 @@ def cache_runtime(manifest_bytes: bytes, launcher: bytes, content: dict[str, byt base = Path(os.environ.get("LOCALAPPDATA") or Path.home() / ".cache") if not base.is_absolute(): raise ValueError("Runtime cache base must be absolute, outside the target repository") - cache_parent = base / "InferRSI" / "workswarm-runtime" + # Extend the base before parent I/O, keeping owned children unresolved so + # _plain still rejects a linked InferRSI or workswarm-runtime directory. + cache_parent = _io_path(base) / "InferRSI" / "workswarm-runtime" for directory in (cache_parent.parent, cache_parent): directory.mkdir(mode=0o700, parents=True, exist_ok=True) _plain(directory) @@ -81,7 +83,6 @@ def cache_runtime(manifest_bytes: bytes, launcher: bytes, content: dict[str, byt attributes = directory.stat() if attributes.st_uid != os.getuid() or attributes.st_mode & 0o022: raise ValueError("Runtime cache must be owned by this user and not shared writable") - cache_parent = _io_path(cache_parent) identity = hashlib.sha256(manifest_bytes + launcher).hexdigest() root = cache_parent / identity expected = {"runtime/" + name: data for name, data in content.items()} diff --git a/tests/test_workswarm_runtime.py b/tests/test_workswarm_runtime.py index f826c4a9e..908b92957 100644 --- a/tests/test_workswarm_runtime.py +++ b/tests/test_workswarm_runtime.py @@ -82,6 +82,22 @@ def test_cache_is_persistent_and_concurrent_first_use_is_atomic(cached_runtime): assert not list(root.parent.glob(".staging-*")) +def test_cache_supports_long_absolute_base(cached_runtime, tmp_path, monkeypatch): + base = tmp_path / "cache with spaces" / "\u4e2d\u6587" + while len(str(base)) < 310: + base /= "controlled-profile-segment" + monkeypatch.setenv("LOCALAPPDATA", str(base)) + + first = cached_runtime() + assert first == cached_runtime() + root = first.parents[3] + assert (root / "runtime/inferrsi/data/reference.md").read_bytes() == b"persistent resource" + assert not list(root.parent.glob(".staging-*")) + (root / "runtime/inferrsi/__init__.py").write_bytes(b"must not execute") + with pytest.raises(ValueError, match="cache verification failed"): + cached_runtime() + + @pytest.mark.parametrize("tamper", ["changed", "missing", "extra"]) def test_cache_rejects_modified_missing_or_extra_files(cached_runtime, tamper): root = cached_runtime().parents[3] @@ -96,11 +112,14 @@ def test_cache_rejects_modified_missing_or_extra_files(cached_runtime, tamper): cached_runtime() -def test_cache_rejects_linked_parent_before_writing(cached_runtime, tmp_path): +@pytest.mark.parametrize("linked", ["InferRSI", "InferRSI/workswarm-runtime"]) +def test_cache_rejects_linked_parent_before_writing(cached_runtime, tmp_path, linked): outside = tmp_path / "outside" outside.mkdir() + link = tmp_path / linked + link.parent.mkdir(mode=0o700, parents=True, exist_ok=True) try: - (tmp_path / "InferRSI").symlink_to(outside, target_is_directory=True) + link.symlink_to(outside, target_is_directory=True) except OSError: pytest.skip("Creating directory symlinks is unavailable") with pytest.raises(ValueError, match="links or reparse points"):