Skip to content

companion: binding HTTPS server to single static IP breaks phone pairing on network change / multi-interface Macs #109

Open

Description

@Yash990-bit

Problem Description

In apps/desktop/src/main/engine/companion.ts, CompanionServer.listen() resolves the first local IP address at start time (const host = this.addresses()[0]) and directly binds the HTTPS server socket to that single specific address:

// apps/desktop/src/main/engine/companion.ts:85-95
private async listen(generation: number): Promise<CompanionStatus> {
  const host = this.addresses()[0];
  if (!host) throw new Error("Connect your Mac to a local network before turning on the iPhone companion.");
  const { key, cert } = this.certificate();
  const server = https.createServer({ key, cert, minVersion: "TLSv1.2" }, (req, res) => {
    void this.handle(server, req, res).catch(() => this.reply(res, 500, { error: "Your Mac could not complete the request." }));
  });
  const listen = (port: number): Promise<void> => new Promise((resolve, reject) => {
      server.once("error", reject);
      server.listen(port, host, () => { server.off("error", reject); resolve(); });
    });

Activity

  1. Yash990-bit commented on Oct 5, 2026

    @Yash990-bit
    ContributorAuthor

    I have reproduced this locally and identified the root cause in CompanionServer.listen() and status().

    I'd be happy to take this up and implement the fix (binding to 0.0.0.0, dynamic host resolution in pairingUri, and unit tests in apps/desktop/test/companion.test.ts).

    Please let me know or drop a 👍 if this approach looks good to proceed with, and I'll submit a PR!

  2. self-assigned this
    on Oct 5, 2026
  3. BunsDev commented on Oct 7, 2026

    @BunsDev
    Contributor

    Thanks @Yash990-bit for reporting this and contributing #124. I reviewed the implementation against current main and closed that PR without merging; this issue remains open because the multi-interface limitation is valid.

    Current main already rebinds after a primary-address change and uses Bonjour to recover saved pairings, but it still selects a single primary address. The proposed wildcard binding preserves the listener during LAN changes, but also listens beyond the private-address selection; QR and Bonjour still advertise only the primary address. That leaves a phone on an isolated secondary LAN without a reachable advertised endpoint. Authentication was not bypassed in the probe: an unauthenticated request correctly returned 401.

    A replacement should:

    1. Bind only the supported local addresses, without accepting companion connections on excluded/public interfaces.
    2. Make pairing/discovery supply an endpoint reachable from a secondary LAN, while preserving certificate pinning and keeping credentials out of Bonjour.
    3. Preserve the token/certificate and avoid unnecessary teardown on unaffected interfaces; cover DHCP/interface changes, offline recovery, occupied ports, and stop/revocation races.
    4. Verify both allowed and excluded interface behavior, and exercise secondary-LAN discovery end to end. Existing loopback tests alone cannot prove this.

    The original PR and signed follow-up 8d16684 retain useful regression work for that replacement. Full rationale: #124 (comment). No wildcard-binding change was merged or released.

  4. Yash990-bit commented on Oct 7, 2026

    @Yash990-bit
    ContributorAuthor

    Thanks @BunsDev for keeping #109 open and outlining the clear requirements for the replacement.

    The criteria (binding only supported local interfaces, providing reachable secondary-LAN endpoints in QR/Bonjour without leaking credentials, avoiding teardown on unaffected interfaces, and adding end-to-end negative/excluded interface tests) provide a solid blueprint.

    I'm aligned with this approach and will look into how we can structure the multi-listener and discovery replacement around these specifications. Thanks again for the guidance!

  5. mariscate commented on Oct 22, 2026

    @mariscate

    Refreshed goal prompts — 2026-10-22 (main at 0e674e7)

    Where it stands: the replacement described in the 2026-10-07 comment shipped in v0.0.11 ("Companion on multiple networks", release review):

    • CompanionServer opens one HTTPS listener per supported private IPv4 address, with no wildcard binding;
    • there's a credential-free Bonjour advertisement per address (companion-discovery.ts), and pairing offers a link for each network;
    • after a DHCP change, only the affected listeners are removed, and the certificate and token are kept;
    • the iPhone app rotates through the endpoints it discovers.

    Still open: physical secondary-LAN acceptance (the test Mac had a single LAN), and an explicit test that excluded interfaces (public, link-local, VPN or loopback where unsupported) never get a listener. Once both have evidence, this issue can close.

    Long-running goal prompt

    Close #109 in TypeSafeAI/modex with evidence, not new architecture. Read AGENTS.md, this issue, apps/desktop/src/main/engine/{companion,companion-discovery}.ts, docs/ios-companion.md and the v0.0.11 companion ledger (docs/reviews/2026-10-11-companion-apple-readiness.md), then verify main's SHA. Create your own worktree. (1) Audit the address filter against the four requirements in the 2026-10-07 comment, and add unit tests with mocked os.networkInterfaces. Allowed private interfaces each get a listener. Public, link-local and other excluded interfaces get none. Removing one interface leaves the others' sockets and the token untouched. Cover occupied ports and stop or revoke during a rebind. Fix any gap with red/green tests. (2) Write a physical acceptance procedure for Val: a Mac on two LANs (for example Ethernet plus Wi-Fi on different subnets), a phone on the secondary LAN, pairing by QR and by Bonjour, then a DHCP change and recovery. Record pass/fail and versions in docs/reviews. (3) Once both are evidenced, propose closing here with links. Credit @Yash990-bit's #124 regression work where it was reused. Open a signed PR without merging.
    

    Follow-up / resume prompt

    Resume #109. Fetch main and re-read this issue for Val's physical results. Re-run the companion unit tests and e2e/companion specs. If the physical test failed, reproduce the failure with mocked interfaces before changing code. If both gates have evidence, post the closing summary with links and leave closing the issue to Val.
    
  6. BunsDev commented on Oct 23, 2026

    @BunsDev
    Contributor

    Refreshed goal prompts — 2026-10-23 (main at 203a350)

    On main already:

    Correction to the 2026-10-22 refresh: it listed an excluded-interface test as missing, but #166 added one. Since 0e674e7, companion.ts has changed only for questions (#373, #389). The listener and address code is unchanged.

    Still open:

    • Physical secondary-LAN acceptance. The test Mac had a single LAN (docs/reviews/2026-10-11-companion-apple-readiness.md, docs/reviews/2026-10-12-v0.0.11-release-review.md).
    • VPN interfaces. localAddresses() filters by address range only, so a VPN interface such as utun with a private address gets a listener. The 2026-10-07 requirements don't say whether it should. That is Val's call.
    • Stop and revoke during a DHCP change. Tests cover shutdown while a listener is being added. No test changes the interface list mid-revoke.

    No open PR touches the companion network code. The companion-device worktree has no commits ahead of main and no PR.

    Long-running goal prompt

    Close #109 in TypeSafeAI/modex with evidence, not new architecture. Read AGENTS.md, this issue (especially the four requirements in the 2026-10-07 comment), apps/desktop/src/main/engine/{companion,companion-discovery}.ts, apps/desktop/test/companion-network.test.ts, docs/ios-companion.md and docs/reviews/2026-10-11-companion-apple-readiness.md, then verify main's SHA (203a350 or later). Create your own worktree with scripts/worktree.sh new <name>; never edit the primary checkout. (1) Map each requirement to the existing tests and name what each test proves. (2) Ask Val on this issue whether VPN interfaces (utun and similar) with private addresses should get a listener. Change localAddresses() only after Val answers, with a mocked-interface test written first. (3) Add the missing race test: the interface list changes while Forget paired phones or Turn off is running, and no listener survives with the old token. Red/green; fix only what the test proves. (4) Write a physical acceptance procedure for Val: a Mac on two LANs on different subnets (for example Ethernet and Wi-Fi), a phone on the secondary LAN, pairing by QR and by Bonjour, then a DHCP change and recovery. Record pass/fail, macOS, Modex and Companion versions in a new dated docs/reviews file. Run pnpm build && pnpm test, and pnpm test:e2e if anything user-visible changes. Update docs/ios-companion.md and docs/status.md where behaviour or evidence changes. Credit @Yash990-bit's #124 regression work wherever it is reused. Open a signed PR (git commit -S) and wait for Val's go-ahead to merge. Once both gates have evidence, propose closing here with links, and leave the close to Val.
    

    Follow-up / resume prompt

    Resume #109. Confirm you own the worktree (scripts/worktree.sh check), fetch main, and read this issue for Val's VPN answer and physical results. Re-run apps/desktop/test/companion-network.test.ts, companion.test.ts and e2e/companion.spec.ts. If a physical test failed, reproduce it with mocked interfaces before changing code. If both gates have evidence, post the closing summary with links. Otherwise post a checkpoint: SHA, results, what's left for Val, and one next action.
    
  7. BunsDev commented on Oct 28, 2026

    @BunsDev
    Contributor

    Question for Val: VPN and virtual interfaces — 2026-10-28 (main at 6c9000f)

    localAddresses() in apps/desktop/src/main/engine/companion.ts selects by address range only (10/8, 172.16/12, 192.168/16, IPv4, not internal). It never looks at the interface name or type. So today:

    • a VPN interface (utun, ppp, ipsec) whose address is private gets its own HTTPS listener and Bonjour advertisement, and the phone can pair over it if the VPN routes to the phone;
    • a Tailscale utun with a 100.64/10 address is excluded by the CGNAT rule, so it gets nothing;
    • virtual bridges get listeners too: on the Mac running this session, two Parallels bridges (bridge100, bridge101) with 10.x host-only addresses each get a listener today, beside en0.

    The 2026-10-07 requirements do not say what should happen here, and I have not changed the filter. A mocked-interface test now pins the current behaviour by name (a VPN or virtual interface with a private address currently gets a listener (policy pending on #109)) so a decision flips it red first.

    Please pick one:

    1. Keep as is. Any private IPv4 address on a non-internal interface gets a listener, including VPN tunnels and virtual bridges.
    2. Exclude tunnels only. Skip utun*, ppp*, ipsec* and other point-to-point interfaces; keep Ethernet, Wi-Fi and bridges.
    3. Exclude tunnels and virtual bridges. Only en* (Ethernet, Wi-Fi, Thunderbolt) and similar physical interfaces get a listener.

    If 2 or 3, I will flip the mocked test first, then change localAddresses() to match, in the same PR as the race test (link to follow).

  8. BunsDev commented on Oct 28, 2026

    @BunsDev
    Contributor

    Checkpoint — 2026-10-28 (main at 6c9000f, PR #421 at e02f618)

    Done in #421 (evidence only, no production change, not merged):

    • Requirement map. docs/reviews/2026-10-28-companion-two-lan-acceptance.md ties each of the four requirements from the 2026-10-07 comment to the tests that prove it and says what each test does not prove. Requirements 1 and 3 are covered by mocked-interface and real-socket tests; requirement 2 by socket, e2e UI and phone model tests; requirement 4's secondary-LAN end to end has no automated proof.
    • The missing race. Two tests in companion-network.test.ts: the interface list changes while Forget paired phones or Turn off is running (a removed listener is still closing; the interface returns mid-action). The kept interface refuses the old token on its unchanged socket; nothing survives Turn off; a late monitor tick does not re-enable it. Both are green on main and each went red against a mutation of the generated JS only (Forget skipping rotation during a reconcile; Turn off leaving listeners to the in-flight reconcile). No fix was needed.
    • VPN policy pinned, not changed. A mocked-interface test records that utun and bridge interfaces with private addresses get a listener today. localAddresses() is unchanged pending the question above.
    • Physical procedure for a Mac on two subnets with the phone on the secondary LAN: QR pairing, Bonjour recovery across a port change, primary-LAN change, secondary-LAN DHCP change, Forget and Turn off during recovery, and a note on VPN or bridge addresses. Eight-row pass/fail table with macOS, Modex and Companion versions to fill in.
    • pnpm build && pnpm test exit 0 (desktop 1995 passed, 17 skipped, LAN cases ran). No e2e: nothing user-visible changed.

    Reuses the #166 listener infrastructure, the #406 rebind race, and the in-flight revocation shape from @Yash990-bit's #124 regression work.

    Left for Val

    1. Answer the VPN and bridge question (keep, exclude tunnels, or exclude tunnels and bridges).
    2. Run the physical procedure and record the table.
    3. Go-ahead to merge Race the interface list against Forget and Turn off, and map #109 evidence #421.

    Next action: when 1 is answered, flip the pinning test and change localAddresses() in #421 if needed; when 2 has results, post the closing summary here and leave the close to Val.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions