Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
Z

zen-rails-security-checklist

> 后端框架
Open source

Checklist of security precautions for Ruby on Rails applications.

1.8K stars0 likes1 views
WebsiteGitHub

About

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

  • The Checklist
    • Injection
    • Authentication
    • Sessions & Cookies
    • Cross-Site Scripting (XSS)
      • Handling User Input
      • Output Escaping & Sanitization
      • XSS protection in HAML templates
      • Content Security Policy (CSP)
    • Insecure Direct Object Reference
    • HTTP & TLS
      • Security-related headers
    • Memcached Security
    • Authorization (Pundit)
    • Files
      • File Uploads
      • File Downloads
    • Cross-Site Request Forgery (CSRF)
    • Cross Origin Resource Sharing (CORS)
    • Sensitive Data Exposure
      • Credentials
    • Routing, Template Selection, and Redirection
    • Third-party Software
    • Security Tools
    • Testing
    • Others
  • Details and Code Samples
    • Command Injection example
    • Password validation regex
    • Pundit: ensure all actions are authorized
    • Pundit: only display appropriate records in select boxes
    • rack-cors configuration
    • Convert filter_parameters into a whitelist
    • Throttling Requests
    • HAML: XSS protection
  • Authors
  • Contributing
  • TODO
  • References and Further Reading
  • License

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:

  • Ruby on Rails Security Guide - SQL Injection
  • Rails SQL Injection Examples
  • Step Up the Security of Your Rails App | Part 1

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.
    • Devise: use a Devise-specific gem such as devise-security, devise_zxcvbn or one of the following authentication-agnostic solutions.
    • The StrongPassword gem or a regex validation (code sample) should work with most authentication setups.
  • Lock the account after multiple failed login attempts. Mitigates brute-force attacks.
    • Devise: activate the lockable module.
  • 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.
    • Devise: use the timeoutable module.
  • 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.
    • Devise: see the devise-two-factor and authy-devise gems.
  • 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).
    • Devise: use the devise-security gem.
  • 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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubychecklistrailsrubyruby-on-rails

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架