php 如何使用Slim 4和slim/csrf实施CSRF保护?

juud5qan  于 2023-03-07  发布在  PHP
关注(0)|答案(2)|浏览(142)

Slim 4已经在这里了,我正在尝试迁移到Slim 4。一切都很好,但是当我尝试实现它时CSRF返回了一个错误。我尝试了最简单的设置,但是我得到了这个错误:

    • 留言**:传递给Slim\Csrf\Guard::__invoke()的参数2必须是Psr\Http\Message\ResponseInterface的示例,给定的Slim\Routing\RouteRunner的示例,在第180行的/Volumes/Web/slim/vendor/slim/slim/Slim/MiddlewareDispatcher.php中调用
    • 文件**:/卷/网站/slim/供应商/slim/csrf/src/Guard.php

下面是我的代码:

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Csrf\Guard;

require __DIR__ . '/../vendor/autoload.php';

/**
 * Instantiate App
 *
 * In order for the factory to work you need to ensure you have installed
 * a supported PSR-7 implementation of your choice e.g.: Slim PSR-7 and a supported
 * ServerRequest creator (included with Slim PSR-7)
 */
$app = AppFactory::create();

$app->add(Guard::class);

// Add Routing Middleware
$app->addRoutingMiddleware();

/*
 * Add Error Handling Middleware
 *
 * @param bool $displayErrorDetails -> Should be set to false in production
 * @param bool $logErrors -> Parameter is passed to the default ErrorHandler
 * @param bool $logErrorDetails -> Display error details in error log
 * which can be replaced by a callable of your choice.

 * Note: This middleware should be added last. It will not handle any exceptions/errors
 * for middleware added after it.
 */
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

// Define app routes
$app->get('/', function (Request $request, Response $response, $args) {
    $response->getBody()->write('Hello');
    return $response;
});

// Run app
$app->run();

任何帮助都非常感谢!谢谢!

xu3bshqb

xu3bshqb1#

这个包与Slim4不兼容,我写了一个 Package 器,这样你就可以使用它了。
'

<?php

declare(strict_types=1);

namespace App\Application\Middleware;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Csrf\Guard as Guard;

class CsrfMiddleware extends Guard implements Middleware
{

    /**
     * Process middleware
     *
     * @param  ServerRequestInterface  $request  request object
     * @param  RequestHandlerInterface $handler handler object
     *
     * @return ResponseInterface response object
     */
    public function process(Request $request, RequestHandler $handler): Response
    {
        $this->validateStorage();
        // Validate POST, PUT, DELETE, PATCH requests
        if (in_array($request->getMethod(), ['POST', 'PUT', 'DELETE', 'PATCH'])) {
            $body = $request->getParsedBody();
            $body = $body ? (array) $body : [];
            $name = isset($body[$this->prefix . '_name']) ? $body[$this->prefix . '_name'] : false;
            $value = isset($body[$this->prefix . '_value']) ? $body[$this->prefix . '_value'] : false;
            if (!$name || !$value || !$this->validateToken($name, $value)) {
                // Need to regenerate a new token, as the validateToken removed the current one.
                $request = $this->generateNewToken($request);

                $failureCallable = $this->getFailureCallable();
                return $failureCallable($request, $handler);
            }
        }
        // Generate new CSRF token if persistentTokenMode is false, or if a valid keyPair has not yet been stored
        if (!$this->persistentTokenMode || !$this->loadLastKeyPair()) {
            $request = $this->generateNewToken($request);
        } elseif ($this->persistentTokenMode) {
            $pair = $this->loadLastKeyPair() ? $this->keyPair : $this->generateToken();
            $request = $this->attachRequestAttributes($request, $pair);
        }
        // Enforce the storage limit
        $this->enforceStorageLimit();

        return $handler->handle($request);
    }

    /**
     * Getter for failureCallable
     *
     * @return callable|\Closure
     */
    public function getFailureCallable()
    {
        if (is_null($this->failureCallable)) {
            $this->failureCallable = function (Request $request, RequestHandler $handler): Response {
                $response = $handler->handle($request);
                $stream = $response->getBody();
                $stream->write('CSRF fail');
                return $response->withStatus(400);
            };
        }
        return $this->failureCallable;
    }
}

'

snvhrwxg

snvhrwxg2#

相关位为:

$app->add(Guard::class);

中间件回调的签名发生了变化,在Slim/3中曾经是这样的:

public function __invoke(
    ServerRequestInterface $request,
    ResponseInterface $response,
    callable $next
): ResponseInterface

...然后该方法必须像$next($request, $response)一样调用$next
在Slim/4中是这样的:

public function __invoke(
    ServerRequestInterface $request,
    RequestHandlerInterface $handler
): ResponseInterface

..,并且对$handler的内部调用是$handler->handle($request)
该库似乎没有针对Slim/4进行更新。它在composer.json中声明Slim/3为dev(?)依赖项,并在README.md中提及。修复该库或在其上编写兼容的 Package 器可能并不困难,但如果您不熟悉整个生态系统,安装a replacement可能更容易。

相关问题