Validating postMessage from an iframe you don't control
When your widget is embedded on domains you don't own, the usual event.origin check doesn't work. There is no fixed list of merchant domains to whitelist, and accepting everything means any page can drive your UI. The fi
When your widget is embedded on domains you don't own, the usual event.origin check doesn't work. There is no fixed list of merchant domains to whitelist, and accepting everything means any page can drive your UI. The fix is one line: compare event.source with the contentWindow of the iframe you created.
I hit this while building an embeddable live-stream widget. The parent page listens for postMessage events to resize the panel, enter fullscreen, and hand over the screen wake lock. The iframe lives on merchant sites, so event.origin === window.location.origin is always false there.
The check that looks right but isn't
The first version of the handler had no sender check at all:
window.addEventListener('message', function (ev) {
var d = ev.data || {};
if (!d.type) return;
var f = document.getElementById('widget-frame');
if (!f) return;
if (d.type === 'panel_state' && d.open) {
f.style.width = '375px';
f.style.height = '667px';
}
});
Any window that can get a reference to your page can post the same message and change the layout. On a merchant's site that includes every ad script and every analytics tag.
The tempting fix is an origin whitelist. It doesn't survive contact with reality: merchants embed you on their own domain, their staging domain, their Shopify preview URL, a page builder subdomain. You cannot enumerate them, and every new merchant means a deploy.
Compare the source instead
MessageEvent.source is the WindowProxy of the sender. For an iframe you own, it is exactly equal to that iframe's contentWindow β even cross-origin, even after the iframe navigates.
window.addEventListener('message', function (ev) {
var d = ev.data || {};
if (!d || !d.type) return;
var f = document.getElementById('widget-frame');
if (!f) return;
// Only the iframe we created can be the sender.
if (ev.source !== f.contentWindow) return;
if (d.type === 'panel_state') {
if (d.open) {
f.style.width = '375px';
f.style.height = '667px';
} else {
f.style.width = '80px';
f.style.height = '80px';
}
}
});
Two details that matter:
-
contentWindowis a live reference. It stays stable across iframe reloads, so you don't need to re-bind on navigation. - This is not a substitute for validating the payload. Check
d.typeagainst an allowlist, and clamp numbers βwidth: d.widthfrom a compromised frame is still a layout bug.
The sending side is just as important
Cross-origin postMessage with '*' leaks the message to whoever ends up in that window. If the parent is same-origin, name it:
// payment-result.html β opened in an iframe by the same site
window.parent.postMessage(
{ type: 'payment_paid', source: 'billing' },
window.location.origin
);
If the parent really can be any merchant domain, keep '*' but never put tokens, customer data, or URLs with credentials in the payload. The message should be a UI signal, not a data channel.
A quick checklist
- [ ] Every
messagelistener checksevent.sourceagainst the iframe it owns - [ ]
event.datais treated as untrusted input (allowlist.type, clamp sizes) - [ ] Outbound messages use an explicit
targetOriginwhenever the receiver's origin is known - [ ] No secrets in messages that must use
'*' - [ ] One listener per iframe, not a global listener comparing against the last-known frame
The whole fix is about ten lines. The hard part is remembering that "we're behind an iframe" doesn't make the parent page a trusted sender.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.