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..33e11c1 100644 --- a/src/backends/adb-backend.ts +++ b/src/backends/adb-backend.ts @@ -229,6 +229,16 @@ function describeDumpRemediation(failures: string): string { ); } +/** + * Single-quotes text for the device shell: `adb shell` joins its argv unescaped. + * + * @param text - The text to quote. + * @returns The quoted text. + */ +function quoteForDeviceShell(text: string): string { + return `'${text.replace(/'/gu, `'\\''`)}'`; +} + export class AdbBackend implements DeviceBackend { readonly kind = 'adb' as const; @@ -397,9 +407,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 +475,7 @@ export class AdbBackend implements DeviceBackend { 'pm', 'list', 'packages', - bundleId, + quoteForDeviceShell(bundleId), ]); if (pmResult.includes(bundleId)) { return { bundleId, state: 'Installed (not running)' }; @@ -841,7 +857,7 @@ export class AdbBackend implements DeviceBackend { 'shell', 'monkey', '-p', - bundleId, + quoteForDeviceShell(bundleId), '-c', 'android.intent.category.LAUNCHER', '1', @@ -849,7 +865,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 +982,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