From 4692e6bb968e298349df94b9597378b6b6643d29 Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Tue, 24 Nov 2026 17:13:08 -0700 Subject: [PATCH] fix(drm): end a licence with no usable key in one line for every DRM system --- tests/core/test_playready_multi_header.py | 23 +++++++++++++++ tests/core/test_widevine_get_content_keys.py | 29 +++++++++++++++---- .../test_server_cdm_service_injected_keys.py | 3 +- unshackle/core/drm/clearkey_cenc.py | 5 ++-- unshackle/core/drm/playready.py | 15 ++++++++-- unshackle/core/drm/widevine.py | 13 ++++----- 6 files changed, 69 insertions(+), 19 deletions(-) diff --git a/tests/core/test_playready_multi_header.py b/tests/core/test_playready_multi_header.py index f1efb11f..2985d1b2 100644 --- a/tests/core/test_playready_multi_header.py +++ b/tests/core/test_playready_multi_header.py @@ -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) diff --git a/tests/core/test_widevine_get_content_keys.py b/tests/core/test_widevine_get_content_keys.py index 41b92510..3cb5a83d 100644 --- a/tests/core/test_widevine_get_content_keys.py +++ b/tests/core/test_widevine_get_content_keys.py @@ -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) diff --git a/tests/remote/unit/test_server_cdm_service_injected_keys.py b/tests/remote/unit/test_server_cdm_service_injected_keys.py index 12d71acb..83cc1f10 100644 --- a/tests/remote/unit/test_server_cdm_service_injected_keys.py +++ b/tests/remote/unit/test_server_cdm_service_injected_keys.py @@ -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) diff --git a/unshackle/core/drm/clearkey_cenc.py b/unshackle/core/drm/clearkey_cenc.py index d93ac809..f29a48ab 100644 --- a/unshackle/core/drm/clearkey_cenc.py +++ b/unshackle/core/drm/clearkey_cenc.py @@ -6,6 +6,7 @@ from pathlib import Path from typing import Any, Callable, Iterable, Optional, Union from uuid import UUID +import click from requests import Session from unshackle.core.config import config @@ -192,10 +193,10 @@ class ClearKeyCENC: class KIDNotFound(Exception): """KID (Encryption Key ID) was not found.""" - class CEKNotFound(Exception): + class CEKNotFound(click.ClickException): """CEK (Content Encryption Key) for KID was not found in License.""" - class EmptyLicense(Exception): + class EmptyLicense(click.ClickException): """License returned no Content Encryption Keys.""" diff --git a/unshackle/core/drm/playready.py b/unshackle/core/drm/playready.py index 884fab9d..533cbc73 100644 --- a/unshackle/core/drm/playready.py +++ b/unshackle/core/drm/playready.py @@ -7,6 +7,7 @@ from threading import Lock from typing import Any, Callable, Optional, Union from uuid import UUID +import click import m3u8 from construct import Container from pymp4.parser import Box @@ -491,7 +492,15 @@ class PlayReady: except ValueError: pass - cdm.parse_license(session_id, license_str) + try: + cdm.parse_license(session_id, license_str) + except Exception as e: + if self.detect_revocation(license_str) or self.detect_revocation(str(e)): + raise + kids = ", ".join(kid.hex for kid in required_kids) + raise PlayReady.Exceptions.EmptyLicense( + f"No usable PlayReady licence for KID {kids}: {e}" + ) from e log_event( "drm_license_response", level="DEBUG", @@ -561,10 +570,10 @@ class PlayReady: class KIDNotFound(Exception): pass - class CEKNotFound(Exception): + class CEKNotFound(click.ClickException): pass - class EmptyLicense(Exception): + class EmptyLicense(click.ClickException): pass class DeviceRevoked(Exception): diff --git a/unshackle/core/drm/widevine.py b/unshackle/core/drm/widevine.py index 8b5a7538..fb154292 100644 --- a/unshackle/core/drm/widevine.py +++ b/unshackle/core/drm/widevine.py @@ -10,7 +10,6 @@ import m3u8 from construct import Container from pymp4.parser import Box from pywidevine.cdm import Cdm as WidevineCdm -from pywidevine.exceptions import InvalidLicenseMessage from pywidevine.pssh import PSSH from unshackle.core.config import config @@ -251,10 +250,10 @@ class Widevine: license_res = call_licence(licence, challenge, pssh=self.pssh, session_id=session_id) try: cdm.parse_license(session_id, license_res) - except InvalidLicenseMessage: - raise click.ClickException( - f"The licence server sent no usable Widevine licence for KID {kid.hex}" - ) from None + except Exception as e: + raise Widevine.Exceptions.EmptyLicense( + f"No usable Widevine licence for KID {kid.hex}: {e}" + ) from e log_event( "drm_license_response", level="DEBUG", @@ -318,10 +317,10 @@ class Widevine: class KIDNotFound(Exception): """KID (Encryption Key ID) was not found.""" - class CEKNotFound(Exception): + class CEKNotFound(click.ClickException): """CEK (Content Encryption Key) for KID was not found in License.""" - class EmptyLicense(Exception): + class EmptyLicense(click.ClickException): """License returned no Content Encryption Keys."""