2021-06-06 01:18:44 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
2021-06-06 15:13:13 +02:00
|
|
|
namespace Taproot\IndieAuth\Middleware;
|
2021-06-06 01:18:44 +02:00
|
|
|
|
|
|
|
use Nyholm\Psr7\Response;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Dflydev\FigCookies;
|
2021-06-06 14:47:05 +02:00
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Psr\Log\NullLogger;
|
2021-06-06 15:13:13 +02:00
|
|
|
use function Taproot\IndieAuth\generateRandomString;
|
2021-06-06 01:18:44 +02:00
|
|
|
|
2021-06-07 20:32:02 +02:00
|
|
|
/**
|
|
|
|
* Development reference
|
|
|
|
*
|
|
|
|
* CSRF protection cheat sheet: https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html
|
|
|
|
* Example CSRF protection cookie middleware: https://github.com/zakirullin/csrf-middleware/blob/master/src/CSRF.php
|
|
|
|
*/
|
|
|
|
|
2021-06-06 14:47:05 +02:00
|
|
|
class DoubleSubmitCookieCsrfMiddleware implements MiddlewareInterface, LoggerAwareInterface {
|
2021-06-06 01:18:44 +02:00
|
|
|
const READ_METHODS = ['HEAD', 'GET', 'OPTIONS'];
|
|
|
|
const TTL = 60 * 20;
|
|
|
|
const ATTRIBUTE = 'csrf';
|
|
|
|
const DEFAULT_ERROR_RESPONSE_STRING = 'Invalid or missing CSRF token!';
|
|
|
|
const CSRF_TOKEN_LENGTH = 128;
|
|
|
|
|
|
|
|
public string $attribute;
|
|
|
|
|
|
|
|
public int $ttl;
|
|
|
|
|
2021-06-06 17:03:13 +02:00
|
|
|
public $errorResponse;
|
2021-06-06 01:18:44 +02:00
|
|
|
|
|
|
|
public int $tokenLength;
|
|
|
|
|
2021-06-06 14:47:05 +02:00
|
|
|
public LoggerInterface $logger;
|
|
|
|
|
2021-06-06 17:03:13 +02:00
|
|
|
public function __construct(?string $attribute=self::ATTRIBUTE, ?int $ttl=self::TTL, $errorResponse=self::DEFAULT_ERROR_RESPONSE_STRING, $tokenLength=self::CSRF_TOKEN_LENGTH, $logger=null) {
|
|
|
|
$this->attribute = $attribute ?? self::ATTRIBUTE;
|
|
|
|
$this->ttl = $ttl ?? self::TTL;
|
|
|
|
$this->tokenLength = $tokenLength ?? self::CSRF_TOKEN_LENGTH;
|
2021-06-06 01:18:44 +02:00
|
|
|
|
|
|
|
if (!is_callable($errorResponse)) {
|
|
|
|
if (!$errorResponse instanceof ResponseInterface) {
|
|
|
|
if (!is_string($errorResponse)) {
|
|
|
|
$errorResponse = self::DEFAULT_ERROR_RESPONSE_STRING;
|
|
|
|
}
|
|
|
|
$errorResponse = new Response(400, ['content-type' => 'text/plain'], $errorResponse);
|
|
|
|
}
|
|
|
|
$errorResponse = function (ServerRequestInterface $request) use ($errorResponse) { return $errorResponse; };
|
|
|
|
}
|
|
|
|
$this->errorResponse = $errorResponse;
|
2021-06-06 14:47:05 +02:00
|
|
|
|
|
|
|
if (!$logger instanceof LoggerInterface) {
|
|
|
|
$logger = new NullLogger();
|
|
|
|
}
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLogger(LoggerInterface $logger) {
|
|
|
|
$this->logger = $logger;
|
2021-06-06 01:18:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
|
2021-06-06 17:03:13 +02:00
|
|
|
// Generate a new CSRF token, add it to the request attributes, and as a cookie on the response.
|
2021-06-06 01:18:44 +02:00
|
|
|
$csrfToken = generateRandomString($this->tokenLength);
|
|
|
|
$request = $request->withAttribute($this->attribute, $csrfToken);
|
|
|
|
|
2021-06-06 17:03:13 +02:00
|
|
|
if (!in_array(strtoupper($request->getMethod()), self::READ_METHODS) && !$this->isValid($request)) {
|
|
|
|
// This request is a write method with invalid CSRF parameters.
|
|
|
|
$response = call_user_func($this->errorResponse, $request);
|
|
|
|
} else {
|
|
|
|
$response = $handler->handle($request);
|
|
|
|
}
|
2021-06-06 01:18:44 +02:00
|
|
|
|
|
|
|
// Add the new CSRF cookie, restricting its scope to match the current request.
|
|
|
|
$response = FigCookies\FigResponseCookies::set($response, FigCookies\SetCookie::create($this->attribute)
|
2021-06-07 20:32:02 +02:00
|
|
|
->withValue($csrfToken)
|
|
|
|
->withMaxAge($this->ttl)
|
|
|
|
->withSecure($request->getUri()->getScheme() == 'https')
|
|
|
|
->withDomain($request->getUri()->getHost())
|
|
|
|
->withPath($request->getUri()->getPath()));
|
2021-06-06 01:18:44 +02:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isValid(ServerRequestInterface $request) {
|
2021-06-06 17:03:13 +02:00
|
|
|
if (array_key_exists($this->attribute, $request->getParsedBody() ?? [])) {
|
|
|
|
if (array_key_exists($this->attribute, $request->getCookieParams() ?? [])) {
|
2021-06-06 01:18:44 +02:00
|
|
|
return hash_equals($request->getParsedBody()[$this->attribute], $request->getCookieParams()[$this->attribute]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|