fix(drm): end a licence with no usable key in one line for every DRM system

This commit is contained in:
imSp4rky
2026-11-24 17:13:08 -07:00
parent d954f61100
commit 4692e6bb96
6 changed files with 69 additions and 19 deletions
+23
View File
@@ -4,7 +4,9 @@ import base64
from typing import Any
from uuid import UUID
import click
import pytest
from pyplayready.misc.exceptions import InvalidLicense, InvalidXmrLicense
from pyplayready.system.pssh import PSSH
from pyplayready.system.wrmheader import WRMHeader
@@ -414,3 +416,24 @@ def test_a_single_refused_header_still_raises_on_every_call() -> None:
for _ in range(2):
with pytest.raises(RuntimeError, match="licence refused"):
drm.get_content_keys(cdm=cdm, certificate=_cert, licence=_licence)
@pytest.mark.parametrize(
"error, expected",
[
(InvalidXmrLicense("Cannot parse an empty licence message"), click.ClickException),
(InvalidLicense("bad"), click.ClickException),
(InvalidLicense("server fault 0x8004C065"), PlayReady.Exceptions.DeviceRevoked),
],
)
def test_a_licence_the_cdm_cannot_parse_is_one_line_unless_the_device_is_revoked(
error: Exception, expected: type[Exception]
) -> None:
cdm = FakeCdm({KID_A: KEY_A})
def parse_license(session_id: str, response: str) -> None:
raise error
cdm.parse_license = parse_license # type: ignore[method-assign]
with pytest.raises(expected):
_drm(_wrm_header(KID_A)).get_content_keys(cdm=cdm, certificate=_cert, licence=_licence)
+23 -6
View File
@@ -3,10 +3,15 @@ from __future__ import annotations
from typing import Any
from uuid import UUID
import click
import pytest
import requests
from pywidevine.exceptions import InvalidLicenseMessage
from pywidevine.pssh import PSSH
from unshackle.core.drm import Widevine
from unshackle.core.cdm.custom_remote_cdm import CustomRemoteCDMExceptions
from unshackle.core.cdm.decrypt_labs_remote_cdm import DecryptLabsRemoteCDMExceptions
from unshackle.core.drm import ClearKeyCENC, PlayReady, Widevine
from unshackle.core.utilities import declared_kwargs
KID = UUID("00112233-4455-6677-8899-aabbccddeeff")
@@ -95,13 +100,25 @@ def test_an_empty_challenge_raises_instead_of_licensing() -> None:
)
def test_a_licence_the_cdm_cannot_parse_is_one_line() -> None:
import click
from pywidevine.exceptions import InvalidLicenseMessage
@pytest.mark.parametrize(
"error",
[
InvalidLicenseMessage("Could not parse license_message as a SignedMessage"),
CustomRemoteCDMExceptions.InvalidLicenseMessage("bad"),
DecryptLabsRemoteCDMExceptions.InvalidLicenseMessage("bad"),
requests.RequestException("License decrypt failed: 400"),
],
)
def test_a_licence_the_cdm_cannot_parse_is_one_line(error: Exception) -> None:
class JunkCdm(FakeCdm):
def parse_license(self, session_id: str, response: Any) -> None:
raise InvalidLicenseMessage("Could not parse license_message as a SignedMessage")
raise error
with pytest.raises(click.ClickException, match=KID.hex):
_drm().get_content_keys(JunkCdm(), certificate=lambda **_: None, licence=lambda **_: b"junk licence")
@pytest.mark.parametrize("drm", [Widevine, PlayReady, ClearKeyCENC])
def test_a_licence_with_no_usable_key_is_one_line(drm: Any) -> None:
assert issubclass(drm.Exceptions.EmptyLicense, click.ClickException)
assert issubclass(drm.Exceptions.CEKNotFound, click.ClickException)
@@ -10,6 +10,7 @@ import base64
from types import SimpleNamespace
from uuid import UUID
import click
import pytest
from pyplayready.system.pssh import PSSH as PRPSSH
from pywidevine.pssh import PSSH as WvPSSH
@@ -113,5 +114,5 @@ def test_failed_licence_without_injected_keys_still_raises(server_env):
get_widevine_license=lambda **_: b"garbage",
)
with pytest.raises(ValueError, match="not a licence"):
with pytest.raises(click.ClickException, match="not a licence"):
handlers.handle_single_server_cdm(service, SimpleNamespace(), track, pssh.dumps(), "widevine", None)