Architecture · Guides
How browser native messaging works, and why it beats a localhost WebSocket.
How Chrome and Firefox extensions talk to a desktop app safely: native messaging manifests, stdin/stdout, allowed origins, and why an open local port is risky.
Key takeaways
- A browser extension cannot read your disk or start programs. Native messaging is the sanctioned bridge to a desktop app.
- The browser starts a small helper and talks to it over stdin and stdout, and only extensions listed in the helper's manifest may connect.
- An open WebSocket on
127.0.0.1can be reached by any website you visit, so it needs real authentication. - Practical gotchas on Windows include extension IDs, per-browser registry paths and an app that is not running.
The problem
TurboGet needs a browser extension to spot downloads and videos, and a desktop app to actually download them. Something has to carry a message like "download this URL, with these headers and cookies" from one to the other.
The obvious solution is for the app to listen on a local port, say a WebSocket on localhost, and for the extension to connect. My own early scope document listed that as a fallback. I removed it.
Why a localhost WebSocket is risky
Any web page you open can run JavaScript, and JavaScript can open a connection to ws://127.0.0.1:PORT. Browsers do not stop pages from reaching local servers. If the app accepts connections without authentication, any website could queue downloads on your computer, or worse, read data the app returns.
You can add a token, but then the token has to reach the extension securely, which is the problem you started with.
How native messaging works
Native messaging is a mechanism built into Chrome, Edge and Firefox for exactly this purpose.
- A manifest file describes a host program: its name, the path to the executable, and which extensions may talk to it. Chrome uses
allowed_origins(extension IDs); Firefox usesallowed_extensions. - A registration tells the browser where to find the manifest. On Windows this is a registry key, for example under
HKCU\Software\Google\Chrome\NativeMessagingHosts, and each browser family has its own path. Edge, for instance, reads from its own location rather than Chrome's. - The extension calls
chrome.runtime.connectNative("host.name")orsendNativeMessage. The browser starts the helper program. - Messages flow over stdin and stdout, each prefixed with a four-byte length, in JSON.
Because the browser launches the helper and checks the extension's ID against the manifest, a random web page cannot connect. Only the listed extension can.
From helper to app
The helper is a tiny program. The real work is in the main app, so the helper forwards messages to it. TurboGet does that over a local socket, with two protections:
- It listens on a random port.
- The port and a random token are written to a file readable only by your Windows account. A connection that does not send the token first is dropped.
So there is still a local socket, but one only your user can learn how to use, started and brokered by the browser rather than exposed to every page.
Practical problems I hit on Windows
- Extension IDs from folders. An unpacked Chrome extension's ID is derived from its folder path, so moving the folder silently broke the connection. A fixed public key in the manifest pins the ID.
- Different registry paths per browser. Edge reads native-messaging registrations from its own path, not Chrome's. Opera, Vivaldi, Yandex and others each need their own registration.
- App not running. If the app was not running, the browser cancelled a download and handed it to nobody. Now the helper starts the app on demand, and the extension only takes a download over after the helper has answered.
When to use it
Choose native messaging when an extension needs to hand work to a local program, such as password managers, download managers and developer tools. Avoid it if a purely in-browser solution works, because it adds installation steps: a helper binary and a registration.
Frequently asked questions
Can a website talk to my native messaging host?
No. Only extensions listed in the host's manifest can connect, and the browser enforces that.
Is a localhost server always insecure?
Not always, but it needs authentication and care about which origins it accepts. Native messaging avoids the problem by construction.
Does Firefox support native messaging?
Yes. The manifest uses allowed_extensions with the add-on ID instead of allowed_origins.
This is how TurboGet's browser extension reaches the desktop app.
Read the build story