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: match the deny-list per shell segment, not just the whole line

Durganshu Mishra32de77b69af6
diff --git a/app/src/main/java/com/gotcha/tools/TermuxTool.kt b/app/src/main/java/com/gotcha/tools/TermuxTool.kt
index 2329364..c2523d2 100644
--- a/app/src/main/java/com/gotcha/tools/TermuxTool.kt
+++ b/app/src/main/java/com/gotcha/tools/TermuxTool.kt
@@ -211,7 +211,12 @@ class TermuxTool(
if (total > MAX_COMMAND_CHARS) return TermuxMessages.tooLarge(total, MAX_COMMAND_CHARS)
// 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 {
+ // Check each shell segment as well as the whole line. The root-tree `rm` pattern is
+ // end-anchored — it has to be, or `rm -rf /home/x` would match — and the schema tells
+ // the model to chain steps with `&&`, so `rm -rf / && echo done` is the single most
+ // likely shape for this mistake to arrive in. Whole-line matching alone would miss it.
+ val candidates = normalised.split(SEGMENT_SEPARATORS) + normalised
+ if (candidates.any { segment -> denyPatterns.any { it.containsMatchIn(segment.trim()) } }) {
return TermuxMessages.blocked(trimmed)
}
return null
@@ -341,6 +346,9 @@ class TermuxTool(
internal const val ERRNO_MINOR_FAILURES = 1 // Activity.RESULT_FIRST_USER
internal const val ERRNO_FAILED = 2 // Activity.RESULT_FIRST_USER + 1
+ /** Shell separators the deny-list splits on before matching each part. */
+ private val SEGMENT_SEPARATORS = Regex("""[;&|\n]+""")
+
/** Termux caps a single command near 128KB; leave headroom for the rest of the intent. */
private const val MAX_COMMAND_CHARS = 100 * 1024
diff --git a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt b/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
index 2a59211..340ccf0 100644
--- a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
+++ b/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
@@ -276,7 +276,14 @@ class TermuxToolTest {
"rm -r -f /",
"rm -rf /", // extra whitespace must not slip past
"rm -rf --no-preserve-root /home",
- "fastboot oem unlock"
+ "fastboot oem unlock",
+ // Chained forms. The schema tells the model to chain with '&&', so these are the
+ // most likely shape for this mistake — an end-anchored pattern alone misses them.
+ "rm -rf / && echo done",
+ "pkg update && rm -rf /*",
+ "rm -rf /; pkg install python",
+ "rm -rf /*| sh",
+ "cd /tmp && mkfs.ext4 /dev/block/sda"
).forEach { command ->
val result = tool.runCommand(command)
assertFalse("expected '$command' to be blocked", result.success)
@@ -288,11 +295,18 @@ class TermuxToolTest {
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("'$command' must not be refused as unsafe", result.message.contains("safety policy"))
- }
+ listOf(
+ "rm -rf ~/build",
+ "rm -rf /data/data/com.termux/files/home/tmp",
+ "dd if=in.img of=out.img",
+ // Splitting on separators must not turn ordinary chains into false positives.
+ "cd ~/proj && rm -rf build && ./configure",
+ "pkg install python -y && python3 -c 'print(1)'",
+ "grep -r \"rm -rf /\" notes.txt"
+ ).forEach { command ->
+ val result = tool.runCommand(command)
+ assertFalse("'$command' must not be refused as unsafe", result.message.contains("safety policy"))
+ }
}
@Test