feat(drm): warn when the licence returns the content key that the decode check flagged

This commit is contained in:
imSp4rky
2026-12-01 18:32:37 -07:00
parent 41c2b3d86a
commit 3963e80ec3
3 changed files with 57 additions and 11 deletions
+54 -10
View File
@@ -19,6 +19,7 @@ KID = UUID("11111111-2222-3333-4444-555555555555")
SIBLING = UUID("99999999-2222-3333-4444-555555555555")
BAD = "aa" * 16
GOOD = "bb" * 16
CONFIRMED = ("download", logging.WARNING, f"Licence returned the same content key for KID {KID.hex}, keeping it")
class Remote(SQLite):
@@ -291,9 +292,9 @@ def test_retry_takes_the_next_parallel_answer_without_a_new_request(tmp_path: Pa
@pytest.mark.parametrize("replace", [False, True])
def test_cdm_returning_the_flagged_key_clears_the_flag(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, replace: bool
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, replace: bool
) -> None:
"""A decode failure on a key the CDM then confirms is a false positive: unflag, keep the file."""
"""A decode failure on a key the CDM then confirms is a false positive: unflag, keep the file, warn once."""
local = SQLite("local", tmp_path / "local.db")
poisoned = Remote("poisoned", tmp_path / "poisoned.db")
poisoned.add_key("SVC", KID, BAD)
@@ -312,14 +313,53 @@ def test_cdm_returning_the_flagged_key_clears_the_flag(
else:
drm.content_keys[KID] = BAD # the CDM answers with the same key
keys.decrypt_verified(drm, path, licence, KID)
keys.wait_vault_writes()
with caplog.at_level(logging.WARNING):
keys.decrypt_verified(drm, path, licence, KID)
keys.wait_vault_writes()
assert path.read_bytes() == b"plain:" + BAD.encode()
assert not local.is_bad_key(KID, BAD)
assert local.get_key(KID, "SVC") == BAD
assert caplog.record_tuples == [
("download", logging.WARNING, f"{BAD} from poisoned was bad, trying other vaults"),
("unshackle.core.vaults", logging.WARNING, f"{BAD} from poisoned is flagged bad, skipping"),
CONFIRMED,
]
def test_a_replacement_key_in_a_new_dict_is_checked_and_stored(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
def test_two_tracks_that_relicense_one_kid_warn_once(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A second track that relicenses the flagged pair while the first one waits for its licence."""
local = SQLite("local", tmp_path / "local.db")
poisoned = Remote("poisoned", tmp_path / "poisoned.db")
poisoned.add_key("SVC", KID, BAD)
vaults = Vaults("SVC")
vaults.vaults = [local, poisoned]
keys = make_resolver(monkeypatch, vaults)
monkeypatch.setattr(
key_resolution, "ffmpeg_decodes", lambda path, start=None, seconds=3, video=None: path.name != "video.mp4"
)
video, audio = tmp_path / "video.mp4", tmp_path / "audio.mp4"
video.write_bytes(b"cipher")
audio.write_bytes(b"cipher")
drm_a, drm_b = FakeDRM({KID: vaults.get_key(KID)[0]}), FakeDRM({KID: vaults.get_key(KID)[0]})
def licence_b(drm: FakeDRM, track_kid: UUID) -> None:
drm.content_keys[KID] = BAD
def licence_a(drm: FakeDRM, track_kid: UUID) -> None:
keys.decrypt_verified(drm_b, audio, licence_b, KID)
drm.content_keys[KID] = BAD
with caplog.at_level(logging.WARNING):
keys.decrypt_verified(drm_a, video, licence_a, KID)
keys.wait_vault_writes()
assert caplog.record_tuples.count(CONFIRMED) == 1
def test_a_replacement_key_in_a_new_dict_is_checked_and_stored(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
local = SQLite("local", tmp_path / "local.db")
poisoned = Remote("poisoned", tmp_path / "poisoned.db")
poisoned.add_key("SVC", KID, BAD)
@@ -337,15 +377,17 @@ def test_a_replacement_key_in_a_new_dict_is_checked_and_stored(tmp_path: Path, m
vaults.vaults.append(other)
drm.content_keys = {KID: vaults.get_key(KID)[0]}
keys.decrypt_verified(drm, path, licence, KID)
keys.wait_vault_writes()
with caplog.at_level(logging.WARNING):
keys.decrypt_verified(drm, path, licence, KID)
keys.wait_vault_writes()
assert path.read_bytes() == b"plain:" + GOOD.encode()
assert local.get_key(KID, "SVC") == GOOD
assert CONFIRMED not in caplog.record_tuples
@pytest.mark.parametrize("replace", [True, False])
def test_a_cdm_that_confirms_a_key_flagged_on_entry_clears_the_flag(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, replace: bool
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, replace: bool
) -> None:
"""Another track flagged the pair, then this track's CDM licence answers with the same key."""
local = SQLite("local", tmp_path / "local.db")
@@ -363,11 +405,13 @@ def test_a_cdm_that_confirms_a_key_flagged_on_entry_clears_the_flag(
else:
drm.content_keys[KID] = BAD
keys.decrypt_verified(drm, path, licence, KID)
keys.wait_vault_writes()
with caplog.at_level(logging.WARNING):
keys.decrypt_verified(drm, path, licence, KID)
keys.wait_vault_writes()
assert path.read_bytes() == b"plain:" + BAD.encode()
assert not local.is_bad_key(KID, BAD)
assert local.get_key(KID, "SVC") == BAD
assert CONFIRMED in caplog.record_tuples
def test_a_licence_key_that_replaces_a_bad_local_pair_is_stored(