Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Admin] Security - Add handler to enable Content Security Policy (#11447
) * [Admin] Security - Add handler to enable Content Security Policy * [Admin] Security - Add handler to enable Content Security Policy * [Admin] Security - Add handler to enable Content Security Policy - fix document preview * [Admin] Security - Add handler to enable Content Security Policy - add docs * [Admin] Security - Add handler to enable Content Security Policy - review changes * [Admin] Security - Add handler to enable Content Security Policy - review changes * [Admin] Security - Add handler to enable Content Security Policy - review changes * [Admin] Security - Add handler to enable Content Security Policy - review changes * added missing config node + micro optimizations Co-authored-by: Bernhard Rusch <bernhard.rusch@elements.at>
- Loading branch information
Showing
13 changed files
with
391 additions
and
15 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
bundles/AdminBundle/DependencyInjection/Compiler/ContentSecurityPolicyUrlsPass.php
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\AdminBundle\DependencyInjection\Compiler; | ||
|
||
use Pimcore\Bundle\AdminBundle\Security\ContentSecurityPolicyHandler; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ContentSecurityPolicyUrlsPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$definition = $container->getDefinition(ContentSecurityPolicyHandler::class); | ||
|
||
|
||
$config = $container->getParameter('pimcore_admin.config'); | ||
|
||
if (count($config['admin_csp_header']['additional_urls'])) { | ||
foreach ($config['admin_csp_header']['additional_urls'] as $additionalUrlsKey => $additionalUrlsArr) { | ||
$definition->addMethodCall('addAllowedUrls', [$additionalUrlsKey, $additionalUrlsArr]); | ||
} | ||
} | ||
} | ||
} |
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
83 changes: 83 additions & 0 deletions
83
bundles/AdminBundle/EventListener/AdminSecurityListener.php
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,83 @@ | ||
<?php | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\AdminBundle\EventListener; | ||
|
||
use Pimcore\Bundle\AdminBundle\Security\ContentSecurityPolicyHandler; | ||
use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait; | ||
use Pimcore\Config; | ||
use Pimcore\Http\Request\Resolver\PimcoreContextResolver; | ||
use Pimcore\Http\RequestHelper; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class AdminSecurityListener implements EventSubscriberInterface | ||
{ | ||
use PimcoreContextAwareTrait; | ||
|
||
/** | ||
* @param ContentSecurityPolicyHandler $contentSecurityPolicyHandler | ||
*/ | ||
public function __construct( | ||
protected RequestHelper $requestHelper, | ||
protected ContentSecurityPolicyHandler $contentSecurityPolicyHandler, | ||
protected Config $config | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
KernelEvents::RESPONSE => 'onKernelResponse', | ||
]; | ||
} | ||
|
||
public function onKernelResponse(ResponseEvent $event) | ||
{ | ||
if (!$this->config['admin_csp_header']['enabled']) { | ||
return; | ||
} | ||
|
||
$request = $event->getRequest(); | ||
|
||
if (!$event->isMainRequest()) { | ||
return; | ||
} | ||
|
||
if (!$this->matchesPimcoreContext($request, PimcoreContextResolver::CONTEXT_ADMIN)) { | ||
return; | ||
} | ||
|
||
if ($this->requestHelper->isFrontendRequestByAdmin($request)) { | ||
return; | ||
} | ||
|
||
$response = $event->getResponse(); | ||
|
||
// set CSP header with random nonce string to the response | ||
$response->headers->set("Content-Security-Policy", $this->contentSecurityPolicyHandler->getCspHeader()); | ||
} | ||
|
||
} | ||
|
||
|
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
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
Oops, something went wrong.