8000 bug #37791 Fix redis connect with empty password (alexander-schranz) · symfony/symfony@b2e99e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit b2e99e2

Browse files
committed
bug #37791 Fix redis connect with empty password (alexander-schranz)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- Fix redis connect with empty password | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix - | License | MIT | Doc PR | symfony/symfony-docs#... If you use DSN like this it will fail as auth is called with an empty password: ```env redis://%env(resolve:REDIS_PASSWORD)%@%env(resolve:REDIS_HOST)% ``` The auth should only be called when a password is set to a specific string. Else it will fail with: > ERR Client sent AUTH, but no password is set Redis Conf "redis-nopass.conf": ```ini requirepass "" ``` Commits ------- 9946f7f Fix redis connect with empty password
2 parents 8062943 + 9946f7f commit b2e99e2

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public static function createConnection($dsn, array $options = [])
103103
$params = preg_replace_callback('#^'.$scheme.':(//)?(?:(?:[^:@]*+:)?([^@]*+)@)?#', function ($m) use (&$auth) {
104104
if (isset($m[2])) {
105105
$auth = $m[2];
106+
107+
if ('' === $auth) {
108+
$auth = null;
109+
}
106110
}
107111

108112
return 'file:'.($m[1] ?? '');

src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ public function testAuth()
110110
Connection::fromDsn('redis://password@localhost/queue', [], $redis);
111111
}
112112

113+
public function testNoAuthWithEmptyPassword()
114+
{
115+
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();
116+
117+
$redis->expects($this->exactly(0))->method('auth')
118+
->with('')
119+
->willThrowException(new \RuntimeException());
120+
121+
Connection::fromDsn('redis://@localhost/queue', [], $redis);
122+
}
123+
124+
public function testAuthZeroPassword()
125+
{
126+
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();
127+
128+
$redis->expects($this->exactly(1))->method('auth')
129+
->with('0')
130+
->willReturn(true);
131+
132+
Connection::fromDsn('redis://0@localhost/queue', [], $redis);
133+
}
134+
113135
public function testFailedAuth()
114136
{
115137
$this->expectException(\InvalidArgumentException::class);

src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ public function __construct(array $configuration, array $connectionCredentials =
5555
$this->connection->connect($connectionCredentials['host'] ?? '127.0.0.1', $connectionCredentials['port'] ?? 6379);
5656
$this->connection->setOption(\Redis::OPT_SERIALIZER, $redisOptions['serializer'] ?? \Redis::SERIALIZER_PHP);
5757

58-
if (isset($connectionCredentials['auth']) && !$this->connection->auth($connectionCredentials['auth'])) {
58+
$auth = $connectionCredentials['auth'] ?? null;
59+
if ('' === $auth) {
60+
$auth = null;
61+
}
62+
63+
if (null !== $auth && !$this->connection->auth($auth)) {
5964
throw new InvalidArgumentException('Redis connection failed: '.$redis->getLastError());
6065
}
6166

0 commit comments

Comments
 (0)
0