javascript - How to use CSPs on iFrames - Stack Overflow

I am using an iFrame to contain an external html source .html<iframe src=“.html" scrolling=&quo

I am using an iFrame to contain an external html source .html

<iframe src=“.html" scrolling="no" style="overflow: hidden; height: 700px;"></iframe>

But good/main.html imports and executes the following javascript

.js
.js
.js

Is there a way to prevent good/main.html importing and executing bad/c.js? More specifically, prevent any resource that does not have the same domain as ?

I tried sandboxing the iFrame

<iframe sandbox=“allow-same-origin allow-scripts” src=“.html” scrolling="no" style="overflow: hidden; height: 700px;"></iframe>

But it still imports and executes the .js

Is there a way to prevent the source from importing and executing any external js?

I read about CSPs on iFrames, but I am unsure of it’s usage.

I am using an iFrame to contain an external html source https://good./main.html

<iframe src=“https://good./main.html" scrolling="no" style="overflow: hidden; height: 700px;"></iframe>

But good./main.html imports and executes the following javascript

https://good./a.js
https://good./b.js
https://bad./c.js

Is there a way to prevent good./main.html importing and executing bad./c.js? More specifically, prevent any resource that does not have the same domain as https://good.?

I tried sandboxing the iFrame

<iframe sandbox=“allow-same-origin allow-scripts” src=“https://good./main.html” scrolling="no" style="overflow: hidden; height: 700px;"></iframe>

But it still imports and executes the https://bad./c.js

Is there a way to prevent the source from importing and executing any external js?

I read about CSPs on iFrames, but I am unsure of it’s usage.

Share Improve this question edited May 30, 2019 at 14:50 RPT asked May 30, 2019 at 12:05 RPTRPT 7482 gold badges13 silver badges30 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

Broadly speaking, you can't. Iframes have their own CSP and you can't pass a policy in from the parent page.

That said, the "csp" attribute of the iframe element, that you link to in the question, somewhat allows this. It allows you to request that the iframe source apply the CSP that you set in the "csp" attribute; but you can't enforce it, merely ask. So you can theoretically do:

<iframe csp="default-src 'none';" ...>

The source of the iframe may or may not implement the CSP you request.

Note this is brand new and is (reportedly) only supported in Chrome and Opera.

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

相关推荐

  • javascript - How to use CSPs on iFrames - Stack Overflow

    I am using an iFrame to contain an external html source .html<iframe src=“.html" scrolling=&quo

    22天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信