# 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 {#debug}

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](https://pieceofcake2.github.io/docs/deployment.md#update-core-php).

## Use Your Own Salt and Cipher Seed {#salt}

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](https://pieceofcake2.github.io/security-sast/). See `Security.salt` and
`Security.cipherSeed` in [Core Configuration](https://pieceofcake2.github.io/docs/development/configuration.md#cakephp-core-configuration)
and [Environment Variables](https://pieceofcake2.github.io/docs/development/configuration.md#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](https://pieceofcake2.github.io/security-sast/rules#cakephp2-config-salt) for the
steps.

## Keep Dependencies Up to Date {#dependencies}

Security fixes are released only for the latest release (see
[Supported Versions](https://pieceofcake2.github.io/docs/security/index.md#supported-versions)). Update
`pieceofcake2/cakephp`, the plugins and the other packages regularly with
Composer, and install them without development dependencies in production:

```bash
composer update
composer install --no-dev --optimize-autoloader
```

To know when a package you use gets a published vulnerability, run
[pieceofcake2/security-sca](https://pieceofcake2.github.io/security-sca/)
every week; see [Security Checks](https://pieceofcake2.github.io/docs/security/checks.md).

Keep `composer/ca-bundle` up to date as well; it provides the CA certificates
for HTTPS connections made by `HttpSocket`. See
[Security Fixes](https://pieceofcake2.github.io/docs/security/fixes.md) and
[Install dependencies for production](https://pieceofcake2.github.io/docs/deployment.md#install-dependencies-for-production).

## Use SecurityComponent for Forms and CSRF {#security-component}

Load [SecurityComponent](https://pieceofcake2.github.io/docs/core-libraries/components/security-component.md) 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](https://pieceofcake2.github.io/docs/security/fixes.md)).

## Validate Input {#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](https://pieceofcake2.github.io/docs/security/escaping-and-sanitizing-html.md#validate-input) and
[Data Validation](https://pieceofcake2.github.io/docs/models/data-validation.md).

## Escape Output and Sanitize HTML {#escape-output}

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](https://pieceofcake2.github.io/docs/security/escaping-and-sanitizing-html.md).

## Secure Cookies and Sessions {#cookies-and-sessions}

- CakePHP sets `session.cookie_httponly` for the session cookie, and sets
  `session.cookie_secure` when the request is made over HTTPS, unless you set
  these ini values yourself. Other session ini settings, such as
  `session.cookie_samesite`, can be set in `Session.ini`. See
  [Sessions](https://pieceofcake2.github.io/docs/development/sessions.md).
- `CookieComponent` does not set the `secure` and `httpOnly` flags 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](https://pieceofcake2.github.io/docs/core-libraries/components/cookie.md).
- Encrypt cookies with `'aes'` instead of the default `'cipher'`: set
  `Cookie.type` to `'aes'`, `Cookie.key` to a random value of at least 32
  bytes from the `COOKIE_KEY` environment variable, and `Security.useOpenSsl`
  to `true` in `config/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](https://pieceofcake2.github.io/docs/security/fixes.md#cipher).
- Send `Cache-Control: no-store` with the pages that have a session: set
  `Session.cacheLimiter` to `'nocache'` in `config/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 as `cakephp2-config-session-cache-limiter`. See
  [Sessions](https://pieceofcake2.github.io/docs/development/sessions.md).

## Restrict File Permissions {#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](https://pieceofcake2.github.io/docs/deployment.md#set-document-root),
[Permissions](https://pieceofcake2.github.io/docs/installation.md#permissions) and
[URL Rewriting](https://pieceofcake2.github.io/docs/installation/url-rewriting.md).

## Use HTTPS {#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](https://pieceofcake2.github.io/docs/core-libraries/components/security-component.md#restrict-actions-to-ssl).
Configure TLS and redirects in the web server; see
[URL Rewriting](https://pieceofcake2.github.io/docs/installation/url-rewriting.md) and [Deployment](https://pieceofcake2.github.io/docs/deployment.md).

## Use a Supported PHP Version {#php-version}

`pieceofcake2/cakephp` runs on PHP 8.0 to 8.5 (see
[Requirements](https://pieceofcake2.github.io/docs/installation.md#requirements)). Use a PHP version that still
receives security fixes from the PHP project; see
[Supported Versions](https://www.php.net/supported-versions.php) on php.net.
