|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * --------------------------------------------------------------------- |
| 5 | + * |
| 6 | + * GLPI - Gestionnaire Libre de Parc Informatique |
| 7 | + * |
| 8 | + * http://glpi-project.org |
| 9 | + * |
| 10 | + * @copyright 2015-2026 Teclib' and contributors. |
| 11 | + * @licence https://www.gnu.org/licenses/gpl-3.0.html |
| 12 | + * |
| 13 | + * --------------------------------------------------------------------- |
| 14 | + * |
| 15 | + * LICENSE |
| 16 | + * |
| 17 | + * This file is part of GLPI. |
| 18 | + * |
| 19 | + * This program is free software: you can redistribute it and/or modify |
| 20 | + * it under the terms of the GNU General Public License as published by |
| 21 | + * the Free Software Foundation, either version 3 of the License, or |
| 22 | + * (at your option) any later version. |
| 23 | + * |
| 24 | + * This program is distributed in the hope that it will be useful, |
| 25 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | + * GNU General Public License for more details. |
| 28 | + * |
| 29 | + * You should have received a copy of the GNU General Public License |
| 30 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 31 | + * |
| 32 | + * --------------------------------------------------------------------- |
| 33 | + */ |
| 34 | + |
| 35 | +namespace tests\units\Glpi\Kernel\Listener\ControllerListener; |
| 36 | + |
| 37 | +use Glpi\Controller\Session\ChangeProfileController; |
| 38 | +use Glpi\Exception\Http\AccessDeniedHttpException; |
| 39 | +use Glpi\Exception\SessionExpiredException; |
| 40 | +use Glpi\Http\Firewall; |
| 41 | +use Glpi\Http\SessionManager; |
| 42 | +use Glpi\Kernel\Listener\ControllerListener\CheckCsrfListener; |
| 43 | +use Glpi\Kernel\Listener\ControllerListener\FirewallStrategyListener; |
| 44 | +use Glpi\Tests\DbTestCase; |
| 45 | +use Session; |
| 46 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 47 | +use Symfony\Component\HttpFoundation\Request; |
| 48 | +use Symfony\Component\HttpKernel\Event\ControllerEvent; |
| 49 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 50 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 51 | + |
| 52 | +final class ControllerListenersPriorityTest extends DbTestCase |
| 53 | +{ |
| 54 | + public function testFirewallIsExecutedBeforeCsrfCheck(): void |
| 55 | + { |
| 56 | + $session_manager = new SessionManager(); |
| 57 | + $csrf_listener = new CheckCsrfListener($session_manager); |
| 58 | + $firewall_listener = new FirewallStrategyListener(new Firewall(), $session_manager); |
| 59 | + |
| 60 | + $dispatcher = new EventDispatcher(); |
| 61 | + // Register the listeners in the reverse order of the expected execution order, to be sure that |
| 62 | + // the execution order does not depend on the registration order. |
| 63 | + $dispatcher->addSubscriber($csrf_listener); |
| 64 | + $dispatcher->addSubscriber($firewall_listener); |
| 65 | + |
| 66 | + $this->assertSame( |
| 67 | + [ |
| 68 | + [$firewall_listener, 'onKernelController'], |
| 69 | + [$csrf_listener, 'onKernelController'], |
| 70 | + ], |
| 71 | + $dispatcher->getListeners(KernelEvents::CONTROLLER) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + public function testPostRequestWithExpiredSessionThrowsSessionExpiredException(): void |
| 76 | + { |
| 77 | + // No session, thus no CSRF token. |
| 78 | + $this->assertFalse(Session::getLoginUserID()); |
| 79 | + $this->assertArrayNotHasKey('glpicsrftokens', $_SESSION); |
| 80 | + |
| 81 | + $this->expectException(SessionExpiredException::class); |
| 82 | + |
| 83 | + $this->dispatch(Request::create('/Session/ChangeProfile', 'POST', ['id' => 1])); |
| 84 | + } |
| 85 | + |
| 86 | + public function testPostRequestWithInvalidCsrfTokenIsStillDenied(): void |
| 87 | + { |
| 88 | + $this->login(); |
| 89 | + |
| 90 | + $this->expectException(AccessDeniedHttpException::class); |
| 91 | + |
| 92 | + $this->dispatch(Request::create('/Session/ChangeProfile', 'POST', ['id' => 1, '_glpi_csrf_token' => 'invalid'])); |
| 93 | + } |
| 94 | + |
| 95 | + public function testPostRequestWithValidCsrfTokenIsAccepted(): void |
| 96 | + { |
| 97 | + $this->login(); |
| 98 | + |
| 99 | + $token = Session::getNewCSRFToken(); |
| 100 | + |
| 101 | + $this->dispatch(Request::create('/Session/ChangeProfile', 'POST', ['id' => 1, '_glpi_csrf_token' => $token])); |
| 102 | + |
| 103 | + // Token has been validated and consumed. |
| 104 | + $this->assertArrayNotHasKey($token, $_SESSION['glpicsrftokens']); |
| 105 | + } |
| 106 | + |
| 107 | + private function getDispatcher(): EventDispatcher |
| 108 | + { |
| 109 | + $session_manager = new SessionManager(); |
| 110 | + |
| 111 | + $dispatcher = new EventDispatcher(); |
| 112 | + $dispatcher->addSubscriber(new CheckCsrfListener($session_manager)); |
| 113 | + $dispatcher->addSubscriber(new FirewallStrategyListener(new Firewall(), $session_manager)); |
| 114 | + |
| 115 | + return $dispatcher; |
| 116 | + } |
| 117 | + |
| 118 | + private function dispatch(Request $request): void |
| 119 | + { |
| 120 | + // The controller is not executed, only its attributes are read by the firewall. |
| 121 | + $event = new ControllerEvent( |
| 122 | + $this->createStub(HttpKernelInterface::class), |
| 123 | + new ChangeProfileController(), |
| 124 | + $request, |
| 125 | + HttpKernelInterface::MAIN_REQUEST |
| 126 | + ); |
| 127 | + |
| 128 | + $this->getDispatcher()->dispatch($event, KernelEvents::CONTROLLER); |
| 129 | + } |
| 130 | +} |
0 commit comments