From a00dc828f754ab5e3cfa222111cee87e32454ee2 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 2 Jan 2025 18:13:47 +0100 Subject: [PATCH] [Security] Fix triggering session tracking from ContextListener --- .../Component/Security/Http/Firewall/ContextListener.php | 3 +++ .../Security/Http/Tests/Firewall/ContextListenerTest.php | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/Symfony/Component/Security/Http/Firewall/ContextListener.php b/src/Symfony/Component/Security/Http/Firewall/ContextListener.php index 7aeec196c672b..e8ad79d83cd40 100644 --- a/src/Symfony/Component/Security/Http/Firewall/ContextListener.php +++ b/src/Symfony/Component/Security/Http/Firewall/ContextListener.php @@ -164,6 +164,7 @@ public function onKernelResponse(ResponseEvent $event): void $session = $request->getSession(); $sessionId = $session->getId(); $usageIndexValue = $session instanceof Session ? $usageIndexReference = &$session->getUsageIndex() : null; + $usageIndexReference = \PHP_INT_MIN; $token = $this->tokenStorage->getToken(); if (!$this->trustResolver->isAuthenticated($token)) { @@ -178,6 +179,8 @@ public function onKernelResponse(ResponseEvent $event): void if ($this->sessionTrackerEnabler && $session->getId() === $sessionId) { $usageIndexReference = $usageIndexValue; + } else { + $usageIndexReference = $usageIndexReference - \PHP_INT_MIN + $usageIndexValue; } } diff --git a/src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php b/src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php index f1d76a17e7982..8d0ab72658aff 100644 --- a/src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php @@ -323,6 +323,8 @@ public function testSessionIsNotReported() $listener = new ContextListener($tokenStorage, [], 'context_key', null, null, null, $tokenStorage->getToken(...)); $listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST)); + + $listener->onKernelResponse(new ResponseEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST, new Response())); } public function testOnKernelResponseRemoveListener()