Skip to content

Commit

Permalink
Throw FOSCKEditorBundle exception on bad proxy url (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 authored Mar 5, 2019
1 parent 4aa4256 commit ca2b528
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Exception/BadProxyUrlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the FOSCKEditor Bundle.
*
* (c) 2018 - present Friends of Symfony
* (c) 2009 - 2017 Eric GELOEN <[email protected]>
*
* 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 <[email protected]>
*/
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));
}
}
6 changes: 6 additions & 0 deletions src/Installer/CKEditorInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace FOS\CKEditorBundle\Installer;

use FOS\CKEditorBundle\Exception\BadProxyUrlException;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -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') ?:
Expand Down
23 changes: 23 additions & 0 deletions tests/Installer/CKEditorInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace FOS\CKEditorBundle\Tests\Installer;

use FOS\CKEditorBundle\Exception\BadProxyUrlException;
use FOS\CKEditorBundle\Installer\CKEditorInstaller;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ca2b528

Please sign in to comment.