From d706901a17874b51fff2ff497987c94f20ebef8a Mon Sep 17 00:00:00 2001 From: cryptotavares Date: Fri, 4 Dec 2026 19:35:45 +0000 Subject: [PATCH 1/2] fix: quote Android input text for the device shell 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. --- src/backends/adb-backend.test.ts | 27 ++++++++++++++++++++--- src/backends/adb-backend.ts | 38 ++++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/backends/adb-backend.test.ts b/src/backends/adb-backend.test.ts index 2291152..82cf29f 100644 --- a/src/backends/adb-backend.test.ts +++ b/src/backends/adb-backend.test.ts @@ -914,10 +914,10 @@ describe('AdbBackend.typeText', () => { await backend.typeText('abc'); - expect(inputCalls().at(-1)).toStrictEqual(['input', 'text', 'abc']); + expect(inputCalls().at(-1)).toStrictEqual(['input', 'text', "'abc'"]); }); - it('clears the field, then types the escaped text', async () => { + it('clears the field, then types the quoted text', async () => { await backend.typeText('a b&c'); expect(inputCalls()).toStrictEqual([ @@ -930,10 +930,31 @@ describe('AdbBackend.typeText', () => { 'KEYCODE_MOVE_END', ], ['input', 'keyevent', '67'], - ['input', 'text', 'a%sb\\&c'], + ['input', 'text', "'a b&c'"], ]); }); + it.each([ + ['abc', ["'abc'"]], + ['a b', ["'a b'"]], + ["a'b", ["'a'\\''b'"]], + ['a"b', [`'a"b'`]], + ['$HOME', [`'$HOME'`]], + ['`id`', ["'`id`'"]], + ['!', ["'!'"]], + ['\\', ["'\\'"]], + ['*', ["'*'"]], + ['%', ["'%'"]], + ['50%s (ok)?', [`'50%'`, `'s (ok)?'`]], + ["a'b$c!d e", ["'a'\\''b$c!d e'"]], + ])('quotes %j for the device shell', async (text, expected) => { + await backend.typeText(text); + + expect(inputCalls().slice(3)).toStrictEqual( + expected.map((argument) => ['input', 'text', argument]), + ); + }); + it('only clears the field for an empty string', async () => { await backend.typeText(''); diff --git a/src/backends/adb-backend.ts b/src/backends/adb-backend.ts index 68393e8..3b77639 100644 --- a/src/backends/adb-backend.ts +++ b/src/backends/adb-backend.ts @@ -229,6 +229,19 @@ function describeDumpRemediation(failures: string): string { ); } +/** + * Quote text for the device shell that `adb shell` runs its argv in. + * + * `adb shell` joins its arguments into one command line without escaping + * them, so text carrying spaces or shell metacharacters must be quoted. + * + * @param text - The text to quote. + * @returns The text in single quotes, with each `'` escaped. + */ +function quoteForDeviceShell(text: string): string { + return `'${text.replace(/'/gu, `'\\''`)}'`; +} + export class AdbBackend implements DeviceBackend { readonly kind = 'adb' as const; @@ -397,9 +410,15 @@ export class AdbBackend implements DeviceBackend { if (text.length === 0) { return; } - // Android input text requires escaping spaces and special chars - const escaped = text.replace(/ /gu, '%s').replace(/[&|;<>]/gu, '\\$&'); - await this.#adb(['shell', 'input', 'text', escaped]); + // `input text` reads `%s` as a space, so a literal `%s` is typed in two + // calls; everything else is quoted for the device shell. + const pieces = text.split('%s'); + for (const [index, piece] of pieces.entries()) { + const quoted = quoteForDeviceShell( + `${index > 0 ? 's' : ''}${piece}${index < pieces.length - 1 ? '%' : ''}`, + ); + await this.#adb(['shell', 'input', 'text', quoted]); + } } async swipe( @@ -459,7 +478,7 @@ export class AdbBackend implements DeviceBackend { 'pm', 'list', 'packages', - bundleId, + quoteForDeviceShell(bundleId), ]); if (pmResult.includes(bundleId)) { return { bundleId, state: 'Installed (not running)' }; @@ -841,7 +860,7 @@ export class AdbBackend implements DeviceBackend { 'shell', 'monkey', '-p', - bundleId, + quoteForDeviceShell(bundleId), '-c', 'android.intent.category.LAUNCHER', '1', @@ -849,7 +868,12 @@ export class AdbBackend implements DeviceBackend { } async closeApp(bundleId: string): Promise { - await this.#adb(['shell', 'am', 'force-stop', bundleId]); + await this.#adb([ + 'shell', + 'am', + 'force-stop', + quoteForDeviceShell(bundleId), + ]); } async pressButton(button: DeviceButton): Promise { @@ -961,7 +985,7 @@ export class AdbBackend implements DeviceBackend { async getLogs(durationSeconds = 30, filter?: string): Promise { const args = ['shell', 'logcat', '-d', '-t', String(durationSeconds)]; if (filter) { - args.push('-e', filter); + args.push('-e', quoteForDeviceShell(filter)); } const raw = await this.#adb(args); const entries = raw From 3604ffd42b57c8b5e098629f25c48e2ab7a6c6e7 Mon Sep 17 00:00:00 2001 From: cryptotavares Date: Fri, 4 Dec 2026 19:38:10 +0000 Subject: [PATCH 2/2] refactor: shorten quoteForDeviceShell docblock --- src/backends/adb-backend.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/backends/adb-backend.ts b/src/backends/adb-backend.ts index 3b77639..33e11c1 100644 --- a/src/backends/adb-backend.ts +++ b/src/backends/adb-backend.ts @@ -230,13 +230,10 @@ function describeDumpRemediation(failures: string): string { } /** - * Quote text for the device shell that `adb shell` runs its argv in. - * - * `adb shell` joins its arguments into one command line without escaping - * them, so text carrying spaces or shell metacharacters must be quoted. + * Single-quotes text for the device shell: `adb shell` joins its argv unescaped. * * @param text - The text to quote. - * @returns The text in single quotes, with each `'` escaped. + * @returns The quoted text. */ function quoteForDeviceShell(text: string): string { return `'${text.replace(/'/gu, `'\\''`)}'`;