X-Frame-Options 和 Content-Security-Policy headers 之间的安全区别?

Security difference between X-Frame-Options and Content-Security-Policy headers?

这些 HTTP headers 似乎做同样的事情,尽管后者有更多的灵活性。

Content-Security-Policy 是否提供任何额外的安全性?

X-FRAME-OPTIONS 允许您保护您的网站不被其他网站陷害。

例如 X-FRAME-OPTIONS: SAMEORIGIN 只允许您的站点嵌入同一域的 iframe 中。需要它来防止 clickjacking attacks.

但是 Content-Security-Policy 有完全不同的目的。 Specification of CSP 表示:

Content Security Policy is a declarative policy that lets the authors (or server administrators) of a web application inform the client about the sources from which the application expects to load resources.

所以它的主要目的是通过不允许浏览器加载来自未知域的资源(脚本等)来保护您的站点免受对用户的 XSS 攻击。