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
+19 -18
View File
@@ -192,7 +192,7 @@ class HLS:
All Track objects' URL will be to another M3U(8) document. However, these documents
will be Invariant Playlists and contain the list of segments URIs among other metadata.
A variant or a media entry that the master lists twice gets one track.
A variant or a media entry that the master lists twice with the same attributes gets one track.
"""
session_keys = list(self.manifest.session_keys or [])
if not session_keys:
@@ -218,8 +218,10 @@ class HLS:
seen.add(mime)
cc_by_group_id: dict[str, list[dict[str, Any]]] = {}
cc_entries: set[str] = set()
for media in self.manifest.media:
if media.type == "CLOSED-CAPTIONS":
if media.type == "CLOSED-CAPTIONS" and str(media) not in cc_entries:
cc_entries.add(str(media))
cc_by_group_id.setdefault(media.group_id, []).append(
{
"language": media.language,
@@ -310,9 +312,10 @@ class HLS:
entries: set[str] = set()
for media in self.manifest.media:
if not media.uri or str(media) in entries:
entry = str(media)
if not media.uri or entry in entries:
continue
entries.add(str(media))
entries.add(entry)
joc = 0
track_type: type[Audio] | type[Subtitle]
@@ -345,7 +348,7 @@ class HLS:
tracks.add(
track_type(
id_=hex(crc32(str(media).encode()))[2:],
id_=hex(crc32(entry.encode()))[2:],
url=urljoin(media.base_uri, media.uri),
codec=codec,
language=track_lang, # HLS media may not have language info, fallback if needed
@@ -706,16 +709,14 @@ class HLS:
if ctx.on_segments:
ctx.on_segments(track, wanted_segments)
if track.drm and HLS.drop_unsupported_keys(master):
log.debug(
f"{track.id}: its key tags name key systems unshackle cannot license, using the DRM of the service"
)
# Get session DRM as fallback but prefer media playlist keys for accurate KID matching
if track.drm:
session_drm = track.get_drm_for_cdm(cdm)
else:
session_drm = None
session_drm = track.get_drm_for_cdm(cdm) if track.drm else None
if not isinstance(session_drm, (Widevine, PlayReady)):
for segment in wanted_segments:
if segment.keys:
HLS.resolve_segment_key(segment.keys, cdm, track.drm_preference)
elif HLS.drop_unsupported_keys(master):
log.debug(f"{track.id}: its key tags name key systems unshackle cannot license, using the DRM of the track")
initial_drm_licensed = False
initial_drm_key = None # Track the EXT-X-KEY used for initial licensing
@@ -1472,13 +1473,13 @@ class HLS:
@staticmethod
def drop_unsupported_keys(manifest: M3U8) -> bool:
"""Remove every EXT-X-KEY of `manifest` when each one names a DRM system unshackle cannot license; True when it did.
"""Remove every EXT-X-KEY of `manifest` when each is sample encryption of a DRM system unshackle cannot license.
A service can license such a playlist another way, with the DRM it puts on the track. The download then
reads the playlist as one without an EXT-X-KEY, and the track keeps that DRM.
Returns whether it did. The download then reads the playlist as one without an EXT-X-KEY and decrypts with the
one Widevine or PlayReady DRM of the track. Any other METHOD, such as AES-128 or AES-256, keeps every tag.
"""
keys = [key for key in (manifest.keys or []) if key is not None]
if not keys or any(key.method in ("NONE", "AES-128") or cenc_key(key) for key in keys):
if not keys or any(key.method not in ("SAMPLE-AES", "SAMPLE-AES-CTR") or cenc_key(key) for key in keys):
return False
manifest.keys = []
for segment in manifest.segments: