加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 处理用户输入的代码 – 安全性

发布时间:2020-12-17 02:25:48 所属栏目:百科 来源:网络整理
导读:我的应用程序允许用户输入任何语言的代码( python,c,java,ruby等),我使用 PrismJS作为语法高亮显示器. rails是否处理xss和inject,还是需要进一步清理/验证代码? 安全处理用户输入代码(如stackoverflow)的正确方法是什么? 表格输入 div class="form-group"
我的应用程序允许用户输入任何语言的代码( python,c,java,ruby等),我使用 PrismJS作为语法高亮显示器. rails是否处理xss和inject,还是需要进一步清理/验证代码?

安全处理用户输入代码(如stackoverflow)的正确方法是什么?

表格输入

<div class="form-group">
  <label>Code Snippet</label>
  <%= f.text_area :body,class: "end-field form-control",placeholder: "",rows: 8 %>
</div>

视图

<pre><code class="language-<%= snippet.language %> line-numbers"><%= snippet.body %></code></pre>

目前没有进行任何卫生或验证.

输出:

enter image description here

解决方法

OWASP (XXS prevention cheat sheet,Rule #1)建议您使用以下替换来清理两个标记之间的代码:

& --> &amp;
 < --> &lt;
 > --> &gt;
 " --> &quot;
 ' --> &#x27;     &apos; not recommended because its not in the HTML spec (See: section 24.4.1) &apos; is in the XML and XHTML specs.
 / --> &#x2F;     forward slash is included as it helps end an HTML entity

这是他们所说的:

Rule #1 is for when you want to put untrusted data directly into the
HTML body somewhere. This includes inside normal tags like div,p,b,
td,etc. Most web frameworks have a method for HTML escaping for the
characters detailed below. However,this is absolutely not sufficient
for other HTML contexts. You need to implement the other rules
detailed here as well.

06001

Escape the following characters with HTML
entity encoding to prevent switching into any execution context,such
as script,style,or event handlers. Using hex entities is recommended
in the spec. In addition to the 5 characters significant in XML (&,<,>,“,‘),the forward slash is included as it helps to end an HTML entity.

06002

See the 07001 of HTML entity escaping and unescaping.

06003

我的样本实施(不是确定安全)

def sanitize_inside_tags(text)
    {"&"=>"amp","<"=>"lt",">"=>"gt","""=>"quot","'"=>"#x27","/"=>"#x2F"}.each do |char,replacement|
        text = text.gsub(char,"&#{replacement};")
    end
    return text
end

Rails Sanitize方法

这可能是您的最佳选择,因为它不仅是一个使用良好且经过测试的卫生库,而且还允许一些标签,例如< b>和朋友,以便您的用户可以安全地使用一些HTML标记.

在回答您的问题时,您可以安全地使用导轨卫生库,这可能是您的最佳选择.

如果你想剥离所有标签或允许更少,你可以查看custimization here.

语法Hilighting

正如@ImranAli在他的评论中所建议的那样,请查看this post并尝试查看列出的所有库.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读