Skip to content

Commit b6a93ca

Browse files
committed
Use quotes when using 'su -c ...' on Android
1 parent 0a649e4 commit b6a93ca

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/interceptors/android/adb-commands.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,20 @@ export async function pushFile(
144144
}
145145

146146
const runAsRootCommands = [
147-
['su', 'root'], // Used on official emulators
148-
['su', '-c'] // Normal root
147+
// 'su' as available on official emulators:
148+
(...cmd: string[]) => ['su', 'root', ...cmd],
149+
// Su on many physical rooted devices requires quotes:
150+
(...cmd: string[]) => ['su', '-c', `'${cmd.join(' ')}'`]
149151
];
150152

151-
export async function getRootCommand(adbClient: Adb.DeviceClient): Promise<string[] | undefined> {
153+
type RootCmd = (...cmd: string[]) => string[];
154+
155+
export async function getRootCommand(adbClient: Adb.DeviceClient): Promise<RootCmd | undefined> {
152156
// Run whoami with each of the possible root commands
153157
const rootCheckResults = await Promise.all(
154-
runAsRootCommands.map((cmd) =>
155-
run(adbClient, cmd.concat('whoami'), { timeout: 1000 }).catch(console.log)
156-
.then((whoami) => ({ cmd, whoami }))
158+
runAsRootCommands.map((runAsRoot) =>
159+
run(adbClient, runAsRoot('whoami'), { timeout: 1000 }).catch(console.log)
160+
.then((whoami) => ({ cmd: runAsRoot, whoami }))
157161
)
158162
)
159163

@@ -181,7 +185,7 @@ export async function getRootCommand(adbClient: Adb.DeviceClient): Promise<strin
181185
}).catch(console.log);
182186

183187
return (whoami || '').trim() === 'root'
184-
? [] // all commands now run as root, so no prefix required.
188+
? (...cmd: string[]) => cmd // All commands now run as root
185189
: undefined; // Still not root, no luck.
186190
}
187191

@@ -217,7 +221,7 @@ export async function hasCertInstalled(
217221

218222
export async function injectSystemCertificate(
219223
adbClient: Adb.DeviceClient,
220-
rootCmd: string[],
224+
runAsRoot: RootCmd,
221225
certificatePath: string
222226
) {
223227
const injectionScriptPath = `${ANDROID_TEMP}/htk-inject-system-cert.sh`;
@@ -265,13 +269,13 @@ export async function injectSystemCertificate(
265269
);
266270

267271
// Actually run the script that we just pushed above, as root
268-
const scriptOutput = await run(adbClient, rootCmd.concat('sh', injectionScriptPath));
272+
const scriptOutput = await run(adbClient, runAsRoot('sh', injectionScriptPath));
269273
console.log(scriptOutput);
270274
}
271275

272276
export async function setChromeFlags(
273277
adbClient: Adb.DeviceClient,
274-
rootCmd: string[],
278+
runAsRoot: RootCmd,
275279
flags: string[]
276280
) {
277281
const flagsFileContent = `chrome ${flags.join(' ')}`;
@@ -313,11 +317,11 @@ export async function setChromeFlags(
313317
);
314318

315319
// Actually run the script that we just pushed above, as root
316-
const scriptOutput = await run(adbClient, rootCmd.concat('sh', chromeFlagsScriptPath));
320+
const scriptOutput = await run(adbClient, runAsRoot('sh', chromeFlagsScriptPath));
317321
console.log(scriptOutput);
318322

319323
// Try to restart chrome, now that the flags have probably been changed:
320-
await run(adbClient, rootCmd.concat('am', 'force-stop', 'com.android.chrome')).catch(() => {});
324+
await run(adbClient, runAsRoot('am', 'force-stop', 'com.android.chrome')).catch(() => {});
321325
}
322326

323327
export async function bringToFront(

0 commit comments

Comments
 (0)