English 中文(简体)
Security in a Rails app - User submitted data
原标题:

I m currently in the process of writing my first Rails app. I m writing a simple blog app that will allow users to comment on posts. I m pretty new to Rails, so I m looking for a bit of guidance on how to address security concerns with user input.

On the front end, I am using TinyMCE to accept user input. It is my understanding that TinyMCE will strip out any suspicious tags (e.g. <script>) from user input before posting to server. It seems that this could be bypassed by disabling javascript on the page, allowing a user to have free reign in the text area. TinyMCE recommends using javascript to create the TextArea. Therefore if the user disables javascript, there will be no text area. Is this the standard solution? It seems like a bit of a hack.

On the back end, what is the best way to strip out malicious code? Would I want to put some sort of validation in the create and update methods inside my comments controller? Is there some functionality built into Rails that can assist with this?

When displaying the information back out to the user, I m assuming that I don t want to escape the HTML markup (with <%= h *text*%>), because that s how its stored in the back end. Is this bad practice?

最佳回答

I m generally a big fan of cleaning out the data prior popping that stuff into the database. This is a debatable practice, but I usually lean toward this.

I use a modified version of the old white_list plugin to not strip out the html, but to convert anything I do want into a safer format.

<tag>

becomes

&lt;tag&gt;

This way I m not really altering the content of the submission.

There are some plugins that specifically handle sanitization using a white/black list model.

http://github.com/rgrove/sanitize/ # Have not used, but looks very interesting

http://github.com/imanel/white_list_model # Used, not bad

There is also act_as_sanitized, but I have no real info on that.

And of course using the h().

问题回答

Your suspicions are justified, but the creation of a text area in javascript won t make you any less vulnerable. A user could always use something like curl to force a form submission without ever visiting your site through a web browser.

You should assume that a user can post malicious scripts into the comments, and escape it on the frontend. Using <%= h(...) %> is one way to do it, or you can use the sanitize method in the same way. It will strip any scripts and escape all other html except for a few common tags that aren t harmful. Documentation for sanitize.

In addition to nowk s suggestions there is also the xss_terminate plugin. I have been using it in some of my applications. I found it to be easy to use, it needs almost no configuration, and has been working like a charm.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签