Skip to content

Commit c5b91ab

Browse files
committed
Add support for urlencoding of passwords, parse params using parse_str, refs snc#248
1 parent 0d2164c commit c5b91ab

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

Diff for: DependencyInjection/Configuration/RedisDsn.php

+19-20
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,17 @@ protected function parseDsn($dsn)
155155
$dsn = str_replace('redis://', '', $dsn); // remove "redis://"
156156
if (false !== $pos = strrpos($dsn, '@')) {
157157
// parse password
158-
$this->password = str_replace('\@', '@', substr($dsn, 0, $pos));
158+
$password = substr($dsn, 0, $pos);
159159

160-
if (strstr($this->password, ':')) {
161-
list(, $this->password) = explode(':', $this->password);
160+
if (strstr($password, ':')) {
161+
list(, $password) = explode(':', $password, 2);
162162
}
163163

164+
$this->password = urldecode($password);
165+
164166
$dsn = substr($dsn, $pos + 1);
165167
}
166-
$dsn = preg_replace_callback('/\?(weight|alias)=[^&]+.*$/', array($this, 'parseParameters'), $dsn); // parse parameters
168+
$dsn = preg_replace_callback('/\?(.*)$/', array($this, 'parseParameters'), $dsn); // parse parameters
167169
if (preg_match('#^(.*)/(\d+)$#', $dsn, $matches)) {
168170
// parse database
169171
$this->database = (int) $matches[2];
@@ -192,22 +194,19 @@ protected function parseDsn($dsn)
192194
*/
193195
protected function parseParameters($matches)
194196
{
195-
$parameters = explode('&', substr($matches[0], 1));
196-
foreach ($parameters as $parameter) {
197-
$kv = explode('=', $parameter, 2);
198-
if (2 === count($kv)) {
199-
switch ($kv[0]) {
200-
case 'weight':
201-
if ($kv[1]) {
202-
$this->weight = (int) $kv[1];
203-
}
204-
break;
205-
case 'alias':
206-
if ($kv[1]) {
207-
$this->alias = $kv[1];
208-
}
209-
break;
210-
}
197+
parse_str($matches[1], $params);
198+
199+
foreach ($params as $key => $val) {
200+
if (!$val) {
201+
continue;
202+
}
203+
switch ($key) {
204+
case 'weight':
205+
$this->weight = (int) $val;
206+
break;
207+
case 'alias':
208+
$this->alias = $val;
209+
break;
211210
}
212211
}
213212

Diff for: Tests/DependencyInjection/Configuration/RedisDsnTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,20 @@ public static function passwordValues()
174174
array('redis://localhost', null),
175175
array('redis://localhost/1', null),
176176
array('redis://pw@localhost:63790/10', 'pw'),
177-
array('redis://p\@w@localhost:63790/10', 'p@w'),
177+
array('redis://user:pw@localhost:63790/10', 'pw'),
178+
array('redis://user:pw:withcolon@localhost:63790/10', 'pw:withcolon'),
179+
array('redis://Pw%3AColon%25@localhost:63790/10', 'Pw:Colon%'),
180+
array('redis://p%40w@localhost:63790/10', 'p@w'),
178181
array('redis://mB(.z9},6o?zl>v!LM76A]lCg77,;.@localhost:63790/10', 'mB(.z9},6o?zl>v!LM76A]lCg77,;.'),
179182
array('redis://127.0.0.1', null),
180183
array('redis://127.0.0.1/1', null),
181184
array('redis://[email protected]:63790/10', 'pw'),
182-
array('redis://p\@w@127.0.0.1:63790/10', 'p@w'),
185+
array('redis://p%40w@127.0.0.1:63790/10', 'p@w'),
183186
array('redis://mB(.z9},6o?zl>v!LM76A]lCg77,;[email protected]:63790/10', 'mB(.z9},6o?zl>v!LM76A]lCg77,;.'),
184187
array('redis:///redis.sock', null),
185188
array('redis:///redis.sock/1', null),
186189
array('redis://pw@/redis.sock/10', 'pw'),
187-
array('redis://p\@w@/redis.sock/10', 'p@w'),
190+
array('redis://p%40w@/redis.sock/10', 'p@w'),
188191
array('redis://mB(.z9},6o?zl>v!LM76A]lCg77,;.@/redis.sock/10', 'mB(.z9},6o?zl>v!LM76A]lCg77,;.'),
189192
);
190193
}

Diff for: UPDATE.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Update notes #
22

3+
## 2.0.0 ##
4+
5+
Passwords in DSNs must now be properly urlencoded if they contain any of
6+
the following special characters: `@`, `%` or `:`. Encode them as `%40`,
7+
`%25` and `%3A` respectively. The `\@` notation for escaping `@` has been
8+
removed.
9+
310
## 1.0.11 and 1.1.6 ##
411

512
The monolog handler was renamed from `monolog.handler.redis` to

0 commit comments

Comments
 (0)