跳转至

Index

不安全的 HTTP 头设置:X-Frame-Options

描述

X-Frame-Options HTTP 响应头字段指示了一种策略,用于指定浏览器是否应在 <frame><iframe> 中渲染传输的资源。服务器可以在其 HTTP 响应头中声明此策略以防止点击劫持(Clickjacking)攻击,从而确保其内容不会被嵌入到其他页面或框架中。

建议

  • 在 HTTP 响应头中发送正确的 X-Frame-Options,指示浏览器不允许来自其他域的框架嵌套。

    • X-Frame-Options: DENY:完全拒绝在 <frame><iframe> 中加载。
    • X-Frame-Options: SAMEORIGIN:仅当尝试加载的站点具有同源(Same-Origin)时才允许。
    • X-Frame-Options: ALLOW-FROM URL:允许特定的 URL 在 <iframe> 中加载自身。但是请注意,并非所有浏览器都支持此指令。
  • 在 UI 中部署防御性代码,以确保当前框架是顶级窗口(Top-Level Window)。

if (window !== window.top) {
    // Code is being executed within a frame
    // You may choose to handle this situation appropriately,
    // such as by redirecting the user to another page
    // or displaying a warning message.
    window.top.location.href = "about:blank"; 
}

链接

标准

  • OWASP_ASVS_L1:
    • V14_4_7
  • OWASP_ASVS_L2:
    • V14_4_7
  • OWASP_ASVS_L3:
    • V14_4_7
  • PCI_STANDARDS:
    • REQ_2_2
    • REQ_6_2
    • REQ_6_3
    • REQ_6_4
    • REQ_11_3
  • HIPAA_CONTROLS:
    • SECURITY212
    • SECURITY213