Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 290d5e5

Browse files
minor #49072 Remove full DSNs from exception messages (nicolas-grekas)
This PR was merged into the 6.3 branch. Discussion ---------- Remove full DSNs from exception messages | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Follow up of #49065 We should be careful when reviewing notifier bridges, to ensure that transports' `__toString()` don't embed any secrets. /cc `@OskarStark` Commits ------- 1cbd95e Remove full DSNs from exception messages
2 parents 395eb9a + 1cbd95e commit 290d5e5

File tree

47 files changed

+103
-101
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+103
-101
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
126126
return CouchbaseCollectionAdapter::createConnection($dsn, $options);
127127
}
128128

129-
throw new InvalidArgumentException(sprintf('Unsupported DSN: "%s".', $dsn));
129+
throw new InvalidArgumentException('Unsupported DSN: it does not start with "redis[s]:", "memcached:" nor "couchbase:".');
130130
}
131131

132132
public function commit(): bool

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
7878

7979
foreach ($servers as $dsn) {
8080
if (!str_starts_with($dsn, 'couchbase:')) {
81-
throw new InvalidArgumentException(sprintf('Invalid Couchbase DSN: "%s" does not start with "couchbase:".', $dsn));
81+
throw new InvalidArgumentException('Invalid Couchbase DSN: it does not start with "couchbase:".');
8282
}
8383

8484
preg_match($dsnPattern, $dsn, $matches);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $dsn
7171

7272
foreach ($dsn as $server) {
7373
if (!str_starts_with($server, 'couchbase:')) {
74-
throw new InvalidArgumentException(sprintf('Invalid Couchbase DSN: "%s" does not start with "couchbase:".', $server));
74+
throw new InvalidArgumentException('Invalid Couchbase DSN: it does not start with "couchbase:".');
7575
}
7676

7777
preg_match($dsnPattern, $server, $matches);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(Connection|string $connOrDsn, string $namespace = ''
6464
$this->conn = $connOrDsn;
6565
} else {
6666
if (!class_exists(DriverManager::class)) {
67-
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrDsn));
67+
throw new InvalidArgumentException('Failed to parse DSN. Try running "composer require doctrine/dbal".');
6868
}
6969
$this->conn = DriverManager::getConnection(['url' => $connOrDsn]);
7070
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
113113
continue;
114114
}
115115
if (!str_starts_with($dsn, 'memcached:')) {
116-
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s" does not start with "memcached:".', $dsn));
116+
throw new InvalidArgumentException('Invalid Memcached DSN: it does not start with "memcached:".');
117117
}
118118
$params = preg_replace_callback('#^memcached:(//)?(?:([^@]*+)@)?#', function ($m) use (&$username, &$password) {
119119
if (!empty($m[2])) {
@@ -123,15 +123,15 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
123123
return 'file:'.($m[1] ?? '');
124124
}, $dsn);
125125
if (false === $params = parse_url($params)) {
126-
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
126+
throw new InvalidArgumentException('Invalid Memcached DSN.');
127127
}
128128
$query = $hosts = [];
129129
if (isset($params['query'])) {
130130
parse_str($params['query'], $query);
131131

132132
if (isset($query['host'])) {
133133
if (!\is_array($hosts = $query['host'])) {
134-
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
134+
throw new InvalidArgumentException('Invalid Memcached DSN: query parameter "host" must be an array.');
135135
}
136136
foreach ($hosts as $host => $weight) {
137137
if (false === $port = strrpos($host, ':')) {
@@ -150,7 +150,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
150150
}
151151
}
152152
if (!isset($params['host']) && !isset($params['path'])) {
153-
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
153+
throw new InvalidArgumentException('Invalid Memcached DSN: missing host or path.');
154154
}
155155
if (isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) {
156156
$params['weight'] = $m[1];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class PdoAdapter extends AbstractAdapter implements PruneableInterface
5858
public function __construct(#[\SensitiveParameter] \PDO|string $connOrDsn, string $namespace = '', int $defaultLifetime = 0, array $options = [], MarshallerInterface $marshaller = null)
5959
{
6060
if (\is_string($connOrDsn) && str_contains($connOrDsn, '://')) {
61-
throw new InvalidArgumentException(sprintf('Usage of Doctrine DBAL URL with "%s" is not supported. Use a PDO DSN or "%s" instead. Got "%s".', __CLASS__, DoctrineDbalAdapter::class, $connOrDsn));
61+
throw new InvalidArgumentException(sprintf('Usage of Doctrine DBAL URL with "%s" is not supported. Use a PDO DSN or "%s" instead.', __CLASS__, DoctrineDbalAdapter::class));
6262
}
6363

6464
if (isset($namespace[0]) && preg_match('#[^-+.A-Za-z0-9]#', $namespace, $match)) {

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
8787
} elseif (str_starts_with($dsn, 'rediss:')) {
8888
$scheme = 'rediss';
8989
} else {
90-
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s" does not start with "redis:" or "rediss".', $dsn));
90+
throw new InvalidArgumentException('Invalid Redis DSN: it does not start with "redis[s]:".');
9191
}
9292

9393
if (!\extension_loaded('redis') && !class_exists(\Predis\Client::class)) {
94-
throw new CacheException(sprintf('Cannot find the "redis" extension nor the "predis/predis" package: "%s".', $dsn));
94+
throw new CacheException('Cannot find the "redis" extension nor the "predis/predis" package.');
9595
}
9696

9797
$params = preg_replace_callback('#^'.$scheme.':(//)?(?:(?:(?<user>[^:@]*+):)?(?<password>[^@]*+)@)?#', function ($m) use (&$auth) {
@@ -111,7 +111,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
111111
}, $dsn);
112112

113113
if (false === $params = parse_url($params)) {
114-
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
114+
throw new InvalidArgumentException('Invalid Redis DSN.');
115115
}
116116

117117
$query = $hosts = [];
@@ -124,7 +124,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
124124

125125
if (isset($query['host'])) {
126126
if (!\is_array($hosts = $query['host'])) {
127-
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
127+
throw new InvalidArgumentException('Invalid Redis DSN: query parameter "host" must be an array.');
128128
}
129129
foreach ($hosts as $host => $parameters) {
130130
if (\is_string($parameters)) {
@@ -148,7 +148,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
148148
$params['dbindex'] = $m[1];
149149
$params['path'] = substr($params['path'], 0, -\strlen($m[0]));
150150
} elseif (isset($params['host'])) {
151-
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s", the "dbindex" parameter must be a number.', $dsn));
151+
throw new InvalidArgumentException('Invalid Redis DSN: query parameter "dbindex" must be a number.');
152152
}
153153
}
154154

@@ -160,17 +160,17 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
160160
}
161161

162162
if (!$hosts) {
163-
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
163+
throw new InvalidArgumentException('Invalid Redis DSN: missing host.');
164164
}
165165

166166
$params += $query + $options + self::$defaultConnectionOptions;
167167

168168
if (isset($params['redis_sentinel']) && !class_exists(\Predis\Client::class) && !class_exists(\RedisSentinel::class)) {
169-
throw new CacheException(sprintf('Redis Sentinel support requires the "predis/predis" package or the "redis" extension v5.2 or higher: "%s".', $dsn));
169+
throw new CacheException('Redis Sentinel support requires the "predis/predis" package or the "redis" extension v5.2 or higher.');
170170
}
171171

172172
if ($params['redis_cluster'] && isset($params['redis_sentinel'])) {
173-
throw new InvalidArgumentException(sprintf('Cannot use both "redis_cluster" and "redis_sentinel" at the same time: "%s".', $dsn));
173+
throw new InvalidArgumentException('Cannot use both "redis_cluster" and "redis_sentinel" at the same time.');
174174
}
175175

176176
if (null === $params['class'] && \extension_loaded('redis')) {
@@ -179,14 +179,14 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
179179
$class = $params['class'] ?? \Predis\Client::class;
180180

181181
if (isset($params['redis_sentinel']) && !is_a($class, \Predis\Client::class, true) && !class_exists(\RedisSentinel::class)) {
182-
throw new CacheException(sprintf('Cannot use Redis Sentinel: class "%s" does not extend "Predis\Client" and ext-redis >= 5.2 not found: "%s".', $class, $dsn));
182+
throw new CacheException(sprintf('Cannot use Redis Sentinel: class "%s" does not extend "Predis\Client" and ext-redis >= 5.2 not found.', $class));
183183
}
184184
}
185185

186186
if (is_a($class, \Redis::class, true)) {
187187
$connect = $params['persistent'] || $params['persistent_id'] ? 'pconnect' : 'connect';
188188

189-
$initializer = static function () use ($class, $connect, $params, $dsn, $auth, $hosts, $tls) {
189+
$initializer = static function () use ($class, $connect, $params, $auth, $hosts, $tls) {
190190
$redis = new $class();
191191
$hostIndex = 0;
192192
do {
@@ -213,7 +213,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
213213
} while (++$hostIndex < \count($hosts) && !$address);
214214

215215
if (isset($params['redis_sentinel']) && !$address) {
216-
throw new InvalidArgumentException(sprintf('Failed to retrieve master information from sentinel "%s" and dsn "%s".', $params['redis_sentinel'], $dsn));
216+
throw new InvalidArgumentException(sprintf('Failed to retrieve master information from sentinel "%s".', $params['redis_sentinel']));
217217
}
218218

219219
try {
@@ -233,21 +233,21 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
233233
}
234234
if (!$isConnected) {
235235
$error = preg_match('/^Redis::p?connect\(\): (.*)/', $error ?? $redis->getLastError() ?? '', $error) ? sprintf(' (%s)', $error[1]) : '';
236-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$error.'.');
236+
throw new InvalidArgumentException('Redis connection failed: '.$error.'.');
237237
}
238238

239239
if ((null !== $auth && !$redis->auth($auth))
240240
|| ($params['dbindex'] && !$redis->select($params['dbindex']))
241241
) {
242242
$e = preg_replace('/^ERR /', '', $redis->getLastError());
243-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e.'.');
243+
throw new InvalidArgumentException('Redis connection failed: '.$e.'.');
244244
}
245245

246246
if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) {
247247
$redis->setOption(\Redis::OPT_TCP_KEEPALIVE, $params['tcp_keepalive']);
248248
}
249249
} catch (\RedisException $e) {
250-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
250+
throw new InvalidArgumentException('Redis connection failed: '.$e->getMessage());
251251
}
252252

253253
return $redis;
@@ -268,14 +268,14 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
268268
try {
269269
$redis = new $class($hosts, $params);
270270
} catch (\RedisClusterException $e) {
271-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
271+
throw new InvalidArgumentException('Redis connection failed: '.$e->getMessage());
272272
}
273273

274274
if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) {
275275
$redis->setOption(\Redis::OPT_TCP_KEEPALIVE, $params['tcp_keepalive']);
276276
}
277277
} elseif (is_a($class, \RedisCluster::class, true)) {
278-
$initializer = static function () use ($class, $params, $dsn, $hosts) {
278+
$initializer = static function () use ($class, $params, $hosts) {
279279
foreach ($hosts as $i => $host) {
280280
$hosts[$i] = match ($host['scheme']) {
281281
'tcp' => $host['host'].':'.$host['port'],
@@ -287,7 +287,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
287287
try {
288288
$redis = new $class(null, $hosts, $params['timeout'], $params['read_timeout'], (bool) $params['persistent'], $params['auth'] ?? '', ...\defined('Redis::SCAN_PREFIX') ? [$params['ssl'] ?? null] : []);
289289
} catch (\RedisClusterException $e) {
290-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
290+
throw new InvalidArgumentException('Redis connection failed: '.$e->getMessage());
291291
}
292292

293293
if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) {

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/SessionHandlerFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function createHandler(object|string $connection, array $options =
5454
case str_starts_with($connection, 'rediss:'):
5555
case str_starts_with($connection, 'memcached:'):
5656
if (!class_exists(AbstractAdapter::class)) {
57-
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
57+
throw new \InvalidArgumentException('Unsupported Redis or Memcached DSN. Try running "composer require symfony/cache".');
5858
}
5959
$handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
6060
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
@@ -63,7 +63,7 @@ public static function createHandler(object|string $connection, array $options =
6363

6464
case str_starts_with($connection, 'pdo_oci://'):
6565
if (!class_exists(DriverManager::class)) {
66-
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require doctrine/dbal".', $connection));
66+
throw new \InvalidArgumentException('Unsupported PDO OCI DSN. Try running "composer require doctrine/dbal".');
6767
}
6868
$connection = DriverManager::getConnection(['url' => $connection])->getWrappedConnection();
6969
// no break;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(#[\SensitiveParameter] Connection|string $connOrUrl)
4747
$this->conn = $connOrUrl;
4848
} else {
4949
if (!class_exists(DriverManager::class)) {
50-
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl));
50+
throw new InvalidArgumentException('Failed to parse DSN. Try running "composer require doctrine/dbal".');
5151
}
5252
$this->conn = DriverManager::getConnection(['url' => $this->filterDsn($connOrUrl)]);
5353
}
@@ -246,7 +246,7 @@ private function unlockShared(Key $key): void
246246
private function filterDsn(#[\SensitiveParameter] string $dsn): string
247247
{
248248
if (!str_contains($dsn, '://')) {
249-
throw new InvalidArgumentException(sprintf('String "%" is not a valid DSN for Doctrine DBAL.', $dsn));
249+
throw new InvalidArgumentException('DSN is invalid for Doctrine DBAL.');
250250
}
251251

252252
[$scheme, $rest] = explode(':', $dsn, 2);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __construct(Connection|string $connOrUrl, array $options = [], f
6666
$this->conn = $connOrUrl;
6767
} else {
6868
if (!class_exists(DriverManager::class)) {
69-
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl));
69+
throw new InvalidArgumentException('Failed to parse the DSN. Try running "composer require doctrine/dbal".');
7070
}
7171
$this->conn = DriverManager::getConnection(['url' => $connOrUrl]);
7272
}

0 commit comments

Comments
 (0)