ruby中的双等号==问题详解
技术  /  管理员 发布于 5年前   194
前两天在写代码的时候,突然收到警告说项目代码中存在 XSS 漏洞,遂立即根据报告的 URL 排查页面代码,虽然很快就修复了,而且同样问题的讨论两年前就有了,一般来说相对有经验的同学也应该都知道这个点,但是还是觉得有必要写出来,再次提醒一下其他小伙伴,避免踩坑。
问题根源
其中,在找到的漏洞出现的地方,都存在类似以下这样的 slim 代码:
input class='xxx' value==params[:account]
问题就出在双等号 == 上,因为在 slim 跟 ERB 模板(其他模板比如 HAML 之类的就不清楚了)中,双等号其实是 Rails 的 raw 这个 helper 方法的缩写
To insert something verbatim use the raw helper rather than calling html_safe:<%= raw @cms.current_template %> <%# inserts @cms.current_template as is %>or, equivalently, use <%==:<%== @cms.current_template %> <%# inserts @cms.current_template as is %>
也就是说上面的代码等同于:
input class='xxx' value=raw(params[:account])
其中 raw 方法在 Rails 文档中的解释是这样子的:
This method outputs without escaping a string. Since escaping tags is now default, this can be used when you don't want Rails to automatically escape tags. This is not recommended if the data is coming from the user's input.
大概意思就是,这个方法将会跳过对传入的字符串进行标签过滤以及其他处理,直接将字符串输出到 HTML 中。
所以到现在原因就很清晰了,因为不小心在代码里多加了一个等号,变成了双等号,导致将会直接把用户的输入输出到待渲染的 HTML 中,在不自知的情况下留下了 XSS 漏洞。于是乎,修复方案仅需去掉一个等号即可:
input class='xxx' value=params[:account]
这样,Rails 就能继续自动过滤输入的 :account 的参数并且自动过滤恶意内容了。
raw、String#html_safe 以及 <%== %>
在查看 raw 方法的文档时,顺便看了其源码,极其简单,只有一行:
# File actionview/lib/action_view/helpers/output_safety_helper.rb, line 16def raw(stringish) stringish.to_s.html_safeend
raw 只是先确保将 stringish 参数转化为字符串,然后调用了 String#html_safe 方法而已。而且在 String#html_safe 的文档中,同样反复强调慎重使用这两个方法:
It will be inserted into HTML with no additional escaping performed. It is your responsibilty to ensure that the string contains no malicious content. This method is equivalent to the raw helper in views.
所以,可以总结一下,以下三种写法的代码都是等价的,都是不安全的:
input class='xxx' value==params[:account]input class='xxx' value=raw(params[:account])input class='xxx' value=params[:account].html_safe
那在切实需要输出包含 HTML 内容比如富文本编辑器编辑的内容时,如何保证安全?
方案很简单,只需要使用文档中推荐的 sanitize helper 方法:
It is recommended that you use sanitize instead of this method(html_safe).(#sanitize)Sanitizes HTML input, stripping all tags and attributes that aren't whitelisted.
或者使用一些其他第三方的 gem 用来做过滤处理。
总结
原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..博主 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 @ mashrdn 多切换几个节点测试,免费ssr是没那么稳..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号