CakeDC Blog

5 CakePHP security tips

This article is part of the CakeDC Advent Calendar 2024 (December 20th 2024)

We all know the importance of security in our sites, so here we have 5 quick tips that can improve the security of your site quickly:

  • Ensure all cookies are configured for security
// config/app.php
'Session' => [
        // .. other configurations
        'cookie' => 'CUSTOM_NAME_FOR_YOUR_SESSION_COOKIE',
        'ini' => [
            'session.cookie_secure' => true,
            'session.cookie_httponly' => true,
            'session.cookie_samesite' => 'Strict',
        ],
    ],
  • Audit your dependencies
    • Both backend and frontend dependencies could be impacted by security issues. In the case of the backend, you can have a quick look by running composer audit. In case of issues, you'll see an output similar to:
$ composer audit
Found 7 security vulnerability advisories affecting 4 packages:
+-------------------+----------------------------------------------------------------------------------+
| Package           | composer/composer                                                                |
| CVE               | CVE-2024-35241                                                                   |
| Title             | Composer has a command injection via malicious git branch name                   |
| URL               | https://github.com/advisories/GHSA-47f6-5gq3-vx9c                                |
| Affected versions | >=2.3,<2.7.7|>=2.0,<2.2.24                                                       |
| Reported at       | 2024-06-10T21:36:32+00:00                                                        |
+-------------------+----------------------------------------------------------------------------------+
// in src/Application::middleware()

    // Cross Site Request Forgery (CSRF) Protection Middleware
    // https://book.cakephp.org/4/en/security/csrf.html#cross-site-request-forgery-csrf-middleware
    ->add(new CsrfProtectionMiddleware([
        'httponly' => true,
    ]));
  • Enforce HTTPS
    • Ensure your live applications are enforcing HTTPS to prevent downgrading to HTTP. You can handle that in a number of ways, for example using your webserver configuration, or a Proxy. If you want to handle it via CakePHP builtins, add
// in src/Application::middleware()

    ->add(new HttpsEnforcerMiddleware([
        'hsts' => [
            'maxAge' => 10,
            'includeSubDomains' => true,
            'preload' => false, // use preload true when you are sure all subdomains are OK with HTTPS
        ],
    ]))
// in src/Application::middleware()
    $securityHeaders = (new SecurityHeadersMiddleware())
        ->setReferrerPolicy() // limit referrer info leaked
        ->setXFrameOptions() // mitigates clickjacking attacks
        ->noOpen() // don't save file in downloads auto
        ->noSniff(); // mitigates mime type sniffing
    $middlewareQueue
        // ...
        ->add($securityHeaders)
        // ...

This is just a quick example of 5 changes in code you could apply today to improve your CakePHP website security. Keep your projects safe!

This article is part of the CakeDC Advent Calendar 2024 (December 20th 2024)

Back to all articles
We Bake with CakePHP