Skip to content

Commit 9ef306b

Browse files
committed
Try to fall back to 'adb root' if su doesn't exist
This notably works with LineageOS if you haven't rooted the device, but you have enabled 'ADB only' root access in the developer options. Conveniently, it seems that that also avoids tripping SafetyNet too.
1 parent 34a9539 commit 9ef306b

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/interceptors/android/adb-commands.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export async function getRootCommand(adbClient: adb.AdbClient, deviceId: string)
6969
// Run whoami with each of the possible root commands
7070
const rootCheckResults = await Promise.all(
7171
runAsRootCommands.map((cmd) =>
72-
run(adbClient, deviceId, cmd.concat('whoami')).catch(() => {})
72+
run(adbClient, deviceId, cmd.concat('whoami')).catch(console.log)
7373
.then((whoami) => ({ cmd, whoami }))
7474
)
7575
)
@@ -79,7 +79,17 @@ export async function getRootCommand(adbClient: adb.AdbClient, deviceId: string)
7979
.filter((result) => (result.whoami || '').trim() === 'root')
8080
.map((result) => result.cmd);
8181

82-
return validRootCommands[0];
82+
if (validRootCommands.length >= 1) return validRootCommands[0];
83+
84+
// If no explicit root commands are available, try to restart adb in root
85+
// mode instead. If this works, *all* commands will run as root.
86+
// We prefer explicit "su" calls if possible, to limit access & side effects.
87+
await adbClient.root(deviceId).catch(() => console.log);
88+
const whoami = await run(adbClient, deviceId, ['whoami']).catch(console.log);
89+
90+
return (whoami || '').trim() === 'root'
91+
? [] // all commands now run as root, so no prefix required.
92+
: undefined; // Still not root, no luck.
8393
}
8494

8595
export async function hasCertInstalled(

0 commit comments

Comments
 (0)