即使'X-Frame-Options'是'ALLOWALL',也无法在iframe中显示我的rails 4 app

时间:2013-07-09 07:32:34

标签: ruby-on-rails responsive-design x-frame-options

我正在尝试测试响应式设计。我正在使用Rails 4。 我知道它将'X-Frame-Options'设置为SAME ORIGIN。所以我在development.rb中使用

覆盖了它
config.action_dispatch.default_headers = {
    'X-Frame-Options' => 'ALLOWALL'
  }

它有效。我在Chrome控制台中查看了网络请求,结果如下:

enter image description here

但仍然像responsive.is和responsinator.com这样的网站给我以下错误:

Refused to display 'http://localhost:3000/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'. about:blank:1

怎么回事?

7 个答案:

答案 0 :(得分:65)

尝试删除此标题'X-Frame-Options'。 也许这种方式在控制器中:

before_filter :allow_iframe_requests
...
def allow_iframe_requests
  response.headers.delete('X-Frame-Options')
end

答案 1 :(得分:27)

我遇到了和你一样的问题,整夜都在寻找解决这个问题的方法。

我终于找到了它为什么会发生。这是因为Chrome缓存。

您可以看到header['X-Frame-Options']ALLOWALL,但它不起作用。

只需尝试打开“新的隐身窗口”并转到同一页面就可以了!

此问题仅发生在我的测试中的开发模式中。它在生产模式中运行良好。

答案 2 :(得分:22)

Rails 4 added默认X-Frame-Options HTTP标头值SAMEORIGIN。这对安全性有好处,但当您希望在action中调用iframe时,您可以这样做:


允许所有来源:

class MyController < ApplicationController
  def iframe_action
    response.headers.delete "X-Frame-Options"
    render_something
  end
end

允许特定原产地:

class MyController < ApplicationController
  def iframe_action
    response.headers["X-FRAME-OPTIONS"] = "ALLOW-FROM http://some-origin.com"
    render_something
  end
end

使用:after_filter

如果您需要在action中使用多个iframe,那么制作方法并使用:after_filter调用它是个好主意:

class ApplicationController < ActionController::Base

  private
  def allow_iframe
    response.headers.delete "X-Frame-Options"
  end
end

在控制器中使用它,如下所示:

class MyController < ApplicationController
  after_filter :allow_iframe, only: [:basic_embed, :awesome_embed]

  def basic_embed
      render_something
  end

  def awesome_embed
      render_something
  end

  # Other Actions...
end

在浏览器中进行硬刷新,或使用其他浏览器查看更改

通过:Rails 4: let specific actions be embedded as iframes

答案 3 :(得分:1)

请尝试ALLOW-FROM http://example.com?如果您有足够新版本的Chrome [2]

,则可以在Chrome中使用ALLOWALL

[1] https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options

[2] https://stackoverflow.com/a/16101968/800526

答案 4 :(得分:0)

如果您希望此更改在所有环境中生效,请将其放在application.rb。

答案 5 :(得分:0)

我找到了另一个原因。假设实现了ALLOWALL或类似修复,下一个问题是尝试在https网站中使用http内容,这会导致安全风险,并被mozilla,IE和其他浏览器阻止。我花了6个小时来识别这个,希望通过分享,我可以减少某些人的痛苦......

可以通过以下方式检查:

  • 使用您应该显示错误的浏览器网络工具。
  • 网络日志与您的供应网站没有任何关联。
  • 将您的内容网址替换为银行https主页应该证明iframe无效。

解决方案是询问来源是否有https内容或找到其他供应商。

REF:

答案 6 :(得分:0)

我只是想在这里提供有关在iframe中嵌入Rails应用程序的最新答案。

简单地删除X-Frame-Options标头而不执行其他安全措施来防止Clickjacking(这是X-Frame-Options在很大程度上试图保护您免受攻击的漏洞)不是一个好主意。

问题是大多数主要浏览器不再接受X-Frame-Options的“ ALLOW-FROM”选项。

截至2020年5月28日撰写本文时,防止Clickjacking和在iframe中托管应用程序的最佳解决方案是实施内容安全策略并设置“ frame_ancestors”政策。 “ frame_ancestors”键指定哪些域可以将您的应用嵌入为iframe。目前主要浏览器都支持它,并且会覆盖您的X-Frame-Options。

您可以在初始化程序中使用Rails 5.2设置内容安全策略(以下示例),对于Rails <5.2,您可以使用诸如Secure Headers gem之类的gem:https://github.com/github/secure_headers

如果需要,您还可以基于控制器/操作来覆盖策略规范。

Content-Security-Policies非常适合高级安全保护。检查一下您可以在Rails文档中配置的所有内容:https://edgeguides.rubyonrails.org/security.html

用于内容安全策略的Rails 5.2示例:

# config/initializers/content_security_policy.rb    
Rails.application.config.content_security_policy do |policy|
  policy.frame_ancestors :self, 'some_website_that_embeds_your_app.com'
end

特定于控制器的策略更改示例:

# Override policy inline
class PostsController < ApplicationController
  content_security_policy do |p|
    p.frame_ancestors :self, 'some_other_website_that_can_embed_posts.com'
  end
end