Skip to content

fix: keep the phone page from hanging on "Connecting to PC" (#259) - #266

Merged
nextestudios merged 1 commit into
mainfrom
fix/259-phone-connecting
Sep 29, 2026
Merged

nextestudios merged 1 commit into
mainfrom
fix/259-phone-connecting

Conversation

@nextestudios

Copy link
Copy Markdown
Owner

Refs #259

Root cause (reproduced)

The phone page's only <script> is allowed by a SHA-256 in the Content-Security-Policy. CompanionPage hashed the embedded HTML as it is stored in the assembly, but browsers hash the script after HTML line-ending normalization (CRLF becomes LF). When the resource has CRLF (Windows checkout with core.autocrlf, which is how Windows builds get it), the hash never matches, the browser blocks the script ("Refused to execute a script because its hash ... does not appear in the script-src directive") and the page stays forever on its initial "Conectando ao PC…" with no error, exactly the issue's symptom.

Reproduced on this Mac by running the real PhoneLinkServer (throwaway harness, outside the repo) and driving the served page with Playwright WebKit and Chromium: LF resource connects in both; CRLF resource is blocked in both and stays on "Conectando ao PC…". After the fix, CRLF resources connect in both.

Other suspects checked and ruled out with the real server: Safari upgrade headers (permessage-deflate, Connection: Upgrade), Host/Origin exact match, 101 response, single-use key not consumed by the page fetch (the page GET only checks validity), hand-written parser accepts real Safari request headers.

Changes

  • CompanionPage.Compose normalizes line endings to LF before hashing; .gitattributes keeps Companion/* LF.
  • Page state machine: 12 s to open the channel, 10 s to verify it, "Aguardando permissão no PC" with countdown, distinct messages (PC unreachable / QR expired or used / secure check failed / connection lost / refused), Tentar de novo with the same key while the session is valid, terminal states tell the user to generate and scan a new QR. New GET /<session>/ping probe (204/410, consumes nothing) separates unreachable from expired. A script that cannot start now shows an on-screen message (also the static default text says the browser could not run the page).
  • PC side: PhoneAttempt events show "Estado" on the QR dialog (page opened / refused / handshake failed + short code); PhoneLinkServer takes a trace callback wired to the local startup.log with reason codes only (no keys, session ids, URLs, content). Security design of feat: use a phone for navigation and text input over local P2P #223 unchanged (fragment key, HKDF, counters, single use).
  • Tests: PhoneChannelTests.Companion_page_hash_* (CRLF regression), PhoneServerProtocolTests (Safari-style raw requests against the real server, CSP hash vs served script, no secrets in the trace), PhoneJourneyTests (dialog status).
  • Docs: CHANGELOG pair, GUIDE pair (troubleshooting), security-model, TESTING.

Manual evidence (not part of CI; not a real iPhone)

WebKit and Chromium via Playwright against the real server: normal pairing, waiting for permission (countdown and hint at 30 s), open timeout then retry, session gone (unreachable message). Real iPhone/Android checks remain manual and are listed as not hardware-validated in docs/TESTING.md.

🤖 Generated with Claude Code

The embedded phone page was hashed with CRLF line endings on Windows checkouts while browsers hash LF, so the CSP blocked the script.
Also bound every step of the phone handshake with clear messages, retry and a probe, and log refusal reason codes.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@nextestudios
nextestudios merged commit 02c9055 into main Sep 29, 2026
3 checks passed
@nextestudios
nextestudios deleted the fix/259-phone-connecting branch September 29, 2026 04:29
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