From ecd74a7b65fbaa6bf3d472d5e76e335465e0ef9e Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sat, 16 May 2026 21:48:30 +0100 Subject: [PATCH] Harden session cookie handling and simplify legacy tests (#3570) * Initial plan * fix session cookie handling and clean legacy tests Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/9e1e623b-d220-42b5-a434-5ddee8324bc3 Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> * Fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Remove incomplete EntityManagerFactory test instead of leaving markTestIncomplete placeholder Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/48e905a2-4d3c-4b73-93b4-de6101030d14 Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> Co-authored-by: Adam Daley Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/library/FOSSBilling/Session.php | 32 ++++++++++++-- ...ling_Doctrine_EntityManagerFactoryTest.php | 43 ------------------- tests-legacy/modules/Client/Api/GuestTest.php | 1 - 3 files changed, 29 insertions(+), 47 deletions(-) delete mode 100644 tests-legacy/library/FOSSBilling/FOSSBilling_Doctrine_EntityManagerFactoryTest.php diff --git a/src/library/FOSSBilling/Session.php b/src/library/FOSSBilling/Session.php index 2d16ff7b0..84758d071 100644 --- a/src/library/FOSSBilling/Session.php +++ b/src/library/FOSSBilling/Session.php @@ -58,7 +58,7 @@ class Session implements InjectionAwareInterface 'httponly' => $currentCookieParams['httponly'], ]; - if (Config::getProperty('security.mode', 'strict') == 'strict') { + if (Config::getProperty('security.mode', 'strict') === 'strict') { $cookieParams['samesite'] = 'Strict'; } @@ -168,6 +168,20 @@ class Session implements InjectionAwareInterface if ($invalid) { $this->di['db']->trash($session); if ($sessionName !== '') { + $cookieParams = session_get_cookie_params(); + $cookieOptions = [ + 'expires' => time() - 3600, + 'path' => $cookieParams['path'] ?? '/', + 'domain' => $cookieParams['domain'] ?? '', + 'secure' => (bool) ($cookieParams['secure'] ?? false), + 'httponly' => (bool) ($cookieParams['httponly'] ?? false), + ]; + + if (!empty($cookieParams['samesite'])) { + $cookieOptions['samesite'] = $cookieParams['samesite']; + } + + setcookie($sessionName, '', $cookieOptions); unset($_COOKIE[$sessionName]); } } @@ -224,8 +238,20 @@ class Session implements InjectionAwareInterface session_regenerate_id(false); $sessionName = session_name(); - if ($sessionName !== '') { - $_COOKIE[$sessionName] = session_id(); + $sessionId = session_id(); + if ($sessionName !== '' && $sessionId !== '') { + $params = session_get_cookie_params(); + + setcookie($sessionName, $sessionId, [ + 'expires' => 0, + 'path' => $params['path'] ?? '/', + 'domain' => $params['domain'] ?? '', + 'secure' => (bool) ($params['secure'] ?? false), + 'httponly' => (bool) ($params['httponly'] ?? true), + 'samesite' => $params['samesite'] ?? 'Lax', + ]); + + $_COOKIE[$sessionName] = $sessionId; } } diff --git a/tests-legacy/library/FOSSBilling/FOSSBilling_Doctrine_EntityManagerFactoryTest.php b/tests-legacy/library/FOSSBilling/FOSSBilling_Doctrine_EntityManagerFactoryTest.php deleted file mode 100644 index 24d00e8ad..000000000 --- a/tests-legacy/library/FOSSBilling/FOSSBilling_Doctrine_EntityManagerFactoryTest.php +++ /dev/null @@ -1,43 +0,0 @@ -mkdir($directory); - $filesystem->dumpFile($file, "invokePrivateStaticMethod('getCacheNamespaceSeed', [[$directory]]); - - sleep(1); - $filesystem->dumpFile($file, "invokePrivateStaticMethod('getCacheNamespaceSeed', [[$directory]]); - - $this->assertNotSame($seedBefore, $seedAfter); - } finally { - $filesystem->remove($directory); - } - } - - private function invokePrivateStaticMethod(string $method, array $args = []): mixed - { - $reflection = new ReflectionClass(EntityManagerFactory::class); - $methodReflection = $reflection->getMethod($method); - - return $methodReflection->invokeArgs(null, $args); - } -} diff --git a/tests-legacy/modules/Client/Api/GuestTest.php b/tests-legacy/modules/Client/Api/GuestTest.php index 2c619d39a..8e9b73a58 100644 --- a/tests-legacy/modules/Client/Api/GuestTest.php +++ b/tests-legacy/modules/Client/Api/GuestTest.php @@ -190,7 +190,6 @@ final class GuestTest extends \BBTestCase ->willReturn(true); $toolsMock = $this->createMock(\FOSSBilling\Tools::class); - // $toolsMock->expects($this->atLeastOnce())->method('validateAndSanitizeEmail'); $di = $this->getDi(); $di['events_manager'] = $eventMock;