简体   繁体   English

X-Frame-Options在meta标签中不起作用吗?

[英]X-Frame-Options is not working in meta tag?

I want to restrict my site content to be used in other domains through iframe control. 我想通过iframe控件限制我的网站内容在其他域中使用。 The recommented meta tag ie <meta http-equiv="X-Frame-Options" content="deny"> is not working. 建议的元标记(即<meta http-equiv="X-Frame-Options" content="deny"> )不起作用。 What can i do? 我能做什么?

You can't set X-Frame-Options in a metatag, only using the HTTP header. 您不能仅在HTTP标头中在metatag中设置X-Frame-Options。

read more here: 在这里阅读更多:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/X-Frame-Options

For example, if you are using Apache, you should add a line like this in the .htaccess file 例如,如果您使用的是Apache,则应在.htaccess文件中添加这样的行

Header set X-Frame-Options DENY

avoid doing it in meta-tag. 避免在meta-tag中这样做。 do it in IIS, or in the application: 在IIS或应用程序中执行此操作:

protected void Application_BeginRequest(object sender, EventArgs e)
{
  HttpContext.Current.Response.AddHeader("x-frame-options", "DENY");
}

or 要么

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

If you want to allow specific domains, then use allow-from option and not deny. 如果要允许特定域,请使用allow-from选项,而不要拒绝。

This header may not work with old browsers, for example Mozilla 3.0, so you need to implement also a client validation, named busting JS. 该标头可能不适用于旧的浏览器,例如Mozilla 3.0,因此您还需要实现一个名为busting JS的客户端验证。 check this here: https://www.owasp.org/index.php/Clickjacking_Defense_Cheat_Sheet 在这里检查: https : //www.owasp.org/index.php/Clickjacking_Defense_Cheat_Sheet

It will not work. 不起作用。 Browser error: X-Frame-Options may only be set via an HTTP header sent along with a document. 浏览器错误:X-Frame-Options只能通过与文档一起发送的HTTP标头进行设置。 It may not be set inside . 可能没有设置在里面。

Note: Setting the meta tag is useless! 注意:设置meta标签是没有用的! For instance, has no effect. 例如,没有任何作用。 Do not use it! 不要使用它! Only by setting through the HTTP header like the examples below, X-Frame-Options will work. 只有通过设置HTTP标头(如下面的示例),X-Frame-Options才能起作用。

Source Link 链接

Configuring Apache: 配置Apache:

Header set X-Frame-Options "deny"
Header always set X-Frame-Options "sameorigin"

Configuring nginx: 配置nginx:

add_header X-Frame-Options sameorigin always;

Configuring IIS: 配置IIS:

<system.webServer>
  ...
  <httpProtocol>
    <customHeaders>
      <add name="X-Frame-Options" value="sameorigin" />
    </customHeaders>
  </httpProtocol>
  ...
</system.webServer>

X-Frame-Options is a mitigation technique for clickjacking attacks. X-Frame-Options是一种用于缓解点击劫持攻击的技术。 It is an HTTP response header sent by the server to indicate under what circumstances page contents should be displayed in a frame context. 它是服务器发送的HTTP响应标头,用于指示在何种情况下应在框架上下文中显示页面内容。 A browser that understands the header will not display the contents of a page if the header directive is violated (for instance, if evil-example.com puts good-site.com in an iframe but good-site.com sends a header that says X-Frame-Options: DENY. Thus, no clickjacking can occur because no UI elements can be displayed to a victim. It provides no protection against CSRF. Read more: clickjacking and .. , Security through HTTP response 如果标头指令被违反,则了解标头的浏览器将不会显示页面的内容(例如,如果evil-example.com将good-site.com放在iframe中,但是good-site.com发送的标头显示为X-Frame-Options:DENY。因此,不会发生点击劫持,因为无法向受害者显示任何UI元素,因此无法针对CSRF提供保护。阅读更多: clickjacking和..通过HTTP响应的安全性

If you need to add a header to a response, consider registering a callback to do this when the headers are about to be sent. 如果您需要在响应中添加标头,请考虑在将要发送标头时注册回调以执行此操作。

In between BeginRequest and the response being sent, the code could completely replace or clear the headers collection (though probably not, if you have written all the code yourself). 在BeginRequest和发送的响应之间,代码可以完全替换或清除标头集合(尽管如果您自己编写了所有代码,则可能没有)。

In this example, I have some downstream code which sometimes adds an x-frame-options header, but misses some HTML pages. 在此示例中,我有一些下游代码,有时会添加x-frame-options标头,但会丢失一些HTML页面。 So the code ensures the header is set in the callback: 因此,代码确保在回调中设置了标头:

protected void Application_BeginRequest()
{
    HttpContext.Current.Response.AddOnSendingHeaders(httpContext =>
    {
        if (isHtmlResponse() && hasNoFrameOptionsHeader())
        {
            httpContext.Response.AddHeader("x-frame-options", "SAMEORIGIN");
        }

        bool isHtmlResponse () {
            var contentTypeValue = httpContext.Response.Headers["content-type"];
            return contentTypeValue is null ? false : contentTypeValue.ToLower().Contains("text/html");
        }
        bool hasNoFrameOptionsHeader () => httpContext.Response.Headers["x-frame-options"] is null;
    });            
} 

您也可以尝试php代码

<?php header('X-Frame-Options:SAMEORIGIN',true);?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM