Validating postMessage from an iframe you don't control

javascript dev.to

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. The handler with no sender check window.addEventListener('message', function (ev) { var d = ev.data || {}; if (!d.type) return; var f = document.getElementById('widget-frame

Read Full Tutorial open_in_new
arrow_back Back to Tutorials