8000 [Cache] Add url decoding of password in `RedisTrait` DSN · symfony/symfony@2764fd7 · GitHub
[go: up one dir, main page]

Skip to content
Dismiss alert

Commit 2764fd7

Browse files
[Cache] Add url decoding of password in RedisTrait DSN
1 parent 37f04cf commit 2764fd7

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

.github/workflows/integration-tests.yml

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ jobs:
4848
image: redis:6.2.8
4949
ports:
5050
- 16379:6379
51+
redis-authenticated:
52+
image: redis:6.2.8
53+
ports:
54+
- 16380:6379
55+
env:
56+
REDIS_ARGS: "--requirepass p@ssword"
5157
redis-cluster:
5258
image: grokzen/redis-cluster:6.2.8
5359
ports:
@@ -170,6 +176,7 @@ jobs:
170176
run: ./phpunit --group integration -v
171177
env:
172178
REDIS_HOST: 'localhost:16379'
179+
REDIS_AUTHENTICATED_HOST: 'localhost:16380'
173180
REDIS_CLUSTER_HOSTS: 'localhost:7000 localhost:7001 localhost:7002 localhost:7003 localhost:7004 localhost:7005'
174181
REDIS_SENTINEL_HOSTS: 'unreachable-host:26379 localhost:26379 localhost:26379'
175182
REDIS_SENTINEL_SERVICE: redis_sentinel

src/Symfony/Component/Cache/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add url decoding of password in `RedisTrait` DSN
8+
49
7.0
510
---
611

src/Symfony/Component/Cache/Tests/Traits/RedisTraitTest.php

+16-7
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Cache\Traits\RedisTrait;
1616

17+
/**
18+
* @requires extension redis
19+
*/
1720
class RedisTraitTest extends TestCase
1821
{
19-
public static function setUpBeforeClass(): void
20-
{
21-
if (!getenv('REDIS_CLUSTER_HOSTS')) {
22-
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
23-
}
24-
}
25-
2622
/**
2723
* @dataProvider provideCreateConnection
2824
*/
@@ -41,6 +37,19 @@ public function testCreateConnection(string $dsn, string $expectedClass)
4137
self::assertInstanceOf($expectedClass, $connection);
4238
}
4339

40+
public function testUrlDecodeParameters()
41+
{
42+
if (!getenv('REDIS_AUTHENTICATED_HOST')) {
43+
self::markTestSkipped('REDIS_AUTHENTICATED_HOST env var is not defined.');
44+
}
45+
46+
$mock = self::getObjectForTrait(RedisTrait::class);
47+
$connection = $mock::createConnection('redis://:p%40ssword@'.getenv('REDIS_AUTHENTICATED_HOST'));
48+
49+
self::assertInstanceOf(\Redis::class, $connection);
50+
self::assertSame('p@ssword', $connection->getAuth());
51+
}
52+
4453
public static function provideCreateConnection(): array
4554
{
4655
$hosts = array_map(fn ($host) => sprintf('host[%s]', $host), explode(' ', getenv('REDIS_CLUSTER_HOSTS')));

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
101101
$params = preg_replace_callback('#^'.$scheme.':(//)?(?:(?:(?<user>[^:@]*+):)?(?<password>[^@]*+)@)?#', function ($m) use (&$auth) {
102102
if (isset($m['password'])) {
103103
if (\in_array($m['user'], ['', 'default'], true)) {
104-
$auth = $m['password'];
104+
$auth = urldecode($m['password']);
105105
} else {
106-
$auth = [$m['user'], $m['password']];
106+
$auth = [$m['user'], urldecode($m['password'])];
107107
}
108108

109109
if ('' === $auth) {
@@ -369,9 +369,9 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
369369
if (\is_array($auth)) {
370370
// ACL
371371
$params['parameters']['username'] = $auth[0];
372-
$params['parameters']['password'] = $auth[1];
372+
$params['parameters']['password'] = urldecode($auth[1]);
373373
} else {
374-
$params['parameters']['password'] = $auth;
374+
$params['parameters']['password'] = urldecode($auth);
375375
}
376376
}
377377

0 commit comments

Comments
 (0)
0