Skip to content

Commit

Permalink
Remove full DSNs from exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jan 23, 2023
1 parent 395eb9a commit 8258695
Show file tree
Hide file tree
Showing 42 changed files with 88 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
return CouchbaseCollectionAdapter::createConnection($dsn, $options);
}

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

public function commit(): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser

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

preg_match($dsnPattern, $dsn, $matches);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $dsn

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

preg_match($dsnPattern, $server, $matches);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(Connection|string $connOrDsn, string $namespace = ''
$this->conn = $connOrDsn;
} else {
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrDsn));
throw new InvalidArgumentException('Failed to parse DSN. Try running "composer require doctrine/dbal".');
}
$this->conn = DriverManager::getConnection(['url' => $connOrDsn]);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
continue;
}
if (!str_starts_with($dsn, 'memcached:')) {
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s" does not start with "memcached:".', $dsn));
throw new InvalidArgumentException('Invalid Memcached DSN: it does not start with "memcached:".');
}
$params = preg_replace_callback('#^memcached:(//)?(?:([^@]*+)@)?#', function ($m) use (&$username, &$password) {
if (!empty($m[2])) {
Expand All @@ -123,15 +123,15 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
return 'file:'.($m[1] ?? '');
}, $dsn);
if (false === $params = parse_url($params)) {
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
throw new InvalidArgumentException('Invalid Memcached DSN.');
}
$query = $hosts = [];
if (isset($params['query'])) {
parse_str($params['query'], $query);

if (isset($query['host'])) {
if (!\is_array($hosts = $query['host'])) {
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
throw new InvalidArgumentException('Invalid Memcached DSN: query parameter "host" must be an array.');
}
foreach ($hosts as $host => $weight) {
if (false === $port = strrpos($host, ':')) {
Expand All @@ -150,7 +150,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $ser
}
}
if (!isset($params['host']) && !isset($params['path'])) {
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
throw new InvalidArgumentException('Invalid Memcached DSN: missing host or path.');
}
if (isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) {
$params['weight'] = $m[1];
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PdoAdapter extends AbstractAdapter implements PruneableInterface
public function __construct(#[\SensitiveParameter] \PDO|string $connOrDsn, string $namespace = '', int $defaultLifetime = 0, array $options = [], MarshallerInterface $marshaller = null)
{
if (\is_string($connOrDsn) && str_contains($connOrDsn, '://')) {
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));
throw new InvalidArgumentException(sprintf('Usage of Doctrine DBAL URL with "%s" is not supported. Use a PDO DSN or "%s" instead.', __CLASS__, DoctrineDbalAdapter::class));
}

if (isset($namespace[0]) && preg_match('#[^-+.A-Za-z0-9]#', $namespace, $match)) {
Expand Down
30 changes: 15 additions & 15 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
} elseif (str_starts_with($dsn, 'rediss:')) {
$scheme = 'rediss';
} else {
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s" does not start with "redis:" or "rediss".', $dsn));
throw new InvalidArgumentException('Invalid Redis DSN: it does not start with "redis[s]:".');
}

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

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

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

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

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

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

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

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

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

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

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

if (isset($params['redis_sentinel']) && !is_a($class, \Predis\Client::class, true) && !class_exists(\RedisSentinel::class)) {
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));
throw new CacheException(sprintf('Cannot use Redis Sentinel: class "%s" does not extend "Predis\Client" and ext-redis >= 5.2 not found.', $class));
}
}

Expand Down Expand Up @@ -213,7 +213,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
} while (++$hostIndex < \count($hosts) && !$address);

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

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

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

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

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

if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) {
Expand All @@ -287,7 +287,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
try {
$redis = new $class(null, $hosts, $params['timeout'], $params['read_timeout'], (bool) $params['persistent'], $params['auth'] ?? '', ...\defined('Redis::SCAN_PREFIX') ? [$params['ssl'] ?? null] : []);
} catch (\RedisClusterException $e) {
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
throw new InvalidArgumentException('Redis connection failed: '.$e->getMessage());
}

if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static function createHandler(object|string $connection, array $options =

case !\is_string($connection):
throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', get_debug_type($connection)));

case str_starts_with($connection, 'file://'):
$savePath = substr($connection, 7);

Expand All @@ -54,7 +55,7 @@ public static function createHandler(object|string $connection, array $options =
case str_starts_with($connection, 'rediss:'):
case str_starts_with($connection, 'memcached:'):
if (!class_exists(AbstractAdapter::class)) {
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
throw new \InvalidArgumentException('Unsupported Redis or Memcached DSN. Try running "composer require symfony/cache".');
}
$handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
Expand All @@ -63,7 +64,7 @@ public static function createHandler(object|string $connection, array $options =

case str_starts_with($connection, 'pdo_oci://'):
if (!class_exists(DriverManager::class)) {
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require doctrine/dbal".', $connection));
throw new \InvalidArgumentException('Unsupported PDO OCI DSN. Try running "composer require doctrine/dbal".');
}
$connection = DriverManager::getConnection(['url' => $connection])->getWrappedConnection();
// no break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(#[\SensitiveParameter] Connection|string $connOrUrl)
$this->conn = $connOrUrl;
} else {
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl));
throw new InvalidArgumentException('Failed to parse DSN. Try running "composer require doctrine/dbal".');
}
$this->conn = DriverManager::getConnection(['url' => $this->filterDsn($connOrUrl)]);
}
Expand Down Expand Up @@ -246,7 +246,7 @@ private function unlockShared(Key $key): void
private function filterDsn(#[\SensitiveParameter] string $dsn): string
{
if (!str_contains($dsn, '://')) {
throw new InvalidArgumentException(sprintf('String "%" is not a valid DSN for Doctrine DBAL.', $dsn));
throw new InvalidArgumentException('DSN is invalid for Doctrine DBAL.');
}

[$scheme, $rest] = explode(':', $dsn, 2);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Lock/Store/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(Connection|string $connOrUrl, array $options = [], f
$this->conn = $connOrUrl;
} else {
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl));
throw new InvalidArgumentException('Failed to parse the DSN. Try running "composer require doctrine/dbal".');
}
$this->conn = DriverManager::getConnection(['url' => $connOrUrl]);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Lock/Store/StoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
class StoreFactory
{
public static function createStore(object|string $connection): PersistingStoreInterface
public static function createStore(#[\SensitiveParameter] object|string $connection): PersistingStoreInterface
{
switch (true) {
case $connection instanceof \Redis:
Expand All @@ -49,6 +49,7 @@ public static function createStore(object|string $connection): PersistingStoreIn

case !\is_string($connection):
throw new InvalidArgumentException(sprintf('Unsupported Connection: "%s".', get_debug_type($connection)));

case 'flock' === $connection:
return new FlockStore();

Expand All @@ -62,7 +63,7 @@ public static function createStore(object|string $connection): PersistingStoreIn
case str_starts_with($connection, 'rediss:'):
case str_starts_with($connection, 'memcached:'):
if (!class_exists(AbstractAdapter::class)) {
throw new InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
throw new InvalidArgumentException('Unsupported Redis or Memcached DSN. Try running "composer require symfony/cache".');
}
$storeClass = str_starts_with($connection, 'memcached:') ? MemcachedStore::class : RedisStore::class;
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Lock/Store/ZookeeperStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public function __construct(\Zookeeper $zookeeper)
public static function createConnection(#[\SensitiveParameter] string $dsn): \Zookeeper
{
if (!str_starts_with($dsn, 'zookeeper:')) {
throw new InvalidArgumentException(sprintf('Unsupported DSN: "%s".', $dsn));
throw new InvalidArgumentException('Unsupported DSN for Zookeeper.');
}

if (false === $params = parse_url($dsn)) {
throw new InvalidArgumentException(sprintf('Invalid Zookeeper DSN: "%s".', $dsn));
throw new InvalidArgumentException('Invalid Zookeeper DSN.');
}

$host = $params['host'] ?? '';
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Mailer/Tests/Transport/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ public function invalidDsnProvider(): iterable
{
yield [
'some://',
'The "some://" mailer DSN is invalid.',
'The mailer DSN is invalid.',
];

yield [
'//sendmail',
'The "//sendmail" mailer DSN must contain a scheme.',
'The mailer DSN must contain a scheme.',
];

yield [
'file:///some/path',
'The "file:///some/path" mailer DSN must contain a host (use "default" by default).',
'The mailer DSN must contain a host (use "default" by default).',
];
}
}
Loading

0 comments on commit 8258695

Please sign in to comment.