From 9946f7fecf6ade82466d73bda64d8ea6118598a8 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Mon, 10 Aug 2020 14:12:18 +0200 Subject: [PATCH] Fix redis connect with empty password --- .../Component/Cache/Traits/RedisTrait.php | 4 ++++ .../Transport/RedisExt/ConnectionTest.php | 22 +++++++++++++++++++ .../Transport/RedisExt/Connection.php | 7 +++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Cache/Traits/RedisTrait.php b/src/Symfony/Component/Cache/Traits/RedisTrait.php index cda180c13c..72c18bf3c2 100644 --- a/src/Symfony/Component/Cache/Traits/RedisTrait.php +++ b/src/Symfony/Component/Cache/Traits/RedisTrait.php @@ -103,6 +103,10 @@ trait RedisTrait $params = preg_replace_callback('#^'.$scheme.':(//)?(?:(?:[^:@]*+:)?([^@]*+)@)?#', function ($m) use (&$auth) { if (isset($m[2])) { $auth = $m[2]; + + if ('' === $auth) { + $auth = null; + } } return 'file:'.($m[1] ?? ''); diff --git a/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php b/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php index e7d4d8d46d..f73db2b642 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php @@ -110,6 +110,28 @@ class ConnectionTest extends TestCase Connection::fromDsn('redis://password@localhost/queue', [], $redis); } + public function testNoAuthWithEmptyPassword() + { + $redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock(); + + $redis->expects($this->exactly(0))->method('auth') + ->with('') + ->willThrowException(new \RuntimeException()); + + Connection::fromDsn('redis://@localhost/queue', [], $redis); + } + + public function testAuthZeroPassword() + { + $redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock(); + + $redis->expects($this->exactly(1))->method('auth') + ->with('0') + ->willReturn(true); + + Connection::fromDsn('redis://0@localhost/queue', [], $redis); + } + public function testFailedAuth() { $this->expectException(\InvalidArgumentException::class); diff --git a/src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php b/src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php index bb818512c2..c0e9ba707e 100644 --- a/src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php +++ b/src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php @@ -55,7 +55,12 @@ class Connection $this->connection->connect($connectionCredentials['host'] ?? '127.0.0.1', $connectionCredentials['port'] ?? 6379); $this->connection->setOption(\Redis::OPT_SERIALIZER, $redisOptions['serializer'] ?? \Redis::SERIALIZER_PHP); - if (isset($connectionCredentials['auth']) && !$this->connection->auth($connectionCredentials['auth'])) { + $auth = $connectionCredentials['auth'] ?? null; + if ('' === $auth) { + $auth = null; + } + + if (null !== $auth && !$this->connection->auth($auth)) { throw new InvalidArgumentException('Redis connection failed: '.$redis->getLastError()); }