如何在CakePHP 3.4上设置CSRF cookie到同一站点?

erhoui1w  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(201)

我正在使用CakePHP 3.4(无法升级),为了保护系统免受跨站点请求伪造我需要将CSRF标记cookie设置为SameSite = Strict。但是,似乎这个版本的CakePHP无法处理这样的设置。
我尝试过使用CsrfComponent类并在AppController中加载该组件

$this->loadComponent('Csrf', [
            'secure' => true,
            'httpOnly' => true,
        ]);

如何解决将此cookie设置为SameSite = Strict或其他替代设置以防止跨站点请求伪造的问题?

q0qdq0h2

q0qdq0h21#

在CakePHP 3.9.3中添加了对带有CSRF cookie的samesite的支持,不过你必须切换到CSRF保护中间件。
如果无法升级,则需要一些自定义代码,即一个自定义/扩展的CSRF组件,它接受属性的进一步选项,以及一个自定义/扩展的响应对象,它相应地创建具有该属性的cookie。
在早于PHP 7.3的PHP版本中,您可以通过cookie路径hack来分别 * 必须 * 注入SameSite属性,这包括将更多的cookie属性附加到路径中,只需用分号将路径结束即可。在PHP 7.3之前的PHP版本中,您可以使用当时支持的samesite来表示setcookie()
顺便说一句,对于会话cookie,您需要相应地修改您的session.cookie_pathsession.cookie_samesite PHP INI选项,并且CakePHP中设置cookie的其他地方可能也需要修改,例如cookie组件,即使您的应用不使用它,它也可能被第三方插件使用。
示例:

<?php
// in src/Controller/Component/CsrfComponent.php
namespace App\Controller\Component;

use Cake\Controller\ComponentRegistry;
use Cake\Http\Response;
use Cake\Http\ServerRequest;

class CsrfComponent extends \Cake\Controller\Component\CsrfComponent
{
    public function __construct(ComponentRegistry $registry, array $config = [])
    {
        // Use Lax by default
        $config += [
            'samsite' => 'Lax',
        ];

        parent::__construct($registry, $config);
    }

    protected function _setCookie(ServerRequest $request, Response $response)
    {
        parent::_setCookie($request, $response);

        // Add samesite option to the cookie that has been created by the parent
        $cookie = $response->cookie($this->getConfig('cookieName'));
        $cookie['samesite'] = $this->getConfig('samesite');

        $response->cookie($cookie);
    }
}

http://github.com/cakephp/cakephp/blob/3.4.14/src/Controller/Component/CsrfComponent.php#L125

// in src/Http/Response.php
namespace App\Http;

class Response extends \Cake\Http\Response
{
    protected function _setCookies()
    {
        foreach ($this->_cookies as $name => $c) {
            if (version_compare(PHP_VERSION, '7.3.0', '<')) {
                // Use regular syntax (with possible path hack) in case
                // no samesite has been set, or the PHP version doesn't
                // support the samesite option.

                if (isset($c['samesite'])) {
                    $c['path'] .= '; SameSite=' . $c['samesite'];
                }

                setcookie(
                    $name,
                    $c['value'],
                    $c['expire'],
                    $c['path'],
                    $c['domain'],
                    $c['secure'],
                    $c['httpOnly']
                );
            } else {
                setcookie($name, $c['value'], [
                    'expires' => $c['expire'],
                    'path' => $c['path'],
                    'domain' => $c['domain'],
                    'secure' => $c['secure'],
                    'httponly' => $c['httpOnly'],
                    'samesite' => $c['samesite'],
                ]);
            }
        }
    }
}

http://github.com/cakephp/cakephp/blob/3.4.14/src/Http/Response.php#L540

AppController的构造函数中注入自定义响应对象:

// in src/Controller/AppController.php

use Cake\Http\Response;
use Cake\Http\ServerRequest;

// ...

class AppController extends Controller
{
    // ...

    public function __construct(
        ServerRequest $request = null,
        Response $response = null,
        $name = null,
        $eventManager = null,
        $components = null
    ) {
        if ($response !== null) {
            throw new \InvalidArgumentException(
                'This should not happen, we want to use a custom response class.'
            );
        }
        $response = new \App\Http\Response();

        parent::__construct($request, $response, $name, $eventManager, $components);
    }

    // ...
}

最后,使用自定义组件类为Csrf组件设置别名,并设置您的samesite配置:

// in src/Controller/AppController.php

// ...

class AppController extends Controller
{
    // ...

    public function initialize()
    {
        parent::initialize();

        // ...
        $this->loadComponent('Csrf', [
            'className' => \App\Controller\Component\CsrfComponent::class,
            'secure' => true,
            'httpOnly' => true,
            'samesite' => 'Strict',
        ]);
    }

    // ...
}

最后要注意的是,在以后的CakePHP 3.x版本中,cookie数组已经被cookie对象所取代,因此需要进行相应的更改,但是由于您无法升级,这应该没问题,一旦您决定升级,就应该全力以赴,升级到最新的版本。

相关问题