Difference Between targetOrigin and event.origin

In JavaScript’s window.postMessage API, targetOrigin and event.origin serve as complementary security controls for cross-window communication. While targetOrigin is defined by the sender to specify which domain is authorized to receive a message, event.origin is checked by the receiver to verify which domain sent the message. Understanding the distinction between these two properties is essential for preventing sensitive data leaks and unauthorized script execution across different origins.

What is targetOrigin?

targetOrigin is an argument supplied by the sender when calling the postMessage() method. It dictates the exact origin (scheme, hostname, and port) that the recipient window must have for the message to be dispatched.

// Syntax: targetWindow.postMessage(message, targetOrigin, [transfer]);
const targetWindow = document.getElementById('myIframe').contentWindow;

// Sending a message securely to a specific origin
targetWindow.postMessage({ secretToken: '12345' }, 'https://trusted-domain.com');

Key characteristics of targetOrigin: * Controlled by: The sender. * Purpose: Outbound protection. It ensures that data is not intercepted if the target window changes its URL or gets hijacked by an unexpected domain. * Behavior: If the recipient window’s actual origin does not match the specified targetOrigin, the browser silently drops the event. * Security Risk: Setting targetOrigin to '*' allows any origin to read the message, which should never be done when transmitting sensitive information.


What is event.origin?

event.origin is a read-only property on the MessageEvent object delivered to the recipient’s message event listener. It identifies the origin of the window that dispatched the message.

// Receiver listening for incoming messages
window.addEventListener('message', (event) => {
  // Verify the sender's identity
  if (event.origin !== 'https://trusted-domain.com') {
    // Reject messages from unauthorized origins
    return;
  }

  // Safely process data
  console.log('Received message:', event.data);
});

Key characteristics of event.origin: * Controlled by: The browser (read-only for the receiver). * Purpose: Inbound validation. It allows the recipient to authenticate the source before acting on the incoming payload. * Security Risk: Failing to check event.origin leaves the receiving application vulnerable to processing malicious inputs or executing unauthorized commands sent by untrusted third-party sites.


Summary of Differences

Feature targetOrigin event.origin
Role Sender parameter Receiver property
Direction Outbound message control Inbound message verification
Set By Developer calling postMessage Browser (read-only)
Security Goal Prevents data leaks to unintended recipients Prevents execution of untrusted data from malicious senders
Failure Result Browser cancels message delivery Developer ignores/rejects the received message

For robust security in cross-window messaging, both properties must be used together: always specify an explicit targetOrigin when sending data, and always validate event.origin before processing received data.