How Secured is your Rails App?
What do you prefer in terms of Authentication?
Plugin – Restful Authentication (recommended) – easy to use and you can tweak it according to your requirements.
Build your own authentication. You should rarely need to do this … Restful Authentication is quite flexible.
OpenID – a universal authentication system to avoid use of multiple username and password on the Internet. OpenID is getting quite famous now-a-days.
Access Control : To easily proivde different priviliges to your users. There are a lot of cool plugins available for access control.
Centralized Authentication Server – is used to implement single login/password for your users across multiple application. It can also be used for a single sign-on system. For example, Gmail and Google Reader have a single sign-on between them.
Use Google Authentication API to let your users login using their google username and password.
More Plugins :
Alternate Solution – use hash for specifying conditions in
Conditional validation using
:if options. Checkout this cool video
Be careful using validates_uniqueness_of, it has problems when used with
:scope option. Open bug tickets :
Use :allow_blank to pass validations if value is nil or empty string
Testing Validations – do read the comments in this article
- Its easy to manage ‘nil’ values using
:allow_nil, its quite handy. For ex: set
:allow_nil => truein validates_uniqueness_of to check uniqueness of non-nil values and ignore nil values
- validates_presence_of is not required if you are using validates_format_of, unless regular expression accepts empty string.
Creating records directly from parametersWhile creating database records directly from form params, a malicious user can add extra fields into the params and manually submit the web page which will set values of fields which you do not want user to set.
Alternate Solution – Trim the parameters to keep the required keys and remove the others.
hide_action : If non-action controller methods must be public, hide them using hide_action.
Be careful of bypassing private and protected using meta-programming
Always authorize user request. By tweaking form parameters or url a user can send request to view/modify other users information if there is no proper authorization of parameters.
For example :
1 2 3 4 5 6 7 8
## To find information of an order which belongs to a particular user. #Incorrect : @order = Order.find(order_id) #Correct : @order = @user.orders.find(order_id)
Do not ignore hidden fields – a user can easily modify their value, so suspect them similar to params[:id]
Prevent logs of sensitive unencrypted data using
#filter_parameter_logging in controller. The default behavior is to log request parameters in production as well as development environment, and you would not like logging of password, credit card number, etc.
In a CSRF attack, the attacker makes victim click on a link of his choice which would contain a GET/POST request and causes web application to take malicious action. The link could be embedded in a iframe or an img tag. Its recommended to use secret token while communicating with user to avoid this attack.
Its little complex to understand this attack. So, only those readers who are very enthusiastic to know about it, please read the Description below. Rest can directly move ahead to use the plugin.
Use Get and Post appropiately (note : Both get and post are vulnerable to CSRF)
Example – Gmail CSRF security flaw
Plugin – CSRF Killer (recommended) – it requires edge rails
If an attacker has session-id of your user, he can create HTTP requests to access user account. An attacker can get session-id by direct access to user machine or is able to successfully run malicious scripts at user machine. In this section we will talk about how to avoid or minimize the risk if attacker has user session-id. Following steps are helpful:
- Store IP Address, but creates problem if user moves from one network to another.
- Create a new session everytime someone logs in.
- Expire session on user logout, user is idle for a time period or on closing of browser/tab. For maximum security expire sessions on all the three conditions.
Code for session expiry on timeout
1 2 3 4 5 6 7 8 9 10 11 12
## Timeout after inactivity of one hour. MAX_SESSION_PERIOD = 3600 before_filter :session_expiry def session_expiry reset_session if session[:expiry_time] and session[:expiry_time] < Time.now session[:expiry_time] = MAX_SESSION_PERIOD.seconds.from_now return true end
Plugin – Session Expiration for session expiry on timeout
Do not put expiry time in the cookie unless your cookie information is properly encrypted. If not, use server side session expiry.
Persistent session / login in rails – global setting in enviornment.rb
ActionController::Base.session_options[:session_expires] = <i>say after two years</i>
Persistent session / login in rails – to give your users a feature – remember me
Avoid access to your website from IP addresses which are present in DNS Blacklist(DNSBL).
Plugin – DNSBL check
Page caching does bypass any security filters in your application. So avoid caching authenticated pages and use action or fragment caching instead.
How secured is your view?
Cross site scripting(XSS) attack
Can we avoid tedious use of h() in views?
Sanitize() is used to escape script tags and other malicious content other than html tags. Avoid using it … its unsecure. Use white_list instead.
When using Captcha do ensure the following :
- Images are rendered on webpage using
send_dataand are not stored at the server, because its not required to store images and are redundant.
- Avoid using algorithm used by standard Catpcha plugins as they can easily be hacked, instead tweak an existing algorithm or write your own.
- Use a Captcha which does not store secret code or images in filesystem, as you will have trouble using Captcha with multiple servers.
Tutorial – a nice article on concepts of captcha
Plugin – ReCaptcha (recommended)
Plugin – BrainBuster – a logic captcha based on simple puzzles, math and word problems. By default, it has limited set of problems and you would have to come up with large set of your own problems.
Plugin – Simple Captcha (not recommended) as it breaks all the must have features of a good Captcha implementation.
Captcha with Multiple Servers
Mailto links in a webpage can be attacked by e-mail harvesting bots. Use the plugin CipherMail to generate a 1024 bit random key and obfuscate the mailto link.
Plugin – CipherMail
A lot of people have used password strength evaluators simply because its used by google in their registration form. You can use it to help your users register with strong password. But I don’t think its a must have security addon. Uptill now I have not found a good algorithm to assess strength of a password, but some of them are reasonable.
Also, if there is an open source tool or algorithm for evaluating password strength, it can easily be broken. So, you might consider tweaking the algorithm or building one from scratch.
Mongrel, rails, apache and SSL
Controller in SSL subdomain
Sample SSL code in rails
Be very careful when you allow your users to upload files and make them available for other users to download.
Must read – Section 26.7 of Agile web development with rails – 2nd edition
In place file upload
3 plugins for file upload reviewed at :