fix(desktop): native messaging review follow-ups

Merged
KI
19d ago
+645-123

Follow-ups to the open review threads on #9327.

Fixed:

  • Windows: stop the host's whole process tree with taskkill /T /F, not only the cmd.exe wrapper.
  • Windows: query both registry views (/reg:32 then /reg:64, Chrome's order) and expand REG_EXPAND_SZ manifest paths.
  • A manifest that exists but cannot be read (anything but ENOENT/ENOTDIR) is logged and stops the lookup instead of falling through to a lower-priority manifest.
  • Host messages are decoded as strict UTF-8; invalid bytes close the port as a framing error.
  • The 64 MiB extension message limit is measured in UTF-8 bytes in the preload and in main.
  • The unread-input cap counts the frame being added (one large frame still goes out when nothing is pending), and the queue of messages posted before the host starts is capped at the same 128 MiB.
  • A host that closes its stdin ends the port with an I/O error and is stopped; output it already wrote still drains first.
  • A failed host lookup or start sends a disconnect event instead of leaving the port open.
  • sendNativeMessage disconnects the port it opened when postMessage throws.
  • nativeMessaging granted at runtime through permissions.request is honored: main checks the granted set (same accessor as declarativeNetRequest), and the preload exposes the APIs when the permission is required or optional.
  • activeTab grants are cleared when the extension unloads (patch regenerated from the pristine package).
  • Spawn failures, unreadable manifests, getManifest() failures and startTask() failures are logged.
  • The deaf-host test now asserts the host process is actually stopped (pid file).

Declined:

  • Reject chrome-extension://<id>/* in allowed_origins: Chrome rejects only match_all_urls/match_subdomains patterns (native_messaging_host_manifest.cc); a path wildcard parses and matches the origin, so rejecting it would break hosts that work in Chrome.
  • Close native ports on forced quit / exitImmediately(): app.exit() inside the first before-quit listener does not stop the synchronous emit, so the native-messaging listener still runs and its SIGTERM/pipe close are synchronous. exitImmediately() (Restart) skips before-quit by design; pipes close when the process exits, so hosts see EOF.
  • Shrink the deaf-host test loop to two posts: under Bun, write callbacks run synchronously once the pipe accepts a frame, so the loop has to outgrow the pipe. The new cap test drives a fake child for the exact boundary.

Tests: every behavior fix has a test that fails with that fix reverted.


Summary by cubic

Follow-ups to the open review threads on #9327, tightening failure handling and permission checks in native messaging.

Fixed

  • Windows stops the whole host process tree with taskkill /T /F and queries both registry views, expanding REG_EXPAND_SZ paths; registry failures other than a missing key are logged.
  • An unreadable manifest stops the lookup and is logged instead of falling through to a lower-priority one.
  • Host messages decode as strict UTF-8; invalid bytes close the port as a framing error.
  • Message size caps are measured in UTF-8 bytes and count the frame being added, including the queue while a host starts.
  • A host that closes stdin ends the port and is stopped; already-written output drains first.
  • Failed lookups and starts send a disconnect event instead of leaving the port open, and host process errors are logged distinctly from start failures.
  • sendNativeMessage disconnects the port it opened when postMessage throws, which it does for values that cannot be serialized to JSON.
  • nativeMessaging granted at runtime through permissions.request is honored in main and preload.
  • activeTab grants clear when an extension unloads.

Declined

  • Rejecting chrome-extension://<id>/* in allowed_origins would break hosts that work in Chrome, which only rejects whole-origin and subdomain patterns.
  • Native ports are not closed on forced quit: pipes already close when the process exits, and the synchronous before-quit listener cannot be interrupted.

Summary by CodeRabbit

  • New Features
    • Extension access to tab details, cookies, navigation, notifications, and context menus follows granted permissions.
    • Native messaging is available when its permission is granted, including through optional runtime permissions.
  • Bug Fixes
    • Active-tab access is revoked when a tab’s URL changes or closes, or its extension unloads.
    • Native messaging validates UTF-8 and message sizes, and handles invalid messages and host startup or communication failures more reliably.
    • Windows native host discovery checks both registry views and supports environment-variable paths.