feat(remote): name the missing KID in the server-device error

This commit is contained in:
imSp4rky
2026-11-30 11:03:47 -07:00
parent ea073209da
commit 40114fff19
4 changed files with 98 additions and 16 deletions
+4 -3
View File
@@ -359,9 +359,10 @@ the wire:
A service can also offer an option that builds the remote session on the server's own
device and identity, for the quality that device reaches. The session then makes no live
licence at all: each content key comes from the server vault or the client's own vaults,
and a content key that no vault holds stops the download with an error. The create
response sets `server_device` to `true` for such a remote session. The service then uses
the server's own config: the client's identity values do not apply. The client reads the
and a content key that no vault holds stops the download with an error that names its KID,
when the client knows it. The create response sets `server_device` to `true` for such a
remote session. The service then uses the server's own config: the client's identity
values do not apply. The client reads the
server vault only when the API key has the `server_vault` grant (or `server_cdm`); without
it, the client reads only its own vaults. The login tokens of such a remote session hold the
server's device identity, so the server keeps them: `DELETE` returns no cache files, and the
+1 -1
View File
@@ -289,7 +289,7 @@ def test_the_client_stops_with_a_clear_message_when_no_vault_has_the_key() -> No
svc = object.__new__(RemoteService)
with pytest.raises(click.ClickException, match="in no vault"):
svc.license_locally(SimpleNamespace(id="vid"), {"reason": "server_device"})
svc.license_locally(SimpleNamespace(id="vid", drm=None), {"reason": "server_device"})
class ClosingBridge(DirectBridge):
@@ -9,10 +9,13 @@ import logging
from types import SimpleNamespace
from uuid import UUID
import click
import pytest
from tests.core.key_resolver import key_table, resolver
from tests.remote.key_source import remote_keys
from unshackle.core.remote_protocol import LicensingMode, Licensor
from unshackle.core.api.errors import categorize_exception
from unshackle.core.remote_protocol import APIErrorCode, LicensingMode, Licensor
pytestmark = pytest.mark.unit
@@ -100,8 +103,6 @@ def test_proxy_mode_keeps_the_callers_type():
def test_a_refused_relayed_licence_raises_a_click_exception_with_the_server_message(caplog):
import click
from unshackle.core.remote_service import RemoteError
svc = _service("widevine", {})
@@ -163,11 +164,77 @@ def test_a_track_with_no_header_sends_nothing_and_gets_no_keys():
def test_a_deferred_refusal_raises_before_any_request():
import click
svc = _service("widevine", {})
svc.deferred_refusals["vid"] = "capped"
with pytest.raises(click.ClickException, match="^capped$"):
svc.license_track(SimpleNamespace(id="vid", drm=[_Widevine()]), None)
assert svc.client.posts == []
class _Track(SimpleNamespace):
"""A track whose text never holds the KIDs of its DRM."""
def __str__(self):
return "VID | [H.265] | ko"
KID_404 = UUID("40400000-0000-4000-8000-000000000001")
KID_429 = UUID("00000429-0000-4000-8000-000000000001")
PLACEHOLDER = UUID("00010203-0405-0607-0809-0a0b0c0d0e0f")
LOCAL_VAULTS = "none of your vaults, and this API key cannot read the server vault"
ALL_VAULTS = "no vault, neither the server's nor yours"
def _drm(*kids, keys=()):
drm = _Widevine()
drm.kids = list(kids)
drm.content_keys = dict.fromkeys(keys, "k")
return drm
def _lent_device(vaults):
"""A remote session on the server's device whose refusal comes from the call of license_track that gives ``vaults``."""
svc = _service("widevine", {"error_code": APIErrorCode.SERVER_CDM_CAPPED, "details": {"reason": "server_device"}})
svc.licensing = LicensingMode(Licensor.LENT_DEVICE, server_vault=vaults == ALL_VAULTS)
return svc
@pytest.mark.parametrize("vaults", [LOCAL_VAULTS, ALL_VAULTS])
@pytest.mark.parametrize(
("drm", "track_drm", "kid", "named"),
[
pytest.param(_drm(KID, KID_429), [], KID_404, [KID_404], id="the-kid-dl-needs"),
pytest.param(_drm(KID, KID_404), [], KID_429, [KID_429], id="the-kid-dl-needs-with-429"),
pytest.param(_drm(KID, KID_404, keys=[KID]), [], None, [KID_404], id="the-drm-without-the-held-kid"),
pytest.param(_drm(KID_404, KID_429, PLACEHOLDER), [], None, [KID_429, KID_404], id="sorted-real-kids"),
pytest.param(None, [_drm(KID_404), _drm(KID_404, KID_429)], None, [KID_429, KID_404], id="the-track-drm"),
pytest.param(_drm(KID_404), [_drm(KID)], None, [KID_404], id="the-drm-before-the-track-drm"),
pytest.param(_drm(KID_404), [], PLACEHOLDER, [KID_404], id="a-placeholder-kid"),
pytest.param(_drm(PLACEHOLDER), [], PLACEHOLDER, [], id="only-placeholders"),
pytest.param(_drm(KID, keys=[KID]), [], None, [], id="every-kid-held"),
],
)
def test_the_server_device_refusal_is_a_drm_error_that_names_the_missing_kid(vaults, drm, track_drm, kid, named):
svc = _lent_device(vaults)
with pytest.raises(click.ClickException) as caught:
svc.license_track(_Track(id="vid", drm=track_drm or [drm]), drm, kid)
assert len(svc.client.posts) == (vaults == ALL_VAULTS)
assert categorize_exception(caught.value).error_code is APIErrorCode.DRM_ERROR
article = "A" if len(named) > 1 else "The"
of_kid = f" of KID {', '.join(k.hex for k in named)}" if named else ""
assert caught.value.message == (
f"{article} content key{of_kid} for VID | [H.265] | ko is in {vaults}; "
"drop the service's server-identity option to license it locally"
)
@pytest.mark.parametrize("vaults", [LOCAL_VAULTS, ALL_VAULTS])
def test_the_server_device_refusal_in_a_download_names_only_the_kid_dl_needs(vaults):
drm = _drm(KID, KID_404)
prepare_drm = resolver(source=_lent_device(vaults)).licence_for(_Track(id="vid", drm=[drm]), None, key_table())
with pytest.raises(click.ClickException, match=f"^The content key of KID {KID_404.hex} for .* is in {vaults};"):
prepare_drm(drm, track_kid=KID_404)
+21 -7
View File
@@ -47,7 +47,7 @@ from unshackle.core.constants import AnyTrack
from unshackle.core.credential import Credential
from unshackle.core.dl_options import remote_dl_params
from unshackle.core.drm import cbcs, real_kids, whole_file_cipher
from unshackle.core.drm.cenc import drm_stub
from unshackle.core.drm.cenc import PLACEHOLDER_KIDS, drm_stub
from unshackle.core.drm.discovery import track_pssh
from unshackle.core.launch import launch_keys
from unshackle.core.manifests.snapshot import match_track
@@ -1574,11 +1574,14 @@ class RemoteService(TitleSource):
self.deferred_refusals[str(track.id)] = e.message
self.log.debug(f"{e.message}; trying the local vaults first")
def license_locally(self, track: AnyTrack, refusal: dict[str, Any]) -> None:
def license_locally(
self, track: AnyTrack, refusal: dict[str, Any], drm: Any = None, kid: UUID | None = None
) -> None:
"""Hand a track the server CDM refused to license live to this machine's own device.
dl then licenses it through the challenge relay. An HLS track with no DRM yet passes, as its
media playlist names the DRM and the downloader honours ``drm_preference``.
media playlist names the DRM and the downloader honours ``drm_preference``. On the server's device
there is no such licence: the error names ``kid``, else each KID of ``drm`` with no content key.
"""
from unshackle.core.cdm.detect import cdm_system, is_playready_cdm
@@ -1588,8 +1591,19 @@ class RemoteService(TitleSource):
if self.local_vaults_only
else "no vault, neither the server's nor yours"
)
raise click.ClickException(
f"The content key for {track} is in {vaults}; drop the service's server-identity option to license it locally"
if kid and kid not in PLACEHOLDER_KIDS:
kids = [kid.hex]
else:
drms: Iterable[Any] = [drm] if drm is not None else track.drm or []
kids = sorted({k.hex for d in drms for k in real_kids(d) if k not in d.content_keys})
article = "A" if len(kids) > 1 else "The"
of_kid = f" of KID {', '.join(kids)}" if kids else ""
raise coded(
click.ClickException(
f"{article} content key{of_kid} for {track} is in {vaults}; "
"drop the service's server-identity option to license it locally"
),
APIErrorCode.DRM_ERROR,
)
cap = refusal.get("max_height")
if cap:
@@ -1721,7 +1735,7 @@ class RemoteService(TitleSource):
from a server vault stands in for the licence. A failed request logs one warning and returns no keys.
"""
if self.local_vaults_only:
self.license_locally(track, {"reason": "server_device"})
self.license_locally(track, {"reason": "server_device"}, drm, kid)
if deferred := self.deferred_refusals.get(str(track.id)):
raise click.ClickException(deferred)
server_type = self.server_cdm_type
@@ -1752,7 +1766,7 @@ class RemoteService(TitleSource):
except (Exception, RemoteError) as e:
self.log.warning("server_cdm license failed: %s", e)
return {}
self.license_locally(track, resp.get("details") or {})
self.license_locally(track, resp.get("details") or {}, drm, kid)
return None
def license_file(self, track: AnyTrack) -> str: