Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

fix: harden the Termux deny-list against whitespace and flag-splitting

Durganshu Mishra9387e1c6cdf2
diff --git a/app/src/main/java/com/gotcha/tools/TermuxTool.kt b/app/src/main/java/com/gotcha/tools/TermuxTool.kt
index 22db5d0..f9b57d0 100644
--- a/app/src/main/java/com/gotcha/tools/TermuxTool.kt
+++ b/app/src/main/java/com/gotcha/tools/TermuxTool.kt
@@ -67,11 +67,21 @@ class TermuxTool(
* Irreversible, device-destroying operations we refuse even here. Deliberately
* [RootTool]'s narrow list rather than [TerminalTool]'s: `rm -r` inside Termux's own
* `$HOME` is ordinary housekeeping, and `pkg` needs it.
+ *
+ * **This is a guardrail, not a security boundary.** It matches text, so anything indirect
+ * walks straight through it — a variable, an escape, a here-doc, `base64 -d | sh`. It exists
+ * to stop a plausible-looking mistake from wiping a device, not to contain an attacker who
+ * already controls what the model asks for. The real boundary is the user granting Termux
+ * access at all. Do not add checks here that only make sense against a determined evader:
+ * they would suggest a guarantee this cannot make.
*/
private val denyPatterns = listOf(
Regex("""\bmkfs\b"""), // reformat a filesystem
Regex("""\bdd\b[^\n]*\bof=/dev/"""), // raw write to a block device
- Regex("""\brm\s+-[a-z]*r[a-z]*f?\s+/(\s|$)"""), // rm -rf / (wipe the root tree)
+ // `rm` whose target is the root tree itself: `rm -rf /`, `rm -rf /*`, `rm -r -f /`.
+ Regex("""\brm\s+(?:-\S+\s+)*/\s*\*?\s*$"""),
+ // Defeats coreutils' own refusal to remove `/`, so its presence is intent enough.
+ Regex("""--no-preserve-root"""),
Regex("""\bfastboot\b"""),
Regex("""\brecovery\b[^\n]*--wipe""")
)
@@ -192,7 +202,9 @@ class TermuxTool(
// Command and stdin ride in the same binder transaction, so they share the budget.
val total = trimmed.length + (stdin?.length ?: 0)
if (total > MAX_COMMAND_CHARS) return TermuxMessages.tooLarge(total, MAX_COMMAND_CHARS)
- denyPatterns.firstOrNull { it.containsMatchIn(trimmed) }?.let {
+ // Collapse whitespace first, so `rm -rf /` is not a way around the patterns.
+ val normalised = trimmed.replace(Regex("""\s+"""), " ")
+ denyPatterns.firstOrNull { it.containsMatchIn(normalised) }?.let {
return TermuxMessages.blocked(trimmed)
}
return null
diff --git a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt b/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
index 975f07e..17275dc 100644
--- a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
+++ b/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
@@ -247,11 +247,30 @@ class TermuxToolTest {
fun `device-destroying commands are blocked by the deny-list`() = runTest {
installTermux()
grantRunCommand()
- listOf("mkfs.ext4 /dev/block/sda", "dd if=/dev/zero of=/dev/block/sda", "rm -rf / ", "fastboot oem unlock")
+ listOf(
+ "mkfs.ext4 /dev/block/sda",
+ "dd if=/dev/zero of=/dev/block/sda",
+ "rm -rf /",
+ "rm -rf /*",
+ "rm -r -f /",
+ "rm -rf /", // extra whitespace must not slip past
+ "rm -rf --no-preserve-root /home",
+ "fastboot oem unlock"
+ ).forEach { command ->
+ val result = tool.runCommand(command)
+ assertFalse("expected '$command' to be blocked", result.success)
+ assertTrue("'$command' should cite the policy: ${result.message}", result.message.contains("safety policy"))
+ }
+ }
+
+ @Test
+ fun `ordinary destructive-looking commands are not blocked`() = runTest {
+ // The deny-list guards the root tree, not the user's own files — `pkg` and everyday
+ // housekeeping both need recursive deletes to work.
+ listOf("rm -rf ~/build", "rm -rf /data/data/com.termux/files/home/tmp", "dd if=in.img of=out.img")
.forEach { command ->
val result = tool.runCommand(command)
- assertFalse("expected '$command' to be blocked", result.success)
- assertTrue(result.message.contains("safety policy"))
+ assertFalse("'$command' must not be refused as unsafe", result.message.contains("safety policy"))
}
}