fix(remote): forward nested service cache files to and from the server

Cache keys are now the path relative to the service cache dir without .json, so Cacher keys like session_web/<sha1> reach the remote session. The credential filter runs on every path segment, the suffix is appended rather than substituted, and both sides cap the map at 64 keys.
This commit is contained in:
imSp4rky
2026-09-19 21:42:01 -06:00
parent af866e0c95
commit 9c9e059e2d
7 changed files with 292 additions and 38 deletions
+15 -9
View File
@@ -21,7 +21,7 @@ from aiohttp import web
from unshackle.core.api.compression import safe_inflate
from unshackle.core.api.errors import APIError, APIErrorCode, categorize_exception, handle_api_exception
from unshackle.core.api.input_bridge import AuthStatus, InputBridge
from unshackle.core.api.sanitize import safe_cache_key, sanitize_log
from unshackle.core.api.sanitize import MAX_SESSION_CACHE_KEYS, safe_cache_key, sanitize_log
from unshackle.core.api.session_log import SessionLogBuffer, SessionLogMirror, capture_service_logs
from unshackle.core.api.session_store import SessionStore
from unshackle.core.cacher import Cacher
@@ -1111,7 +1111,6 @@ def drm_preference_name(track: Any) -> Optional[str]:
MANIFEST_DATA_KEYS = ("hls", "dash", "ism")
MAX_SESSION_CACHE_KEYS = 64
def serialize_track_data(track: Any) -> Optional[Dict[str, Any]]:
@@ -3016,7 +3015,12 @@ async def session_create_handler(data: Dict[str, Any], request: Optional[web.Req
if not isinstance(content, str):
raise APIError(APIErrorCode.INVALID_INPUT, "cache values must be base64 strings")
decompressed = safe_inflate(base64.b64decode(content)).decode("utf-8")
(cache_dir / safe_name).with_suffix(".json").write_text(decompressed, encoding="utf-8")
target = cache_dir / f"{safe_name}.json"
try:
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(decompressed, encoding="utf-8")
except OSError as e:
log.warning(f"Skipping session cache key {sanitize_log(key)}: {e}")
bridge = InputBridge()
service_instance._input_bridge = bridge
@@ -4452,12 +4456,14 @@ async def session_delete_handler(session_id: str, request: Optional[web.Request]
if cache_tag and session.client_auth:
cache_dir = app_config.directories.cache / cache_tag
if cache_dir.is_dir():
for f in cache_dir.glob("*.json"):
if not f.stem.startswith("titles_"):
try:
cache_data[f.stem] = base64.b64encode(zlib.compress(f.read_bytes())).decode("ascii")
except OSError:
pass
for f in sorted(cache_dir.rglob("*.json")):
key = f.relative_to(cache_dir).as_posix()[: -len(".json")]
if f.stem.startswith("titles_") or not safe_cache_key(key):
continue
try:
cache_data[key] = base64.b64encode(zlib.compress(f.read_bytes())).decode("ascii")
except OSError:
pass
await store.delete(session_id)
+22 -7
View File
@@ -2,9 +2,13 @@
from __future__ import annotations
import re
from pathlib import PureWindowsPath
from typing import Optional
MAX_CACHE_KEY_DEPTH = 8
MAX_SESSION_CACHE_KEYS = 64
def sanitize_log(value: object) -> str:
"""Sanitise a value for safe logging by removing newlines and control characters."""
@@ -12,17 +16,28 @@ def sanitize_log(value: object) -> str:
def safe_cache_key(key: object) -> Optional[str]:
"""Return a bare filename for a peer-supplied cache key, or None if it escapes its directory.
"""Return a relative posix path for a peer-supplied cache key, or None if it escapes its directory.
unshackle writes each cache key as ``<key>.json`` inside a fixed directory. A cache key
that holds a path separator, ``..``, or an absolute path would let the peer write outside
that directory, so this function rejects anything that is not already a plain filename.
unshackle writes each cache key as ``<key>.json`` inside a fixed directory, and a key may
hold ``/`` to place the file in a subdirectory (``Cacher`` keys such as ``session_web/<sha1>``).
A cache key with a drive letter, a root, a ``..`` or ``.`` segment, an empty segment, a
control character, or more than ``MAX_CACHE_KEY_DEPTH`` segments would let the peer write
outside that directory or build an unbounded tree, so this function rejects it.
PureWindowsPath treats both ``/`` and ``\\`` as separators, so this function rejects a
cache key like ``..\\..\\secret`` even on POSIX (where backslash is a plain character).
The function turns a surviving backslash into ``/`` so the same cache key names the
same file on every platform.
"""
text = str(key)
name = PureWindowsPath(text).name
if not name or name != text or name in {".", ".."}:
if not text or any(ord(ch) < 32 or ch == "\x7f" for ch in text):
return None
return name
path = PureWindowsPath(text)
if path.drive or path.root or path.anchor:
return None
parts = re.split(r"[\\/]", text)
if len(parts) > MAX_CACHE_KEY_DEPTH:
return None
if any(not part or part in {".", ".."} or part.endswith((" ", ".")) or ":" in part for part in parts):
return None
return "/".join(parts)
+22 -7
View File
@@ -1408,7 +1408,11 @@ class RemoteService:
return
from unshackle.core.api.compression import safe_inflate
from unshackle.core.api.sanitize import safe_cache_key
from unshackle.core.api.sanitize import MAX_SESSION_CACHE_KEYS, safe_cache_key
if len(cache_data) > MAX_SESSION_CACHE_KEYS:
self.log.warning(f"Ignoring {len(cache_data)} cache files from server: more than {MAX_SESSION_CACHE_KEYS}")
return
cache_dir = config.directories.cache / self.service_tag
cache_dir.mkdir(parents=True, exist_ok=True)
@@ -1420,7 +1424,10 @@ class RemoteService:
continue
try:
decompressed = safe_inflate(base64.b64decode(content))
(cache_dir / safe_name).with_suffix(".json").write_bytes(decompressed)
# Not with_suffix: that would turn the key "keys.v2" into "keys.json".
target = cache_dir / f"{safe_name}.json"
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(decompressed)
except Exception as e:
self.log.warning(f"Failed to save returned cache file '{safe_name}': {e}")
@@ -1432,10 +1439,17 @@ class RemoteService:
The client cannot rely on the server to filter, so it sends only the files
it can tie to the active credential or profile, plus service-global state.
At worst, a withheld file makes the server authenticate again.
Each cache key is the file path relative to the service cache directory,
with posix separators and no ``.json`` suffix, so it equals the ``Cacher``
cache key the service reads it with (``session_web/<sha1>``). The relevance check
runs on every path segment, because a service may embed the credential
digest or profile name in a directory name as well as in the basename.
"""
import zlib
from unshackle.commands.dl import dl
from unshackle.core.api.sanitize import safe_cache_key
cache_dir = config.directories.cache / self.service_tag
if not cache_dir.is_dir():
@@ -1450,13 +1464,14 @@ class RemoteService:
foreign = set(profiles) - {active} if isinstance(profiles, dict) else set()
files: Dict[str, str] = {}
for f in cache_dir.glob("*.json"):
if f.stem.startswith("titles_"):
for f in sorted(cache_dir.rglob("*.json")):
key = f.relative_to(cache_dir).as_posix()[: -len(".json")]
if f.stem.startswith("titles_") or not safe_cache_key(key):
continue
if not cache_stem_is_relevant(f.stem, allowed, active, foreign):
self.log.debug(f"Withholding cache file from the remote server: {f.stem}")
if not all(cache_stem_is_relevant(part, allowed, active, foreign) for part in key.split("/")):
self.log.debug(f"Withholding cache file from the remote server: {key}")
continue
files[f.stem] = base64.b64encode(zlib.compress(f.read_bytes())).decode("ascii")
files[key] = base64.b64encode(zlib.compress(f.read_bytes())).decode("ascii")
return files