Skip to content

refactor(remote): one paired E2EE handshake core for desktop, CLI, web and mobile clients - #27523

Merged
OrcaWin merged 3 commits into
mainfrom
np/d2b-handshake-core
Oct 11, 2026
Merged

OrcaWin merged 3 commits into
mainfrom
np/d2b-handshake-core

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 23 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​691 $\color{#cf222e}{\Huge{\mathbf{−}}}$​87 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​604
Prod 46 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1061 $\color{#cf222e}{\Huge{\mathbf{−}}}$​628 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​433

ELI5

Every Orca app that talks to a paired host (the desktop app, the orca command line, the browser client and the phone) opens an encrypted connection with the same three steps: say hello, check the host's answer, then prove which device it is. Each app had its own copy of those steps, six copies in all. This PR replaces the copies with one shared piece of code. Nothing the user sees changes, and the bytes on the wire stay exactly the same.

What Changed

  • Before: the desktop/CLI request socket, the cached request connection, the subscription socket, the shared control connection, the relay passthrough socket, the web client and the phone's direct socket each built the hello, checked the ready, wrote the auth frame and encrypted frames on their own. The newer (v2) handshake existed only in the phone.
  • After: all of them call src/shared/paired-e2ee-client-handshake.ts, which owns:
    • the hello for the older (legacy) and newer (v2) framing,
    • the check of the host's ready, including the per-host "this host already spoke v2, never fall back" rule,
    • sealing the auth frame and reading the host's answer,
    • one channel object (sealText / openText / sealBinary / openBinary) that callers hold instead of a raw shared key.
  • Each platform keeps its own encryption and base64 helpers and passes them in (Node, browser, phone), so size limits and phone-specific quirks are unchanged.
  • The phone's v2 session moves to src/shared. The v2 key schedule is passed in like the other primitives: desktop and CLI use the existing node:crypto schedule (moved from src/main to src/shared/mobile-e2ee-v2-node-key-schedule.ts so the CLI can reach it), while the browser and phone use a pure-JS schedule on @noble/[email protected] (the version the phone already pins). The browser one works on plain http://<lan-ip>, where WebCrypto is missing. @noble/hashes is a dev dependency, bundled into the web client only. The packaged CLI never loads it.
  • The phone's "v2 seen" latch rule moves to src/shared/paired-e2ee-framing-policy.ts; the phone still stores the latch in its own storage.
  • The auth step is now async. Callers re-check that the socket is still the current one before sending the auth frame.

Desktop, CLI and web still connect with the legacy framing and the same auth fields. Switching them to v2 is later work.

Why

The next steps of the pairing work add per-device keys for the browser and then the desktop and CLI. Those keys sign the v2 handshake, and a browser's key can only sign asynchronously. Adding that to six hand-written handshakes would mean six chances to get the security check wrong. One core keeps the check in one place and lets later changes stay small. Each platform injects its own primitives instead of the core importing them, because the browser has no Buffer and the phone needs its own typed-array handling.

The alternative was to switch every client to v2 in the same PR. That would change the wire and mix a refactor with a protocol change, so it is left for later.

Linked Issue

Part of #17394 (prepares for device-key auth). No user-visible fix on its own.

Visual Proof

N/A: no UI or behaviour change. The wire bytes are pinned by fixture tests.

Testing

  • src/shared/paired-e2ee-client-handshake.test.ts: the core writes the captured legacy hello, auth and binary frames byte for byte (mobile-e2ee-legacy-fixtures.ts). It writes the normative v2 hello and derives the vector transcript (mobile-e2ee-v2-fixtures.ts). It refuses a legacy hello, ready or auth once the host is latched to v2. It rejects a v2 "authenticated" reply that does not echo the transcript.

  • src/shared/mobile-e2ee-v2-node-key-schedule.test.ts: the pure-JS key schedule matches the node:crypto one across fresh nonces.

  • Cross-version (tests/e2e/cross-version-wire/cross-version-paired-e2ee-handshake-core.unit.test.ts, run against the latest release tag):

    • the core writes the released client's captured frames;
    • the core authenticates and round-trips a request against the released host, for legacy framing and for direct v2;
    • the core does the same against this host;
    • the released client's hello and auth still authenticate against this host.
  • The existing transport suites (request, subscription, shared control, passthrough, web, phone direct and relay) pass. Tests that injected a raw shared key now inject a fixed-key handshake.

  • pnpm tc, mobile tsc, oxlint, pnpm run check:code-quality:changed, pnpm lint.

  • Automated tests added/updated

Review

P0/P1 review by the author, plus an independent adversarial review.

Agent skill upstream boundary

  • Not applicable.

Notes

  • SSH and remote: this changes client handshake code only. Hosts are untouched, and old/new clients and hosts interoperate in both directions (cross-version test above).
  • Mobile: the phone imports @noble/hashes from src/shared, the same way it already imports tweetnacl there. mobile/tsconfig.json maps it to the phone's own copy, as it does for tweetnacl.
  • Windows/Linux/macOS: no platform-specific code.
  • The desktop's subscription socket still encrypts frames when they are queued, not when they are sent. That is fine for legacy framing. A v2 runtime client must seal at send time (as the phone does), which is noted for the later desktop/CLI migration.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • N/A for visuals, with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered

m4air added 3 commits October 11, 2026 11:54
…b and mobile clients

Every paired client now builds its hello, checks the host's ready, seals its
auth frame and opens host frames through src/shared/paired-e2ee-client-handshake.
Platforms inject their own NaCl/base64 primitives. The v2 client session and its
pure-JS key schedule (@noble/hashes 1.8.0, the version mobile pins) move to
src/shared, and the per-host v2 latch policy is shared with an injected store.

No wire change: frames match the captured legacy and v2 fixtures byte for byte,
and a cross-version test drives the core against the released host and the
released client against this host. The auth step is now async so a device-key
signer can slot in later.
The packaged CLI loads src/shared output from disk, so a bare @noble/hashes
import failed the packaged-runtime check. The v2 key schedule is now a
primitive: Node clients use the node:crypto schedule (moved to src/shared so
the CLI can reach it), while the browser and phone keep the pure-JS one.
The auth step is async, so another socket can latch the host to v2 while a
legacy auth frame is being sealed. Refuse it at send time, as before.
@OrcaWin
OrcaWin force-pushed the np/d2b-handshake-core branch from de51381 to 1eb704a Compare October 11, 2026 18:56
@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

Walkthrough

The change adds shared paired-E2EE handshake, framing-policy, and v2 session implementations. Mobile, web, and remote-runtime transports now use handshake objects and channels for authentication and encrypted frames. Tests cover handshake validation, key-schedule parity, transport behavior, and cross-version compatibility.


Priority: ➖ Normal

Merge Risk: 🔵 Low · up to 1eb70

The handshake refactor appears consistent across clients. A test helper encrypts the auth frame with a different key than the rest of the session, which could confuse future tests. Production behavior is not affected. The change is mergeable, with this fixture as a small follow-up.

Pre-merge checks | Passed 4 | Inconclusive 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage Inconclusive Docstring coverage is 9.62% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 52 functions across 50 files. (17 skipped:… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly identifies the main change: consolidating paired E2EE handshake logic into one shared core for desktop, CLI, web, and mobile clients.
Description check Passed The description is complete and closely follows the repository template. It explains the change, motivation, issue reference, testing, compatibility considerations, review status, and checklist items.…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.

Full details: Docstring Coverage

Explanation

Docstring coverage is 9.62% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 52 functions across 50 files. (17 skipped: 2 unsupported, 15 over the file limit.)


✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/shared/paired-e2ee-fixed-key-test-fixture.ts (1)

13-20: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Make sealAuth use the same fixed key as channel.

The fixture replaces only channel. The spread sealAuth still closes over the legacy channel that createLegacyHandshake built. That channel uses the ECDH key from the dummy host key (Buffer.alloc(32, 9)), not sharedKey. The result is that sealAuth seals the auth frame with a different key than channel.sealText.

Current tests pass because the fake hosts do not decrypt the auth frame. A future test that opens the auth frame with sharedKey will fail, and the cause will be hard to find. Override sealAuth so it also seals through the fixed-key channel.

♻️ Proposed fix
 export function createFixedKeyLegacyHandshakeForTest(
   sharedKey: Uint8Array
 ): PairedE2EEClientHandshake {
+  const channel = createPairedE2EELegacyChannel(NODE_PAIRED_E2EE_PRIMITIVES, sharedKey)
   return {
     ...createPairedE2EEClientHandshake({
       primitives: NODE_PAIRED_E2EE_PRIMITIVES,
       hostPublicKeyB64: Buffer.alloc(32, 9).toString('base64'),
       framing: 'legacy'
     }),
-    channel: createPairedE2EELegacyChannel(NODE_PAIRED_E2EE_PRIMITIVES, sharedKey)
+    channel,
+    sealAuth: async (credential, options) =>
+      channel.sealText(
+        JSON.stringify({
+          type: 'e2ee_auth',
+          deviceToken: credential.deviceToken,
+          ...(options?.clientCapabilities ? { clientCapabilities: options.clientCapabilities } : {})
+        })
+      )
   }
 }

ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3bbf15ac-d872-4143-88ee-6d6cef6ea4d8
📥 Commits

Reviewing files that changed from the base of the PR and between e969881 and 1eb704a.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (68)
  • mobile/src/transport/cellular-connecting-label-stall.test.ts
  • mobile/src/transport/direct-e2ee-framing-policy.ts
  • mobile/src/transport/direct-socket-framing.test.ts
  • mobile/src/transport/direct-socket-framing.ts
  • mobile/src/transport/e2ee.ts
  • mobile/src/transport/legacy-direct-framing-test-policy.ts
  • mobile/src/transport/mobile-e2ee-v2-client-session.test.ts
  • mobile/src/transport/mobile-e2ee-v2-client-session.ts
  • mobile/src/transport/mobile-e2ee-v2-key-schedule.test.ts
  • mobile/src/transport/mobile-e2ee-v2-physical-channel.test.ts
  • mobile/src/transport/mobile-e2ee-v2-physical-channel.ts
  • mobile/src/transport/mobile-paired-e2ee-primitives.ts
  • mobile/src/transport/rpc-client-socket-factory.ts
  • mobile/src/transport/rpc-client-socket-session.ts
  • mobile/tsconfig.json
  • package.json
  • src/main/runtime/relay/simulated-mobile-e2ee-v2-peer.ts
  • src/main/runtime/rpc/e2ee-channel-v2.test.ts
  • src/main/runtime/rpc/mobile-e2ee-v2-desktop-session.test.ts
  • src/main/runtime/rpc/mobile-e2ee-v2-desktop-session.ts
  • src/main/runtime/rpc/mobile-socket-wiring.test.ts
  • src/renderer/src/runtime/paired-host-client-capabilities.test.ts
  • src/renderer/src/web/web-paired-e2ee-primitives.ts
  • src/renderer/src/web/web-runtime-client-file-watch-replay.test.ts
  • src/renderer/src/web/web-runtime-client-heartbeat.test.ts
  • src/renderer/src/web/web-runtime-client.test.ts
  • src/renderer/src/web/web-runtime-client.ts
  • src/renderer/src/web/web-runtime-connection-frame-router.test.ts
  • src/renderer/src/web/web-runtime-connection-frame-router.ts
  • src/renderer/src/web/web-runtime-connection-transport.ts
  • src/shared/e2ee-crypto.ts
  • src/shared/mobile-e2ee-v2-framing.ts
  • src/shared/mobile-e2ee-v2-node-key-schedule.test.ts
  • src/shared/mobile-e2ee-v2-node-key-schedule.ts
  • src/shared/paired-e2ee-client-handshake.test.ts
  • src/shared/paired-e2ee-client-handshake.ts
  • src/shared/paired-e2ee-fixed-key-test-fixture.ts
  • src/shared/paired-e2ee-framing-policy.ts
  • src/shared/paired-e2ee-node-primitives.ts
  • src/shared/paired-e2ee-primitives.ts
  • src/shared/paired-e2ee-v2-client-session.ts
  • src/shared/paired-e2ee-v2-key-schedule.ts
  • src/shared/remote-runtime-client-handshake.ts
  • src/shared/remote-runtime-passthrough-socket.ts
  • src/shared/remote-runtime-request-connection-stale.test.ts
  • src/shared/remote-runtime-request-connection.ts
  • src/shared/remote-runtime-request-frames.test.ts
  • src/shared/remote-runtime-request-frames.ts
  • src/shared/remote-runtime-request-response-router.test.ts
  • src/shared/remote-runtime-request-response-router.ts
  • src/shared/remote-runtime-request-socket.ts
  • src/shared/remote-runtime-request-websocket.ts
  • src/shared/remote-runtime-shared-control-connection-actions.ts
  • src/shared/remote-runtime-shared-control-connection-frame.ts
  • src/shared/remote-runtime-shared-control-connection.test.ts
  • src/shared/remote-runtime-shared-control-connection.ts
  • src/shared/remote-runtime-shared-control-frame-handler.ts
  • src/shared/remote-runtime-shared-control-open.ts
  • src/shared/remote-runtime-shared-control-protocol.ts
  • src/shared/remote-runtime-shared-control-ready-wait.ts
  • src/shared/remote-runtime-shared-control-ready.ts
  • src/shared/remote-runtime-subscription-frame-router.test.ts
  • src/shared/remote-runtime-subscription-frame-router.ts
  • src/shared/remote-runtime-subscription-request-channel.ts
  • src/shared/remote-runtime-subscription-transport.ts
  • src/shared/remote-runtime-transport-error-agreement.test.ts
  • tests/e2e/cross-version-wire/cross-version-mobile-direct-e2ee-v2.unit.test.ts
  • tests/e2e/cross-version-wire/cross-version-paired-e2ee-handshake-core.unit.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

@OrcaWin
OrcaWin merged commit f9794d8 into main Oct 11, 2026
39 checks passed
brennanb2025 added a commit that referenced this pull request Oct 15, 2026
#28049's Grok session paths import @noble/hashes/blake3. Main declares
@noble/hashes 1.8.0 directly (from #27523, not picked); without it the
1.4.224 base resolves a transitive 2.x that doesn't export ./blake3.
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