【问题标题】:ruby on rails allow embedding of your website in other sites using frame_ancestors content security policy or X-Frame-Optionsruby on rails 允许使用 frame_ancestors 内容安全策略或 X-Frame-Options 将您的网站嵌入其他站点
【发布时间】:2021-08-06 06:10:20
【问题描述】:

我正在尝试允许其他人在许多网站上嵌入我的 rails 应用程序中的页面。我可以使用 X-Frame-Options 让它在 Chrome 和 Firefox 中工作。是否有等效的内容安全策略 response.headers['X-Frame-Options'] = "ALLOW-FROM *"

这是使用 X-Frame-Options 的位

class PeopleController < ApplicationController
  def embed
    response.headers['X-Frame-Options'] = "ALLOW-FROM *"
    @company = People.new
  end
end

但在我使用内容安全策略时在 Chrome 和 Google 中使用内容安全策略时不起作用

class PeopleController < ApplicationController

  content_security_policy do |p|
    p.frame_ancestors "self", "*"
  end

  def embed
    @company = People.new
  end
end

使用内容安全策略时,会抛出此错误:

Refused to frame 'http://localhost:3000/' because an ancestor violates the following Content Security Policy directive: "frame-ancestors self".

这是嵌入代码的示例:

  <iframe src="http://localhost:3000/people/embed"></iframe>

这是我尝试的另一个:

  <iframe src="/people/embed"></iframe>

更新

使用内容安全策略,这仅适用于 Firefox:

content_security_policy do |p|
  p.frame_ancestors 'self', "*"
end

【问题讨论】:

    标签: ruby-on-rails google-chrome iframe content-security-policy x-frame-options


    【解决方案1】:

    现代 Chrome 和 Firefox 不支持 ALLOW-FROM 标头中的 X-Frame-Options 键。您可以发布X-Frame-Options: ALLOW-FROM ###X-Frame-Options: ALLOW-FROM http://example.com - 它们没有任何限制,带有ALLOW-FROM 键的标题只会被浏览器忽略。

    如果您希望允许无限域的 iframe,根本不发布 X-Frame-Options 标头(和 frame-ancestors 指令)会更容易。

    如果您有一组经过计数的允许域,您可以使用带有frame-ancestors domain1 domain2 ... domainN; 的 CSP 标头。

    使用内容安全策略时,会抛出此错误:... because an ancestor violates the following Content Security Policy directive: "frame-ancestors self"

    此错误表示您确实发布了frame-ancestors 'self',而不是预期的frame-ancestors 'self' *
    也许您同时发布了两个不同的 CSP 标头,也许您的代码有错误。您可以check您在浏览器中实际获得的 CSP 标头。

    注意 1'self' 标记应该是单引号 - 在代码中使用 "'self'" 字符串。

    注意 2'self' 令牌通常只覆盖标准端口 80/443,它不包括 http://localhost:3000(它取决于浏览器)。星号 * 确实涵盖了任何端口号。

    【讨论】:

    • 根据您的建议,从控制器,我尝试了 response.headers.delete "X-Frame-Options" ,但表单提交仅适用于 Firefox 并失败Chrome 除非我添加到控制器 skip_before_action :verify_authenticity_token
    • 这不是 CSP 相关的问题。 Chrome 中的 Can't verify CSRF token authenticityhttp; 的用法相关,而不是 https: Firefox somehow able to redirect http 到 https。但 Chrome 没有。人们通常在 Nginx 中使用proxy_set_header X-Forwarded-Proto $scheme; solve this,但首先你必须尝试清除 Crome 浏览器缓存和 coockie,
    猜你喜欢
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 2017-08-30
    • 2013-11-29
    • 1970-01-01
    • 2013-12-21
    相关资源
    最近更新 更多