fix(drm): send a bridged licence key to the server vault only after its decode check decodes it

This commit is contained in:
imSp4rky
2026-12-01 21:42:36 -07:00
parent cefd5295ac
commit 1288a8e9b7
3 changed files with 42 additions and 18 deletions
+18 -10
View File
@@ -156,7 +156,8 @@ class ContentKeyResolver:
failure can only flag and raise; the next run skips the flagged pair.
After a decrypt that passes, the server key source's vault gets the proven content keys of
``track``: each licence key, and each vault key the decode check passed.
``track``: each licence key, and each vault key the decode check passed. A bridged track sends only
the licence keys that the decode check decoded.
Before the decrypt, each KID the DRM names without a content key is licensed when the file
uses it, or when the file cannot be mapped, because ``_prepare_drm`` licensed only for the track's KID.
@@ -239,11 +240,16 @@ class ContentKeyResolver:
windows = kid_map.windows if kid_map else {}
video = kid_map.video if kid_map else None
def proven(passed: dict[UUID, KeySource]) -> dict[UUID | str, str]:
"""The licence keys and the vault keys in ``passed``, without null keys; a flagged pair never gets here."""
def proven(passed: dict[UUID, KeySource], decoded: set[UUID]) -> dict[UUID | str, str]:
"""The licence keys and the vault keys in ``passed``, without null keys; a flagged pair never gets here.
A bridged DRM's licence key counts only when its KID is in ``decoded``.
"""
sourced = vault_kids()
return {
kid: key for kid, key in keys.items() if (kid in passed or kid not in sourced) and not is_null_key(key)
kid: key
for kid, key in keys.items()
if (kid in passed or (kid in decoded if bridged else kid not in sourced)) and not is_null_key(key)
}
def window_decodes(start: float, end: float) -> bool:
@@ -256,14 +262,15 @@ class ContentKeyResolver:
return {kid: vault for kid, vault in kids.items() if kid in windows}
return kids
def refuse_bad_licence_keys() -> None:
def refuse_bad_licence_keys() -> set[UUID]:
"""Stop a bridged track whose licence content key does not decode, and flag the pair like a bad vault key.
A multi-DRM licence server can issue a PlayReady or Widevine content key that is not the FairPlay one.
The flag goes through the vault writer, after the licence's own write of the pair.
The flag goes through the vault writer, after the licence's own write of the pair. Returns the KIDs that
the check decoded.
"""
if not (bridged and binaries.FFMPEG and path.exists()):
return
return set()
sourced = vault_kids()
licensed = judged_kids({kid: None for kid in real_kids(drm) if kid in keys and kid not in sourced})
if bad := failed_kids(licensed):
@@ -273,6 +280,7 @@ class ContentKeyResolver:
raise ValueError(
f"The licence content key of KID {kid.hex} does not decrypt track {getattr(track, 'id', path.name)}"
)
return set(licensed)
def failed_kids(kids: dict[UUID, Any]) -> set[UUID]:
"""The vault KIDs whose content key did not decode.
@@ -311,12 +319,12 @@ class ContentKeyResolver:
checked = bool(kids and binaries.FFMPEG and path.exists())
failed = failed_kids(kids) if checked else set()
if not failed:
refuse_bad_licence_keys()
decoded = refuse_bad_licence_keys()
passed = kids if checked else {}
self._flush_vault_writes([partial(self.vaults.proven, dict(keys), passed)])
server_vault = self.source.server_vault if self.source and track is not None else None
if server_vault is not None:
self._flush_vault_writes([partial(server_vault.add_keys, self.tag, proven(passed), track)])
if server_vault is not None and (sent := proven(passed, decoded)):
self._flush_vault_writes([partial(server_vault.add_keys, self.tag, sent, track)])
return
bad = {kid: drop(kid) for kid in {*failed, *stale}}
for kid, key in bad.items():