Fix redis connect with empty password

This commit is contained in:
Alexander Schranz 2020-08-10 14:12:18 +02:00 committed by Fabien Potencier
parent 8062943caf
commit 9946f7fecf
3 changed files with 32 additions and 1 deletions

View File

@ -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] ?? '');

View File

@ -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);

View File

@ -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());
}