0

我正在使用 CakePHP 3.4(无法升级),为了保护系统免受跨站点请求伪造,我需要将 CSRF 令牌 cookie 设置为 SameSite = Strict。但是,这个版本的 CakePHP 似乎不能处理这样的设置。

我尝试使用 CsrfComponent 类并在 AppController 中加载组件

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

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

4

1 回答 1

1

在 CakePHP 3.9.3 中添加了对带有 CSRF cookie 的相同站点的支持,但您必须切换到 CSRF 保护中间件。

如果您无法升级,那么您将使用一些自定义代码,即一个自定义/扩展 CSRF 组件,该组件接受该属性的更多选项,以及一个自定义/扩展响应对象,该对象相应地创建具有该属性的 cookie。

在 PHP 7.3 之前的 PHP 版本中,您可以分别通过使用 cookie 路径 hack注入SameSite属性,其中包括将更多的 cookie 属性附加到路径,只需用分号关闭路径即可。在 PHP 7.3 以上的 PHP 版本中,您将使用当时支持samesitesetcookie().

顺便说一句,对于会话 cookie,您需要相应地修改您的session.cookie_pathsession.cookie_samesitePHP INI 选项,而 CakePHP 中设置 cookie 的其他地方也可能需要调整,例如cookie 组件,即使您的应用程序不使用它,它也可能由 3rd 方插件使用。

例子:

<?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);
    }
}

https://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'],
                ]);
            }
        }
    }
}

https://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 对象替换,因此需要相应地进行更改,但是由于您无法升级,这应该没问题,一旦您决定升级,拍摄星星并升级到最新版本。

于 2021-04-23T15:19:40.713 回答