Fix redis connect with empty password
This commit is contained in:
parent
8062943caf
commit
9946f7fecf
@ -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] ?? '');
|
||||
|
@ -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);
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user