-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(security): enforce csrf protection
Enforce csrf protection on unsafe http requests (POST, PUT, DELETE, PATCH) refactor: create Slim app using di container
- Loading branch information
Showing
16 changed files
with
183 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (C) 2023 Davide Franco | ||
* | ||
* This file is part of Bacula-Web. | ||
* | ||
* Bacula-Web is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
* General Public License as published by the Free Software Foundation, either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Bacula-Web is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without | ||
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Bacula-Web. If not, see | ||
* <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace App; | ||
|
||
use Closure; | ||
use Psr\Http\Message\ResponseFactoryInterface; | ||
|
||
class CsrfErrorHandler | ||
{ | ||
/** | ||
* @param ResponseFactoryInterface $responseFactory | ||
* @return Closure | ||
*/ | ||
public function handle(ResponseFactoryInterface $responseFactory): Closure | ||
{ | ||
return function () use ($responseFactory) { | ||
$response = $responseFactory->createResponse()->withStatus(403); | ||
$response->getBody()->write('Invalid CSRF token, go back to <a href="/">Home page</a>'); | ||
return $response; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (C) 2010-2023 Davide Franco | ||
* | ||
* This file is part of Bacula-Web. | ||
* | ||
* Bacula-Web is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
* General Public License as published by the Free Software Foundation, either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Bacula-Web is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without | ||
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Bacula-Web. If not, see | ||
* <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace App\Middleware; | ||
|
||
use Psr\Container\ContainerExceptionInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface as RequestHandler; | ||
use Slim\Csrf\Guard; | ||
use Slim\Views\Twig; | ||
|
||
class CsrfMiddleware implements MiddlewareInterface | ||
{ | ||
private Twig $twig; | ||
private Guard $csrf; | ||
|
||
/** | ||
* @param Twig $twig | ||
* @param ContainerInterface $container | ||
* @throws ContainerExceptionInterface | ||
* @throws NotFoundExceptionInterface | ||
*/ | ||
public function __construct(Twig $twig, ContainerInterface $container) | ||
{ | ||
$this->twig = $twig; | ||
$this->csrf = $container->get('csrf'); | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* @param RequestHandler $handler | ||
* @return ResponseInterface | ||
*/ | ||
public function process(Request $request, RequestHandler $handler): ResponseInterface | ||
{ | ||
$csrfKeyName = $this->csrf->getTokenNameKey(); | ||
$csrfKeyValue = $this->csrf->getTokenValueKey(); | ||
$csrfTokenName = $this->csrf->getTokenName(); | ||
$csrfTokenValue = $this->csrf->getTokenValue(); | ||
|
||
$csrf = " | ||
<input type='hidden' name='$csrfKeyName' value='$csrfTokenName'> | ||
<input type='hidden' name='$csrfKeyValue' value='$csrfTokenValue'> | ||
"; | ||
|
||
$this->twig->getEnvironment()->addGlobal('csrf', $csrf); | ||
|
||
return $handler->handle($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.