Content-Security-Policy 如何与 X-Frame-Options 一起使用?
How does Content-Security-Policy work with X-Frame-Options?
Content-Security-Policy
会忽略服务器返回的 X-Frame-Options
,还是 X-Frame-Options
仍然是主要的?
假设我有:
- 一个网站 http://a.com
X-Frame-Options: DENY
- 和一个网站 http://b.com
Content-Security-Policy: frame-src a.com
浏览器会加载这个框架吗?
不清楚。
一方面,http://a.com明确否认框架。
另一方面,http://b.com 明确允许 http://a.com.
的框架
通过实践检验找到了答案
我已经创建了两个网站并重现了所描述的情况。
X-Frame-Options 似乎是主要的。
如果目标服务器拒绝框架,则客户端网站无法在 iframe
中显示此页面,无论设置 Content-Security-Policy
的哪个值。
但是,我没有在文档中找到任何确认信息。
在 Chrome 54 和 IE 11 上测试。
frame-src
CSP directive(已弃用并被 child-src
取代)决定了页面框架中可以使用哪些来源。
另一方面,X-Frame-Options
响应 header 确定哪些其他页面可以在 iframe 中使用该页面。
在您的情况下,http://a.com
和 X-Frame-Options: DENY
表示没有其他页面可以在框架中使用它。 http://b.com
在其 CSP 中有什么并不重要——任何页面都不能在框架中使用 http://a.com
。
X-Frame-Options
与CSP相交的地方是通过frame-ancestors
directive. From the CSP specificiation(强调我的):
This directive is similar to the X-Frame-Options
header that several
user agents have implemented. The 'none'
source expression is
roughly equivalent to that header’s DENY
, 'self'
to SAMEORIGIN
,
and so on. The major difference is that many user agents implement
SAMEORIGIN
such that it only matches against the top-level
document’s location. This directive checks each ancestor. If any
ancestor doesn’t match, the load is cancelled. [RFC7034]
The frame-ancestors
directive obsoletes the X-Frame-Options
header. If a resource has both policies, the frame-ancestors
policy SHOULD be enforced and the X-Frame-Options
policy SHOULD be ignored.
一个 older question 表示当时这在 Firefox 中不起作用,但希望现在情况有所改变。
2018 年 4 月更新:
Content Security Policy: Directive ‘child-src’ has been deprecated. Please use directive ‘worker-src’ to control workers, or directive ‘frame-src’ to control frames respectively.
看起来 child-src
现在已被弃用,frame-src
又回来了。
None 你的假设是普遍正确的。
- Chrome 忽略
X-Frame-Options
.
- Safari 9 及更低版本忽略 CSP
frame-ancestors
。
- Safari 10-12 遵循 CSP
frame-ancestors
指令,但 优先 X-Frame-Options
如果两者都指定。
Content-Security-Policy
会忽略服务器返回的 X-Frame-Options
,还是 X-Frame-Options
仍然是主要的?
假设我有:
- 一个网站 http://a.com
X-Frame-Options: DENY
- 和一个网站 http://b.com
Content-Security-Policy: frame-src a.com
浏览器会加载这个框架吗?
不清楚。
一方面,http://a.com明确否认框架。
另一方面,http://b.com 明确允许 http://a.com.
通过实践检验找到了答案
我已经创建了两个网站并重现了所描述的情况。
X-Frame-Options 似乎是主要的。
如果目标服务器拒绝框架,则客户端网站无法在 iframe
中显示此页面,无论设置 Content-Security-Policy
的哪个值。
但是,我没有在文档中找到任何确认信息。
在 Chrome 54 和 IE 11 上测试。
frame-src
CSP directive(已弃用并被 child-src
取代)决定了页面框架中可以使用哪些来源。
另一方面,X-Frame-Options
响应 header 确定哪些其他页面可以在 iframe 中使用该页面。
在您的情况下,http://a.com
和 X-Frame-Options: DENY
表示没有其他页面可以在框架中使用它。 http://b.com
在其 CSP 中有什么并不重要——任何页面都不能在框架中使用 http://a.com
。
X-Frame-Options
与CSP相交的地方是通过frame-ancestors
directive. From the CSP specificiation(强调我的):
This directive is similar to the
X-Frame-Options
header that several user agents have implemented. The'none'
source expression is roughly equivalent to that header’sDENY
,'self'
toSAMEORIGIN
, and so on. The major difference is that many user agents implementSAMEORIGIN
such that it only matches against the top-level document’s location. This directive checks each ancestor. If any ancestor doesn’t match, the load is cancelled. [RFC7034]The
frame-ancestors
directive obsoletes theX-Frame-Options
header. If a resource has both policies, theframe-ancestors
policy SHOULD be enforced and theX-Frame-Options
policy SHOULD be ignored.
一个 older question 表示当时这在 Firefox 中不起作用,但希望现在情况有所改变。
2018 年 4 月更新:
Content Security Policy: Directive ‘child-src’ has been deprecated. Please use directive ‘worker-src’ to control workers, or directive ‘frame-src’ to control frames respectively.
看起来 child-src
现在已被弃用,frame-src
又回来了。
None 你的假设是普遍正确的。
- Chrome 忽略
X-Frame-Options
. - Safari 9 及更低版本忽略 CSP
frame-ancestors
。 - Safari 10-12 遵循 CSP
frame-ancestors
指令,但 优先X-Frame-Options
如果两者都指定。