fix(hls): refuse key tags the DRM of the track cannot decrypt

This commit is contained in:
imSp4rky
2026-11-29 22:07:43 -07:00
parent ba2911056d
commit f0163439d4
5 changed files with 123 additions and 26 deletions
+51
View File
@@ -6,6 +6,8 @@ copy raised "Duplicate track ID" and failed the whole title.
from __future__ import annotations
import pytest
from unshackle.core.manifests.hls import HLS
URL = "https://example.test/master.m3u8"
@@ -39,3 +41,52 @@ def test_variants_with_the_same_attributes_and_other_links_stay_apart() -> None:
result = tracks(variant(5_000_000, "video/uhd.m3u8") + variant(5_000_000, "mirror/uhd.m3u8"))
assert len(result.videos) == 2
PLAIN = '#EXT-X-STREAM-INF:BANDWIDTH=5000000,RESOLUTION=3840x2160,CODECS="hvc1.2.4.L153.B0",VIDEO-RANGE=PQ'
COMPOSITE = PLAIN + ',SUPPLEMENTAL-CODECS="dvh1.08.06/db1p"'
def test_variants_of_one_link_with_another_video_range_stay_apart() -> None:
result = tracks(f"{PLAIN}\nuhd.m3u8\n" + f"{PLAIN.replace('=PQ', '=SDR')}\nuhd.m3u8\n")
assert len(result.videos) == 2
def test_variants_with_other_supplemental_codecs_and_other_links_stay_apart() -> None:
result = tracks(f"{PLAIN}\nhdr10.m3u8\n{COMPOSITE}\ndv.m3u8\n")
flags = {str(video.url).rsplit("/", 1)[-1]: video.dv_compatible_bitstream for video in result.videos}
assert flags == {"hdr10.m3u8": False, "dv.m3u8": True}
@pytest.mark.parametrize("first, second", [(PLAIN, COMPOSITE), (COMPOSITE, PLAIN)], ids=["plain", "composite"])
def test_one_link_listed_with_and_without_supplemental_codecs_keeps_the_dv_flag(first: str, second: str) -> None:
"""python-m3u8 drops SUPPLEMENTAL-CODECS, so the two entries count as one; the flag goes by link, not by entry."""
result = tracks(f"{first}\nuhd.m3u8\n{second}\nuhd.m3u8\n")
assert [video.dv_compatible_bitstream for video in result.videos] == [True]
def test_a_media_entry_listed_twice_with_other_channel_counts_keeps_both() -> None:
result = tracks(
AUDIO.replace('CHANNELS="2"', 'CHANNELS="6"')
+ "\n"
+ AUDIO.replace('CHANNELS="2"', 'CHANNELS="5.1"')
+ "\n"
+ variant(5_000_000, "video/uhd.m3u8")
)
assert len(result.audio) == 2
CC = '#EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS,GROUP-ID="cc",NAME="CC1",INSTREAM-ID="CC1"'
CC_VARIANT = variant(5_000_000, "video/uhd.m3u8").replace('AUDIO="en"', 'AUDIO="en",CLOSED-CAPTIONS="cc"')
def test_a_closed_caption_entry_listed_twice_is_carried_once() -> None:
result = tracks(CC + "\n" + CC + "\n" + CC_VARIANT)
assert result.videos[0].closed_captions == [
{"language": None, "name": "CC1", "instream_id": "CC1", "characteristics": None}
]
+42
View File
@@ -14,7 +14,9 @@ import m3u8
import pytest
from pywidevine.pssh import PSSH
from unshackle.core.cdm.detect import cdm_type_stub
from unshackle.core.constants import DOWNLOAD_LICENCE_ONLY
from unshackle.core.drm import ClearKey
from unshackle.core.drm.cenc import drm_stub
from unshackle.core.manifests.hls import HLS
from unshackle.core.tracks import Video
@@ -57,6 +59,14 @@ def test_key_tags_no_licence_route_can_use_are_dropped() -> None:
pytest.param(WIDEVINE_KEY, id="widevine"),
pytest.param('#EXT-X-KEY:METHOD=AES-128,URI="key"', id="aes-128"),
pytest.param("#EXT-X-KEY:METHOD=NONE", id="none"),
pytest.param(
'#EXT-X-KEY:METHOD=AES-256,URI="skd://keys.example/asset",KEYFORMAT="com.apple.streamingkeydelivery"',
id="aes-256",
),
pytest.param(
'#EXT-X-KEY:METHOD=AES-256-GCM,URI="skd://keys.example/asset",KEYFORMAT="com.apple.streamingkeydelivery"',
id="aes-256-gcm",
),
],
)
def test_a_key_tag_unshackle_can_read_keeps_every_tag(key_tag: str) -> None:
@@ -131,3 +141,35 @@ def test_a_track_without_drm_still_refuses_the_key_tag(tmp_path: Path, monkeypat
HLS.download_track(track=track, ctx=ctx)
finally:
DOWNLOAD_LICENCE_ONLY.clear()
@pytest.mark.parametrize(
"drm",
[
pytest.param(None, id="no-drm"),
pytest.param(ClearKey(key=bytes(16)), id="aes-128-session-key"),
],
)
def test_a_track_whose_drm_cannot_stand_in_is_refused_before_the_download(tmp_path: Path, drm: Any) -> None:
"""The refusal comes before a segment is requested: a ClearKey would otherwise write the ciphertext as the output."""
playlist_file = tmp_path / "media.m3u8"
playlist_file.write_text(playlist(SKD_KEY), encoding="utf-8")
track = Video(
url="https://example.test/media.m3u8",
language="en",
descriptor=Track.Descriptor.HLS,
codec=Video.Codec.HEVC,
)
track.from_file = playlist_file
track.drm = [drm] if drm else None
track.downloader = lambda **kwargs: pytest.fail("a segment was requested")
ctx = DownloadContext(
save_path=tmp_path / "video.mp4",
save_dir=tmp_path / "video_segments",
progress=lambda **_: None,
license_widevine=lambda drm, track_kid=None: pytest.fail("a licence was requested"),
cdm=cdm_type_stub("playready"),
)
with pytest.raises(NotImplementedError, match="com.apple.streamingkeydelivery"):
HLS.download_track(track=track, ctx=ctx)