xref: /webtrees/app/Http/Middleware/CheckCsrf.php (revision 901ade1953ff80475c76de02812ca766c7a64210)
1ccb0284cSGreg Roach<?php
23976b470SGreg Roach
3ccb0284cSGreg Roach/**
4ccb0284cSGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6ccb0284cSGreg Roach * This program is free software: you can redistribute it and/or modify
7ccb0284cSGreg Roach * it under the terms of the GNU General Public License as published by
8ccb0284cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ccb0284cSGreg Roach * (at your option) any later version.
10ccb0284cSGreg Roach * This program is distributed in the hope that it will be useful,
11ccb0284cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ccb0284cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ccb0284cSGreg Roach * GNU General Public License for more details.
14ccb0284cSGreg Roach * You should have received a copy of the GNU General Public License
15ccb0284cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16ccb0284cSGreg Roach */
17fcfa147eSGreg Roach
18ccb0284cSGreg Roachdeclare(strict_types=1);
19ccb0284cSGreg Roach
20ccb0284cSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
21ccb0284cSGreg Roach
226ccdf4f0SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
23094bd4e5SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
24*901ade19SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\Logout;
25*901ade19SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\SelectLanguage;
26*901ade19SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\SelectTheme;
27ccb0284cSGreg Roachuse Fisharebest\Webtrees\I18N;
28ccb0284cSGreg Roachuse Fisharebest\Webtrees\Session;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
316ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
326ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
333976b470SGreg Roach
34ba8e6c18SGreg Roachuse function in_array;
35ccb0284cSGreg Roach
36ccb0284cSGreg Roach/**
37ccb0284cSGreg Roach * Middleware to wrap a request in a transaction.
38ccb0284cSGreg Roach */
39c1010edaSGreg Roachclass CheckCsrf implements MiddlewareInterface
40c1010edaSGreg Roach{
41ba8e6c18SGreg Roach    private const EXCLUDE_ROUTES = [
42*901ade19SGreg Roach        Logout::class,
43*901ade19SGreg Roach        SelectLanguage::class,
44*901ade19SGreg Roach        SelectTheme::class,
45ba8e6c18SGreg Roach    ];
46ba8e6c18SGreg Roach
47ccb0284cSGreg Roach    /**
486ccdf4f0SGreg Roach     * @param ServerRequestInterface  $request
496ccdf4f0SGreg Roach     * @param RequestHandlerInterface $handler
50ccb0284cSGreg Roach     *
516ccdf4f0SGreg Roach     * @return ResponseInterface
52ccb0284cSGreg Roach     */
536ccdf4f0SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
54c1010edaSGreg Roach    {
556ccdf4f0SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
56b46c87bdSGreg Roach            $params = (array) $request->getParsedBody();
57b46c87bdSGreg Roach
58b46c87bdSGreg Roach            $route = $params['route'] ?? '';
59ba8e6c18SGreg Roach
60310b7a5aSGreg Roach            if (!in_array($route, self::EXCLUDE_ROUTES, true)) {
61b46c87bdSGreg Roach                $client_token  = $params['csrf'] ?? $request->getHeaderLine('X-CSRF-TOKEN');
62ccb0284cSGreg Roach                $session_token = Session::get('CSRF_TOKEN');
63ccb0284cSGreg Roach
64310b7a5aSGreg Roach                if ($client_token !== $session_token) {
65094bd4e5SGreg Roach                    FlashMessages::addMessage(I18N::translate('This form has expired. Try again.'));
66094bd4e5SGreg Roach
67f567c3d8SGreg Roach                    return redirect((string) $request->getUri());
68ccb0284cSGreg Roach                }
691cfe16bdSGreg Roach
701cfe16bdSGreg Roach                unset($params['csrf']);
711cfe16bdSGreg Roach                $request = $request->withParsedBody($params);
72310b7a5aSGreg Roach            }
73310b7a5aSGreg Roach        }
74ccb0284cSGreg Roach
756ccdf4f0SGreg Roach        return $handler->handle($request);
76ccb0284cSGreg Roach    }
77ccb0284cSGreg Roach}
78