Checklist of security precautions for Ruby on Rails applications.
Zen Rails Security Checklist
Summary
This document provides a not necessarily comprehensive list of security measures
to be implemented when developing a Ruby on Rails application. It is designed to
serve as a quick reference and minimize vulnerabilities caused by developer
forgetfulness. It does not replace developer training on secure coding
principles and how they can be applied.
Describing how each security vulnerability works is outside the scope of this
document. Links to external resources containing further information are
provided in the corresponding sections of the checklist. Please apply only the
suggestions you thoroughly understand.
Please keep in mind that security is a moving target. New vulnerabilities and
attack vectors are discovered every day. We suggest you try to keep up to date,
for instance, by subscribing to security mailing lists related to the software
and libraries you are using.
This checklist is meant to be a community-driven resource. Your
contributions are welcome!
Disclaimer: This document does not cover all possible security
vulnerabilities. The authors do not take any legal responsibility for the
accuracy or completeness of the information herein.
Supported Rails Versions
This document focuses on Rails 4 and 5. Vulnerabilities that were present in
earlier versions and fixed in Rails 4 are not included.
Table of Contents
Table of contents generated by DocToc.
The Checklist
Injection
Injection attacks are #1 at the OWASP Top10.
- Don’t use standard Ruby interpolation (
#{foo}) to insert user inputted
strings into ActiveRecord or raw SQL queries. Use the ? character, named bind
variables or the ActiveRecord::Sanitization
methods
to sanitize user input used in DB queries. Mitigates SQL injection attacks.
- Don't pass user inputted strings to methods capable of evaluating
code or running O.S. commands such as
eval, system, syscall, %x(),
open, popen<n>, File.read, File.write, and exec. Using regular
expressions is a good way to sanitize it (code sample).
Mitigates command injection attacks.
Resources:
Authentication
Broken Authentication and Session Management are #2 at the OWASP Top 10.
- Avoid rolling your own authentication unless you know exactly what you
are doing. Consider using a gem such as
Devise,
Authlogic or
Clearance. Mitigates dozens of
potential vulnerabilities.
- Enforce a minimum password length of 8 characters or more. Mitigates
brute-force attacks.
- Devise: set
config.password_length = 8..128 in
config/initializers/devise.rb.
- Consider validating passwords against:
- Dictionary words. Since passwords have a minimum length requirement, the
dictionary need only include words meeting that requirement.
- A list of commonly used passwords such as
these.
The StrongPassword gem provide
such feature.
- A leaked password database such as PasswordPing.
- Context-specific words, such as the name of the application, the
username, and derivatives thereof.
- Consider the pros and cons of enforcing password complexity rules such as
mixtures of different character types. Most applications use it. However, the
latest NIST Guidelines advise
against it. An alternative is to increase the minimum length requirement and
encourage the usage of passphrases. Mitigate brute-force attacks.
- Lock the account after multiple failed login attempts. Mitigates
brute-force attacks.
- Require users to confirm their e-mail addresses on sign-up and when the
e-mail address is changed. Mitigates the creation of bogus accounts with
non-existing or third-party e-mails.
- Devise: use the confirmable
module
and set
config.reconfirmable = true in config/initializers/devise.rb.
- Require users to input their old password on password change. Mitigates
unauthorized password changes on session hijacking, CSRF or when a user forgets
to log out and leaves the PC or mobile device unattended.
- Devise: does that by default
- Expire the session at log out and expire old sessions at every successful
login. Mitigates CSRF, session hijacking and session fixation attacks by
reducing their time-frame.
- Devise: does that by default.
- Expire sessions after a period of inactivity (e.g., 30 minutes).
Mitigates CSRF, session hijacking and session fixation attacks by reducing
their time-frame.
- Notify user via email on password change. Does not prevent an attacker
from changing the victim's password, but warns the victim so he can contact the
system administrator to revoke the attacker's access.
- Devise: set
config.send_password_change_notification = true in
config/initializers/devise.rb.
- Use generic error messages such as "Invalid email or password" instead of
specifying which part (e-mail or password) is invalid. Mitigates user
enumeration
and brute-force attacks.
- Devise: setting
config.paranoid = true in
config/initializers/devise.rb will protect the confirmable,
recoverable and unlockable modules against user enumeration. To protect
the registerable module, add a captcha to the registration page (see
instructions in the Devise
Wiki).
- Ensure all non-public controllers/actions require authentication. Avoid
unauthorized access due to developer forgetfulness.
- Devise: add
before_action :authenticate_user! to
ApplicationController and
skip_before_action :authenticate_user! to publicly accessible
controllers/actions.
- Consider using two-factor authentication (2FA) as provided by
Authy. Provides a highly effective extra layer of
authentication security.
- Consider requiring authentication in
config/routes.rb. Requiring
authentication in both controllers and routes may not be DRY, but such
redundancy provides additional security (see Defense in
depth).
- Devise: Place non-public resources within a
authenticate :user do block
(see the Devise
Wiki).
- Consider limiting the number of simultaneous sessions per account. May
reduce application exposure on account compromise (e.g. leaked passwords).
- Avoid implementing "security questions" such as "What is your mother's
maiden name?" as their answers may be reused across multiple sites and easily
found by means of social
engineering. See
this article.
- If using role-based access control (RBAC), do not include the role
attribute in the strong parameters of the controller(s) used for user
registration and profile editing. Prevent malicious users from assigning admin
role to themselves.
- Devise: Do not pass the role parameter key to
devise_parameter_sanitizer.permit.
- Consider restricting administrator access by IP. If the client's IP is
dynamic, restrict by IP block/ASN or by country via IP geolocation.
Sessions & Cookies
Broken Authentication and Session Management are #2 at the OWASP Top 10.
- Don't store data such as money/point balances or user privileges in a
cookie or a CookieStore Session. Store it in the database instead. Mitigates
replay attacks.
- Consider always using encrypted cookies. This is the default behavior in