mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-02 05:55:08 +00:00
fix(drm): stop a bridged FairPlay track whose server-CDM answer or licence key does not decrypt it
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
"""A FairPlay-only HLS track on the server CDM: the stub of the client names the key IDs that the server bridged."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
from collections.abc import Callable, Iterator
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.core.key_resolver import key_table, resolver
|
||||
from tests.core.test_hls_service_drm_keys import (
|
||||
KID,
|
||||
OTHER_KID,
|
||||
ROTATION,
|
||||
SKD_KEY,
|
||||
WIDEVINE_KEY,
|
||||
InitServer,
|
||||
download,
|
||||
hls_video,
|
||||
playlist,
|
||||
rotation_server,
|
||||
)
|
||||
from tests.remote.key_source import one_title, remote_session
|
||||
from tests.remote.sessions import session_entry
|
||||
from unshackle.core.api import server_keys
|
||||
from unshackle.core.api.key_policy import KeyPolicy
|
||||
from unshackle.core.cdm.detect import cdm_type_stub
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED
|
||||
from unshackle.core.drm import PlayReady, Widevine, cenc
|
||||
from unshackle.core.tracks import Video
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
KEY = "ab" * 16
|
||||
OTHER_KEY = "cd" * 16
|
||||
FAIRPLAY_ONLY = playlist(SKD_KEY)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def no_ffprobe(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
|
||||
"""No FFprobe, and a licence failure that cancels the run does not reach the next test."""
|
||||
monkeypatch.setattr(cenc, "ffprobe", lambda data: {})
|
||||
yield
|
||||
DOWNLOAD_CANCELLED.clear()
|
||||
|
||||
|
||||
def server_answer(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
text: str,
|
||||
licence: dict[UUID, str],
|
||||
system: str = "playready",
|
||||
origin: Callable[[], InitServer] = InitServer,
|
||||
) -> dict[str, Any]:
|
||||
"""The batch answer of the server CDM for one HLS video of ``text``, whose licence holds ``licence``.
|
||||
|
||||
The server service has the FairPlay bridge hook and serves an init segment with an all-zero tenc KID and a pssh
|
||||
box for KID. Each field of the batch goes into the answer when it is not empty, as the licence route sends it.
|
||||
"""
|
||||
monkeypatch.setattr("unshackle.core.tracks.track.utf8_text", lambda response: text)
|
||||
monkeypatch.setattr(server_keys, "licence_candidates", lambda *args: [system])
|
||||
monkeypatch.setattr(server_keys, "find_title_for_track", lambda *args: None)
|
||||
monkeypatch.setattr(
|
||||
server_keys, "handle_single_server_cdm", lambda *args, **kwargs: {k.hex: v for k, v in licence.items()}
|
||||
)
|
||||
service = SimpleNamespace(session=origin(), fairplay_bridge=lambda: print)
|
||||
session = session_entry(service_instance=service, tracks={"vid": hls_video(tmp_path, text)})
|
||||
batch = server_keys.resolve_batch(session, ["vid"], system, KeyPolicy())
|
||||
return {name: value for name, value in dataclasses.asdict(batch).items() if value}
|
||||
|
||||
|
||||
def licence_on_the_client(
|
||||
tmp_path: Path,
|
||||
text: str,
|
||||
answer: dict[str, Any],
|
||||
licence: dict[UUID, str],
|
||||
origin: Callable[[], InitServer] = InitServer,
|
||||
asked: list[UUID | None] | None = None,
|
||||
) -> Video:
|
||||
"""Take the server answer as dl does, then run the licence step of the download; return the track.
|
||||
|
||||
A single-track licence of the server CDM answers with ``licence`` and lands in ``asked``.
|
||||
"""
|
||||
|
||||
def license_track(track: Video, drm: Any, kid: UUID | None = None) -> dict[UUID, str]:
|
||||
if asked is not None:
|
||||
asked.append(kid)
|
||||
return dict(licence)
|
||||
|
||||
remote = remote_session(None, answer)
|
||||
remote.license_track = license_track # type: ignore[method-assign,assignment]
|
||||
track = hls_video(tmp_path, text)
|
||||
track.id = "vid"
|
||||
title = one_title(track)
|
||||
remote.resolve_server_keys(title) # type: ignore[arg-type]
|
||||
keys = resolver(cdm=cdm_type_stub("playready"))
|
||||
keys.source = remote
|
||||
download(track, None, keys.licence_for(track, title, key_table()), True, origin()) # type: ignore[arg-type]
|
||||
return track
|
||||
|
||||
|
||||
def test_an_answer_without_the_content_key_of_a_bridged_track_stops_it(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
"""The licence holds a content key for another KID only; with the all-zero tenc KID it would decrypt the track."""
|
||||
licence = {OTHER_KID: OTHER_KEY}
|
||||
answer = server_answer(monkeypatch, tmp_path, FAIRPLAY_ONLY, licence)
|
||||
|
||||
with pytest.raises(PlayReady.Exceptions.CEKNotFound, match=f"No Content Key for KID {KID.hex} "):
|
||||
licence_on_the_client(tmp_path, FAIRPLAY_ONLY, answer, licence)
|
||||
|
||||
|
||||
def test_the_stub_of_a_bridged_track_names_the_key_ids_of_the_server(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
licence = {OTHER_KID: OTHER_KEY, KID: KEY}
|
||||
answer = server_answer(monkeypatch, tmp_path, FAIRPLAY_ONLY, licence)
|
||||
|
||||
track = licence_on_the_client(tmp_path, FAIRPLAY_ONLY, answer, licence)
|
||||
|
||||
assert answer["bridge_kids"] == {"vid": [KID.hex]}
|
||||
[drm] = track.drm or []
|
||||
assert isinstance(drm, PlayReady) and drm.bridged
|
||||
assert drm.kids == [KID] and drm.content_keys == licence
|
||||
|
||||
|
||||
def test_the_stub_of_a_track_the_server_did_not_bridge_is_unchanged(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
answer = server_answer(monkeypatch, tmp_path, playlist(WIDEVINE_KEY), {KID: KEY}, "widevine")
|
||||
remote = remote_session(None, answer)
|
||||
track = hls_video(tmp_path, playlist(WIDEVINE_KEY))
|
||||
track.id = "vid"
|
||||
|
||||
remote.resolve_server_keys(one_title(track)) # type: ignore[arg-type]
|
||||
|
||||
[drm] = track.drm or []
|
||||
assert "bridge_kids" not in answer
|
||||
assert isinstance(drm, Widevine) and not drm.bridged
|
||||
|
||||
|
||||
def test_a_rotation_asks_the_server_cdm_for_a_key_id_its_answer_lacks(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
answer = server_answer(monkeypatch, tmp_path, ROTATION, {KID: KEY}, origin=rotation_server)
|
||||
asked: list[UUID | None] = []
|
||||
|
||||
track = licence_on_the_client(tmp_path, ROTATION, answer, {OTHER_KID: OTHER_KEY}, rotation_server, asked)
|
||||
|
||||
assert answer["bridge_kids"] == {"vid": [KID.hex, OTHER_KID.hex]}
|
||||
assert asked == [OTHER_KID]
|
||||
[drm] = track.drm or []
|
||||
assert isinstance(drm, PlayReady) and drm.content_keys == {KID: KEY, OTHER_KID: OTHER_KEY}
|
||||
|
||||
|
||||
def test_a_rotation_without_a_later_content_key_stops_before_a_segment(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
answer = server_answer(monkeypatch, tmp_path, ROTATION, {KID: KEY}, origin=rotation_server)
|
||||
|
||||
with pytest.raises(PlayReady.Exceptions.CEKNotFound, match=f"No Content Key for KID {OTHER_KID.hex} "):
|
||||
licence_on_the_client(tmp_path, ROTATION, answer, {}, rotation_server)
|
||||
Reference in New Issue
Block a user