Skip to content

fix: quote Android input text for the device shell - #71

Draft
cryptotavares wants to merge 2 commits into
mainfrom
cryptotavares/android-input-text-quoting
Draft

cryptotavares wants to merge 2 commits into
mainfrom
cryptotavares/android-input-text-quoting

Conversation

@cryptotavares

Copy link
Copy Markdown
Contributor

Part of the mm CLI v2 effort (audit item K7).

Summary: AdbBackend.typeText now quotes text for the device shell that adb shell runs. A password with ', ", $, a backtick, !, \ or a glob is typed exactly. Before, the shell expanded it or the command failed.

Problem

adb shell joins its argv into one command line for the device shell. It does not escape the arguments (AOSP adb_shell: "We don't escape here, just like ssh(1)"). typeText escaped only spaces (%s) and &|;<>, so the remaining metacharacters reached the shell.

input:    a'b$c!d e
old argv: adb shell input text a'b$c!d%se      -> /system/bin/sh: no closing quote, exit 1
new argv: adb shell input text 'a'\''b$c!d e'  -> field: a'b$c!d e

The Android unlock types the real wallet password through this method.

Solution

quoteForDeviceShell single-quotes the text and rewrites each ' as '\''. input text converts %s to a space, so a literal %s is split over two calls (the first ends with %, the next starts with s). Spaces stay literal. The logcat filter and the bundle IDs passed to pm list packages, monkey -p and am force-stop use the same quoting.

flowchart TD
    A["typeText(text)"] --> B["Single-quote the text; rewrite each '"]
    B --> C{"contains literal %s?"}
    C -->|yes| D["first call ends with %, next starts with s"]
    C -->|no| E["one input text call"]
    D --> F["adb -s serial shell input text"]
    E --> F
    F --> G["device shell passes one argument to input text"]
    G --> H["field holds the exact text"]

    classDef new fill:#fff3cd,stroke:#d39e00
    class B,C,D new
Loading

Yellow boxes are new.

What changes

Read back from the Settings search box on emulator-5556 (Android 16), after typeText through the built backend.

Input Before After Read back
a'b$c!d e argv a'b$c!d%se; exit 1, no closing quote argv 'a'\''b$c!d e' a'b$c!d e
x"y\`z argv x"y\`z; exit 1, no closing quote argv 'x"y\`z' x"y\`z
50% (ok)? argv 50%%s(ok)?; exit 1, syntax error: unexpected '(' argv '50% (ok)?' 50% (ok)?
50%s (ok)? argv 50%s%s(ok)?; exit 1 on ( argv '50%', then 's (ok)?' 50%s (ok)?
*#~ argv *#~; already exact argv '*#~' *#~

All 14 live cases matched exactly, including $HOME, a\b*c?, all shell punctuation and a 151-character string with four literal %s.

Decisions for reviewers

Decision Reason
Keep literal spaces instead of rewriting them to %s A quoted space reaches input text as one argument and types a space. Live read-back confirms it.
Split a literal %s into two calls input text converts %s to a space even inside quotes. Two calls keep the characters.
Quote bundle IDs and the logcat filter too Both are tool input on the same device-shell path. An unquoted filter .*|Settings is a shell pipeline.
No length chunking A 151-character string typed exactly. input text injects per character, and shell protocol v2 does not have the legacy service-string cap.

Review guide

# File What to check
1 src/backends/adb-backend.ts quoteForDeviceShell, the typeText loop, and the four other call sites.
2 src/backends/adb-backend.test.ts The it.each table: input → exact input text argv.
Testing
  • yarn build, yarn lint and yarn test pass (38 files, 1002 passed, 6 skipped).
  • Live: emulator-5556 (AVD Medium_Phone_API_36.1), Android Settings search box. 14/14 strings read back exactly with uiautomator dump.
  • Before/after on the same device: the old argv for a'b$c!d e and x"y\`z exits 1 with no closing quote; 50% (ok)? exits 1 with syntax error: unexpected '('.
  • Mutation check: remove the quoting; the table fails. Remove the %s split; the 50%s (ok)? row fails because input text would type a space.

adb shell joins argv into one device-shell command line without escaping, so typeText sent quotes, $ , backticks, !, \ and globs to the shell unquoted. Single-quote the text (POSIX '\'' escaping), and split a literal %s into two input text calls because input text reads %s as a space. Quote the logcat filter and bundle IDs on the same path.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant