Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
61e7178
fix(android-helper): defensive bounded session protocol over an admit…
thymikee Nov 19, 2026
811e7a1
fix(android): preflight helper framing and preserve SDK compatibility
thymikee Nov 19, 2026
4ee65e1
fix(android): buffer session reads and require authenticated readiness
thymikee Nov 19, 2026
55bc5ee
fix(android): separate helper request reads from response writes
thymikee Nov 19, 2026
00236cd
fix(android): preserve snapshot connection timeout and test session r…
thymikee Nov 19, 2026
f5f830b
fix(android-helper): separate command read and capture deadlines
thymikee Nov 19, 2026
1925572
fix(android-helper): remember unsupported session transports by helpe…
thymikee Nov 19, 2026
178a45b
fix(android-helper): diagnose verified unsupported builds and check t…
thymikee Nov 19, 2026
3efebd6
fix(android-helper): consolidate capture option type imports
thymikee Nov 19, 2026
799e636
test(android-helper): keep the real command byte cap in the session l…
thymikee Nov 19, 2026
834b0be
refactor(android-helper): remember verified unsupported builds in the…
thymikee Nov 19, 2026
dc2dd72
fix(android-helper): key the unsupported-build decision by helper build
thymikee Nov 19, 2026
c7b6e8f
test(android-helper): cover the five session transport defects before…
thymikee Nov 19, 2026
dbaba3d
fix(android-helper): close incomplete session reads and write each re…
thymikee Nov 19, 2026
6085510
fix(android): keep remembered installs out of the unsupported-build d…
thymikee Nov 19, 2026
ffc943d
docs(android-helper): note how LocalSocket reports a read timeout
thymikee Nov 19, 2026
f6c662f
fix(android): record the declined over-limit session frame
thymikee Nov 19, 2026
3ed1de1
test(android-helper): cover repeated unverified starts and a slow max…
thymikee Nov 19, 2026
dd49ec6
fix(android-helper): extend the read deadline per received byte and e…
thymikee Nov 19, 2026
b2ee5c5
fix(android): mark an unsupported session transport by helper build u…
thymikee Nov 19, 2026
ccf41b6
test(android): cover interleaved readiness output and an unverified u…
thymikee Nov 19, 2026
30632ac
fix(android-helper): bound each response by one deadline and keep enc…
thymikee Nov 19, 2026
e7e71cc
fix(android): classify session readiness from one complete stdout record
thymikee Nov 19, 2026
8953878
refactor(android-helper): share one capture step between one-shot and…
thymikee Nov 19, 2026
9821674
fix(android-helper): wait for send room only as long as the frame dea…
thymikee Nov 19, 2026
7e8be5e
fix(android): never mark a helper build unsupported for good
thymikee Nov 19, 2026
9d0c373
fix(android-helper): read commands nonblocking against the accept dea…
thymikee Nov 19, 2026
652c465
fix(android): require the launched session port in the readiness record
thymikee Nov 19, 2026
639d6f8
refactor(android-helper): activate sessions by versioned mode, not by…
thymikee Nov 19, 2026
d51238b
refactor(android-helper): drop the artifact capability gate and thin …
thymikee Nov 19, 2026
1a79bce
fix(android-helper): poll in short slices so a pause cannot overrun t…
thymikee Nov 20, 2026
8043156
fix(android-helper): prove a per-session secret on every connection
thymikee Nov 20, 2026
23ff71b
fix(android): send session commands only after the helper proves the …
thymikee Nov 20, 2026
68adbe9
fix(android): back off a session whose peer did not prove the secret
thymikee Nov 20, 2026
e15bc9d
fix(android): call a peer unverified only after it answered without a…
thymikee Nov 20, 2026
8eaec9b
fix(android): accept the session hello only as one nonce and one proof
thymikee Nov 20, 2026
2496202
fix(android): keep at most one hello line from an unverified peer
thymikee Nov 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions android/snapshot-helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,27 @@ The `gesture` payload is a base64-encoded JSON object using protocol `android-to
## Persistent Session

Passing `-e sessionPort <port>` keeps the instrumentation alive after startup and serves repeated
commands over a local TCP server on `127.0.0.1:<port>`, instead of exiting after one snapshot. This
avoids paying UiAutomation connect/teardown cost per call. Each command uses one short-lived
connection: the client connects, sends a single command line, reads the response, and the server
closes that connection; the process stays alive for the next connection:
commands over an abstract `localabstract:` UNIX socket named `agentdevice-snapshot-helper-<port>`,
reached from the host through `adb forward tcp:<port> localabstract:agentdevice-snapshot-helper-<port>`,
instead of exiting after one snapshot. This avoids paying UiAutomation connect/teardown cost per
call. Each command uses one short-lived connection: the client connects, sends a single command
line, reads the response, and the server closes that connection; the process stays alive for the
next connection.

Each accepted connection is admitted before any command byte is read: the helper reads the
forwarded peer's kernel credentials with `LocalSocket.getPeerCredentials()` and serves only adbd —
shell uid 2000, or root uid 0 when adb runs in its supported root mode. Both are the adb operator
trust boundary; any other uid (or an unreadable credential) is closed without a response. The socket
name derives from the port and carries no secret, and no token travels in argv.

The command line of each connection is bounded twice. Its read runs under an absolute per-connection
deadline fixed at accept time, owned by the session's own `timeoutMs` (fallback 8000 ms), so a
silent or drip-feeding peer cannot hold the command reader past that budget; and the line itself is
capped at 256 KiB, a bound derived from the largest supported producer of this channel (a 10 s
two-contact gesture carries 1,252 densified samples, at most ~130 KB base64; the documented 64 KiB
`fill --text-stdin` contract encodes to ~88 KB — the cap keeps at least 2x headroom over both). A
connection that hits either read bound receives a session error and closes. A response write failure
closes that connection. The session keeps serving the next connection:

- `snapshot <requestId>` — capture and return an XML snapshot, same semantics as the default mode
- `snapshot-immediate <requestId>` — capture fresh XML without an idle wait when the caller owns settling
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.callstack.agentdevice.snapshothelper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;

/**
* Pure admission and bound decisions for persistent-session connections. Every decision is a
* function of its inputs only, so the test suite drives it with synthetic peers: no socket, no
* clock, no Android runtime.
*
* <p>The session transport is an abstract {@code localabstract:} socket reached through
* {@code adb forward tcp:<host port> localabstract:<name>}. The forwarded peer of such a
* connection is adbd, which runs as root when adb is rooted and as shell otherwise; both uids are
* the adb operator trust boundary, and the forwarded uid is not the original host user's identity.
* Any other uid is an untrusted co-installed peer and is rejected before any command byte is read.
*/
final class SessionPeerPolicy {
/** adbd with {@code adb root} (a supported adb mode); the root-mode adb operator. */
static final int PEER_UID_ROOT = 0;
/** adbd in its default shell configuration. */
static final int PEER_UID_SHELL = 2000;

/**
* Prefix of the abstract socket name, shared with the host's forward spec. The name is derived
* from the session port and carries no secret: admission is the peer uid, not name secrecy.
*/
static final String SESSION_SOCKET_PREFIX = "agentdevice-snapshot-helper-";

/**
* The per-connection command-line byte cap. Derived from the largest supported producer of this
* channel, measured with the repository's own plan builders: a 10 s two-contact gesture carries
* 1,252 densified samples (2 pointers x 626 samples on the 16 ms grid), whose base64 command line
* is at most ~130 KB, and the documented 64 KiB {@code fill --text-stdin} contract encodes to
* ~88 KB. 256 KiB bounds both with at least 2x headroom. The IME helper's 64,000-base64-char
* cap belongs to its separate am/broadcast channel and is deliberately not reused here.
*/
static final int MAX_COMMAND_LINE_BYTES = 256 * 1024;

enum Admission {
ADMITTED,
REJECTED_UNAUTHORIZED_PEER
}

enum LineDecision {
ACCEPTED,
REJECTED_EMPTY,
REJECTED_OVER_LIMIT
}

private SessionPeerPolicy() {}

static Admission admitPeer(int uid) {
return uid == PEER_UID_ROOT || uid == PEER_UID_SHELL
? Admission.ADMITTED
: Admission.REJECTED_UNAUTHORIZED_PEER;
}

/**
* The abstract socket name for a session port. Deterministic and secret-free, so the host and
* the helper derive the same name from the same port without passing anything sensitive in argv.
*/
static String sessionSocketName(int sessionPort) {
return SESSION_SOCKET_PREFIX + sessionPort;
}

/**
* The per-connection command read budget, owned by the session's own configured timeout: a host
* sends its command immediately after connecting, so a peer silent past that budget is rejected
* instead of occupying the command reader. Zero or negative falls back to the caller's default
* timeout, never to an unbounded read.
*/
static long commandReadDeadlineMs(long sessionTimeoutMs, long fallbackTimeoutMs) {
long budgetMs = sessionTimeoutMs > 0 ? sessionTimeoutMs : Math.max(0, fallbackTimeoutMs);
return Math.min(Integer.MAX_VALUE, budgetMs);
}

@FunctionalInterface
interface ReadTimeout {
void setMillis(int timeoutMs) throws IOException;
}

@FunctionalInterface
interface MonotonicClock {
long nowMs();
}

/** Reads one command using the deadline fixed at accept and at most maxBytes + 1 bytes. */
static String readSessionCommandLine(
InputStream input, ReadTimeout timeout, MonotonicClock clock, long deadlineAtMs, int maxBytes)
throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
byte[] chunk = new byte[8 * 1024];
while (bytes.size() <= maxBytes) {
long remainingMs = deadlineAtMs - clock.nowMs();
if (remainingMs <= 0) {
throw new SocketTimeoutException("session command read deadline exceeded");
}
timeout.setMillis((int) remainingMs);
int count = input.read(chunk, 0, Math.min(chunk.length, maxBytes + 1 - bytes.size()));
if (clock.nowMs() >= deadlineAtMs) {
throw new SocketTimeoutException("session command read deadline exceeded");
}
if (count < 0) {
if (bytes.size() == 0) return null;
break;
}
int end = 0;
while (end < count && chunk[end] != '\n') end++;
bytes.write(chunk, 0, end);
if (end < count) break;
}
return new String(bytes.toByteArray(), StandardCharsets.UTF_8);
}

static LineDecision commandLineDecision(String line, int maxLineBytes) {
if (line == null || line.isEmpty()) {
return LineDecision.REJECTED_EMPTY;
}
return line.getBytes(StandardCharsets.UTF_8).length > maxLineBytes
? LineDecision.REJECTED_OVER_LIMIT
: LineDecision.ACCEPTED;
}
}
Loading
Loading