fix(hls): bridge a FairPlay playlist whose clear part has a METHOD=NONE tag and its own init segment

This commit is contained in:
imSp4rky
2026-12-01 18:32:31 -07:00
parent f680df7eff
commit e0880812d5
4 changed files with 121 additions and 10 deletions
+90 -3
View File
@@ -33,6 +33,7 @@ SKD_KEY = (
'#EXT-X-KEY:METHOD=SAMPLE-AES,URI="skd://keys.example/asset",'
'KEYFORMAT="com.apple.streamingkeydelivery",KEYFORMATVERSIONS="1"'
)
NONE_KEY = "#EXT-X-KEY:METHOD=NONE"
WIDEVINE_KEY = (
'#EXT-X-KEY:METHOD=SAMPLE-AES,URI="data:text/plain;base64,'
f'{PSSH.new(key_ids=[KID], system_id=PSSH.SystemId.Widevine).dumps()}",'
@@ -129,6 +130,36 @@ def test_a_key_tag_unshackle_can_read_keeps_every_tag(key_tag: str) -> None:
assert [segment.keys for segment in manifest.segments] == segment_keys
IDENTITY_KEY = '#EXT-X-KEY:METHOD=SAMPLE-AES,URI="https://keys.example/key"'
@pytest.mark.parametrize(
"text, unsupported",
[
pytest.param(playlist(IDENTITY_KEY), False, id="no-keyformat"),
pytest.param(playlist(f'{IDENTITY_KEY},KEYFORMAT="identity"'), False, id="identity"),
pytest.param(playlist(SKD_KEY), True, id="fairplay"),
pytest.param(playlist(IDENTITY_KEY, SKD_KEY), True, id="identity-beside-fairplay"),
pytest.param(
playlist(SKD_KEY).replace("seg0.m4s\n", f"seg0.m4s\n{IDENTITY_KEY}\n"), False, id="identity-later"
),
],
)
def test_only_a_key_tag_of_a_drm_system_is_unsupported(text: str, unsupported: bool) -> None:
"""An identity key file is the content key itself: no DRM system serves it."""
assert HLS.only_unsupported_keys(m3u8.loads(text)) is unsupported
def test_the_server_builds_no_stub_for_an_identity_sample_aes_track(tmp_path: Path, monkeypatch: Any) -> None:
monkeypatch.setattr("unshackle.core.tracks.track.utf8_text", lambda response: playlist(IDENTITY_KEY))
track = hls_video(tmp_path, playlist(IDENTITY_KEY))
probe = init_kid_probe(monkeypatch, KID)
assert not track.load_drm_from_playlist(InitServer(), bridge=True)
assert not track.drm
assert not probe.called
def test_a_playlist_with_no_key_tag_is_left_alone() -> None:
assert not HLS.drop_unsupported_keys(m3u8.loads(playlist()))
@@ -273,10 +304,15 @@ def encrypted_init(tenc: UUID, *pssh: UUID, piff: bool = False) -> bytes:
ZERO_INIT = encrypted_init(ZERO_KID, KID)
def stitched(*inits: str, keyed_from: int = 1) -> str:
"""A playlist with one discontinuity block for each init segment, where block ``keyed_from`` holds the key tag."""
def stitched(*inits: str, keyed_from: int = 1, clear_from: int | None = None) -> str:
"""A playlist with one discontinuity block for each init segment, where block ``keyed_from`` holds the key tag.
Block ``clear_from`` holds a METHOD=NONE tag.
"""
blocks = [f'#EXT-X-MAP:URI="{name}"\n#EXTINF:6.0,\n{i}.m4s\n' for i, name in enumerate(inits)]
blocks[keyed_from] = f"{SKD_KEY}\n{blocks[keyed_from]}"
if clear_from is not None:
blocks[clear_from] = f"{NONE_KEY}\n{blocks[clear_from]}"
body = "#EXT-X-DISCONTINUITY\n".join(blocks)
return f"#EXTM3U\n#EXT-X-VERSION:7\n#EXT-X-TARGETDURATION:6\n{body}#EXT-X-ENDLIST\n"
@@ -327,9 +363,59 @@ STITCHED_ZERO = [
["lead.mp4", "real.mp4"],
id="tag-on-the-clear-lead-of-a-real-tenc",
),
pytest.param(stitched("lead.mp4", "init.mp4", "lead.mp4", clear_from=2), {}, ["init.mp4"], id="method-none-tail"),
]
@pytest.mark.parametrize(
"text, bridged",
[
pytest.param(stitched("lead.mp4", "init.mp4", "lead.mp4", clear_from=2), True, id="tail-with-its-own-init"),
pytest.param(
stitched("lead.mp4", "init.mp4", "init.mp4", clear_from=2), False, id="tail-under-the-encrypted-init"
),
pytest.param(playlist(NONE_KEY), False, id="no-key"),
pytest.param(playlist(SKD_KEY, NONE_KEY), False, id="one-span-of-both"),
],
)
def test_a_method_none_tag_counts_only_when_no_encrypted_segment_shares_its_init(text: str, bridged: bool) -> None:
"""A decrypter that reads the encrypted init segment treats clear samples as encrypted and writes wrong frames."""
manifest = m3u8.loads(text)
assert HLS.drop_unsupported_keys(manifest) == bridged
assert (manifest.keys == [] and all(segment.keys == [] for segment in manifest.segments)) == bridged
def test_the_drm_of_the_service_licenses_a_track_with_a_method_none_tail(tmp_path: Path) -> None:
track = hls_video(tmp_path, stitched("lead.mp4", "init.mp4", "lead.mp4", clear_from=2))
track.drm = [drm_stub("playready", [KID.hex])]
licensed: list[UUID | None] = []
download(track, None, lambda drm, track_kid=None: licensed.append(track_kid), True, InitServer())
assert licensed == [KID]
def test_the_key_id_of_two_keys_comes_from_the_init_after_a_method_none_lead(tmp_path: Path) -> None:
text = stitched("lead.mp4", "real.mp4", "real.mp4", clear_from=0)
text = text.replace("#EXTINF:6.0,\n2.m4s", f"{SKD_KEY.replace('asset', 'other')}\n#EXTINF:6.0,\n2.m4s")
manifest = m3u8.loads(text, "https://example.test/media.m3u8")
session = InitServer(real=encrypted_init(KID))
assert HLS.get_bridge_kid(manifest, manifest.segments, hls_video(tmp_path, ""), session) == KID
assert session.requests == ["real.mp4"]
def test_a_method_none_tail_under_the_encrypted_init_is_refused(tmp_path: Path) -> None:
with pytest.raises(NotImplementedError, match="com.apple.streamingkeydelivery"):
download(
hls_video(tmp_path, stitched("lead.mp4", "init.mp4", "init.mp4", clear_from=2)),
cdm_type_stub("playready"),
session=InitServer(),
)
@pytest.mark.parametrize("text, files, requests", STITCHED_ZERO)
def test_a_stitched_one_key_playlist_takes_the_key_id_that_every_encrypted_init_names(
tmp_path: Path, text: str, files: dict[str, bytes], requests: list[str]
@@ -457,6 +543,7 @@ def test_the_server_takes_the_same_key_id_of_a_stitched_playlist(
cdm_type_stub("playready"),
id="not-sample-encryption",
),
pytest.param(playlist(IDENTITY_KEY), KID, cdm_type_stub("playready"), id="identity-sample-aes"),
],
)
def test_a_track_with_no_drm_that_cannot_be_bridged_is_refused_before_the_download(
@@ -466,7 +553,7 @@ def test_a_track_with_no_drm_that_cannot_be_bridged_is_refused_before_the_downlo
probe = init_kid_probe(monkeypatch, kid)
session = InitServer()
with pytest.raises(NotImplementedError, match="com.apple.streamingkeydelivery"):
with pytest.raises(NotImplementedError, match="None of the key systems are supported: SAMPLE-AES"):
download(track, cdm, session=session)
assert not track.drm