Skip to content

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

tools: refuse debloat lists that are not bare package names (#165)

xbsheng32b3c0033a7b
diff --git a/tests/test_debloat_paths.py b/tests/test_debloat_paths.py
index acb1a8280..d67b4c990 100644
--- a/tests/test_debloat_paths.py
+++ b/tests/test_debloat_paths.py
@@ -20,6 +20,39 @@ class DebloatPathsTest(unittest.TestCase):
with self.subTest(script=script, path_kind=path_kind):
self.check_list_path(script, path_kind)
+ def test_injected_command_is_refused(self):
+ # A contributor-supplied list must not be able to add shell syntax (#165).
+ for script in ("disable.sh", "restore.sh"):
+ with self.subTest(script=script):
+ self.check_injected_line(script)
+
+ def check_injected_line(self, script):
+ with tempfile.TemporaryDirectory() as tmp:
+ base = Path(tmp)
+ tools = base / "repo" / "tools"
+ tools.mkdir(parents=True)
+ shutil.copy2(ROOT / "tools" / script, tools / script)
+ client = tools / "venv" / "bin" / "python"
+ client.parent.mkdir(parents=True)
+ client.write_text('#!/bin/sh\ncat > "$CAPTURE"\n')
+ client.chmod(0o755)
+
+ package_list = base / "debloat.txt"
+ package_list.write_text(
+ "# My TV\ncom.example.good\ncom.example.evil;settings put global adb_enabled 0\n"
+ )
+ capture = base / "commands.txt"
+ result = subprocess.run(
+ ["bash", str(tools / script), str(package_list)], cwd=base,
+ env=dict(os.environ, CAPTURE=str(capture)),
+ capture_output=True, text=True,
+ )
+ self.assertNotEqual(result.returncode, 0, "the list should have been refused")
+ self.assertIn("not a package name", result.stderr)
+ sent = capture.read_text() if capture.exists() else ""
+ self.assertNotIn("settings put", sent)
+ self.assertNotIn("com.example.good", sent)
+
def check_list_path(self, script, path_kind):
with tempfile.TemporaryDirectory() as tmp:
base = Path(tmp)
diff --git a/tools/disable.ps1 b/tools/disable.ps1
index 89962bf8e..d11131483 100644
--- a/tools/disable.ps1
+++ b/tools/disable.ps1
@@ -17,6 +17,11 @@ if (-not (Test-Path $py)) { throw 'run: python -m venv tools\venv; tools\venv\Sc
if (-not $TvHost) { throw 'pass -TvHost <tv ip> or set $env:TV_HOST' }
$env:TV_HOST = $TvHost
-$pkgs = Get-Content $List | ForEach-Object { ($_ -replace '#.*$', '').Trim() } | Where-Object { $_ }
+$pkgs = Get-Content $List | ForEach-Object { ($_ -replace '#.*$', '').Trim() } | Where-Object { $_ } | ForEach-Object { ($_ -split '\s+')[0] }
+# Lists come from contributors and each name is pasted into a shell command on the TV (#165).
+$bad = @($pkgs | Where-Object { $_ -notmatch '^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+$' })
+if ($bad.Count -gt 0) {
+ throw "refusing $List, not a package name: $($bad -join ', ')"
+}
$cmds = @($pkgs | ForEach-Object { "pm disable-user --user 0 $_" }) + 'pm list packages -d'
$cmds | & $py -I (Join-Path $repo 'tools\tv.py') --batch
diff --git a/tools/disable.sh b/tools/disable.sh
index cf6b22e20..d0e05bded 100755
--- a/tools/disable.sh
+++ b/tools/disable.sh
@@ -13,7 +13,15 @@ PY=tools/venv/bin/python
[ -x "$PY" ] || PY=tools/venv/Scripts/python.exe
export MSYS_NO_PATHCONV=1
[ -x "$PY" ] || { echo "run: python3 -m venv tools/venv && tools/venv/bin/pip install -r tools/requirements.txt"; exit 1; }
+# Lists come from contributors, and the package name is pasted into a shell command on the TV.
+# Refuse the whole list unless every entry is a bare package name (#165).
+PKGS=$(grep -vE '^\s*(#|$)' "$LIST" | sed -E 's/\s+#.*$//' | awk '{print $1}')
+BAD=$(printf '%s\n' "$PKGS" | grep -vE '^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+$' || true)
+if [ -n "$BAD" ]; then
+ { echo "refusing $LIST: not a package name:"; printf '%s\n' "$BAD" | sed 's/^/ /'; } >&2
+ exit 1
+fi
{
- grep -vE '^\s*(#|$)' "$LIST" | sed -E 's/\s+#.*$//' | awk '{print "pm disable-user --user 0 " $1}'
+ printf '%s\n' "$PKGS" | sed 's/^/pm disable-user --user 0 /'
echo "pm list packages -d"
} | "$PY" -I tools/tv.py --batch
diff --git a/tools/restore.ps1 b/tools/restore.ps1
index f7a51f950..9b363526f 100644
--- a/tools/restore.ps1
+++ b/tools/restore.ps1
@@ -16,7 +16,12 @@ $py = Join-Path $repo 'tools\venv\Scripts\python.exe'
if (-not $TvHost) { throw 'pass -TvHost <tv ip> or set $env:TV_HOST' }
$env:TV_HOST = $TvHost
-$pkgs = Get-Content $List | ForEach-Object { ($_ -replace '#.*$', '').Trim() } | Where-Object { $_ }
+$pkgs = Get-Content $List | ForEach-Object { ($_ -replace '#.*$', '').Trim() } | Where-Object { $_ } | ForEach-Object { ($_ -split '\s+')[0] }
+# Same check as disable.ps1: the list is contributor-supplied (#165).
+$bad = @($pkgs | Where-Object { $_ -notmatch '^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+$' })
+if ($bad.Count -gt 0) {
+ throw "refusing $List, not a package name: $($bad -join ', ')"
+}
$cmds = @($pkgs | ForEach-Object { "pm enable $_" }) + @(
"pm enable $Stock",
# resolve the stock HOME component on the TV (Android 9 needs pkg/activity, not just pkg)
diff --git a/tools/restore.sh b/tools/restore.sh
index eb32e1b04..12ebdd272 100755
--- a/tools/restore.sh
+++ b/tools/restore.sh
@@ -13,8 +13,16 @@ STOCK="${2:-com.google.android.tvlauncher}"
PY=tools/venv/bin/python
[ -x "$PY" ] || PY=tools/venv/Scripts/python.exe
export MSYS_NO_PATHCONV=1
+# Same check as disable.sh: the list is contributor-supplied and each entry becomes a shell
+# command on the TV (#165).
+PKGS=$(grep -vE '^\s*(#|$)' "$LIST" | sed -E 's/\s+#.*$//' | awk '{print $1}')
+BAD=$(printf '%s\n' "$PKGS" | grep -vE '^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+$' || true)
+if [ -n "$BAD" ]; then
+ { echo "refusing $LIST: not a package name:"; printf '%s\n' "$BAD" | sed 's/^/ /'; } >&2
+ exit 1
+fi
{
- grep -vE '^\s*(#|$)' "$LIST" | sed -E 's/\s+#.*$//' | awk '{print "pm enable " $1}'
+ printf '%s\n' "$PKGS" | sed 's/^/pm enable /'
echo "pm enable $STOCK"
# resolve the stock HOME component on the TV (Android 9 needs pkg/activity, not just pkg)
echo "cmd package set-home-activity --user 0 \$(cmd package resolve-activity --brief -a android.intent.action.MAIN -c android.intent.category.HOME $STOCK | tail -n 1)"