From ca2b528d9a9939ca068fa01f0cddbce6cebcff13 Mon Sep 17 00:00:00 2001 From: Marko Kunic Date: Tue, 5 Mar 2019 21:04:46 +0000 Subject: [PATCH] Throw FOSCKEditorBundle exception on bad proxy url (#176) --- src/Exception/BadProxyUrlException.php | 26 +++++++++++++++++++++++ src/Installer/CKEditorInstaller.php | 6 ++++++ tests/Installer/CKEditorInstallerTest.php | 23 ++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 src/Exception/BadProxyUrlException.php diff --git a/src/Exception/BadProxyUrlException.php b/src/Exception/BadProxyUrlException.php new file mode 100644 index 00000000..1f00f258 --- /dev/null +++ b/src/Exception/BadProxyUrlException.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\CKEditorBundle\Exception; + +use RuntimeException; + +/** + * @author Marko Kunic + */ +final class BadProxyUrlException extends RuntimeException implements FOSCKEditorException +{ + public static function fromEnvUrl(string $url): self + { + return new static(sprintf('Unable to parse provided proxy url "%s".', $url)); + } +} diff --git a/src/Installer/CKEditorInstaller.php b/src/Installer/CKEditorInstaller.php index 3a5b5348..af55cf90 100644 --- a/src/Installer/CKEditorInstaller.php +++ b/src/Installer/CKEditorInstaller.php @@ -12,6 +12,7 @@ namespace FOS\CKEditorBundle\Installer; +use FOS\CKEditorBundle\Exception\BadProxyUrlException; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -186,6 +187,11 @@ private function createStreamContext(callable $notifier = null) if ($proxy) { $proxyUrl = parse_url($proxy); + + if (false === $proxyUrl || !isset($proxyUrl['host']) || !isset($proxyUrl['port'])) { + throw BadProxyUrlException::fromEnvUrl($proxy); + } + $context['http'] = [ 'proxy' => 'tcp://'.$proxyUrl['host'].':'.$proxyUrl['port'], 'request_fulluri' => (bool) getenv('https_proxy_request_fulluri') ?: diff --git a/tests/Installer/CKEditorInstallerTest.php b/tests/Installer/CKEditorInstallerTest.php index db21c0f4..14d7c499 100644 --- a/tests/Installer/CKEditorInstallerTest.php +++ b/tests/Installer/CKEditorInstallerTest.php @@ -12,6 +12,7 @@ namespace FOS\CKEditorBundle\Tests\Installer; +use FOS\CKEditorBundle\Exception\BadProxyUrlException; use FOS\CKEditorBundle\Installer\CKEditorInstaller; use PHPUnit\Framework\TestCase; @@ -143,6 +144,28 @@ public function testInstallWithHttpsProxyRequestFullUri(): void $this->assertInstall(); } + /** + * @group proxy + */ + public function testInstallWithProxyUrlMissingHost(): void + { + putenv('http_proxy=notgonnahappen'); + + $this->expectException(BadProxyUrlException::class); + $this->installer->install(); + } + + /** + * @group proxy + */ + public function testInstallWithProxyUrlMissingPort(): void + { + putenv('http_proxy=http://notgonnahappen.com'); + + $this->expectException(BadProxyUrlException::class); + $this->installer->install(); + } + public function testReinstall(): void { $this->installer->install();