feat(remote): warn when the client cache has more files than a server takes

This commit is contained in:
imSp4rky
2026-11-24 18:53:38 -07:00
parent 126283b256
commit 2dfed221d5
3 changed files with 32 additions and 2 deletions
+1 -1
View File
@@ -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 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 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 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. existing file as a directory, and logs a warning.
### Remote session info response ### Remote session info response
@@ -358,6 +358,31 @@ def test_load_cache_files_wiring(tmp_path, monkeypatch) -> None:
assert set(sent) == {"tokens_default", "session_guid"} 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: def test_load_cache_files_forwards_nested_keys(tmp_path, monkeypatch) -> None:
"""A service that keeps its cache in subfolders (session_web/<sha1>.json) must have """A service that keeps its cache in subfolders (session_web/<sha1>.json) must have
those files forwarded under the Cacher key, and the credential filter must still apply those files forwarded under the Cacher key, and the credential filter must still apply
+6 -1
View File
@@ -1963,7 +1963,7 @@ class RemoteService:
""" """
import zlib 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 cache_dir = config.directories.cache / self.service_tag
if not cache_dir.is_dir(): if not cache_dir.is_dir():
@@ -1979,6 +1979,11 @@ class RemoteService:
self.log.debug(f"Withholding cache file from the remote server: {key}") self.log.debug(f"Withholding cache file from the remote server: {key}")
continue continue
files[key] = base64.b64encode(zlib.compress(f.read_bytes())).decode("ascii") 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 return files