Skip to content

fix(remote): pair against the invitation the desktop is showing - #817

Merged
huichen merged 3 commits into
mainfrom
fix/pair-handshake-identity-fork
Sep 24, 2026
Merged

huichen merged 3 commits into
mainfrom
fix/pair-handshake-identity-fork

Conversation

@huichen

@huichen huichen commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

Problem

Scanning a valid, unexpired pairing QR from a current desktop failed with PA005 ("update both apps and scan again") — on both sides reporting nothing. Reproduced in a test, then bisected to #554.

establish() mints a fresh invitation (new desktop NKey, new secure identity, new PSK) whenever the pairing was never confirmed — an unscanned code, a NATS disconnect, suspend/resume. #554 made shared_runtime cache the bridge identity per pair_id, so a rebuilt generation kept serving the old identity while the GUI displayed the new QR. Every scan then failed secure_open with remote_secure_channel_invalid; the phone folded that into desktop_handshake_failed → PA005, and neither side wrote anything to a log. Before #554 every generation built HandshakeState from the credentials it had just resolved, so the QR and the served identity could not diverge.

Changes

  1. Serve the invitation the desktop is showing. The cached runtime is reused only while it still serves the invitation the current credentials describe, identified by the two public keys the phone authenticates against (desktopKey and secureKey). A credential refresh keeps both, so a rotated JWT still inherits its single-flight reply slots and pairing confirmation; a re-minted invitation inherits neither.
  2. Make a refused pairing say why. Transport::handshake now carries its reason (invitation_expired, invitation_already_used, peer_mismatch, open_undecryptable, …) after the byte-stable remote_secure_channel_invalid prefix; it reaches the desktop's stderr through the quota-limited HANDSHAKE_EPISODE and is returned to the phone. The signature-challenge path names the field that disagreed rather than replying a bare pairing_identity_mismatch.
  3. Stop blaming the app version. The phone discarded the desktop's error, and its desktop_handshake_failed wrapper made the screen match on the word "handshake" — so an unreachable desktop told users to update both apps. Timeouts now read as a network problem (NW001), a refused handshake as "generate a new code on the desktop" (PA002), and PA005 is left for a genuine protocol mismatch. The desktop's reason is appended to the stable pairing_signature_invalid token so the credential classifier and connection presentation keep matching on it.

Verification

  • cargo test --lib remote:: — 173 passed (desktop/src-tauri), including two new end-to-end tests: a reissued code for the same pair_id now pairs, with a control proving a new pair_id alone was never the trigger.
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings — clean.
  • Mobile: npx jest 125 suites / 1907 tests, tsc --noEmit, eslint — clean. New test asserts the three outcomes produce three different actions.

Docs: the PA002 support-code row now covers a refused handshake; PA005 is narrowed to the protocol mismatch it is reserved for.

A bridge generation rebuilt after the first invitation was minted (an
unscanned code, a NATS disconnect, suspend/resume) re-ran `establish()` and
minted a fresh invitation — new desktop NKey, new secure identity, new PSK —
for the same `pair_id`. `shared_runtime` reused the cached runtime for that
pair_id, and with it the cached `HandshakeState`, so the bridge kept offering
the *old* identity while the GUI showed the new QR. Every scan then failed
`secure_open` with `remote_secure_channel_invalid`; the phone folds that into
`desktop_handshake_failed` and shows PA005 ("update both apps"), and nothing on
either side logs why. Only stopping the bridge (which drops `bridge_shared`)
cleared it.

Reuse the cached runtime only while it still serves the invitation these
credentials describe, identified by the two public keys the phone
authenticates against (`desktopKey` and `secureKey`). A credential refresh
keeps both, so a rotated JWT still inherits its single-flight reply slots and
pairing confirmation; a re-minted invitation inherits neither, and the cached
identity is replaced rather than reused.

Regression: #554 cached the handshake state per `pair_id`; before it, every
generation built its `HandshakeState` from the credentials it had just
resolved, so the QR and the served identity could not diverge.
… app version

Two follow-ups to the identity fix, both about the same gap: a pairing that
cannot succeed was invisible and gave the wrong instruction.

Desktop side. `secure::Transport::handshake` collapsed every refusal into the
opaque `remote_secure_channel_invalid` and the command loop dropped it on the
floor, so an expired invitation, an already-consumed one, a peer proving the
wrong key, and an undecryptable open all looked identical — and when the phone
sent no reply subject, nothing was reported anywhere at all. A refusal now
carries its reason (`invitation_expired`, `invitation_already_used`,
`peer_mismatch`, `open_undecryptable`, ...) after the unchanged
`remote_secure_channel_invalid` prefix, reaches the desktop's stderr through
the quota-limited `HANDSHAKE_EPISODE`, and is returned to the phone. The
signature-challenge path names the field that disagreed instead of replying a
bare `pairing_identity_mismatch`; a QR describing an identity the bridge no
longer serves fails exactly there.

Mobile side. The phone discarded the desktop's error and its handshake wrapper
turned every outcome into `desktop_handshake_failed: ...`, which the screen
then matched on the word "handshake". So a desktop that was simply unreachable
told the user to update both apps. Timeouts now read as a network problem
(NW001) and a refused handshake as "generate a new code on the desktop"
(PA002); PA005 is left for what it actually means — the two builds cannot
complete the handshake (`pairing_confirmation_mismatch`, a confirmation
without `e2ee_v2`). The desktop's reason is appended to the stable
`pairing_signature_invalid` token, which the credential classifier and
connection presentation both match on, so it stays in the phone's console for
support without changing either classification.

Docs: the PA002 row now covers a refused handshake, and PA005 is narrowed to
the protocol mismatch it is reserved for.
@huichen
huichen enabled auto-merge (squash) September 24, 2026 05:10
@huichen
huichen merged commit 2da21fb into main Sep 24, 2026
12 checks passed
@huichen
huichen deleted the fix/pair-handshake-identity-fork branch September 24, 2026 05:19
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.

1 participant