Securing Rails, Django and Laravel applications
When a Rails, Django or Laravel application starts, it reads a small set of settings that decide whether errors are shown to visitors in full, which secret key signs and encrypts cookies and sessions, and which host names and connections the application accepts. Several of them are meant to differ between development and production, and the production values have to be in place before the application faces the internet.1 A wrong value in one of them is a security misconfiguration.
Steps
1. Turn debug mode off
In debug mode a framework answers an error with a page that holds the stack trace and much of the program's state. Django's version shows "excerpts of your source code, local variables, settings, libraries used".1 A Laravel site left in debug mode in production risks "exposing sensitive configuration values" to its users.2 In Rails, the same switch also makes /rails/info/properties show the application's runtime context.3
| Framework | Setting | Production value |
|---|---|---|
| Django | DEBUG in settings.py | False |
| Laravel | APP_DEBUG in .env, read by config/app.php | false |
| Rails | config.consider_all_requests_local in config/environments/production.rb | false (the default) |
Laravel's default config/app.php reads env('APP_DEBUG', false), so debug is off when the variable is missing.4 Rails sets the flag to true only in the development and test environments.3
2. Generate a random secret key and keep it out of the code
Each framework derives its signing and encryption from one secret. In Django it is SECRET_KEY, which signs sessions, password reset tokens and other signed data. Running with a key that others know "can lead to privilege escalation and remote code execution".5 In Laravel it is APP_KEY; Laravel encrypts every cookie with it, session cookies included, and signs the result with a MAC so that it cannot be altered.6 In Rails it is secret_key_base, from which the keys for encrypted and signed cookies are derived. By default Rails keeps the session itself inside an encrypted cookie, which the client cannot read or change without breaking the encryption. That protection lasts only while the key stays secret.7
Create the key with the framework's own command and load it from outside the source tree:
# Django: settings.py
import os
SECRET_KEY = os.environ["SECRET_KEY"]
# Laravel: writes APP_KEY into .env
php artisan key:generate
# Rails: prints a new random value
bin/rails secretThe production key should be used nowhere else.1 Rails stores secret_key_base in config/credentials.yml.enc, encrypted with config/master.key or the RAILS_MASTER_KEY environment variable. The encrypted file may go into version control; the master key may not.7
3. Rotate a key that has leaked
Replacing the key invalidates what it signed. Django lists sessions and password reset tokens; Rails adds encrypted cookies, signed cookies and Active Storage files; Laravel logs every user out.576 Django and Laravel accept old keys for reading during a changeover, through SECRET_KEY_FALLBACKS and APP_PREVIOUS_KEYS. New data is signed with the current key only. Remove an old key from the fallback list once the sessions and tokens that used it have expired.56 The general procedure is in Responding to a leaked API key.
4. Keep the environment file out of the web root and the repository
Laravel's .env holds the database password, mail credentials and APP_KEY, and it does not belong in a commit.4 The web server's document root must be the public directory, as in root /srv/example.com/public; for Nginx, because serving the project root exposes its configuration files to the internet.2 Where a team wants the file under version control, php artisan env:encrypt writes an encrypted copy, .env.encrypted, and prints the key that decrypts it.4 The wider problem is described in Exposed sensitive files.
5. Serve only over HTTPS and mark cookies Secure
Anyone on the same network can read a session cookie sent over plain HTTP and reuse it for Session hijacking.7 In Rails, config.force_ssl = true redirects every request to HTTPS and adds the HSTS header.7 Django ships SESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE as False, SECURE_SSL_REDIRECT as False and SECURE_HSTS_SECONDS as 0; each is set by hand once HTTPS works.51 Laravel's session file reads SESSION_SECURE_COOKIE from the environment, with no default, and sets HttpOnly and SameSite lax by default.8 Django's session cookie is HttpOnly by default.5
# Django: settings.py
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000
# Laravel: .env
SESSION_SECURE_COOKIE=true
# Rails: config/environments/production.rb
config.force_ssl = trueBehind a load balancer that ends TLS, Rails sees plain HTTP; config.assume_ssl = true tells it the request arrived over HTTPS.3
6. List the host names the application answers to
Django refuses to run with DEBUG = False until ALLOWED_HOSTS is set, and uses it against some CSRF attacks.1 Rails checks the Host header only in development unless config.hosts is filled in for production.3
# Django
ALLOWED_HOSTS = ["example.com"]
# Rails
config.hosts << "example.com"7. Leave CSRF protection on
New Rails applications place a security token in every form they generate and verify it on the server.7 Code that turns the check off for a route reopens that route to Cross-site request forgery.
8. Run the framework's own checks
In Django, python manage.py check --deploy adds checks that only matter in a deployment; run it against the production settings or on the server itself.9 Rails filters chosen request parameters out of its logs with config.filter_parameters << :password, which writes [FILTERED] in their place.7
Androxgh0st
On 16 January 2024 the FBI and CISA described Androxgh0st, Python malware run from a botnet that searches for Laravel sites. It requests /.env to read credentials for mail services and AWS, and also sends a POST request to the same address, a method the advisory links to sites "in debug mode". With a site's application key, it encrypts PHP code and sends it in the XSRF-TOKEN cookie.10 That exploits CVE-2018-15133: Laravel up to 5.5.40, and 5.6.x up to 5.6.29, passed the decrypted value to PHP's unserialize, which can run code. The attack works only with the application key in hand.11 With stolen AWS keys, the operators created new users and started servers for more scanning. Among its mitigations, the advisory tells operators to keep live Laravel sites out of debug mode and to revoke cloud credentials stored in .env files.10
Prompt for a coding agent
Check the production configuration of this Rails, Django or Laravel project. 1) Debug mode must be off in production: DEBUG = False (Django), APP_DEBUG=false (Laravel), config.consider_all_requests_local = false (Rails). 2) The secret key (SECRET_KEY, APP_KEY or secret_key_base) must come from an environment variable or the encrypted credentials file, never from a committed file; list any key you find in the repository or its history so I can rotate it. 3) .env and config/master.key must be in .gitignore, and the web server root must be the public directory. 4) Session and CSRF cookies must be Secure, and the app must redirect to HTTPS (SESSION_COOKIE_SECURE, CSRF_COOKIE_SECURE, SECURE_SSL_REDIRECT, SECURE_HSTS_SECONDS; SESSION_SECURE_COOKIE; config.force_ssl). 5) Set ALLOWED_HOSTS or config.hosts to the real domain. 6) Report every place where CSRF protection is switched off. For Django, run python manage.py check --deploy with the production settings and fix what it reports. List each file and setting you changed.
In SecFlash
A site that answers with framework debug output shows on the Security page as SF-SEC-636 or SF-SEC-637, and on the Exposure page as SF-EXP-206 for stack traces, SF-EXP-205 for reachable debug pages and SF-EXP-405 when the page data says debug mode is on. A readable .env file is SF-SEC-617 on the Security page and SF-EXP-307 on the Exposure page. On the Security page, session cookies without HttpOnly, Secure or SameSite appear as SF-SEC-501, code that switches those flags off as SF-SEC-761, code that turns off the CSRF check as SF-SEC-739, and a missing HSTS header as SF-SEC-101.
See also
- Category: Security misconfiguration
- Exposed sensitive files
- Session management
- Cross-site request forgery
- HTTP security headers
- Secrets management
- Responding to a leaked API key
- Category: Guides
References
- a b c d e Django Software Foundation (n.d.). "Deployment checklist". Django 5.2 documentation.
- a b Laravel (n.d.). "Deployment". Laravel 12.x documentation.
- a b c d Rails (n.d.). "Configuring Rails Applications". Rails Guides.
- a b c Laravel (n.d.). "Configuration". Laravel 12.x documentation.
- a b c d e Django Software Foundation (n.d.). "Settings". Django 5.2 documentation.
- a b c Laravel (n.d.). "Encryption". Laravel 12.x documentation.
- a b c d e f g Rails (n.d.). "Securing Rails Applications". Rails Guides.
- back Laravel (n.d.). "config/session.php". laravel/laravel on GitHub.
- back Django Software Foundation (n.d.). "django-admin and manage.py". Django 5.2 documentation.
- a b FBI and CISA (16 January 2024). "Known Indicators of Compromise Associated with Androxgh0st Malware" (AA24-016A). Cybersecurity and Infrastructure Security Agency.
- back NIST (9 August 2018). "CVE-2018-15133". National Vulnerability Database.
External links
- Deployment checklist, Django documentation
- Deployment, Laravel documentation
- Securing Rails Applications, Rails Guides