Content-Security-Policy (CSP) Internet Explorer 解决方法

Content-Security-Policy (CSP) workaround for internet explorer

我们正在构建一个 ASP.NET 网站,希望只允许 一些域 可以 iFrame 我们的网站。 Internet Explorer 不支持 CSP。我正在设置类似的东西 Response.AddHeader("Content-Security-Policy", "frame-ancestors mydomain1.com mydomain2.com")

每个人都如何处理 Internet Explorer。我读到 IE 支持 X-Content-Security-Policy 但它没有 frame-ancestors.

此外,我正在删除 IIS 添加的默认 X-Frame-Options header

Response.Headers.Remove("X-Frame-Options")

X-Frame-Options 正在被 Content-Security-Policy 取代,但正如您所说,并非所有浏览器都完全支持 Content-Security-Policy。

你说你有意删除 X-Frame-Options,但你不应该这样做。这是受 Internet Explorer 支持的,因此如果您将它与内容安全策略一起使用,您将在更广泛的浏览器中获得相同的效果。

在此处查看 X-Frame-Options 文档,其中提到了 IE 支持: https://developer.mozilla.org/en-US/docs/Web/HTTP/X-Frame-Options

Internet Explorer 8 到 11 仅支持 X-Frame-Options,您可以使用 ALLOW-FROM 值。在那里指定您的 iframe 的 URL。

请记住只有最新的 Internet Explorer 浏览器才支持 X-Content-Security-Policy。

Microsoft推荐的解决方案如下:

  1. 在内部,白名单 domain1.com 和 domain2.com
  2. 嵌入您的 iframe URL 时,在 URL 中添加一个指定来源的参数:iframe src="http://example.org/frame.html?origin=http://domain1.com"
  3. 在您的服务器上,检查原始值是否已列入白名单。用它来设置X-Frame-Options:ALLOW-FROMhttp://domain1.com

您还可以检查 Referer header 是否存在。

X-Content-Security-Policy 适用于 IE,使用 https://content-security-policy.com/browser-test/

测试浏览器是否支持 csp

express 中的代码段如下所示:

function applyCSPforIE(req, res, next) {
    res.setHeader('X-Content-Security-Policy', 'frame-ancestors \'self\' http://whatever.com/');
    next();
}

您可以同时使用两者并且它可以工作,但在此 article.

处有一个关于它的警告

以下 apache 配置适用于所有主流浏览器(2018 年 4 月):

<IfModule mod_headers.c>

    Header set Content-Security-Policy "frame-ancestors http://*.example.com/ 'self';"

    # For IE 11 and below
    Header set X-Frame-Options SAMEORIGIN
    Header append X-Frame-Options "ALLOW-FROM http://example.com/" </IfModule>