Repository navigation
fix(electron-chrome-extensions): buffer native messaging host output across stdout chunks - #191
Open
mutewinter wants to merge 1 commit into
Open
mutewinter wants to merge 1 commit into
mutewinter wants to merge 1 commit into
Conversation
…across stdout chunks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A native messaging host whose reply is larger than one stdout read crashes the main process's handler, and the extension never gets the message. With the 1Password desktop app connected to its extension,
NmRequestAccountsreturns more than 64 KB, and main logs two uncaught exceptions back to back:The first is the first 64 KB chunk (65536 minus the 4-byte header). The second is the rest of that message read as a new one. To the user, 1Password unlocks but its extension has no accounts, which looks like an extension bug, not a framing bug.
Cause
receiveis thestdoutdatahandler, and it assumes one chunk is one message:Pipe reads give no guarantee about message boundaries. A message larger than the pipe's buffer arrives over several chunks, and two small messages written close together can arrive in one chunk, where everything after the first is silently dropped. The existing specs echo small values, which always fit in one chunk.
Fix
receiveadds each chunk to a buffer and parses only complete length-prefixed messages, handling any number per chunk and keeping the leftover for the next read. A message that fails to parse is logged throughdebugand skipped, so it can't throw out of a stream handler.Scope
Unchanged: the message format,
send, and the keepAlive and one-shot response paths.Not addressed, deliberately:
sendsizes its buffer withmessage.length(UTF-16 code units) rather thanBuffer.byteLength(message), so a payload with non-ASCII characters is cut short and its length prefix is wrong. That's a separate bug, and I can send a one-line fix for it.Tests
Added
receives a message larger than one stdout chunktochrome-nativeMessaging-spec.ts: it echoes a 256 KB object throughruntime.sendNativeMessage. The test host read stdin with a singlereadSync, which can return less than a whole large message from a pipe, so it now loops until it has the full length.On macOS with Electron 44,
node ./script/spec-runner.js --grep nativeMessaging:native-messaging-host.tsreverted tomaster(same spec and test host): the new spec fails withSyntaxError: Unterminated string in JSON at position 65532, followed by uncaughtUnexpected token 'x'errors from the leftover chunks. Verified by reverting and re-running, not inferred.Only the native messaging specs were run, not the full suite. Like the existing native messaging specs, the new one is skipped on Linux. The same change, patched into 4.10.0, ships in a production Electron app where the 1Password errors above came from.
This PR was written with an AI coding agent (Claude) and reviewed by me before submitting.
Authored with Claude Opus 5.5