forked from hwi/HWIOAuthBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add domains white list for target path url (hwi#1600)
* add domains white list for target path url * cr fixes * use expectException and expectExceptionMessage to avoid deperactedd warning for PHPUnit 9.
- Loading branch information
Showing
9 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,12 @@ | |
namespace HWI\Bundle\OAuthBundle\Controller; | ||
|
||
use HWI\Bundle\OAuthBundle\Security\OAuthUtils; | ||
use HWI\Bundle\OAuthBundle\Util\DomainWhitelist; | ||
use RuntimeException; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | ||
|
||
/** | ||
* @author Alexander <[email protected]> | ||
|
@@ -27,6 +29,11 @@ final class RedirectToServiceController | |
*/ | ||
private $oauthUtils; | ||
|
||
/** | ||
* @var DomainWhitelist | ||
*/ | ||
private $domainWhitelist; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
|
@@ -47,9 +54,10 @@ final class RedirectToServiceController | |
*/ | ||
private $useReferer; | ||
|
||
public function __construct(OAuthUtils $oauthUtils, array $firewallNames, ?string $targetPathParameter, bool $failedUseReferer, bool $useReferer) | ||
public function __construct(OAuthUtils $oauthUtils, DomainWhitelist $domainWhitelist, array $firewallNames, ?string $targetPathParameter, bool $failedUseReferer, bool $useReferer) | ||
{ | ||
$this->oauthUtils = $oauthUtils; | ||
$this->domainWhitelist = $domainWhitelist; | ||
$this->firewallNames = $firewallNames; | ||
$this->targetPathParameter = $targetPathParameter; | ||
$this->failedUseReferer = $failedUseReferer; | ||
|
@@ -92,6 +100,11 @@ private function storeReturnPath(Request $request, string $authorizationUrl): vo | |
$sessionKeyFailure = '_security.'.$providerKey.'.failed_target_path'; | ||
|
||
if (!empty($param) && $targetUrl = $request->get($param)) { | ||
|
||
if (!$this->domainWhitelist->isValidTargetUrl($targetUrl)) { | ||
throw new AccessDeniedHttpException('Not allowed to redirect to '.$targetUrl); | ||
} | ||
|
||
$session->set($sessionKey, $targetUrl); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="hwi_oauth.util.domain_whitelist" class="HWI\Bundle\OAuthBundle\Util\DomainWhitelist"> | ||
<argument>%hwi_oauth.target_path_domains_whitelist%</argument> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the HWIOAuthBundle package. | ||
* | ||
* (c) Hardware Info <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace HWI\Bundle\OAuthBundle\Tests\Util; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use HWI\Bundle\OAuthBundle\Util\DomainWhitelist; | ||
|
||
class DomainWhitelistTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider targetUrlProvider | ||
* | ||
* @param string $targetUrl | ||
* @param array $domainsWhitelistParameter | ||
* @param bool $isValidTargetUrl | ||
*/ | ||
public function testValidateTargetUrl($targetUrl, $domainsWhitelistParameter, $isValidTargetUrl) | ||
{ | ||
$domainsWhitelist = new DomainWhitelist($domainsWhitelistParameter); | ||
$this->assertSame($isValidTargetUrl, $domainsWhitelist->isValidTargetUrl($targetUrl)); | ||
} | ||
|
||
public function targetUrlProvider() | ||
{ | ||
return [ | ||
['https://example.com/redirect', ['example.com'], true], | ||
['https://example.com/redirect', ['foobar.com'], false], | ||
['blabla', ['foobar.com'], false], | ||
['https://example.com/redirect', ['foobar.com', 'example.com'], true], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the HWIOAuthBundle package. | ||
* | ||
* (c) Hardware Info <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace HWI\Bundle\OAuthBundle\Util; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class DomainWhitelist | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $targetPathDomainsWhiteList; | ||
|
||
/** | ||
* @param array $targetPathDomainsWhiteList | ||
*/ | ||
public function __construct(array $targetPathDomainsWhiteList) | ||
{ | ||
$this->targetPathDomainsWhiteList = $targetPathDomainsWhiteList; | ||
} | ||
|
||
/** | ||
* @param string $targetUrl | ||
* @return bool | ||
*/ | ||
public function isValidTargetUrl(string $targetUrl) | ||
{ | ||
if (0 === count($this->targetPathDomainsWhiteList)) { | ||
return true; | ||
} | ||
|
||
$urlParts = parse_url($targetUrl); | ||
if (!isset($urlParts['host'])) { | ||
return false; | ||
} | ||
|
||
if (!in_array($urlParts['host'], $this->targetPathDomainsWhiteList, true)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |