diff --git a/docs/guide/troubleshooting.md b/docs/guide/troubleshooting.md index 6bd3132c..2b057db9 100644 --- a/docs/guide/troubleshooting.md +++ b/docs/guide/troubleshooting.md @@ -294,7 +294,7 @@ Each command reports how many files it removed and how much space it freed, then **Inspect:** enable `debug_keys` and operate unshackle with `-d`. Then examine the JSON debug log's `drm_*` and `vault_*` operations to see which content keys unshackle fetched, and from where. Also make sure that shaka-packager (or `mp4decrypt`) shows a sane version in the `download_init` binary-versions entry. See [DRM & CDM](drm-and-cdm.md). !!! example "A vault served a wrong content key" - **Symptom:** a warning names a content key and the vault it came from, either `was bad, trying other vaults` (the decode check failed) or `is flagged bad, skipping` (an earlier run flagged it). The download stops with `The content key from the vault did not decrypt the track; run again`, `was bad and no other source has the key`, or `No vault or CDM produced a content key that decrypts the track` when no source is left to try. + **Symptom:** a warning names a content key and the vault it came from, either `was bad, trying other vaults` (the decode check failed) or `is flagged bad, skipping` (an earlier run flagged it). The download stops with `The content key from the vault did not decrypt the track; run again`, `was bad and no other source has the key`, or `No vault or CDM produced a content key that decrypts the track` when no source is left to try. If the licence returns the same content key that the check flagged, unshackle keeps the key and the download continues. The warning `Licence returned the same content key for KID , keeping it` then shows that the decode check failed for a different cause. **Inspect:** while it decrypts a track whose content key came from a vault, unshackle keeps the ciphertext as a `.enc` sibling of the track file in the temp directory, and removes it afterwards; a leftover one is from a killed run and is safe to delete. Read the `bad_keys` table of your local SQLite vault to see which pairs are flagged and which vault each came from. See [Wrong keys from a poisoned vault](vaults.md#wrong-keys-from-a-poisoned-vault). diff --git a/tests/core/test_vault_bad_keys.py b/tests/core/test_vault_bad_keys.py index 19084769..6561cc09 100644 --- a/tests/core/test_vault_bad_keys.py +++ b/tests/core/test_vault_bad_keys.py @@ -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( diff --git a/unshackle/core/drm/key_resolution.py b/unshackle/core/drm/key_resolution.py index ebe8f595..a4586650 100644 --- a/unshackle/core/drm/key_resolution.py +++ b/unshackle/core/drm/key_resolution.py @@ -199,6 +199,8 @@ class ContentKeyResolver: source = self.vaults.source_of(kid, new) if source is None: if new == key: + if self.vaults.first(("licence", kid, key), self.vaults.warned): + self.log.warning(f"Licence returned the same content key for KID {kid.hex}, keeping it") self.vaults.unflag_bad_key(kid, key) if new == key or not self.vaults.held_locally(kid, new): self._flush_vault_writes([partial(self._cache_keys_to_vaults, {kid: new})])