【问题标题】:Rails 4 redirects to 'data:,' in ChromeRails 4 重定向到 Chrome 中的“data:,”
【发布时间】:2013-10-01 00:33:35
【问题描述】:

谷歌浏览器中有一个奇怪的行为,这个问题也有描述:rails redirects to 'data:,'

当创建新资源并且我的控制器重定向到显示操作时,chrome 会启动加载地址栏中带有'data:,' 的空白页面。提出上述问题的作者的回复如下:

这是一项安全功能,新页面的 HTML 内容与提交的表单的 HTML 内容相匹配,Chrome 会阻止。

但是没有解释如何修复它。该行为仅存在于 Chrome 浏览器中。

【问题讨论】:

  • 如果有人知道,我也很好奇这其中的安全部分。为什么 Chrome 会认为这是一项安全功能?
  • 我已经遇到了类似的问题。谷歌浏览器缓存 301 重定向。您只需要清除浏览器缓存或使用隐身模式进行开发。 bugsquash.blogspot.fr/2008/12/… 已解决
  • 您能否发布导致此问题的操作以及该操作的任何可能跟踪信息?
  • BTW ... 清除缓存或使用隐身是不是解决方案。我有一个问题here,这个建议没有帮助。仍在寻找实际答案。
  • 您应该在返回响应的位置发布代码。 respond_to 块中的代码格式可能不正确。此外,如果您正在通过 javascript 进行重定向,请同时发布该代码块。

标签: ruby-on-rails google-chrome ruby-on-rails-4


【解决方案1】:

我一直在谷歌上搜索,发现在 Rails 4.0 中使用 iframe 编辑帖子会导致重定向到“data:”

Rails 4 现在为所有请求设置 X-XSS-Protection 标头,因此 iframe 在提交表单后会触发 Chrome 中的 XSS 保护。 (https://github.com/elektronaut/sugar/issues/41#issuecomment-25987368)

解决方案,将其添加到您的控制器中:

before_filter :disable_xss_protection

protected
def disable_xss_protection
  # Disabling this is probably not a good idea,
  # but the header causes Chrome to choke when being
  # redirected back after a submit and the page contains an iframe.
  response.headers['X-XSS-Protection'] = "0"
end

【讨论】:

  • 我可以确认这样做可以“解决”问题。虽然我同意这可能不是最好的解决方案。
【解决方案2】:

好的,我想我知道这是什么。您可以在 data: 协议中指定图像和文本,我相信 Chrome 会看到转义的 HTML 并认为它是数据。由于未指定 mime 类型,因此在冒号后将 mime 类型留空,仅打印逗号。

http://guides.rubyonrails.org/security.html#redirection

Rails 4 会自动转义 HTML,因此如果您尝试呈现 HTML,则必须指明不要转义它。查看渲染选项:

http://guides.rubyonrails.org/security.html#redirection

您可以使用raw() 直接呈现 HTML。

http://www.webbydude.com/posts/9-the-h-helper-in-rails-3

【讨论】:

    【解决方案3】:

    我不相信这与 mimetype 问题有关。当用户发布其内容中包含 iframe 的博客条目时,我遇到了这个问题。保存条目后,它会重定向到“显示”操作,该操作将包含用户的内容(原始/html_safe)。 Chrome 将显示该页面片刻,然后由于某种原因再次重定向到空白的“数据:”页面(在历史上它只会留下数据:和提交页面)。

    这是我注册的响应头:

    Ruby 2.0.0 / Rails 4 迁移的应用程序行为不正确(暂存服务器)

    Cache-Control:max-age=0, no-cache, no-store Cache-Control:max-age=0, private, must-revalidate Connection:Keep-Alive Content-Encoding:gzip Content-Length:25359 Content-Type:text/html; charset=utf-8 Date:Thu, 23 Jan 2014 16:37:11 GMT ETag:"6d9d4961ea2df12de67f8a92c43579fb" Server:Apache Set-Cookie: _**********_session_dev=1774518c571bf4e65189d607b276e65e; domain=*********.com; path=/; expires=Thu, 23 Jan 2014 18:37:11 -0000; HttpOnly Status:200 OK Vary:Accept-Encoding X-Content-Type-Options:nosniff X-Frame-Options:SAMEORIGIN X-Mod-Pagespeed:1.6.29.7-3566 X-Request-Id:9f5314a5-ad01-4aec-bd0f-04e8afd9bdac X-UA-Compatible:chrome=1 X-XSS-Protection:1; mode=block

    具有正确行为的 Ruby 1.8.7 / Rails 2 应用程序(产品服务器)

    HTTP/1.1 200 OK Date: Thu, 23 Jan 2014 16:32:53 GMT Server: Apache ETag: "f12135ddd373205352f9754328368217" Cache-Control: private, max-age=0, must-revalidate Status: 200 X-Mod-Pagespeed: 1.4.26.5-3533 Cache-Control: max-age=0, no-cache, no-store Vary: Accept-Encoding Content-Length: 27167 X-Cnection: close Content-Type: text/html; charset=utf-8 Connection: Keep-Alive Content-Encoding: gzip

    还尝试将其作为初始 html:

    <!DOCTYPE html> <html> <head>...

    也只是(作为随机测试来检测可能出现的问题)

    <!DOCTYPE html> <head>...

    我所知道的是,如果提交的内容有 iframe,当重定向到博客“显示”页面时,chrome 的奇怪行为就会出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      相关资源
      最近更新 更多