Symfony Configuration Cheat Sheet27 Sep 2018 by Nils Werner
Symfony is one of the most widely used PHP frameworks with many components and options. Our Symfony Configuration Cheat Sheet shows how to ensure a secure baseline for your framework in 10 steps.
The Symfony framework provides web developers with a great foundation for their PHP applications. Several components can be used for many recurring tasks that are required in every application, such as handling input forms or accessing a database. In addition to functional tasks, security-relevant tools such as user management or protection mechanisms against popular web attacks are available. Our cheat sheet provides an overview of the 10 most important settings you should verify in your Symfony configuration.
1. Use Strong Database Credentials
Do not use the root user for your database connection and choose a strong password that is long and secure. Create different users for different applications. Use environment variables for secrets.A database, whether as a server or SQLite binary, often contains the most sensitive data of your users and customers. You have to make sure that this data is stored securely. The first steps are secure secrets and no default values as credentials when configuring the database.
2. Enable CSRF Protection
Enable the built-in CSRF protection globally. If necessary, disable protection only for specific form controller.Cross-Site Request Forgery (CSRF) is an often forgotten vulnerability in web applications. It allows attackers to submit requests in the name of other users to impersonate their privileges. With an activated CSRF protection, a secret token prevents that attackers can immitate arbitrary requests.
3. Use Strong Hashing Algorithms
It is important that your passwords are secure, even if your database is leaked. Use a strong hashing algorithm and never save the passwords as plaintext.A strong algorithm, such as bcrypt, makes it very difficult for an attacker to deduce the plaintext password from the password hash.
4. Avoid Hardcoded Credentials
Do not store user data in files located on the server. Internal and external attackers can steal hardcoded credentials and these are hard to manage on production systems.
5. Use Data Validation
Use the input validation options of Symfony. These allow to check the correctness of user data in forms or database entries and to reject malicious input.It often helps to use annotations in your code. These can also be used to define validation for properties.
6. Enforce HTTPS
Always ensure that controllers that process or display user data can only be accessed via a secure protocol.This configuration prevents that Man-in-the-Middle attackers can read sensitive data when transmitted over insecure networks.
7. Set Global Access Control
Make sure to set access permissions in the global security configuration for all controllers, following the least-privilege design principle.Setting access rights in each individual controller can cause you to forget a path or controller so that an unauthorized user can have access to sensitive data. Always whitelist, not blacklist to capture all cases.
8. Select Random App Secret
Use a long, random and unique string as secret. Never use the same secret for two different apps.The secret is used to create unique CSRF tokens, but it is also used for other elements where a unique and random string is required. The secret can also be stored in an env variable.
9. Review CORS headers
Cross-Origin Resource Sharing helps you if you want to load content and scripts from other servers. If you work with CORS, use it wisely and limit it as much as you can.The NelmioCorsBundle offers Symfony the possibility to allow sources for CORS. Again it is recommended to use environment variables and always whitelist, not blacklist to capture all cases.
10. Avoid Outdated Dependencies
Use tools to continuously check your dependencies for known and new vulnerabilities.Complex applications nowadays consist of more public libraries and dependencies than own lines of code. Since your security relies on these external packages, you should regulary check their security. You can use the security checker from Symfony. Load and use it with composer as follows:
Our cheat sheet is designed to help users that are new to the Symfony framework to verify if their configuration is secure. But also advanced users can use this guide to double check if any settings were misconfigured over time, especially those that are secure by default.
There are, of course, other configuration options that need to be considered. With the help of static analysis solutions, framework configuration files can be automatically scanned for all subtle misconfigurations. More importantly, static analysis can detect unknown, critical vulnerabilities in your custom code that is build on top of the Symfony framework.