Skip to content

Commit 2911875

Browse files
minor #31993 [Mailer] Catch missing scheme in DSN (derrabus)
This PR was merged into the 4.3 branch. Discussion ---------- [Mailer] Catch missing scheme in DSN | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | N/A The `Symfony\Component\Mailer\Transport::createTransport()` method parses and validates a passed DSN. I noticed that we never check if the DSN contains a valid scheme, but we always assume that the scheme is present in then parse result. If someone passes a DSN without a scheme to that method, they would almost certainly run into a PHP notice. This PR makes sure that a scheme is present in the URL and throws a proper exception otherwise. Commits ------- 3eba36c088 [Mailer] Catch missing scheme in DSN.
2 parents 4407c59 + 3337099 commit 2911875

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

Tests/TransportTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,18 @@ public function testFromInvalidDsn()
7171
Transport::fromDsn('some://');
7272
}
7373

74+
public function testNoScheme()
75+
{
76+
$this->expectException(InvalidArgumentException::class);
77+
$this->expectExceptionMessage('The "//sendmail" mailer DSN must contain a transport scheme.');
78+
Transport::fromDsn('//sendmail');
79+
}
80+
7481
public function testFromInvalidDsnNoHost()
7582
{
7683
$this->expectException(InvalidArgumentException::class);
77-
$this->expectExceptionMessage('The "?!" mailer DSN must contain a mailer name.');
78-
Transport::fromDsn('?!');
84+
$this->expectExceptionMessage('The "file:///some/path" mailer DSN must contain a mailer name.');
85+
Transport::fromDsn('file:///some/path');
7986
}
8087

8188
public function testFromInvalidTransportName()

Transport.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ private static function createTransport(string $dsn, EventDispatcherInterface $d
6464
throw new InvalidArgumentException(sprintf('The "%s" mailer DSN is invalid.', $dsn));
6565
}
6666

67+
if (!isset($parsedDsn['scheme'])) {
68+
throw new InvalidArgumentException(sprintf('The "%s" mailer DSN must contain a transport scheme.', $dsn));
69+
}
70+
6771
if (!isset($parsedDsn['host'])) {
6872
throw new InvalidArgumentException(sprintf('The "%s" mailer DSN must contain a mailer name.', $dsn));
6973
}

0 commit comments

Comments
 (0)