Skip to content

Commit 09a2f49

Browse files
committed
Add a timeout to ADB root detection commands
It turns out that on some devices "su root whoami" does not fail or run whoami - it just successfully starts a shell. In these cases, we have a problem, as that blocks the rest of the root detection. To fix this, we now time out in any such cases. It would be a bit better to properly get root using the interactive shell, but the practicality is a bit complicated, so let's ignore that for now until a real device that needs that comes up.
1 parent 79a8637 commit 09a2f49

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/interceptors/android/adb-commands.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,28 @@ export function stringAsStream(input: string) {
8585
return contentStream;
8686
}
8787

88-
async function run(adbClient: adb.AdbClient, deviceId: string, command: string[]): Promise<string> {
89-
return adbClient.shell(deviceId, command)
90-
.then(adb.util.readAll)
91-
.then(buffer => buffer.toString('utf8'));
88+
async function run(
89+
adbClient: adb.AdbClient,
90+
deviceId: string,
91+
command: string[],
92+
options: {
93+
timeout?: number
94+
} = {
95+
timeout: 10000
96+
}
97+
): Promise<string> {
98+
return Promise.race([
99+
adbClient.shell(deviceId, command)
100+
.then(adb.util.readAll)
101+
.then(buffer => buffer.toString('utf8')),
102+
...(options.timeout
103+
? [
104+
delay(options.timeout)
105+
.then(() => { throw new Error(`Timeout for ADB command ${command}`) })
106+
]
107+
: []
108+
)
109+
]);
92110
}
93111

94112
export async function pushFile(
@@ -115,7 +133,7 @@ export async function getRootCommand(adbClient: adb.AdbClient, deviceId: string)
115133
// Run whoami with each of the possible root commands
116134
const rootCheckResults = await Promise.all(
117135
runAsRootCommands.map((cmd) =>
118-
run(adbClient, deviceId, cmd.concat('whoami')).catch(console.log)
136+
run(adbClient, deviceId, cmd.concat('whoami'), { timeout: 1000 }).catch(console.log)
119137
.then((whoami) => ({ cmd, whoami }))
120138
)
121139
)

0 commit comments

Comments
 (0)