Repository navigation
fix: quote Android input text for the device shell - #71
Draft
cryptotavares wants to merge 2 commits into
Draft
cryptotavares wants to merge 2 commits into
cryptotavares wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of the mm CLI v2 effort (audit item K7).
Problem
adb shelljoins its argv into one command line for the device shell. It does not escape the arguments (AOSPadb_shell: "We don't escape here, just like ssh(1)").typeTextescaped only spaces (%s) and&|;<>, so the remaining metacharacters reached the shell.The Android unlock types the real wallet password through this method.
Solution
quoteForDeviceShellsingle-quotes the text and rewrites each'as'\''.input textconverts%sto a space, so a literal%sis split over two calls (the first ends with%, the next starts withs). Spaces stay literal. The logcat filter and the bundle IDs passed topm list packages,monkey -pandam force-stopuse 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 newYellow boxes are new.
What changes
Read back from the Settings search box on
emulator-5556(Android 16), aftertypeTextthrough the built backend.a'b$c!d ea'b$c!d%se; exit 1,no closing quote'a'\''b$c!d e'a'b$c!d ex"y\`zx"y\`z; exit 1,no closing quote'x"y\`z'x"y\`z50% (ok)?50%%s(ok)?; exit 1,syntax error: unexpected '(''50% (ok)?'50% (ok)?50%s (ok)?50%s%s(ok)?; exit 1 on('50%', then's (ok)?'50%s (ok)?*#~*#~; already exact'*#~'*#~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
%sinput textas one argument and types a space. Live read-back confirms it.%sinto two callsinput textconverts%sto a space even inside quotes. Two calls keep the characters..*|Settingsis a shell pipeline.input textinjects per character, and shell protocol v2 does not have the legacy service-string cap.Review guide
src/backends/adb-backend.tsquoteForDeviceShell, thetypeTextloop, and the four other call sites.src/backends/adb-backend.test.tsit.eachtable: input → exactinput textargv.Testing
yarn build,yarn lintandyarn testpass (38 files, 1002 passed, 6 skipped).emulator-5556(AVDMedium_Phone_API_36.1), Android Settings search box. 14/14 strings read back exactly withuiautomator dump.a'b$c!d eandx"y\`zexits 1 withno closing quote;50% (ok)?exits 1 withsyntax error: unexpected '('.%ssplit; the50%s (ok)?row fails becauseinput textwould type a space.