Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Firefox cross-origin object access #94

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pnd280
Copy link

@pnd280 pnd280 commented Oct 22, 2024

PR: Resolve Firefox cross-origin object access issue in message port handling

Issue

Firefox was throwing the following error when attempting to send message from window context:

Error: Not allowed to define cross-origin object as property on [Object] or [Array] XrayWrapper

image

Solution

The fix involves modifying how we handle the message port in the getMessagePort function. Instead of directly accessing and modifying ports[0], we now store it in a local variable port. This approach avoids the cross-origin object access issue.

Changes

In src/internal/message-port.ts:

export const getMessagePort = (
  // ... existing code ...
) => {
  // ... existing code ...
  window.addEventListener('message', function acceptMessagingPort(event) {
    const { data: { cmd, scope, context }, ports } = event
    if (cmd === 'webext-port-offer' && scope === namespace && context !== thisContext) {
      window.removeEventListener('message', acceptMessagingPort)
      const port = ports[0]
      port.onmessage = onMessage
      port.postMessage('port-accepted')
      return resolve(port)
    }
  }
  // ... existing code ...
}

Impact

This change resolves the Firefox-specific error without affecting functionality on other browsers. It ensures consistent behavior across different browser environments when handling message ports in cross-origin contexts.

Testing

  • Verified that the error no longer occurs on Firefox.
  • Ensured that the functionality remains intact on other supported browsers.

Please review and test this change to confirm it resolves the issue without introducing any regressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant