Hardening Checklist
Check these points before you run a CakePHP 2 application in production. Each item links to the page of this book that describes the setting.
Disable Debug Mode
Set debug to 0 in production. With a higher debug level, error messages, stack traces and SQL queries are shown to visitors. Applications created from the pieceofcake2/app skeleton read the level from the APP_DEBUG environment variable and use level 0 when it is not set, so do not set APP_DEBUG (or set APP_DEBUG=0) on the production server. See Update core.php.
Use Your Own Salt and Cipher Seed
Set Security.salt and Security.cipherSeed to secret values that are unique to your application, not the values shipped with the skeleton. The salt is used to sign CSRF tokens and form tampering tokens, and is the key of CookieComponent when Cookie.key is not set or empty. The skeleton reads the values from the SECURITY_SALT and SECURITY_CIPHER_SEED environment variables. CakePHP shows a notice about the default salt only with debug on and AuthComponent loaded, so its absence in production proves nothing: check the configuration of production with pieceofcake2/security-sast. See Security.salt and Security.cipherSeed in Core Configuration and Environment Variables.
Changing the salt of an application in use breaks what it signed or hashed: the passwords of the default Simple password hasher (no user can log in), the values of Security::encrypt(), the sessions with Session.checkAgent (the default), the cookies whose key is the salt, and the forms that are open. Move the passwords to the Blowfish hasher first; see cakephp2-config-salt for the steps.
Keep Dependencies Up to Date
Security fixes are released only for the latest release (see Supported Versions). Update pieceofcake2/cakephp, the plugins and the other packages regularly with Composer, and install them without development dependencies in production:
composer update
composer install --no-dev --optimize-autoloaderTo know when a package you use gets a published vulnerability, run pieceofcake2/security-sca every week; see Security Checks.
Keep composer/ca-bundle up to date as well; it provides the CA certificates for HTTPS connections made by HttpSocket. See Security Fixes and Install dependencies for production.
Use SecurityComponent for Forms and CSRF
Load SecurityComponent in controllers that handle forms. It checks forms for tampering, and its CSRF protection ($csrfCheck, enabled by default) validates a token on every request except GET, HEAD and OPTIONS. Do not disable the checks for whole controllers; unlock only the actions or fields that need it.
Do not build view, layout or element names or Xml::build() input from user input without checking it (see Security Fixes).
Validate Input
Validate every field from the request on the server with model validation rules, and save user data with validation enabled. Use allow lists and length limits, limit the saved fields with fieldList, and since v2.12.6 reject invalid UTF-8 with the utf8 rule. See Validate Input and Data Validation.
Escape Output and Sanitize HTML
Escape every value you output with h() (templates do not escape automatically), encode values for URLs and JavaScript, and pass HTML from users through a maintained sanitizer such as Symfony HtmlSanitizer or HTML Purifier. Add a Content Security Policy as a second layer. See Output Escaping and HTML Sanitization.
Secure Cookies and Sessions
- CakePHP sets
session.cookie_httponlyfor the session cookie, and setssession.cookie_securewhen the request is made over HTTPS, unless you set these ini values yourself. Other session ini settings, such assession.cookie_samesite, can be set inSession.ini. See Sessions. CookieComponentdoes not set thesecureandhttpOnlyflags by default. Set$this->Cookie->secure = true;so that cookies are sent only over HTTPS, and$this->Cookie->httpOnly = true;for cookies that JavaScript does not need to read. See Cookie.- Encrypt cookies with
'aes'instead of the default'cipher': setCookie.typeto'aes',Cookie.keyto a random value of at least 32 bytes from theCOOKIE_KEYenvironment variable, andSecurity.useOpenSsltotrueinconfig/core.php(v2.12.6 and later). Applications created from the skeleton v2.4.1 or later have these lines already. Existing'cipher'cookies are lost once when you switch. See Weak 'cipher' encryption. - Send
Cache-Control: no-storewith the pages that have a session: setSession.cacheLimiterto'nocache'inconfig/core.php. The default,must-revalidate, is a value that PHP does not know, so PHP sends no cache header, and a browser can show the pages of a logged-in user again after the logout, or a shared cache can store them. Applications created from the skeleton v2.5.3 or later have this setting already; pieceofcake2/security-sast reports it ascakephp2-config-session-cache-limiter. See Sessions.
Restrict File Permissions
Set the document root to the webroot/ directory, so that the application code, the configuration and vendor/ cannot be accessed through a URL. The web server needs write access only to tmp/ and logs/. See Set document root, Permissions and URL Rewriting.
Use HTTPS
Serve the application over HTTPS only. The session cookie is marked secure when CakePHP detects HTTPS (the HTTPS server variable). Behind a proxy or a load balancer that ends HTTPS, CakePHP does not see it: set 'session.cookie_secure' => 1 in the Session.ini of production. Actions can require HTTPS with SecurityComponent::requireSecure(); see Restrict actions to SSL. Configure TLS and redirects in the web server; see URL Rewriting and Deployment.
Use a Supported PHP Version
pieceofcake2/cakephp runs on PHP 8.0 to 8.5 (see Requirements). Use a PHP version that still receives security fixes from the PHP project; see Supported Versions on php.net.
