fix: support postMessage options overload and handle transferable clone failures in Firefox#3078
Open
crisryantan wants to merge 1 commit intoDevExpress:masterfrom
Open
Conversation
…ne failures
MessageSandbox.postMessage previously did not support the modern
postMessage(message, { targetOrigin, transfer }) overload, silently
dropping such calls. This broke iframe communication for applications
using the options-based API.
Additionally, in Firefox the Hammerhead message envelope can cause
DataCloneError ("MessagePort object could not be cloned") when
transferable objects are present in the transfer list. The structured
clone algorithm in Firefox fails to reconcile the wrapped message
with the original transfer list.
This commit:
- Adds support for the postMessage(message, options) overload by
detecting an object second argument and extracting targetOrigin
and transfer from it.
- Wraps the fastApply call in a try/catch so that when structured
clone fails (e.g. Firefox with MessagePort transfers), the
original unwrapped message is sent as a fallback.
- Updates the receiving side (MessageEvent.data getter and
_onWindowMessage) to pass through messages that do not carry the
Hammerhead user-message envelope, since they may arrive from the
fallback path.
- Updates tests to cover the options overload and adjusts the
existing "object as targetOrigin" test to reflect the new
behavior.
Made-with: Cursor
|
Thank you for your contribution to TestCafe. When a member of the TestCafe team becomes available, they will review this PR. |
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.
Closes #3077
Problem
When testing an application that uses iframe communication with
MessagePorttransfers, Firefox throwsDataCloneError: MessagePort object could not be clonedunder Hammerhead's proxy. This breaks any application that relies onMessageChannel-based communication between a parent page and iframes.The root cause is in
MessageSandbox.postMessage: the method wraps the original message in a Hammerhead envelope before callingfastApply. Firefox's structured clone algorithm cannot reconcile the wrapped envelope object with theMessagePorttransferables in the transfer list — the ports reference the original message, not the new envelope.Additionally, the modern
postMessage(message, { targetOrigin, transfer })overload is not supported. When the second argument is an object, the call is silently dropped becausetypeof targetUrl !== 'string'evaluates totrueand the method returnsnull.This does not affect Chrome because TestCafe uses native automation (CDP) for Chrome, which bypasses Hammerhead entirely.
Solution
1. Support the
postMessage(message, options)overloadThe
postMessagemethod now detects when the second argument is an object (rather than a stringtargetOrigin) and delegates to a new_postMessageWithOptionsOverloadhandler that extractstargetOriginandtransferfrom the options object.2. Graceful fallback for structured clone failures
Both
_postMessageWrappedand_postMessageWithOptionsOverloadnow wrap thefastApplycall in atry/catch. When the wrapped message fails to be structured-cloned (as happens in Firefox withMessagePorttransfers), the fallback sends the original unwrapped message withtargetOrigin: '*'. This preserves the transfer semantics while still routing through the proxy.3. Updated receiving side to handle unwrapped messages
_onWindowMessage: Messages that don't carry the Hammerhead envelope (MessageType.UserorMessageType.Service) are now passed through to the original listener. Since these arrive via the browser's nativepostMessage, the browser has already validated the target origin.MessageEvent.datagetter: Only unwraps messages that explicitly carry theMessageType.Usertype. Raw messages from the fallback path are returned as-is.Tests
postMessage(message, { targetOrigin })options overloadpostMessage(message, { targetOrigin, transfer })withMessagePortMade with Cursor