Skip to content

Commit 37f87a2

Browse files
Add http(s):// validation for graph/auth endpoints
1 parent 1c74239 commit 37f87a2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/Tests/Transport/MicrosoftGraphTransportFactoryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Mailer\Bridge\MicrosoftGraph\TokenManager;
1717
use Symfony\Component\Mailer\Bridge\MicrosoftGraph\Transport\MicrosoftGraphApiTransport;
1818
use Symfony\Component\Mailer\Bridge\MicrosoftGraph\Transport\MicrosoftGraphTransportFactory;
19+
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
1920
use Symfony\Component\Mailer\Test\AbstractTransportFactoryTestCase;
2021
use Symfony\Component\Mailer\Test\IncompleteDsnTestTrait;
2122
use Symfony\Component\Mailer\Transport\Dsn;
@@ -74,4 +75,23 @@ public static function incompleteDsnProvider(): iterable
7475
yield [new Dsn('microsoft+graphapi', 'default', self::USER, self::PASSWORD)];
7576
yield [new Dsn('microsoft+graphapi', 'non-default', self::USER, self::PASSWORD, null, ['tenantId' => self::TENANT])];
7677
}
78+
79+
/** @dataProvider invalidHttpDsnProvider */
80+
public function testValidatesHttpNotProvided(string $graph, string $auth, string $failingType)
81+
{
82+
$factory = $this->getFactory();
83+
$dsn = new Dsn('microsoft+graphapi', $graph, self::USER, self::PASSWORD, null, ['tenantId' => self::TENANT, 'authEndpoint' => $auth]);
84+
85+
$this->expectException(InvalidArgumentException::class);
86+
$this->expectExceptionMessage($failingType.' endpoint needs to be provided without http(s)://.');
87+
$factory->create($dsn);
88+
}
89+
90+
public static function invalidHttpDsnProvider(): iterable
91+
{
92+
yield ['http://graph', 'auth', 'Graph'];
93+
yield ['https://graph', 'auth', 'Graph'];
94+
yield ['graph', 'http://auth', 'Auth'];
95+
yield ['graph', 'https://auth', 'Auth'];
96+
}
7797
}

src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/Transport/MicrosoftGraphTransportFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Mailer\Bridge\MicrosoftGraph\TokenManager;
1515
use Symfony\Component\Mailer\Exception\IncompleteDsnException;
16+
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
1617
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
1718
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
1819
use Symfony\Component\Mailer\Transport\Dsn;
@@ -44,6 +45,14 @@ public function create(Dsn $dsn): TransportInterface
4445
throw new IncompleteDsnException("Transport 'microsoft+graphapi' requires the 'authEndpoint' option when not using the default graph endpoint.");
4546
}
4647

48+
if (0 !== preg_match('#^https?://#', $authEndpoint)) {
49+
throw new InvalidArgumentException('Auth endpoint needs to be provided without http(s)://.');
50+
}
51+
52+
if (0 !== preg_match('#^https?://#', $graphEndpoint)) {
53+
throw new InvalidArgumentException('Graph endpoint needs to be provided without http(s)://.');
54+
}
55+
4756
$tokenManager = new TokenManager($graphEndpoint, $authEndpoint, $tenantId, $this->getUser($dsn), $this->getPassword($dsn), $this->client);
4857

4958
return new MicrosoftGraphApiTransport($graphEndpoint, $tokenManager, $dsn->getBooleanOption('noSave'), $this->client, $this->dispatcher, $this->logger);

0 commit comments

Comments
 (0)