slimphp / Slim-Csrf

Slim Framework CSRF protection middleware
MIT License
338 stars 58 forks source link

CSRF 1.5 csrf fails to press F5 and resend data #188

Closed matejik closed 5 months ago

matejik commented 5 months ago

Hi, csrf fails to press F5 and resend data. It works without $guard->setFailureHandler, but I need to catch csrf_status.

Thanks Jiri

Fatal error: Uncaught TypeError: DI\Definition\Source\DefinitionFile::{closure}(): Argument #1 ($request) must be of type ServerRequestInterface, Nyholm\Psr7\ServerRequest given, called

'csrf' => function(ContainerInterface $container) {
    $guard = new Guard($container->get(ResponseFactoryInterface::class));

    $guard->setFailureHandler(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
       $request = $request->withAttribute("csrf_status", false);
        return $handler->handle($request);
    });       

    return $guard;
},

ResponseFactoryInterface::class => function (ContainerInterface $container) {
    return $container->get(Psr17Factory::class);
},
akrabat commented 5 months ago

Does it work with Slim-Csrf v1.4?

akrabat commented 5 months ago

With this code:

$guard->setFailureHandler(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
   $request = $request->withAttribute("csrf_status", false);
    return $handler->handle($request);
});

Do you have:

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

at the top of the file?

matejik commented 5 months ago

Hi, you're right, but it's a stupid mistake. I'm converting a foreign project from Slim 3 to Slim 4, including conversion to PHP 8. I'm sorry.

akrabat commented 5 months ago

Glad you found it 👍