Switchyard

Insecure configuration

Production config that only looks like production

Verbose errors, open redirects, and missing defaults, usually from an app that’s been upgraded since Rails 5 without ever adopting the new ones.

In September 2015, someone pulled Patreon’s database and source code (about 2.3 million accounts’ worth of email addresses, mailing addresses, and private messages) and posted all of it online. The way in was a debug server running Patreon’s Flask app with the Werkzeug debugger switched on and reachable from the internet. That debugger gives anyone who can trigger an error page a Python console on the server, and this server held a snapshot of the production database. Detectify says it had been visible on Shodan for weeks. Rails has its own version of that debugger in two gems; more on those below.

New Rails apps ship with pretty sane production settings. Apps that started on Rails 5 and got upgraded one version at a time often don’t, because rails app:update hands you the new defaults as an opt-in new_framework_defaults file and most teams never flip them on. Check config.load_defaults in config/application.rb. If it says 5.2 and you’re running Rails 7, keep reading.

The basic bug

config.consider_all_requests_local = true

This renders the full developer error page, with the backtrace, source snippets, and request parameters, to anyone who manages to raise an exception. It gets switched on to debug one production issue and then nobody switches it off.

The basic fix

config.consider_all_requests_local = false

If you need production errors, that’s what your error tracker is for.

The hardcore version

def create
  user = User.authenticate_by(email: params[:email], password: params[:password])
  return redirect_to new_session_path, alert: "Try again." unless user

  reset_session
  session[:user_id] = user.id
  redirect_to params[:return_to] || root_path
end

Send someone a link to your real login page with ?return_to=https://yourapp-login.example on the end. They check the domain, it’s yours, they sign in, and you bounce them to a pixel-perfect copy of your app that asks them to "confirm" their password. Rails 7.0 added raise_on_open_redirects, which makes redirect_to refuse other hosts. It’s on in new apps through load_defaults 7.0 and off in every upgraded app that skipped the defaults file.

The Host header version

class ApplicationController < ActionController::Base
  before_action :set_mailer_host

  private

  def set_mailer_host
    ActionMailer::Base.default_url_options[:host] = request.host_with_port
  end
end

This shows up when one app serves a few domains and someone wants emails to link back to whichever one the user came from. request.host is whatever the Host header says, though, and production accepts any Host by default: config.hosts is empty in production, which turns host checking off. So a password reset requested with a forged Host header sends the real user a real email with a reset link pointing at somebody else’s server. They click it, and the token goes with them. (It also writes to a global from inside a request, which is a race between threads. Bonus bug.)

config.action_mailer.default_url_options = { host: "yourapp.com", protocol: "https" }
config.hosts << ".yourapp.com"
config.host_authorization = { exclude: ->(request) { request.path == "/up" } }

Mail links come from config, never from the request. With config.hosts set, a request for a host you don’t recognize gets a 403 before it reaches your code (the leading dot allows the domain and its subdomains). The exclude keeps the load balancer’s health check working, since it usually hits the box by IP. If you really do serve a pile of domains, look the tenant up by host and build URLs from the domain you have stored for that tenant.

The shared secrets version

$ RAILS_ENV=staging bin/rails runner 'puts Rails.application.secret_key_base[0, 12]'
4f1c09ab77d2
$ RAILS_ENV=production bin/rails runner 'puts Rails.application.secret_key_base[0, 12]'
4f1c09ab77d2

Staging got cloned from production on day one, environment variables and all, and nobody regenerated the secret. secret_key_base is the root of almost everything Rails signs or encrypts: session cookies, signed and encrypted cookies, signed global IDs, Active Storage URLs, and tokens from generates_token_for. So a session cookie minted on staging is a valid session cookie on production. Everyone with a staging login (contractors, QA, whoever has the seed password) is a few minutes away from being user 1 in production, and user 1 is usually an admin.

$ bin/rails secret

One secret per environment, generated fresh, never copied. Changing it logs everyone out and invalidates outstanding reset links and signed URLs, so do it on purpose at a quiet hour. Rails can rotate secrets gracefully to keep old cookies working; here, old cookies working is the problem.

Guessable secrets have gone badly before. CVE-2019-5420: Rails before 5.2.2.1 derived the development-mode secret_key_base from something an attacker could figure out, and with the secret in hand they could get to remote code execution. Only apps running in development mode were exposed, which sounds like nobody until you count the staging servers running RAILS_ENV=development because the error pages are nicer. It’s in Metasploit.

Fix #1: Only redirect to your own host

redirect_to url_from(params[:return_to]) || root_path

url_from returns the URL if it points at the current host and nil otherwise. One line, and the phishing link lands on your dashboard.

Fix #2: Turn on the new defaults

config.load_defaults 8.0

Don’t flip this in one commit on an old app. Generate the defaults file for each version you skipped, turn the settings on one at a time, and run the suite between each. The security-relevant ones are worth doing first: raise_on_open_redirects in 7.0, the SHA256 key generator in 7.0, SameSite=Lax cookies in 6.1, and raise_on_assign_to_attr_readonly in 7.1. Then grep for allow_other_host: true, which opts back out one call at a time.

Fix #3: Refuse to boot with bad settings

if Rails.env.production?
  config = Rails.application.config

  raise "consider_all_requests_local is on" if config.consider_all_requests_local
  raise "force_ssl is off" unless config.force_ssl
  raise "open redirects are allowed" unless config.action_controller.raise_on_open_redirects
end

Environment files load before initializers, so this sees the final production values. Somebody flips consider_all_requests_local on to debug something, deploys, and the deploy fails instead of quietly shipping. Boring, which is the goal. Add a line every time you find a new way to misconfigure the app.

That guard only runs in production, and Patreon’s box wasn’t production. It just had production’s data on it. web-console and better_errors both give whoever sees the error page a live Ruby console. They only answer requests from localhost by default, and the usual way they get loose is a line like this, added to make them work inside Docker:

config.web_console.permissions = "0.0.0.0/0"

Allow your Docker network instead of the whole internet (docker network inspect will tell you the range), and keep both gems in the development group so they don’t exist anywhere else:

group :development do
  gem "web-console"
  gem "better_errors"
end

Then keep production data off any machine that doesn’t sit behind production’s firewall. A debug box with a copy of the real database is production, whatever its RAILS_ENV says.

Secure your Rails app today.

Find and fix critical vulnerabilities in minutes.

Inquire