X-Frame-Options 回應標頭

X-Frame-Options HTTP 回應標頭 (header) 用來指示文件是否能夠透過 <frame><iframe> 以及 <object> 載入。網站可以利用 X-Frame-Options 來確保本身內容不會遭惡意嵌入到其他網站、避免 clickjacking 攻擊。

僅當訪問文件的用戶使用支持 X-Frame-Options 的瀏覽器時,才提供附加的安全性。

備註: 在支援的瀏覽器中,Content-Security-Policyframe-ancestors 指令標準已經取代了非標準的 X-Frame-Options

使用 X-Frame-Options

共有三種值:

DENY

表示網頁無論如何都無法被嵌入到 frame 中,即使於相同網域內嵌入也不允許。

SAMEORIGIN

唯有當符合同源政策下,才能被嵌入到 frame 中。

ALLOW-FROM uri 已棄用

唯有列表許可的 URI 才能嵌入到 frame 中。新版瀏覽器已不再支援此指令。

設定 Apache

請加入以下指令到網站組態設定檔:

Header always append X-Frame-Options SAMEORIGIN

設定 nginx

請加入以下指令到 http, server 或 location 組態設定檔:

add_header X-Frame-Options SAMEORIGIN;

設定 IIS

請加入以下指令到網站的 Web.config 檔:

<system.webServer>
  ...

  <httpProtocol>
    <customHeaders>
      <add name="X-Frame-Options" value="SAMEORIGIN" />
    </customHeaders>
  </httpProtocol>

  ...
</system.webServer>

設定 HAProxy

請加入以下指令到 frontend, listen, 或 backend 組態設定檔:

rspadd X-Frame-Options:\ SAMEORIGIN

備註: 設定 Meta tag 是無效的,像是 <meta http-equiv="X-Frame-Options" content="deny"> 便沒有任何效果,只有透過設定 HTTP header 才有效果,請勿採用。

規範

Specification
HTML
# the-x-frame-options-header

瀏覽器相容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
X-Frame-Options
ALLOW-FROM
DeprecatedNon-standard
SAMEORIGIN

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Non-standard. Check cross-browser support before using.
Deprecated. Not for use in new websites.
See implementation notes.

延伸閱讀