8000 [Cache][DependencyInjection][Lock][Mailer][Messenger][Notifier][Trans… · symfony/symfony@f0292f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit f0292f2

Browse files
alexandre-dauboisnicolas-grekas
authored andcommitted
[Cache][DependencyInjection][Lock][Mailer][Messenger][Notifier][Translation] Url decode username and passwords from parse_url() results
1 parent d1c2c74 commit f0292f2

File tree

10 files changed

+83
-78
lines changed

10 files changed

+83
-78
lines changed

src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ public static function createConnection($servers, array $options = [])
114114
$params = preg_replace_callback('#^memcached:(//)?(?:([^@]*+)@)?#', function ($m) use (&$username, &$password) {
115115
if (!empty($m[2])) {
116116
[$username, $password] = explode(':', $m[2], 2) + [1 => null];
117+
$username = rawurldecode($username);
118+
$password = null !== $password ? rawurldecode($password) : null;
117119
}
118120

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

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,15 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
253253
}
254254

255255
if ('url' === $prefix) {
256-
$parsedEnv = parse_url($env);
256+
$params = parse_url($env);
257257

258-
if (false === $parsedEnv) {
258+
if (false === $params) {
259259
throw new RuntimeException(sprintf('Invalid URL in env var "%s".', $name));
260260
}
261-
if (!isset($parsedEnv['scheme'], $parsedEnv['host'])) {
261+
if (!isset($params['scheme'], $params['host'])) {
262262
throw new RuntimeException(sprintf('Invalid URL env var "%s": schema and host expected, "%s" given.', $name, $env));
263263
}
264-
$parsedEnv += [
264+
$params += [
265265
'port' => null,
266266
'user' => null,
267267
'pass' => null,
@@ -270,10 +270,13 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
270270
'fragment' => null,
271271
];
272272

273+
$params['user'] = null !== $params['user'] ? rawurldecode($params['user']) : null;
274+
$params['pass'] = null !== $params['pass'] ? rawurldecode($params['pass']) : null;
275+
273276
// remove the '/' separator
274-
$parsedEnv['path'] = '/' === ($parsedEnv['path'] ?? '/') ? '' : substr($parsedEnv['path'], 1);
277+
$params['path'] = '/' === ($params['path'] ?? '/') ? '' : substr($params['path'], 1);
275278

276-
return $parsedEnv;
279+
return $params;
277280
}
278281

279282
if ('query_string' === $prefix) {

src/Symfony/Component/Lock/Store/MongoDbStore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ public function __construct($mongo, array $options = [], float $initialTtl = 300
137137
*/
138138
private function skimUri(string $uri): string
139139
{
140-
if (false === $parsedUrl = parse_url($uri)) {
140+
if (false === $params = parse_url($uri)) {
141141
throw new InvalidArgumentException(sprintf('The given MongoDB Connection URI "%s" is invalid.', $uri));
142142
}
143-
$pathDb = ltrim($parsedUrl['path'] ?? '', '/') ?: null;
143+
$pathDb = ltrim($params['path'] ?? '', '/') ?: null;
144144
if (null !== $pathDb) {
145145
$this->options['database'] = $pathDb;
146146
}

src/Symfony/Component/Mailer/Transport/Dsn.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,24 @@ public function __construct(string $scheme, string $host, string $user = null, s
3737

3838
public static function fromString(string $dsn): self
3939
{
40-
if (false === $parsedDsn = parse_url($dsn)) {
40+
if (false === $params = parse_url($dsn)) {
4141
throw new InvalidArgumentException('The mailer DSN is invalid.');
4242
}
4343

44-
if (!isset($parsedDsn['scheme'])) {
44+
if (!isset($params['scheme'])) {
4545
throw new InvalidArgumentException('The mailer DSN must contain a scheme.');
4646
}
4747

48-
if (!isset($parsedDsn['host'])) {
48+
if (!isset($params['host'])) {
4949
throw new InvalidArgumentException('The mailer DSN must contain a host (use "default" by default).');
5050
}
5151

52-
$user = '' !== ($parsedDsn['user'] ?? '') ? urldecode($parsedDsn['user']) : null;
53-
$password = '' !== ($parsedDsn['pass'] ?? '') ? urldecode($parsedDsn['pass']) : null;
54-
$port = $parsedDsn['port'] ?? null;
55-
parse_str($parsedDsn['query'] ?? '', $query);
52+
$user = '' !== ($params['user'] ?? '') ? rawurldecode($params['user']) : null;
53+
$password = '' !== ($params['pass'] ?? '') ? rawurldecode($params['pass']) : null;
54+
$port = $params['port'] ?? null;
55+
parse_str($params['query'] ?? '', $query);
5656

57-
return new self($parsedDsn['scheme'], $parsedDsn['host'], $user, $password, $port, $query);
57+
return new self($params['scheme'], $params['host'], $user, $password, $port, $query);
5858
}
5959

6060
public function getScheme(): string

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/Connection.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ public function __destruct()
103103
*/
104104
public static function fromDsn(string $dsn, array $options = [], HttpClientInterface $client = null, LoggerInterface $logger = null): self
105105
{
106-
if (false === $parsedUrl = parse_url($dsn)) {
106+
if (false === $params = parse_url($dsn)) {
107107
throw new InvalidArgumentException('The given Amazon SQS DSN is invalid.');
108108
}
109109

110110
$query = [];
111-
if (isset($parsedUrl['query'])) {
112-
parse_str($parsedUrl['query'], $query);
111+
if (isset($params['query'])) {
112+
parse_str($params['query'], $query);
113113
}
114114

115115
// check for extra keys in options
@@ -136,24 +136,24 @@ public static function fromDsn(string $dsn, array $options = [], HttpClientInter
136136

137137
$clientConfiguration = [
138138
'region' => $options['region'],
139-
'accessKeyId' => urldecode($parsedUrl['user'] ?? '') ?: $options['access_key'] ?? self::DEFAULT_OPTIONS['access_key'],
140-
'accessKeySecret' => urldecode($parsedUrl['pass'] ?? '') ?: $options['secret_key'] ?? self::DEFAULT_OPTIONS['secret_key'],
139+
'accessKeyId' => rawurldecode($params['user'] ?? '') ?: $options['access_key'] ?? self::DEFAULT_OPTIONS['access_key'],
140+
'accessKeySecret' => rawurldecode($params['pass'] ?? '') ?: $options['secret_key'] ?? self::DEFAULT_OPTIONS['secret_key'],
141141
];
142142
if (isset($options['debug'])) {
143143
$clientConfiguration['debug'] = $options['debug'];
144144
}
145145
unset($query['region']);
146146

147-
if ('default' !== ($parsedUrl['host'] ?? 'default')) {
148-
$clientConfiguration['endpoint'] = sprintf('%s://%s%s', ($query['sslmode'] ?? null) === 'disable' ? 'http' : 'https', $parsedUrl['host'], ($parsedUrl['port'] ?? null) ? ':'.$parsedUrl['port'] : '');
149-
if (preg_match(';^sqs\.([^\.]++)\.amazonaws\.com$;', $parsedUrl['host'], $matches)) {
147+
if ('default' !== ($params['host'] ?? 'default')) {
148+
$clientConfiguration['endpoint'] = sprintf('%s://%s%s', ($query['sslmode'] ?? null) === 'disable' ? 'http' : 'https', $params['host'], ($params['port'] ?? null) ? ':'.$params['port'] : '');
149+
if (preg_match(';^sqs\.([^\.]++)\.amazonaws\.com$;', $params['host'], $matches)) {
150150
$clientConfiguration['region'] = $matches[1];
151151
}
152152
} elseif (self::DEFAULT_OPTIONS['endpoint'] !== $options['endpoint'] ?? self::DEFAULT_OPTIONS['endpoint']) {
153153
$clientConfiguration['endpoint'] = $options['endpoint'];
154154
}
155155

156-
$parsedPath = explode('/', ltrim($parsedUrl['path'] ?? '/', '/'));
156+
$parsedPath = explode('/', ltrim($params['path'] ?? '/', '/'));
157157
if (\count($parsedPath) > 0 && !empty($queueName = end($parsedPath))) {
158158
$configuration['queue_name'] = $queueName;
159159
}
@@ -163,11 +163,11 @@ public static function fromDsn(string $dsn, array $options = [], HttpClientInter
163163
// https://sqs.REGION.amazonaws.com/ACCOUNT/QUEUE
164164
$queueUrl = null;
165165
if (
166-
'https' === $parsedUrl['scheme']
167-
&& ($parsedUrl['host'] ?? 'default') === "sqs.{$clientConfiguration['region']}.amazonaws.com"
168-
&& ($parsedUrl['path'] ?? '/') === "/{$configuration['account']}/{$configuration['queue_name']}"
166+
'https' === $params['scheme']
167+
&& ($params['host'] ?? 'default') === "sqs.{$clientConfiguration['region']}.amazonaws.com"
168+
&& ($params['path'] ?? '/') === "/{$configuration['account']}/{$configuration['queue_name']}"
169169
) {
170-
$queueUrl = 'https://'.$parsedUrl['host'].$parsedUrl['path'];
170+
$queueUrl = 'https://'.$params['host'].$params['path'];
171171
}
172172

173173
return new self($configuration, new SqsClient($clientConfiguration, null, $client, $logger), $queueUrl);

src/Symfony/Component/Messenger/Bridge/Amqp/Transport/Connection.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,24 @@ public function __construct(array $connectionOptions, array $exchangeOptions, ar
177177
*/
178178
public static function fromDsn(string $dsn, array $options = [], AmqpFactory $amqpFactory = null): self
179179
{
180-
if (false === $parsedUrl = parse_url($dsn)) {
180+
if (false === $params = parse_url($dsn)) {
181181
// this is a valid URI that parse_url cannot handle when you want to pass all parameters as options
182182
if (!\in_array($dsn, ['amqp://', 'amqps://'])) {
183183
throw new InvalidArgumentException('The given AMQP DSN is invalid.');
184184
}
185185

186-
$parsedUrl = [];
186+
$params = [];
187187
}
188188

189189
$useAmqps = 0 === strpos($dsn, 'amqps://');
190-
$pathParts = isset($parsedUrl['path']) ? explode('/', trim($parsedUrl['path'], '/')) : [];
190+
$pathParts = isset($params['path']) ? explode('/', trim($params['path'], '/')) : [];
191191
$exchangeName = $pathParts[1] ?? 'messages';
192-
parse_str($parsedUrl['query'] ?? '', $parsedQuery);
192+
parse_str($params['query'] ?? '', $parsedQuery);
193193
$port = $useAmqps ? 5671 : 5672;
194194

195195
$amqpOptions = array_replace_recursive([
196-
'host' => $parsedUrl['host'] ?? 'localhost',
197-
'port' => $parsedUrl['port'] ?? $port,
196+
'host' => $params['host'] ?? 'localhost',
197+
'port' => $params['port'] ?? $port,
198198
'vhost' => isset($pathParts[0]) ? urldecode($pathParts[0]) : '/',
199199
'exchange' => [
200200
'name' => $exchangeName,
@@ -203,12 +203,12 @@ public static function fromDsn(string $dsn, array $options = [], AmqpFactory $am
203203

204204
self::validateOptions($amqpOptions);
205205

206-
if (isset($parsedUrl['user'])) {
207-
$amqpOptions['login'] = urldecode($parsedUrl['user']);
206+
if (isset($params['user'])) {
207+
$amqpOptions['login'] = rawurldecode($params['user']);
208208
}
209209

210-
if (isset($parsedUrl['pass'])) {
211-
$amqpOptions['password'] = urldecode($parsedUrl['pass']);
210+
if (isset($params['pass'])) {
211+
$amqpOptions['password'] = rawurldecode($params['pass']);
212212
}
213213

214214
if (!isset($amqpOptions['queues'])) {

src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,16 @@ public function getConfiguration(): array
8686

8787
public static function buildConfiguration(string $dsn, array $options = []): array
8888
{
89-
if (false === $components = parse_url($dsn)) {
89+
if (false === $params = parse_url($dsn)) {
9090
throw new InvalidArgumentException('The given Doctrine Messenger DSN is invalid.');
9191
}
9292

9393
$query = [];
94-
if (isset($components['query'])) {
95-
parse_str($components['query'], $query);
94+
if (isset($params['query'])) {
95+
parse_str($params['query'], $query);
9696
}
9797

98-
$configuration = ['connection' => $components['host']];
98+
$configuration = ['connection' => $params['host']];
9999
$configuration += $query + $options + static::DEFAULT_OPTIONS;
100100

101101
$configuration['auto_setup'] = filter_var($configuration['auto_setup'], \FILTER_VALIDATE_BOOLEAN);

src/Symfony/Component/Messenger/Bridge/Redis/Transport/Connection.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,18 @@ private static function initializeRedisCluster(?\RedisCluster $redis, array $hos
155155
public static function fromDsn(string $dsn, array $redisOptions = [], $redis = null): self
156156
{
157157
if (false === strpos($dsn, ',')) {
158-
$parsedUrl = self::parseDsn($dsn, $redisOptions);
158+
$params = self::parseDsn($dsn, $redisOptions);
159159
} else {
160160
$dsns = explode(',', $dsn);
161161
$parsedUrls = array_map(function ($dsn) use (&$redisOptions) {
162162
return self::parseDsn($dsn, $redisOptions);
163163
}, $dsns);
164164

165165
// Merge all the URLs, the last one overrides the previous ones
166-
$parsedUrl = array_merge(...$parsedUrls);
166+
$params = array_merge(...$parsedUrls);
167167

168168
// Regroup all the hosts in an array interpretable by RedisCluster
169-
$parsedUrl['host'] = array_map(function ($parsedUrl) {
169+
$params['host'] = array_map(function ($parsedUrl) {
170170
if (!isset($parsedUrl['host'])) {
171171
throw new InvalidArgumentException('Missing host in DSN, it must be defined when using Redis Cluster.');
172172
}
@@ -209,7 +209,7 @@ public static function fromDsn(string $dsn, array $redisOptions = [], $redis = n
209209
unset($redisOptions['dbindex']);
210210
}
211211

212-
$tls = 'rediss' === $parsedUrl['scheme'];
212+
$tls = 'rediss' === $params['scheme'];
213213
if (\array_key_exists('tls', $redisOptions)) {
214214
trigger_deprecation('symfony/redis-messenger', '5.3', 'Providing "tls" parameter is deprecated, use "rediss://" DSN scheme instead');
215215
$tls = filter_var($redisOptions['tls'], \FILTER_VALIDATE_BOOLEAN);
@@ -242,17 +242,17 @@ public static function fromDsn(string $dsn, array $redisOptions = [], $redis = n
242242
'claim_interval' => $claimInterval,
243243
];
244244

245-
if (isset($parsedUrl['host'])) {
246-
$pass = '' !== ($parsedUrl['pass'] ?? '') ? urldecode($parsedUrl['pass']) : null;
247-
$user = '' !== ($parsedUrl['user'] ?? '') ? urldecode($parsedUrl['user']) : null;
245+
if (isset($params['host'])) {
246+
$user = isset($params['user']) && '' !== $params['user'] ? rawurldecode($params['user']) : null;
247+
$pass = isset($params['pass']) && '' !== $params['pass'] ? rawurldecode($params['pass']) : null;
248248
$connectionCredentials = [
249-
'host' => $parsedUrl['host'] ?? '127.0.0.1',
250-
'port' => $parsedUrl['port'] ?? 6379,
249+
'host' => $params['host'],
250+
'port' => $params['port'] ?? 6379,
251251
// See: https://github.com/phpredis/phpredis/#auth
252252
'auth' => $redisOptions['auth'] ?? (null !== $pass && null !== $user ? [$user, $pass] : ($pass ?? $user)),
253253
];
254254

255-
$pathParts = explode('/', rtrim($parsedUrl['path'] ?? '', '/'));
255+
$pathParts = explode('/', rtrim($params['path'] ?? '', '/'));
256256

257257
$configuration['stream'] = $pathParts[1] ?? $configuration['stream'];
258258
$configuration['group'] = $pathParts[2] ?? $configuration['group'];
@@ -262,7 +262,7 @@ public static function fromDsn(string $dsn, array $redisOptions = [], $redis = n
262262
}
263263
} else {
264264
$connectionCredentials = [
265-
'host' => $parsedUrl['path'],
265+
'host' => $params['path'],
266266
'port' => 0,
267267
];
268268
}
@@ -279,15 +279,15 @@ private static function parseDsn(string $dsn, array &$redisOptions): array
279279
$url = str_replace($scheme.':', 'file:', $dsn);
280280
}
281281

282-
if (false === $parsedUrl = parse_url($url)) {
282+
if (false === $params = parse_url($url)) {
283283
throw new InvalidArgumentException('The given Redis DSN is invalid.');
284284
}
285-
if (isset($parsedUrl['query'])) {
286-
parse_str($parsedUrl['query'], $dsnOptions);
285+
if (isset($params['query'])) {
286+
parse_str($params['query'], $dsnOptions);
287287
$redisOptions = array_merge($redisOptions, $dsnOptions);
288288
}
289289

290-
return $parsedUrl;
290+
return $params;
291291
}
292292

293293
private static function validateOptions(array $options): void

src/Symfony/Component/Notifier/Transport/Dsn.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ public function __construct(string $dsn)
3333
{
3434
$this->originalDsn = $dsn;
3535

36-
if (false === $parsedDsn = parse_url($dsn)) {
36+
if (false === $params = parse_url($dsn)) {
3737
throw new InvalidArgumentException('The notifier DSN is invalid.');
3838
}
3939

40-
if (!isset($parsedDsn['scheme'])) {
40+
if (!isset($params['scheme'])) {
4141
throw new InvalidArgumentException('The notifier DSN must contain a scheme.');
4242
}
43-
$this->scheme = $parsedDsn['scheme'];
43+
$this->scheme = $params['scheme'];
4444

45-
if (!isset($parsedDsn['host'])) {
45+
if (!isset($params['host'])) {
4646
throw new InvalidArgumentException('The notifier DSN must contain a host (use "default" by default).');
4747
}
48-
$this->host = $parsedDsn['host'];
48+
$this->host = $params['host'];
4949

50-
$this->user = '' !== ($parsedDsn['user'] ?? '') ? urldecode($parsedDsn['user']) : null;
51-
$this->password = '' !== ($parsedDsn['pass'] ?? '') ? urldecode($parsedDsn['pass']) : null;
52-
$this->port = $parsedDsn['port'] ?? null;
53-
$this->path = $parsedDsn['path'] ?? null;
54-
parse_str($parsedDsn['query'] ?? '', $this->options);
50+
$this->user = '' !== ($params['user'] ?? '') ? rawurldecode($params['user']) : null;
51+
$this->password = '' !== ($params['pass'] ?? '') ? rawurldecode($params['pass']) : null;
52+
$this->port = $params['port'] ?? null;
53+
$this->path = $params['path'] ?? null;
54+
parse_str($params['query'] ?? '', $this->options);
5555
}
5656

5757
public function getScheme(): string

src/Symfony/Component/Translation/Provider/Dsn.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ public function __construct(string $dsn)
3333
{
3434
$this->originalDsn = $dsn;
3535

36-
if (false === $parsedDsn = parse_url($dsn)) {
36+
if (false === $params = parse_url($dsn)) {
3737
throw new InvalidArgumentException('The translation provider DSN is invalid.');
3838
}
3939

40-
if (!isset($parsedDsn['scheme'])) {
40+
if (!isset($params['scheme'])) {
4141
throw new InvalidArgumentException('The translation provider DSN must contain a scheme.');
4242
}
43-
$this->scheme = $parsedDsn['scheme'];
43+
$this->scheme = $params['scheme'];
4444

45-
if (!isset($parsedDsn['host'])) {
45+
if (!isset($params['host'])) {
4646
throw new InvalidArgumentException('The translation provider DSN must contain a host (use "default" by default).');
4747
}
48-
$this->host = $parsedDsn['host'];
48+
$this->host = $params['host'];
4949

50-
$this->user = '' !== ($parsedDsn['user'] ?? '') ? urldecode($parsedDsn['user']) : null;
51-
$this->password = '' !== ($parsedDsn['pass'] ?? '') ? urldecode($parsedDsn['pass']) : null;
52-
$this->port = $parsedDsn['port'] ?? null;
53-
$this->path = $parsedDsn['path'] ?? null;
54-
parse_str($parsedDsn['query'] ?? '', $this->options);
50+
$this->user = '' !== ($params['user'] ?? '') ? rawurldecode($params['user']) : null;
51+
$this->password = '' !== ($params['pass'] ?? '') ? rawurldecode($params['pass']) : null;
52+
$this->port = $params['port'] ?? null;
53+
$this->path = $params['path'] ?? null;
54+
parse_str($params['query'] ?? '', $this->options);
5555
}
5656

5757
public function getScheme(): string

0 commit comments

Comments
 (0)
0