javascript - how to correctly postMessage into an iframe that has sandbox attribute enabled - Stack Overflow

See below sample code If I ment out the "sandbox" attribute line, everything run just fine.

See below sample code

  • If I ment out the "sandbox" attribute line, everything run just fine.
  • if I unment the "sandbox" attribute line, in chrome open developer console, we will see error "Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('') does not match the recipient window's origin ('null')."

any idea how to solve this problem?

const iframeElement = document.createElement("iframe");
iframeElement.src = ""
//iframeElement.setAttribute("sandbox", "allow-forms allow-modals allow-popups allow-scripts");
iframeElement.onload = (e) => {
  iframeElement.contentWindow.postMessage("foo", "");
};

const containerElement = document.getElementById("place-holder-for-iframe");
containerElement.appendChild(iframeElement);

You can try it out with this jsbin link ,output

  • open js bin link in chrome
  • open chrome developer tool --> go to Console tab
  • unment the sandbox line
  • click "run with js" from jsbin

See below sample code

  • If I ment out the "sandbox" attribute line, everything run just fine.
  • if I unment the "sandbox" attribute line, in chrome open developer console, we will see error "Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://www.bing.') does not match the recipient window's origin ('null')."

any idea how to solve this problem?

const iframeElement = document.createElement("iframe");
iframeElement.src = "https://www.bing."
//iframeElement.setAttribute("sandbox", "allow-forms allow-modals allow-popups allow-scripts");
iframeElement.onload = (e) => {
  iframeElement.contentWindow.postMessage("foo", "https://www.bing.");
};

const containerElement = document.getElementById("place-holder-for-iframe");
containerElement.appendChild(iframeElement);

You can try it out with this jsbin link http://jsbin./gafobulife/edit?js,output

  • open js bin link in chrome
  • open chrome developer tool --> go to Console tab
  • unment the sandbox line
  • click "run with js" from jsbin
Share Improve this question asked May 23, 2018 at 5:05 jojojojo 13.9k36 gold badges95 silver badges126 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

If you don't set allow-same-origin in the sandbox attribute, the content is treated as if it is from a unique origin: See https://developer.mozilla/en-US/docs/Web/HTML/Element/iframe#Attributes, and https://www.html5rocks./en/tutorials/security/sandboxed-iframes/.

Confusingly, allow-same-origin doesn't mean that the iframe will be able to access its parent, as if they were of the same origin (unless they are of the same origin), but it means that it will be able to treated as if it's from its normal origin (in this case, https://www.bing.).

So you can either change:

iframeElement.setAttribute("sandbox", "allow-forms allow-modals allow-popups allow-scripts")'

to

iframeElement.setAttribute("sandbox", "allow-forms allow-modals allow-popups allow-scripts allow-same-origin");

or if you don't want your iframe to maintain its origin, change:

iframeElement.contentWindow.postMessage("foo", "https://www.bing.");

to

iframeElement.contentWindow.postMessage("foo", "*");

For me, there are additional errors if I don't use allow-same-origin, most likely from how bing. is configured.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744091501a4557163.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信