From 2dfed221d507294a451c1a1f64a2bfe6bd174e53 Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Tue, 24 Nov 2026 18:40:36 -0700 Subject: [PATCH] feat(remote): warn when the client cache has more files than a server takes --- docs/dev/rest-api/remote-sessions.md | 2 +- .../unit/test_remote_service_helpers.py | 25 +++++++++++++++++++ unshackle/core/remote_service.py | 7 +++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/dev/rest-api/remote-sessions.md b/docs/dev/rest-api/remote-sessions.md index 2686f989..ccdb9809 100644 --- a/docs/dev/rest-api/remote-sessions.md +++ b/docs/dev/rest-api/remote-sessions.md @@ -663,7 +663,7 @@ suffix, which is the `Cacher` cache key the service reads it with: Both sides reject a cache key with a drive letter, a root, a `..` segment, an empty segment, or more than 8 segments before it becomes a path, so a peer cannot write outside the cache directory. Both sides also refuse a `cache` map with more than -256 entries, and the server refuses the request body of a remote session creation or of a search when it is larger than 8 MiB. The server skips a cache key it cannot write, such as one that names an +256 entries, and the server refuses the request body of a remote session creation or of a search when it is larger than 8 MiB. The client gives a warning when it has more than 256 cache files to send. The server skips a cache key it cannot write, such as one that names an existing file as a directory, and logs a warning. ### Remote session info response diff --git a/tests/remote/unit/test_remote_service_helpers.py b/tests/remote/unit/test_remote_service_helpers.py index 6bf809ce..dd872da4 100644 --- a/tests/remote/unit/test_remote_service_helpers.py +++ b/tests/remote/unit/test_remote_service_helpers.py @@ -358,6 +358,31 @@ def test_load_cache_files_wiring(tmp_path, monkeypatch) -> None: assert set(sent) == {"tokens_default", "session_guid"} +@pytest.mark.parametrize("extra, warned", [(0, False), (1, True)]) +def test_load_cache_files_warns_above_the_server_limit(tmp_path, monkeypatch, caplog, extra, warned) -> None: + import logging + from types import SimpleNamespace + + from unshackle.core.api.sanitize import MAX_SESSION_CACHE_KEYS + from unshackle.core.config import config + from unshackle.core.remote_service import RemoteService + + cache_dir = tmp_path / "TESTSVC" + cache_dir.mkdir() + count = MAX_SESSION_CACHE_KEYS + extra + for i in range(count): + (cache_dir / f"state{i}.json").write_text("{}") + monkeypatch.setattr(config.directories, "cache", tmp_path) + monkeypatch.setattr(config, "credentials", {}) + stub = SimpleNamespace(service_tag="TESTSVC", log=logging.getLogger("test")) + + with caplog.at_level(logging.WARNING, logger="test"): + sent = RemoteService.load_cache_files(stub, None) + + assert len(sent) == count + assert (f"has {count} files to send" in caplog.text) is warned + + def test_load_cache_files_forwards_nested_keys(tmp_path, monkeypatch) -> None: """A service that keeps its cache in subfolders (session_web/.json) must have those files forwarded under the Cacher key, and the credential filter must still apply diff --git a/unshackle/core/remote_service.py b/unshackle/core/remote_service.py index 3b460005..f9f699b5 100644 --- a/unshackle/core/remote_service.py +++ b/unshackle/core/remote_service.py @@ -1963,7 +1963,7 @@ class RemoteService: """ import zlib - from unshackle.core.api.sanitize import safe_cache_key + from unshackle.core.api.sanitize import MAX_SESSION_CACHE_KEYS, safe_cache_key cache_dir = config.directories.cache / self.service_tag if not cache_dir.is_dir(): @@ -1979,6 +1979,11 @@ class RemoteService: self.log.debug(f"Withholding cache file from the remote server: {key}") continue files[key] = base64.b64encode(zlib.compress(f.read_bytes())).decode("ascii") + if len(files) > MAX_SESSION_CACHE_KEYS: + self.log.warning( + f"The {self.service_tag} cache has {len(files)} files to send and a remote server takes " + f"{MAX_SESSION_CACHE_KEYS}: remove old files from {cache_dir}" + ) return files