如何将 iframe 限制为仅使用本地内容?

信息安全 Web应用程序 框架
2021-09-02 17:15:18

如何限制我的 iFrame 仅使用本地(即不来自其他网站)内容?我的问题与如何限制其他网站在 iframe 中使用我的网站的问题相反。

我正在使用 Tomcat/Java/JSP。

1个回答

您可以使用该指令设置Content-Security-Policy标头child-src

例如,这将只允许来自同一域的帧内容:

Content-Security-Policy: child-src 'self'

而不是'self'你还可以指定一个特定的原产地。

是一个简单的演示(meta 标签用于模拟标题)。只有第一帧应该加载:

<meta http-equiv="Content-Security-Policy" content="child-src http://example.com/">
<iframe src="http://example.com/"></iframe>
<iframe src="http://example.org/"></iframe>

请注意,这在 Firefox 和 Chrome 中有效,但在 IE和旧版浏览器中不受支持