Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

recovery.py: check the record's names, states and Home before replaying it

James Coates4c137c7f6ef5
diff --git a/tests/test_recovery_record.py b/tests/test_recovery_record.py
index 882d58bde..e75218195 100644
--- a/tests/test_recovery_record.py
+++ b/tests/test_recovery_record.py
@@ -348,6 +348,53 @@ class RecoveryRecordTest(unittest.TestCase):
self.assertIn("com.example.evil;settings", result.stderr)
self.assertEqual(self.sent(), [])
+ def test_valid_record_restores_the_same_batch(self):
+ # The whole batch for the usual case, so the checks on the record cannot change it.
+ self.tv()
+ self.assertEqual(self.run_script("disable.sh", str(self.list_path)).returncode, 0)
+ self.clear_sent()
+ result = self.run_script("restore.sh", str(self.list_path))
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertEqual(self.sent(), [
+ "pm enable com.example.enabled",
+ "pm enable com.google.android.tvlauncher",
+ f"cmd package set-home-activity --user 0 {STOCK_HOME}",
+ "settings put global window_animation_scale 0.5",
+ "settings delete global transition_animation_scale",
+ "settings put global animator_duration_scale 1.0",
+ "pm list packages -d",
+ ])
+
+ def test_edited_record_is_refused_and_left_alone(self):
+ # The record is replayed on the TV's shell, so a hand-edited one gets the same checks the list
+ # does: nothing reaches the TV and the file stays as it was for inspection.
+ edits = {
+ "package name": lambda r: r["packages"].update(
+ {"com.example.x; settings put global adb_enabled 0 #": "enabled"}),
+ "package state": lambda r: r["packages"].update({"com.example.enabled": "on"}),
+ "home": lambda r: r.update(home=f"{STOCK_HOME}; settings put global adb_enabled 0"),
+ "settings": lambda r: r.update(settings=["window_animation_scale", "0.5"]),
+ }
+ for field, edit in edits.items():
+ for script in ("restore.sh", "disable.sh"):
+ with self.subTest(field=field, script=script):
+ shutil.rmtree(self.home / ".cleanyourtv", ignore_errors=True)
+ self.tv()
+ self.assertEqual(self.run_script("disable.sh", str(self.list_path)).returncode, 0)
+ path = next((self.home / ".cleanyourtv").glob("recovery-*.json"))
+ record = json.loads(path.read_text())
+ edit(record)
+ path.write_text(json.dumps(record))
+ before = path.read_bytes()
+ self.clear_sent()
+
+ result = self.run_script(script, str(self.list_path))
+ self.assertNotEqual(result.returncode, 0)
+ self.assertIn(str(path), result.stderr)
+ self.assertIn("not a usable recovery record", result.stderr)
+ self.assertEqual(self.sent(), [], "nothing reached the TV")
+ self.assertEqual(path.read_bytes(), before, "the file is left for inspection")
+
if __name__ == "__main__":
unittest.main()
diff --git a/tools/recovery.py b/tools/recovery.py
index 40d17baaf..2500157f3 100644
--- a/tools/recovery.py
+++ b/tools/recovery.py
@@ -73,6 +73,22 @@ def load_record(path):
"replace the state recorded before the first change.")
if record.get("version") != VERSION or not isinstance(record.get("packages"), dict):
raise SystemExit(f"{path}: unknown recovery record format; move it aside deliberately.")
+ # The record is replayed on the TV's shell, so it gets the same checks as the list and the TV's
+ # answers did when it was written: a hand-edited or damaged file stops here, before any command.
+ for name, state in record["packages"].items():
+ if not PACKAGE_RE.fullmatch(name):
+ raise SystemExit(f"{path}: not a usable recovery record (not a package name: {name!r}); "
+ "move it aside deliberately.")
+ if state not in ("absent", "disabled", "enabled"):
+ raise SystemExit(f"{path}: not a usable recovery record (unknown state for {name}: "
+ f"{state!r}); move it aside deliberately.")
+ home = record.get("home")
+ if home is not None and not (isinstance(home, str) and HOME_RE.fullmatch(home)):
+ raise SystemExit(f"{path}: not a usable recovery record (not a Home component: {home!r}); "
+ "move it aside deliberately.")
+ if not isinstance(record.get("settings", {}), dict):
+ raise SystemExit(f"{path}: not a usable recovery record (settings is not a mapping); "
+ "move it aside deliberately.")
return record