feat(drm)!: load DRM systems beyond the built-ins from plugins in directories.plugins

This commit is contained in:
imSp4rky
2026-12-03 15:51:42 -07:00
parent f8a248a29e
commit e9adc50594
65 changed files with 1651 additions and 2526 deletions
+80 -11
View File
@@ -20,8 +20,9 @@ from pywidevine.pssh import PSSH as WidevinePSSH
from tests.core.test_zero_kid_fallback import _playready_pssh
from unshackle.core import wire
from unshackle.core.config import config
from unshackle.core.drm import BlowfishStripe, ClearKeyCENC, ClearKeyCTR, PlayReady, Widevine
from unshackle.core.drm import ClearKeyCENC, ClearKeyCTR, CustomDRM, PlayReady, Widevine
from unshackle.core.import_service import ImportService
from unshackle.core.remote_protocol import APIErrorCode
from unshackle.core.titles import Episode, Movie, Song
from unshackle.core.titles.music import SONG_FIELDS
from unshackle.core.tracks import Audio, Chapter, Subtitle, Tracks, Video
@@ -59,7 +60,7 @@ pytestmark = pytest.mark.unit
VIDEO_KID = UUID("00112233-4455-6677-8899-aabbccddeeff")
AUDIO_KID = UUID("ffeeddcc-bbaa-9988-7766-554433221100")
VIDEO_KEY = "0123456789abcdef0123456789abcdef"
BLOWFISH_KEY = bytes(range(16))
HELD_KEY = bytes(range(16))
CTR_CIPHER = ClearKeyCTR(None, bytes(range(16, 32)), b"OggS", skip=7, key=bytes(range(16)))
@@ -246,8 +247,8 @@ def drm_fields(drm: Any) -> dict[str, Any]:
out["pssh"] = drm.pssh_b64
if isinstance(drm, ClearKeyCENC):
out["laurl"] = drm.laurl
if isinstance(drm, BlowfishStripe):
out["key"] = drm.key.hex()
if isinstance(drm, CustomDRM):
out.update(plugin=drm.plugin_id, label=drm.label, display_id=drm.display_id, data=drm.data)
return out
@@ -1065,20 +1066,29 @@ def every_drm() -> list:
return [
widevine(VIDEO_KID, keys=True),
playready(VIDEO_KID, AUDIO_KID),
BlowfishStripe(BLOWFISH_KEY),
CustomDRM("other", data={"key": HELD_KEY.hex(), "secret": "server only"}),
local_plugin_drm(),
CTR_CIPHER,
clearkey(),
]
def test_rest_drm_of_each_system() -> None:
def local_plugin_drm() -> CustomDRM:
"""A plugin DRM that a device licensed: its content key and its ticket never leave the machine."""
drm = CustomDRM("example", kids=[VIDEO_KID], display_id="EXAMPLE-1", data={"ticket": "server only"})
drm.content_keys[VIDEO_KID] = VIDEO_KEY
return drm
def test_rest_drm_of_each_system(example_plugins: None) -> None:
drm = every_drm()
assert serialize_drm(drm) == [
*REST_VIDEO["drm"],
{"type": "blowfishstripe", "key": "000102030405060708090a0b0c0d0e0f"},
{"type": "other", "kids": [], "label": "OTHER", "data": {"key": HELD_KEY.hex()}},
{"type": "example", "kids": [VIDEO_KID.hex], "label": "EXAMPLE", "display_id": "EXAMPLE-1"},
{
"type": "clearkeyctr",
"key": BLOWFISH_KEY.hex(),
"key": HELD_KEY.hex(),
"iv": bytes(range(16, 32)).hex(),
"magic": "4f676753",
"skip": 7,
@@ -1105,13 +1115,22 @@ def test_export_drm_of_each_system() -> None:
}
def test_reconstruct_rest_drm() -> None:
def test_reconstruct_rest_drm(example_plugins: None) -> None:
drm = every_drm()
rebuilt = reconstruct_drm(json_copy(serialize_drm(drm)))
custom = {"class": "CustomDRM", "kids": [], "content_keys": {}, "display_id": None}
assert [drm_fields(d) for d in rebuilt] == [
WIDEVINE_FIELDS,
PLAYREADY_FIELDS,
{"class": "BlowfishStripe", "kids": [], "content_keys": {}, "key": "000102030405060708090a0b0c0d0e0f"},
{**custom, "plugin": "other", "label": "OTHER", "data": {"key": HELD_KEY.hex()}},
{
**custom,
"plugin": "example",
"label": "EXAMPLE",
"kids": [VIDEO_KID.hex],
"display_id": "EXAMPLE-1",
"data": {},
},
{"class": "ClearKeyCTR", "kids": [], "content_keys": {}},
]
@@ -1153,7 +1172,6 @@ def test_export_drm_decode_takes_the_stored_keys() -> None:
"data, message",
[
({"system": "Widevine"}, "Cannot reconstruct DRM without a stored PSSH."),
({"system": "Nope", "pssh_b64": "x"}, "Unsupported DRM system for reconstruction: 'Nope'"),
],
)
def test_export_drm_decode_refuses(data: dict[str, Any], message: str) -> None:
@@ -1161,6 +1179,57 @@ def test_export_drm_decode_refuses(data: dict[str, Any], message: str) -> None:
drm_from_dict(data)
class FutureDRM:
def __init__(self) -> None:
self.kids = [VIDEO_KID]
self.content_keys = {VIDEO_KID: VIDEO_KEY}
def test_rest_drm_of_an_unknown_class_has_no_content_keys() -> None:
assert serialize_drm([FutureDRM()]) == [{"type": "futuredrm", "kids": [str(VIDEO_KID)]}]
@pytest.mark.parametrize("drm", [widevine(VIDEO_KID), playready(VIDEO_KID), clearkey()], ids=lambda d: type(d).__name__)
def test_rest_drm_keeps_the_content_keys_of_the_classes_that_carry_them(drm: Any) -> None:
drm.content_keys[VIDEO_KID] = VIDEO_KEY
(info,) = serialize_drm([drm])
assert info["content_keys"] == {str(VIDEO_KID): VIDEO_KEY}
def test_reconstruct_rest_drm_leaves_hls_clearkey_to_the_manifest() -> None:
assert reconstruct_drm([{"type": "clearkey"}]) == []
@pytest.mark.parametrize(
"entry", [{"type": "futuredrm", "kids": [str(VIDEO_KID)]}, {"type": "futuredrm", "pssh": "AAAA"}]
)
def test_reconstruct_rest_drm_refuses_an_unknown_type(entry: dict[str, Any]) -> None:
with pytest.raises(click.ClickException, match=r"^Unknown DRM type 'futuredrm' from the server$") as e:
reconstruct_drm([entry])
assert e.value.error_code is APIErrorCode.DRM_ERROR # type: ignore[attr-defined]
@pytest.mark.parametrize("kids", [[1], ["not hex"]], ids=["number", "text"])
def test_reconstruct_rest_drm_refuses_a_bad_plugin_kid_with_one_line(example_plugins: None, kids: list) -> None:
with pytest.raises(click.ClickException, match=r"^Bad example DRM entry from the server: ") as e:
reconstruct_drm([{"type": "example", "kids": kids}])
assert e.value.error_code is APIErrorCode.DRM_ERROR # type: ignore[attr-defined]
def test_a_track_with_an_unknown_drm_type_stops_the_title() -> None:
with pytest.raises(click.ClickException, match="futuredrm"):
build_tracks({"video": [{**REST_VIDEO, "drm": [{"type": "futuredrm", "kids": [str(VIDEO_KID)]}]}]})
@pytest.mark.parametrize(
"data", [{"system": "FutureDRM", "kids": [VIDEO_KID.hex]}, {"system": "FutureDRM", "pssh_b64": "x"}]
)
def test_export_drm_decode_refuses_an_unknown_system(data: dict[str, Any]) -> None:
with pytest.raises(click.ClickException, match=r"^Unknown DRM system 'FutureDRM' in the export$") as e:
drm_from_dict(data)
assert e.value.error_code is APIErrorCode.DRM_ERROR # type: ignore[attr-defined]
def test_rest_attachment_by_url() -> None:
attachment = Attachment(
url="https://img.example/poster.jpg", name="Poster", mime_type="image/jpeg", description="Cover"