Skip to content

fix(desktop): let only the main window change the local-gateway choice - #39407

Open
leonlaiyc wants to merge 1 commit into
kirodotdev:mainfrom
leonlaiyc:claude/local-gateway-ipc-main-window
Open

leonlaiyc wants to merge 1 commit into
kirodotdev:mainfrom
leonlaiyc:claude/local-gateway-ipc-main-window

Conversation

@leonlaiyc

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Only the desktop app's main window can read or change whether this machine runs a local gateway; a connection window to another machine cannot.

local-gateway:get and local-gateway:set in website/electron/ipc-registrar.js answer any sender. They back the "Run a local gateway" switch in Settings > Developer, a next-launch choice stored in the app's own config. preload.js exposes window.localGatewayAPI to every window, including connection windows pointed at another machine's gateway. So a page that gateway serves can call localGatewayAPI.set(false) and stop this machine's gateway from starting at the next launch. #37764 gated the theme listeners and named this channel as left for a separate change.

Why it matters

The choice is about this computer, and a remote page should not be able to make it. Every other machine-level bridge is withheld from remote-gateway windows. The three comparable preference rows (close action, tray icon, minimize to tray) already take assertMainWindowDashboard. This one row has no gate, and a flip only shows up at the next launch, when the local gateway silently does not start.

Not a goal

  • Removing localGatewayAPI from remote-gateway windows in preload.js. A main window connected to a remote host must keep the switch, and that is where a person turns the local gateway off. The registrar gate keeps that case; a preload REMOTE_GATEWAY cut would not.
  • Changing the setting's next-launch semantics or its storage.
  • Other ungated ipcMain listeners that do not write this machine's settings.

What changed (motivation → approach → change)

  • Symptom: any renderer could write the next-launch local-gateway choice.
  • Root cause: the two handlers never check their sender.
  • Change: the same main-window gate the neighbouring preference rows use.

Details:

  • ipc-registrar.js: both handlers are now async and call assertMainWindowDashboard(event, "local-gateway:get|set") first. The gate requires the main window's top frame on its own dashboard origin, local or remote. A refusal is logged through logGateRejection and rejects with "... is restricted to the main window's dashboard".
  • website/src/hooks/useLocalGateway.ts: a refused read hides the switch, since this window cannot know or change the value. A refused write puts the switch back instead of leaving it showing the requested value.

Backwards compatibility

Compatible for the main window: the switch reads and writes as before, including when the main window is connected to a configured remote host.

Removes: a connection window's ability to read or write the choice.
Reader: website/src/pages/settings/DeveloperPanel.tsx:DeveloperPanel -- crew page -- in the main window it keeps working (local-gateway channels answer the main window's dashboard, including on a remote gateway); in a connection window the switch is hidden (hides the switch when main.js refuses the read)
Reader: website/src/hooks/useLocalGateway.ts:useLocalGateway -- crew page -- the only caller of the bridge; it handles both refusals (useLocalGateway.test.ts)

Tests

  • website/electron/test/ipc-registrar.test.js:
    • local-gateway:get and local-gateway:set join MAIN_WINDOW_SETTING_CHANNELS. The four refusal sweeps now prove that both channels refuse another window, no window, a subframe, and a main window navigated off its dashboard. assertCloseActionRefused also checks that a refused sender never writes runLocalGateway.
    • New local-gateway channels answer the main window's dashboard, including on a remote gateway.
    • The old ungated assertions in the generic shell test moved into that test.
  • website/src/test/useLocalGateway.test.ts: hides the switch when main.js refuses the read, as it does for a connection window and puts the switch back when main.js refuses the write. These replace the old survives a rejecting bridge without losing its current value, which pinned the switch staying at the requested value after a refused write.

Proof: not provable -- frontend test (prove.py runs Python only)

Negative control by hand on fix commit 6b1bbeeebd69:

  • With main's ipc-registrar.js and useLocalGateway.ts, 4 refusal sweeps fail ("main-window setting channels refuse every sender that is not the main window", "... a subframe ...", "... navigated off its dashboard", "... when there is no main window"), and so do both new hook tests.
  • With the fix, all pass.

Locally (Windows 11):

  • node --test electron/test/ipc-registrar.test.js: 57 of 59 pass. The 2 failures are dashboard:open-file ... symlink tests that fail with EPERM on this unprivileged host, as on main.
  • npx vitest run src/test/useLocalGateway.test.ts src/test/DeveloperPanel.test.tsx src/test/electronBridgeContract.test.ts: 19 passed.
  • eslint on the four files: clean. npx tsc --noEmit -p tsconfig.app.json: clean.

Manual verification

Not done in a packaged desktop build. The registrar's own harness covers every sender shape the gate distinguishes: main window local, main window on a remote host, another window, no window, a subframe, and a navigated-away origin.

Real-machine verification

N/A -- I have no desktop build on this machine. The before/after above is the registrar harness on my own computer (Windows 11, Node), not a launched Electron app. A maintainer can confirm it in a connection window's devtools: on main, await window.localGatewayAPI.set(false) resolves; on this head it rejects with "local-gateway:set is restricted to the main window's dashboard".

Screenshots / video

Why no screenshot: the main window renders exactly as before, since its read and write still succeed. The one change is in a connection window, where the Developer switch is now hidden instead of offered. That window exists only in a packaged desktop build, which I do not have here. useLocalGateway.test.ts pins both outcomes.

Related Issues

Fixes #39406

Sibling sweep

Swept: grep -n "ipcMain.handle("[a-z-]*:set"" website/electron/ipc-registrar.js
Sibling: website/electron/ipc-registrar.js -- changed here: local-gateway:set and local-gateway:get
Sibling: website/electron/preload.js -- not affected -- the bridge stays exposed so a main window on a remote host keeps the switch; the gate lives in main

Pattern harvest

Rule candidate: review-prompt
Pattern: "an ipcMain handler that writes this machine's app config without assertMainWindowDashboard or assertLocalDashboard"

Checklist

  • A Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

🤖 Generated with Claude Code

local-gateway:get/set answered any window, and a connection window to
another machine shares the preload that exposes them, so a page served
by a remote gateway could switch off this machine's own gateway for
the next launch. Both channels now take assertMainWindowDashboard, the
check the close-action and tray rows already use, which keeps the row
working on a main window connected to a remote host. The settings hook
hides the switch when the read is refused and puts it back when a
write is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leonlaiyc
leonlaiyc requested review from a team and KiroCrewCaptain as code owners November 19, 2026 05:18
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention labels Nov 19, 2026

This branch has not been deployed

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

Labels

fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Desktop: any window can change whether this machine runs a local gateway

1 participant