Skip to content

fix(mobile): connect to pinned gateways on ios - #268

Merged
nixuuu merged 1 commit into
devfrom
fix/mobile-ios-connect
Nov 6, 2026
Merged

nixuuu merged 1 commit into
devfrom
fix/mobile-ios-connect

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 6, 2026

Copy link
Copy Markdown
Member

Root cause

The first production iOS build (TestFlight) failed to pair at once with [unknown] Connection interrupted. Try again., while Android works against the same gateway.

I reproduced it on an iOS 26.3 simulator with a Release build (same ATS settings as production), pairing over HTTPS with pinning against a throwaway gateway with network access on (self-signed P-256 listener certificate, reached over Tailscale from the Mac).

The CFNetwork log of the failing request:

Connection 2: asked to evaluate TLS Trust
Task <…> auth completion disp=0 cred=0x…            ← our delegate: pin matches, .useCredential
Trust evaluate failure: [leaf AnchorTrusted OtherTrustValidityPeriod SSLHostname ServerAuthEKU]
System Trust Evaluation yielded status(-9802)
ATS failed system trust
HTTP load failed, 0/0 bytes (error code: -1200 [3:-9802])
  • With ATS on, URLSession runs a second system trust evaluation after the delegate accepts the pinned certificate: SSL policy, host name, system roots. The gateway's self-signed certificate (no SAN or EKU, 20-year validity) can never pass it.
  • I tried setting SecPolicyCreateBasicX509 plus the pinned certificate as the only anchor on the trust object first. It made no difference: the ATS evaluation still failed on AnchorTrusted and SSLHostname.
  • The TLS-only probe uses Network.framework, which ATS doesn't cover. So the route check passed and the first RPC failed.
  • Ruled out:
    • ALPN/HTTP/2: the handshake completed with h2 and TLS 1.3 / ECDHE, which ATS accepts.
    • Network and Local Network permission: the probe connected, and Safari on the phone reaches the gateway.

Fix

  • app.config.ts: NSAppTransportSecurity: { NSAllowsArbitraryLoads: true }.
    • NSExceptionDomains can't take IP addresses, and gateway host names are set by the user, so a per-domain exception isn't possible.
    • NSAllowsLocalNetworking is removed: when it is present, iOS ignores NSAllowsArbitraryLoads.
    • The URLSession delegate's exact SHA-256 pin stays the only trust decision. The app reaches the computer only through this module (HTTPS only, pinned, no redirects or cookies).
  • App Store review: NSAllowsArbitraryLoads needs a justification. Ours: the app connects to a user-hosted server (an IP address or a custom name) with a self-signed certificate, verified by a SHA-256 fingerprint pinned during pairing. This is noted in docs/mobile.md.

Native error codes in the UI

A failure can now be diagnosed from a screenshot. The code goes in parentheses; it never includes an address.

  • iOS: onError carries NSURLErrorDomain -1200 / -9802 (the domain, the code and the TLS stream error). Probe failures carry POSIX 61, TLS -9808, DNS … or timeout.
  • Android: the exception class and its root cause, e.g. SSLHandshakeException / CertPathValidatorException.
  • pinned.ts adds the code to the message. routes.ts adds probe codes to "Cannot reach this computer securely…". Stream reconnects show it in the "Connection lost" banner.

Verified (iOS 26.3 simulator, Release build, HTTPS + pin, gateway reached over Tailscale)

  • Before: [unknown] Connection interrupted. Try again. (same as TestFlight).
  • With error codes, ATS unchanged: [unknown] Could not connect to this computer. Check the network, then try again. (NSURLErrorDomain -1200 / -9802).
  • With ATS relaxed:
    • pairing succeeds;
    • Threads shows Connected (WatchOverview is streaming);
    • New thread → "hello from ios" → the scripted worker's "Echo: hello from ios" arrives (CreateThread + WatchThread stream).
  • The ATS run used the build with error codes, with only its Info.plist edited to the new ATS dict and re-signed ad hoc. The Mac didn't have the disk for another full Release build.
    • That binary also had three SecTrustSet* anchor lines, which I removed afterwards.
    • It did not have the stream.ts banner change, which is JS only.
  • Gates: bun install, gen:check, typecheck, lint, format:check and expo lint are clean.
    • check:tmp: 1454 pass. Three Git-origin tests in add-project/linked-projects failed only because I had set TMPDIR inside the git worktree; they pass with the default TMPDIR. No mobile tests are involved.

Not verified:

  • a physical iPhone (TestFlight builds after merge);
  • the Android build on a device or emulator: the Mac had no disk left for a Gradle build. The Kotlin change only adds a code field (class names), and Android typecheck and expo lint pass.

Copy audit (docs/copy.md §8)

Before After Reason
Connection interrupted. Try again. (any non-pin failure) Before a response: Could not connect to this computer. Check the network, then try again. (code) It is a connect failure, not an interruption. What happened, then what to do.
Connection interrupted. Try again. After a response: Connection interrupted. Try again. (code) Unchanged, plus the native code.
Cannot reach this computer securely. … Same text, plus (code, …) Probe reasons.
Connection lost. Reconnecting automatically… Same text, plus (code) when the transport gave one Diagnosable from a screenshot.
  • The codes are on the surface, not behind Show details. That goes against §4.4 on purpose: the mobile pair screen and the banner have no details disclosure, and the task needs the code to be visible in a screenshot. The codes are short and in parentheses at the end.
  • No new glossary terms. No buttons changed.

Related: #61

With ATS on, URLSession evaluates server trust again after the delegate
accepts the pinned certificate, against the system roots and host name.
The computer's self-signed certificate fails it (NSURLErrorDomain -1200,
-9802), so every RPC failed after a successful TLS probe. IP hosts can't
get a per-domain exception, so ATS is relaxed and the pin stays the only
trust decision. NSAllowsLocalNetworking is dropped: it makes iOS ignore
NSAllowsArbitraryLoads.

Native error domains and codes now end the transport messages on iOS and
Android, including probe failures and the stream reconnect banner.

Related: #61
@nixuuu
nixuuu merged commit fd5448b into dev Nov 6, 2026
9 checks passed
@nixuuu
nixuuu deleted the fix/mobile-ios-connect branch November 6, 2026 10:38
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