From 2aef7e632ccacabc5aa071a0379f77ac39386604 Mon Sep 17 00:00:00 2001 From: MatTheCat Date: Fri, 9 Jun 2023 14:38:09 +0200 Subject: [PATCH] [HttpFoundation] Make `SessionHandlerProxy` implement `SessionIdInterface` --- .../Component/HttpFoundation/CHANGELOG.md | 3 ++- .../Exception/SessionIdCreationException.php | 20 +++++++++++++++++++ .../Storage/Proxy/SessionHandlerProxy.php | 16 ++++++++++++++- .../Storage/Proxy/SessionHandlerProxyTest.php | 18 +++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/HttpFoundation/Exception/SessionIdCreationException.php diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 3f09854ac3221..897a3588602fa 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -9,7 +9,8 @@ CHANGELOG * Add `UriSigner` from the HttpKernel component * Add `partitioned` flag to `Cookie` (CHIPS Cookie) * Add argument `bool $flush = true` to `Response::send()` -* Make `MongoDbSessionHandler` instantiable with the mongodb extension directly + * Make `MongoDbSessionHandler` instantiable with the mongodb extension directly + * Make `SessionHandlerProxy` implement `SessionIdInterface` 6.3 --- diff --git a/src/Symfony/Component/HttpFoundation/Exception/SessionIdCreationException.php b/src/Symfony/Component/HttpFoundation/Exception/SessionIdCreationException.php new file mode 100644 index 0000000000000..e72a5d60f7b9d --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Exception/SessionIdCreationException.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\Exception; + +class SessionIdCreationException extends \RuntimeException +{ + public function __construct(string $message = 'Could not create a session id.', int $code = 0, \Throwable $previous = null) + { + parent::__construct($message, $code, $previous); + } +} diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php index 7bf3f9ff1e1dc..0ad8db5c3cacb 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php @@ -11,12 +11,13 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Proxy; +use Symfony\Component\HttpFoundation\Exception\SessionIdCreationException; use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler; /** * @author Drak */ -class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface +class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface, \SessionIdInterface { protected $handler; @@ -73,4 +74,17 @@ public function updateTimestamp(#[\SensitiveParameter] string $sessionId, string { return $this->handler instanceof \SessionUpdateTimestampHandlerInterface ? $this->handler->updateTimestamp($sessionId, $data) : $this->write($sessionId, $data); } + + public function create_sid(): string + { + if ($this->handler instanceof \SessionIdInterface) { + return $this->handler->create_sid(); + } + + if (!$id = session_create_id()) { + throw new SessionIdCreationException(); + } + + return $id; + } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Proxy/SessionHandlerProxyTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Proxy/SessionHandlerProxyTest.php index d9c4974ef474a..5533cf40b7d3e 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Proxy/SessionHandlerProxyTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Proxy/SessionHandlerProxyTest.php @@ -168,8 +168,26 @@ public static function provideNativeSessionStorageHandler() [new SessionHandlerProxy(new StrictSessionHandler(new \SessionHandler()))], ]; } + + public function testCreateSid() + { + $mock = $this->createMock(SessionIdSessionHandler::class); + $mock->expects($this->once()) + ->method('create_sid') + ->willReturn('a-valid-session-identifier'); + + $proxy = new SessionHandlerProxy($mock); + $this->assertSame('a-valid-session-identifier', $proxy->create_sid()); + + $this->proxy->create_sid(); + $this->addToAssertionCount(1); + } } abstract class TestSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface { } + +abstract class SessionIdSessionHandler implements \SessionHandlerInterface, \SessionIdInterface +{ +}