Skip to content

Don't crash the TcpUsb bridge on a signature no known key verifies - #554

Merged
koral-- merged 1 commit into
DeviceFarmer:masterfrom
jozenstar:tcpusb-auth-verify
Oct 13, 2026
Merged

koral-- merged 1 commit into
DeviceFarmer:masterfrom
jozenstar:tcpusb-auth-verify

Conversation

@jozenstar

Copy link
Copy Markdown

Since #540, the TcpUsb bridge checks an AUTH_SIGNATURE against each of knownPublicKeys. node-forge's verify() throws instead of returning false when it can't verify a signature: Encryption block is invalid. for a signature made by another key, Encrypted message length is invalid. for one of the wrong length. So a client whose signature isn't from the first known key gets an error instead of a new AUTH_TOKEN challenge. The socket emits it as error, and the server re-emits it.

In STF this restarts the device: the connect plugin shares the bridge with lifecycle.share(), which treats any error as fatal. STF passes all of the device owner's adb keys as knownPublicKeys, so anyone with two adb keys registered (for example one per OS on a dual-boot or WSL machine) crashes the device they connect to. The user sees "Someone stole your device", and the remote debug port changes.

Change

  • verifies(): a small helper wraps key.verify() and returns false (with a debug line) when it throws. It is used in the known-keys loop and in the AUTH_RSAPUBLICKEY check.
  • Missing signature: an AUTH_SIGNATURE packet without data is rejected with an AuthError, which closes only that connection. Before, this.signature.toString() threw a TypeError, which became an error event the same way.

Behaviour change: a client that sends an AUTH_SIGNATURE with no data is now disconnected instead of challenged again. adb never sends one.

Tests

test/adb/tcpusb/socket.ts, new known public keys block (4 tests, all fail without the change):

  • a signature that no known key made → challenged again, no error;
  • a signature made by the second known key → accepted;
  • a missing signature → connection closed, no error;
  • a public key that didn't make the signature → connection closed, no error.
npx mocha -r ts-node/register test/adb/tcpusb/socket.ts   # 15 passing

Note: npm test's unquoted test/**/*.ts glob doesn't reach test/adb/tcpusb/, so these tests (and the earlier tcpusb ones) only run when named. Quoting the glob would fix that; I've left it for a separate change.

Found on an STF 3.8.0 provider: Remote ADB had an error Error: Encryption block is invalid. followed by Shutting down due to fatal error, reproduced with node-forge 1.4.0 from the devicefarmer/stf:3.8.0 image.

node-forge's verify() throws instead of returning false when it can't verify a
signature: one made by another key ('Encryption block is invalid'), or one with
the wrong length or no bytes ('Encrypted message length is invalid'). The
known-public-keys loop (DeviceFarmer#540) calls it for every key, so a client whose
signature isn't from the first known key gets an error instead of a new
challenge. The socket emits that error and the server re-emits it; STF treats
any error from the bridge as fatal and restarts the device. STF passes all of
the device owner's adb keys, so anyone with two keys hits it.

Treat a throw as a mismatch, in the loop and in the RSAPUBLICKEY check, and
reject a missing signature as an AuthError, which only closes the connection.

Signed-off-by: Arthur Yazhevich <[email protected]>
@koral--
koral-- merged commit de55d67 into DeviceFarmer:master Oct 13, 2026
2 checks passed
@jozenstar
jozenstar deleted the tcpusb-auth-verify branch October 14, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants