欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

iframe refuses to display

I am trying to load a simple iframe into one of my web pages but it is not displaying. I am getting this error in Chrome:

Refused to display 'https://cw.na1.hgncloud.com/crossmatch/index.do' in a frame because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'self' https://cw.na1.hgncloud.com".


Invalid 'X-Frame-Options' header encountered when loading 'https://cw.na1.hgncloud.com/crossmatch/index.do': 'ALLOW-FROM https://cw.na1.hgncloud.com' is not a recognized directive. The header will be ignored.

This is the code for my iframe:

<p><iframe src="https://cw.na1.hgncloud.com/crossmatch/" width="680" height="500" frameborder="0"></iframe></p>

I am not really sure what that means. I have loaded plenty iframes before and never received such errors.

Any ideas?

Thanks

2 Answers 正确答案 

It means that the http server at cw.na1.hgncloud.com send some http headers to tell web browsers like Chrome to allow iframe loading of that page (https://cw.na1.hgncloud.com/crossmatch/) only from a page hosted on the same domain (cw.na1.hgncloud.com) :

Content-Security-Policy: frame-ancestors 'self' https://cw.na1.hgncloud.com
X-Frame-Options: ALLOW-FROM https://cw.na1.hgncloud.com

You should read that :

The reason for the error is that the host server for https://cw.na1.hgncloud.com has provided some HTTP headers to protect the document. One of which is that the frame ancestors must be from the same domain as the original content. It seems you are attempting to put the iframe at a domain location that is not the same as the content of the iframe - thus violating the Content Security Policy that the host has set.

Check out this link on Content Security Policy for more details.

来自  https://stackoverflow.com/questions/31944552/iframe-refuses-to-display



I would like to render an iframe with the source being Github like so:

<iframe src="https://gist.github.com/user45445/9bf8d568e3350146ba302d7d67ad576f"></iframe>

This is the error I get in the console:Refused to display 'https://gist.github.com/fresh5447/9bf8d568e3350146ba302d7d67ad576f' in a frame because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'none'".

I was researching how to specify myContent Security Policyin myNodeserver, to specify that it should accept any iframes fromgithub

So I installedcsp-helmetand added this to my server code:

varcsp=require('helmet-csp')app.use(csp({// Specify directives as normal.directives:{frameAncestors:['*.github.com'],//I thought these two did the same, so I tried them both.childSrc:['*.github.com']},// Set to true if you only want browsers to report errors, not block them.// You may also set this to a function(req, res) in order to decide dynamically// whether to use reportOnly mode, e.g., to allow for a dynamic kill switch.reportOnly:false,// Set to true if you want to blindly set all headers: Content-Security-Policy,// X-WebKit-CSP, and X-Content-Security-Policy.setAllHeaders:false,// Set to true if you want to disable CSP on Android where it can be buggy.disableAndroid:false,// Set to false if you want to completely disable any user-agent sniffing.// This may make the headers less compatible but it will be much faster.// This defaults to `true`.browserSniff:true}))

But still the same error..

I have been trying to look at theofficial docsand theHTML5 rocks guide

Not sure if I am super close or taking the completely wrong approach.

Update

I have also tried to set the CSP viametatag.

<meta http-equiv="Content-Security-Policy"content="child-src https://gist.github.com; frame-ancestors https://gist.github.com;">

than I received this error:

ContentSecurityPoliciesdelivered via a<meta>element may not contain the frame-ancestors directive.

Thanks in advance.

As oreoshake points out, the problem here is not your CSP, but the CSP on GitHub. It is GitHub that is preventing you from framing them so there is nothing you can do with your CSP to resolve this.

Theframe-ancestorsvalue acts on thesourceof the iframe not the document framing it. Setting CSP on your page will have no effect on the framing. Think offrame-ancestorslikeX-Frame-Optionson steroids: it restricts what is allowed to frame the content. Gist intentionally does not allow directly framing gists but instead provides a way to embed a Gist.

frame-ancestors 'none'==X-Frame-Options: DENY

enter image description here

Your Answer

来自  https://stackoverflow.com/questions/38535491/trying-to-render-i-frame-ancestor-violates-the-followin...



普通分类: