From 041cb46e523943aeaf8e970c4d6174fef2906b96 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Thu, 17 Dec 2020 08:13:15 +0100 Subject: [PATCH] [Mailer] Fix parsing Dsn with empty user/password --- .../Mailer/Tests/Transport/DsnTest.php | 20 +++++++++++++++++++ .../Component/Mailer/Transport/Dsn.php | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Mailer/Tests/Transport/DsnTest.php b/src/Symfony/Component/Mailer/Tests/Transport/DsnTest.php index 04ee14c6cb3df..31d860fd13072 100644 --- a/src/Symfony/Component/Mailer/Tests/Transport/DsnTest.php +++ b/src/Symfony/Component/Mailer/Tests/Transport/DsnTest.php @@ -52,6 +52,26 @@ public function fromStringProvider(): iterable new Dsn('smtp', 'example.com'), ]; + yield 'simple dsn including @ sign, but no user/password/token' => [ + 'scheme://@localhost', + new Dsn('scheme', 'localhost', null, null), + ]; + + yield 'simple dsn including : sign and @ sign, but no user/password/token' => [ + 'scheme://:@localhost', + new Dsn('scheme', 'localhost', null, null), + ]; + + yield 'simple dsn including user, : sign and @ sign, but no password' => [ + 'scheme://user1:@localhost', + new Dsn('scheme', 'localhost', 'user1', null), + ]; + + yield 'simple dsn including : sign, password, and @ sign, but no user' => [ + 'scheme://:pass@localhost', + new Dsn('scheme', 'localhost', null, 'pass'), + ]; + yield 'simple smtp with custom port' => [ 'smtp://user1:pass2@example.com:99', new Dsn('smtp', 'example.com', 'user1', 'pass2', 99), diff --git a/src/Symfony/Component/Mailer/Transport/Dsn.php b/src/Symfony/Component/Mailer/Transport/Dsn.php index b5c2587d6a632..cc25f7a237832 100644 --- a/src/Symfony/Component/Mailer/Transport/Dsn.php +++ b/src/Symfony/Component/Mailer/Transport/Dsn.php @@ -49,8 +49,8 @@ public static function fromString(string $dsn): self throw new InvalidArgumentException(sprintf('The "%s" mailer DSN must contain a host (use "default" by default).', $dsn)); } - $user = isset($parsedDsn['user']) ? urldecode($parsedDsn['user']) : null; - $password = isset($parsedDsn['pass']) ? urldecode($parsedDsn['pass']) : null; + $user = '' !== ($parsedDsn['user'] ?? '') ? urldecode($parsedDsn['user']) : null; + $password = '' !== ($parsedDsn['pass'] ?? '') ? urldecode($parsedDsn['pass']) : null; $port = $parsedDsn['port'] ?? null; parse_str($parsedDsn['query'] ?? '', $query);