Specter is a browser control layer I built for applications that need to launch Chromium, navigate pages and perform ordinary browser operations through Go or Node clients. It supports stock Chromium and a separately packaged browser runtime behind a shared API.
The caller sees commands such as navigate, click and evaluate. Behind those commands are a launched browser process, its profile directory, a Manifest V3 extension and a local WebSocket connection to the control service. They have different lifetimes. Treating the socket as the session itself creates problems as soon as the extension disconnects.
Authenticate the extension against a session that already exists
The manager creates a session record before starting the browser. That record identifies the expected backend and includes a per-session credential. The extension receives the configuration for that launch and connects to the manager's loopback control port.
Its first message is a handshake containing a session identifier, protocol version, backend identity and credential. The manager compares those values with the expectation it created. An extension that presents an unknown session, a mismatched backend, the wrong protocol version or an invalid credential is rejected before it can issue or receive control commands.
Both connections in the figure can reach the port. Only one matches an outstanding session expectation. Binding the server to loopback reduces its exposure, but loopback access alone does not establish which browser launched a connection. A session token provides a separate check. It does not protect against an attacker who already controls the same operating-system account and can read the token.
The protocol tests cover valid round trips and rejection of incorrect versions, backend identities and credentials. A fake extension connection is sufficient for those tests: launching a full browser would make them slower without improving the assertion about handshake validation.
Four identities that are easy to collapse into one
The browser PID, the manager's session ID, the extension's transport connection and the profile directory refer to related things, but they are not interchangeable.
The PID tells us which process was launched. It can disappear or be recycled; it should never be an authorization token. The session ID gives the manager a stable object to which commands and cleanup obligations belong. The WebSocket identifies a particular control-channel attachment that may come and go. The profile path identifies a filesystem resource whose owner determines whether cleanup is permitted.
The manager should create the expected session before accepting a control connection. Otherwise a local peer could connect first and persuade the manager to treat its self-declared identity as authoritative. The extension's handshake is a claim. The already-registered expectation is the thing against which that claim is evaluated.
A useful way to test this is with two synthetic clients. The first sends a correct session identifier but the wrong backend or token. It must not be registered. The second sends the expected tuple and completes a round-trip command. A third test sends the right token with an incompatible protocol version. Authentication cannot make incompatible message formats safe; compatibility validation is a separate gate.
The token has a limited threat model. Binding to 127.0.0.1 reduces network reachability, and a per-session secret makes accidental or opportunistic local impersonation harder. Neither mechanism isolates the control server from fully compromised same-user code that can read the staged extension configuration. Claiming a stronger boundary would require a different isolation model.
When the extension disconnects
Chrome's Manifest V3 service-worker lifecycle allows extension workers to stop and later restart. Chrome has introduced exceptions that keep some active connections alive, but a browser integration still has to handle transport loss.
When the WebSocket disappears, the process may continue running. Its tabs and profile may still exist. The manager should retain its session record so a reconnect can authenticate against the same expected identity. Creating a new session for every incoming socket would lose the relationship to the process and resources that the manager originally launched.
There is another consequence for commands already in flight. Imagine the caller sends click, the browser performs the action, and the socket drops before the reply arrives. The browser layer knows it lost the result. It cannot infer whether the target website accepted the submission. Retrying a click can be harmless for a navigation button and costly for a purchase confirmation. The application using the browser must decide how to reconcile that uncertainty.
The generic SDK can report the transport failure and retain session ownership. The caller can then decide whether a site-specific action is safe to repeat. Hiding the lost reply behind an automatic retry would make the API look simpler while changing the behavior of the website.
Reconnection also changes what a command result means
Imagine this sequence: a caller sends a command with request ID 42; the extension performs it; the WebSocket closes before the reply reaches the SDK. A replacement socket successfully authenticates to the same session. The manager has preserved the process, tabs and profile, but it has not recovered the reply to request 42.
There are two reasonable kinds of operations. Reading the current URL can usually be repeated. Clicking a submission control, downloading a file or changing account settings can have effects outside the browser process. An automatic retry policy cannot treat those cases identically.
For read operations, retrying after reconnect is often acceptable if the caller tolerates state having changed in the meantime. For a write-like operation, a caller should inspect the destination state or use application-specific idempotency, where available. A browser-control library cannot generally provide exactly-once effects on arbitrary websites. The site may have accepted an action and then redirected while the original reply disappeared.
A second complication is stale ownership. If two extension sockets overlap during reconnection, the manager should know which attachment currently owns command delivery. Otherwise a late response on an older socket could be mistaken for the response to a new request. One design is to associate each control attachment with a generation number, correlate responses within that generation and fail pending requests when their transport dies. This is a proposed boundary for reasoning about reconnects, not a claim that every generation check has been independently verified in Specter's runtime.
There's a useful separation in the tests. A fake WebSocket peer can prove that a malformed handshake is rejected. It cannot prove that the browser process exits on shutdown, that an MV3 worker recovers in practice, or that a temporary directory disappears on Windows. Those need browser-and-filesystem integration tests. Chrome's service-worker documentation also notes that active WebSockets can extend worker lifetime in supported versions; this reduces some disconnects but doesn't turn the transport into a permanent resource.
Cleanup has its own identity rules
A browser manager often creates temporary files during launch: a staged extension, a profile directory and sometimes an installed runtime cache. Their cleanup policies differ.
A temporary session profile is disposable after the browser stops. A named persistent profile must survive the session. A caller-supplied profile path belongs to the caller even if the manager successfully opened it. Cleanup follows those ownership rules, including when startup fails halfway through.
Consider a browser that launched but never completed its extension handshake. Closing the failed session should terminate the process and remove only manager-owned temporary resources. It must not recursively delete a persistent directory just because that directory was mentioned in the launch options.
I separate the tests accordingly. Protocol tests exercise the handshake with simulated peers. Lifecycle tests need a real process: launch, authenticate, perform an operation, close, and check what survived. A pinned runtime checksum verifies the downloaded artifact, but it cannot replace that browser-level test.
After a reconnect, the extension should attach to the existing session record. After an explicit close, the manager should confirm that the process it launched has exited and that only manager-owned temporary resources were removed.