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: seed PendingIntent request codes and keep stdin out of the audit log
Durganshu Mishrac18adc8bda61
diff --git a/app/src/main/java/com/gotcha/tools/TermuxTool.kt b/app/src/main/java/com/gotcha/tools/TermuxTool.ktindex c2523d2..050c066 100644--- a/app/src/main/java/com/gotcha/tools/TermuxTool.kt+++ b/app/src/main/java/com/gotcha/tools/TermuxTool.kt@@ -12,6 +12,7 @@ import kotlinx.coroutines.sync.Semaphoreimport kotlinx.coroutines.withTimeoutOrNullimport java.util.concurrent.ConcurrentHashMapimport java.util.concurrent.atomic.AtomicInteger+import kotlin.random.Random/*** Tier 4 — arbitrary shell inside Termux, via Termux's official RUN_COMMAND plugin API.@@ -146,7 +147,7 @@ class TermuxTool(// reach. Without a ceiling, a model that retries a slow `pkg` a few times would leave a// growing pile of live processes behind it, each still holding Termux's package lock.if (!inFlight.tryAcquire()) return TermuxMessages.tooManyInFlight(MAX_CONCURRENT_COMMANDS)- val requestCode = nextRequestCode.getAndIncrement()+ val requestCode = nextRequestCode()val deferred = CompletableDeferred<Bundle>()pendingResults[requestCode] = deferredtry {@@ -376,7 +377,20 @@ class TermuxTool(private val inFlight = Semaphore(MAX_CONCURRENT_COMMANDS)- private val nextRequestCode = AtomicInteger(1)+ /**+ * Seeded per process, not from 1.+ *+ * A PendingIntent outlives us: if Gotcha dies while Termux still holds an unfired one,+ * the next process starting from 1 would ask for the same request code, and+ * `getBroadcast` matches on request code and intent — extras are not part of the+ * identity — so it would hand back the *stale* PendingIntent. Termux would then fire the+ * old command's result into the new command's deferred, and the agent would act on+ * output belonging to a command it never ran. A random start makes the two code spaces+ * disjoint in practice. Masked to stay non-negative, which the receiver relies on.+ */+ private val nextRequestCode = AtomicInteger(Random.nextInt(1, 1 shl 20))++ private fun nextRequestCode(): Int = nextRequestCode.getAndIncrement() and Int.MAX_VALUE/*** In-flight commands, keyed by PendingIntent request code so concurrent calls cannotdiff --git a/app/src/main/java/com/gotcha/tools/ToolExecutor.kt b/app/src/main/java/com/gotcha/tools/ToolExecutor.ktindex 5624b63..f31d9c7 100644--- a/app/src/main/java/com/gotcha/tools/ToolExecutor.kt+++ b/app/src/main/java/com/gotcha/tools/ToolExecutor.kt@@ -11,6 +11,7 @@ import kotlinx.coroutines.withContextimport kotlinx.serialization.json.JsonArrayimport kotlinx.serialization.json.JsonElementimport kotlinx.serialization.json.JsonObject+import kotlinx.serialization.json.JsonPrimitiveimport kotlinx.serialization.json.booleanOrNullimport kotlinx.serialization.json.intOrNullimport kotlinx.serialization.json.jsonArray@@ -30,6 +31,9 @@ class ToolExecutor(private companion object {const val TAG = "Gotcha"++ /** Argument names never written verbatim to the audit log. See [redactedForAudit]. */+ val REDACTED_AUDIT_KEYS = setOf("stdin")}private val appContext = context.applicationContext@@ -120,10 +124,30 @@ class ToolExecutor(TAG,"execute: $name -> success=${result.success}, msgLen=${result.message.length}, perm=${result.needsPermission}")- actionLog.record(name, args.toString(), result)+ actionLog.record(name, args.redactedForAudit(), result)return result}+ /**+ * Arguments as they may be written to the on-disk audit log.+ *+ * The log is plaintext in the app sandbox and deliberately records what each tool was asked+ * to do, but some arguments are secrets by their nature rather than by their content:+ * `stdin` exists precisely so the model can answer a prompt, and the prompts worth answering+ * are passwords, passphrases and tokens. Logging the argument name still leaves the audit+ * trail intact — what ran, and that something was piped to it.+ */+ private fun JsonObject.redactedForAudit(): String =+ if (keys.none { it in REDACTED_AUDIT_KEYS }) {+ toString()+ } else {+ JsonObject(+ mapValues { (key, value) ->+ if (key in REDACTED_AUDIT_KEYS) JsonPrimitive("(redacted)") else value+ }+ ).toString()+ }+/*** Execute an uninstall that was already confirmed by the user.* Bypasses the destructive-action confirmation flow.diff --git a/app/src/test/java/com/gotcha/tools/ActionLogRedactionTest.kt b/app/src/test/java/com/gotcha/tools/ActionLogRedactionTest.ktnew file mode 100644index 0000000..6c783d2--- /dev/null+++ b/app/src/test/java/com/gotcha/tools/ActionLogRedactionTest.kt@@ -0,0 +1,75 @@+package com.gotcha.tools++import androidx.test.core.app.ApplicationProvider+import com.gotcha.testsupport.FakeAndroidKeyStore+import kotlinx.coroutines.test.runTest+import kotlinx.serialization.json.JsonObject+import kotlinx.serialization.json.JsonPrimitive+import org.junit.Assert.assertFalse+import org.junit.Assert.assertTrue+import org.junit.Before+import org.junit.Test+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import java.io.File++/**+ * The audit log records what every tool was asked to do, in plaintext in the app sandbox. That+ * is the point of it — but `run_termux_command`'s `stdin` exists so the model can answer a+ * prompt, and the prompts worth answering are passwords and tokens. Those must not be written+ * down, while the rest of the line stays intact so the audit trail still shows what ran.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class ActionLogRedactionTest {++ private val context = ApplicationProvider.getApplicationContext<android.content.Context>()+ private lateinit var executor: ToolExecutor++ private val logFile: File get() = File(context.filesDir, "action_log.txt")++ @Before+ fun setUp() {+ FakeAndroidKeyStore.setUp()+ if (logFile.exists()) logFile.delete()+ executor = ToolExecutor(context)+ }++ @Test+ fun `stdin is redacted from the audit log but the command survives`() = runTest {+ // Termux is absent here, so the tool fails early — the audit line is written either way,+ // which is exactly the path a secret would leak through.+ executor.execute(+ "run_termux_command",+ JsonObject(+ mapOf(+ "command" to JsonPrimitive("ssh user@host"),+ "stdin" to JsonPrimitive("hunter2-super-secret")+ )+ ),+ agent = AgentMode.OPERATOR,+ isSubAgent = true+ )++ val logged = logFile.readText()+ assertFalse("the secret must not reach disk: $logged", logged.contains("hunter2-super-secret"))+ assertTrue("the redaction should be visible, not silent", logged.contains("(redacted)"))+ assertTrue("the audit trail must still say what ran", logged.contains("ssh user@host"))+ assertTrue(logged.contains("run_termux_command"))+ }++ @Test+ fun `arguments without a redacted key are logged unchanged`() = runTest {+ executor.execute(+ "run_termux_command",+ JsonObject(mapOf("command" to JsonPrimitive("uname -a"))),+ agent = AgentMode.OPERATOR,+ isSubAgent = true+ )++ val logged = logFile.readText()+ assertTrue(logged.contains("uname -a"))+ assertFalse("nothing to redact here", logged.contains("(redacted)"))+ }+}