Your page listens for postMessage? Any site can send it a message
Any window can send postMessage to your page. Check event.origin before you trust a message, check its shape, and set targetOrigin instead of * when you send.

A payment or sign-in page inside an iframe talks to the parent page with one postMessage line, and the listener that receives it often trusts whatever arrives. By the end of this article you will know who can send messages to your page, what to check in each message, and how to send data without it reaching the wrong site.
What is postMessage?
postMessage is a browser method that lets two windows from different sites send messages to each other, like your page and the iframe inside it. The receiving window listens for the message event, and each message arrives with the origin of the window that sent it.
Who can send a message to your page?
Any window can send a message to your page, not only the site you expect.
MDN's postMessage documentation says that any window, including a malicious site, can send a message to any other window in the iframe hierarchy, from the top to every iframe below.
The human mistake here is simple: you write the listener with only your own iframe in mind, so you act on the message as it is:
window.addEventListener("message", (event) => {
// any site can reach this line
document.getElementById("status").innerHTML = event.data.html;
});With this code, another site can open your page and send it its own html, and your page puts it on the page.
How do you check who sent the message?
You check who sent the message by comparing event.origin with an exact value before anything else, and dropping the message if it does not match. The same documentation says to always verify the sender's identity using the origin property, and possibly source.
const TRUSTED_ORIGIN = "https://pay.example.com";
window.addEventListener("message", (event) => {
if (event.origin !== TRUSTED_ORIGIN) return;
// handle the message here
});The origin value is the protocol, the host name, and the port when it is not the default, like https://example.org. So a full comparison with !== is safer than checking whether the value contains your domain name.
Is checking origin enough?
After you confirm the sender, check the shape of the message itself. The documentation warns that a security hole in the site you trust could open a cross-site scripting hole in your site if you skip that check.
window.addEventListener("message", (event) => {
if (event.origin !== TRUSTED_ORIGIN) return;
const data = event.data;
if (typeof data !== "object" || data === null) return;
if (data.type !== "payment-status") return;
document.getElementById("status").textContent = String(data.status);
});Here the page accepts one message type and puts the text in textContent, not innerHTML, so even text with tags in it does not run (details in the article on innerHTML and textContent).
Why should you not send with targetOrigin set to *?
Because * means the message can go to a window on any site. The documentation says to always specify an exact targetOrigin, because a malicious site can change the location of the window without your knowledge and intercept the data you send.
// sends to whatever page is in the frame now
frame.contentWindow.postMessage({ token }, "*");
// sends only if the frame is still on this origin
frame.contentWindow.postMessage({ token }, "https://pay.example.com");The message is delivered only if the window's origin matches the value exactly, including scheme, host name and port.
How do you check your project?
You check your project by finding every listener and every send:
Search the code for
addEventListener("message"andonmessage. Each one should checkevent.originon its first line.If a page does not expect messages from another site, remove the listener. The documentation calls this the foolproof option.
Search for
postMessage(with"*", and replace the star with the origin you mean.Look at what each listener does with the message: if it puts it in
innerHTMLor changes the page URL, check its shape first.
The same "who is allowed to talk to my page" idea shows up in frame-ancestors against clickjacking, and in CORS with credentials.
Where do you start today?
Search your project for addEventListener("message" and open the first result: if its first line is not a check on event.origin, add one today.
And if you have payment or sign-in pages inside iframes and want someone to test them fully, that is what Muhassan does. How many of your message listeners skip the sender check?
FAQ
If my page does not expect messages from other sites, what should I do?
Nothing: do not add a listener for the message event at all. MDN says this is the most reliable way to avoid the problem.
Is it enough to check that origin contains my domain name?
No. Compare the whole origin with an exact value, because a contains check accepts another domain that includes the same word.
What happens if I leave out targetOrigin?
According to MDN it defaults to "/", which is the origin of the page that is sending.


