Skip to content

feat: use a phone for navigation and text input over the local network - #226

Merged
nextestudios merged 3 commits into
mainfrom
feature/223-phone-remote
Sep 28, 2026
Merged

nextestudios merged 3 commits into
mainfrom
feature/223-phone-remote

Conversation

@nextestudios

Copy link
Copy Markdown
Owner

Summary

Menu → Conectar celular… starts a pairing session and shows a QR code. The phone opens a page served by ControlFS itself (no app install, no cloud, no relay). The PC shows the phone's IP and a 6-digit code (the phone shows the same one) and the user must Permitir on the PC. After that the phone sends semantic InputActions and text for the focused on-screen keyboard field. Menu → Desconectar celular (or Desconectar on the phone) ends it.

Security design (shipped)

  • Off by default. A TcpListener exists only during a user-started session: bound to one private LAN IPv4 (RFC 1918; never 0.0.0.0, loopback, link-local or public), random high port, exclusive address use. It stops listening as soon as a phone authenticates. Everything closes on Cancel/Refuse/Disconnect (PC or phone), 2 min unused, 2 min unconfirmed, connection loss (WebSocket keep-alive 5 s / 15 s timeout) and app exit. No auto-reconnect: a new QR is needed.
  • Minimal HTTP/1.1 (HttpRequestParser) for exactly two routes, GET /<sid> (page) and GET /<sid>/ws (upgrade). GET only, no body, ASCII/CRLF only, ≤ 4 KB head, ≤ 32 headers, 5 s head timeout, duplicate headers refused. Exact Host (ip:port, DNS-rebinding defence) and exact Origin for the WebSocket. Only private-LAN peers; ≤ 4 concurrent TCP connections. Then WebSocket.CreateFromStream(isServer: true).
  • Application-level authenticated encryption. A fresh random 32-byte key per pairing lives only in the URL fragment (#k=…, never sent; the page strips it from the address bar) plus a random 128-bit session id in the path. HKDF-SHA256 (salt = session id) derives one key per direction. Each frame is counter(8, BE) ‖ ciphertext ‖ tag with nonce = counter, which must be exactly the next one; a replayed, reordered, tampered or wrong-direction frame closes the connection on the first failure. Keys are zeroed on close. Single use: the first authenticated connection owns the key (≤ 2 concurrent handshakes, 8 failures end the session).
  • Confirmation on the PC (sensitive dialog, initial focus on Recusar): shows the phone IP and a code = HKDF(key, salt = phone's random hello nonce). Nothing from the phone becomes input before Permitir.
  • Input: messages map only to semantic actions (navigate, page, scroll, confirm, back, mark, actions, menu, search, change view, prev/next region) and text (≤ 256 chars, no control chars) / backspace / enter for the focused on-screen keyboard (TypeText, TypeBackspace, OK). Unknown/extra-field/oversized messages are rejected. Rate limit 30/s (burst 60). While a sensitive confirmation is open (delete, replace, undo, mapping), the phone can only send Back. Phone presses are discrete: repeat happens on the phone, so a lost connection leaves no held or repeating state on the PC.
  • Page: a single embedded HTML (no CDNs/fonts/images), CSP allowing only the inline script by SHA-256 hash and connect-src ws://ip:port, no-store, no-referrer, nosniff, DENY framing.
  • Residual risk (documented in docs/security-model.md): the page is plain HTTP on the LAN, so an active LAN attacker could tamper with it. Mitigated by short lifetime, single use, IP + code confirmation on the PC, and the limited action set; not for untrusted networks.

Deviations from the requested design

  • AES-256-GCM instead of ChaCha20-Poly1305. .NET's ChaCha20Poly1305 needs Windows 10 build 20142+ / Windows 11, and ControlFS supports Windows 10 2004 (19041). AesGcm is OS-backed (CNG) on every supported version. Same frame format and nonce/counter rules; the phone uses @noble/ciphers gcm.
  • QR encoder written in-house (Core/Remote/QrCode.cs, byte mode, ECC M, v1–10, following Project Nayuki's MIT reference structure). QRCoder's net6 asset pulls in System.Drawing.Common, which isn't worth it. Validated during development by decoding generated codes for every version 1–10 with jsQR.
  • One network address is used per QR code. With 2+ private networks, "Usar outra rede do PC" cycles through them (networks with a gateway come first, virtual adapters last).

Dependencies / licenses

  • @noble/ciphers 2.4.0 + @noble/hashes 2.4.0 (MIT): only AES-GCM, HKDF and SHA-256, bundled and minified with esbuild into Infrastructure.Remote/Companion/noble-ciphers.min.js with both license texts at the top. Recipe in THIRD_PARTY_NOTICES.md. No new NuGet packages; the new project ControlFS.Infrastructure.Remote depends only on Core.

Tests

CI (this PR):

  • PhoneChannelTests: HTTP parser limits; RFC 6455 accept; key derivation, code and frames cross-checked against vectors produced by the page's noble bundle; tamper, replay, reorder and wrong-direction rejection; session expiry, single use, second connection refused, confirmation required, confirmation timeout, rate limit, too many failures; message → InputAction mapping plus rejection of unknown, extra, oversized and control-char input.
  • PhoneJourneyTests: menu → QR dialog → permission → phone actions and text on the on-screen keyboard → sensitive delete dialog accepts only Back → Menu → Desconectar celular; stale sessions ignored; phone loss closes the dialogs.
  • PhoneLinkIntegrationTests (Windows): real listener on 127.0.0.1, page GET with CSP, foreign Origin refused, ClientWebSocket "phone" deriving keys like the page, listener gone after authentication and a second phone refused, no input before Permitir, navigate/menu/text/backspace/enter reach the AppController, the end message on Disconnect and no listener on the port afterwards; tampered frame ends the session.
  • Screens m10-phone-pairing, m10b-phone-allow (fake link, no network).

Also verified during development: the real server with the real page in headless Chromium at a phone viewport (pairing, matching code, buttons, text diffing, disconnect, port closed afterwards), and C#↔JS crypto interop.

Manual (not hardware-validated, listed in docs/TESTING.md → "Celular como controle (#223)"): real Android Chrome / iOS Safari, Windows Firewall prompt, QR readability at distance, IME/autocorrect typing, screen lock/network loss, multiple adapters.

Docs: CHANGELOG pair, GUIDE pair, README pair (privacy line), PRIVACY, security-model, input-and-focus, ui-redesign (new row), TESTING, architecture, THIRD_PARTY_NOTICES.

Closes #223

🤖 Generated with Claude Code

nextestudios and others added 3 commits September 27, 2026 22:14
Menu > Conectar celular shows a QR code for a short-lived, single-use
pairing session. The phone opens a page served by ControlFS (no app, no
cloud), the PC shows the phone's IP and a verification code and asks to
allow it, then the phone sends semantic actions and text for the focused
on-screen keyboard field over an AES-256-GCM encrypted WebSocket.

Closes #223

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nextestudios
nextestudios merged commit bbc5ea7 into main Sep 28, 2026
3 checks passed
@nextestudios
nextestudios deleted the feature/223-phone-remote branch September 28, 2026 01:31
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.

feat: use a phone for navigation and text input over local P2P

1 participant