Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEXT-9243 - Add more security headers
- generalise csp handling - Fixes NEXT-9176
- Loading branch information
Showing
10 changed files
with
232 additions
and
33 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
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
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,66 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Core\Framework\Routing; | ||
|
||
use Shopware\Core\PlatformRequest; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
class CoreSubscriber implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private $cspTemplates; | ||
|
||
public function __construct($cspTemplates) | ||
{ | ||
$this->cspTemplates = (array) $cspTemplates; | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
KernelEvents::REQUEST => 'initializeCspNonce', | ||
KernelEvents::RESPONSE => 'setSecurityHeaders', | ||
]; | ||
} | ||
|
||
public function initializeCspNonce(RequestEvent $event): void | ||
{ | ||
$nonce = base64_encode(random_bytes(8)); | ||
$event->getRequest()->attributes->set(PlatformRequest::ATTRIBUTE_CSP_NONCE, $nonce); | ||
} | ||
|
||
public function setSecurityHeaders(ResponseEvent $event): void | ||
{ | ||
if (!$event->getResponse()->isSuccessful()) { | ||
return; | ||
} | ||
|
||
$response = $event->getResponse(); | ||
if ($event->getRequest()->isSecure()) { | ||
$response->headers->set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains'); | ||
} | ||
$response->headers->set('X-Frame-Options', 'deny'); | ||
$response->headers->set('X-Content-Type-Options', 'nosniff'); | ||
|
||
$cspTemplate = $this->cspTemplates['default'] ?? ''; | ||
|
||
$scope = $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE); | ||
if ($scope) { | ||
foreach ($scope->getScopes() as $scope) { | ||
$cspTemplate = $this->cspTemplates[$scope] ?? $cspTemplate; | ||
} | ||
} | ||
|
||
$cspTemplate = trim($cspTemplate); | ||
if ($cspTemplate !== '' && !$response->headers->has('Content-Security-Policy')) { | ||
$nonce = $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_CSP_NONCE); | ||
$csp = str_replace('%nonce%', $nonce, $cspTemplate); | ||
$response->headers->set('Content-Security-Policy', $csp); | ||
} | ||
} | ||
} |
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,100 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Core\Framework\Test\Routing; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Shopware\Core\Framework\Test\TestCaseBase\AdminApiTestBehaviour; | ||
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour; | ||
use Shopware\Core\PlatformRequest; | ||
|
||
class CoreSubscriberTest extends TestCase | ||
{ | ||
use IntegrationTestBehaviour; | ||
use AdminApiTestBehaviour; | ||
|
||
public function testDefaultHeadersHttp(): void | ||
{ | ||
$browser = $this->getBrowser(); | ||
$v = $this->getContainer()->getParameter('kernel.supported_api_versions')[0]; | ||
|
||
$browser->request('GET', '/api/v' . $v . '/category'); | ||
$response = $browser->getResponse(); | ||
|
||
static::assertTrue($response->headers->has('X-Frame-Options')); | ||
static::assertTrue($response->headers->has('X-Content-Type-Options')); | ||
static::assertTrue($response->headers->has('Content-Security-Policy')); | ||
|
||
static::assertFalse($response->headers->has('Strict-Transport-Security')); | ||
} | ||
|
||
public function testDefaultHeadersHttps(): void | ||
{ | ||
$browser = $this->getBrowser(); | ||
$browser->setServerParameter('HTTPS', true); | ||
$v = $this->getContainer()->getParameter('kernel.supported_api_versions')[0]; | ||
|
||
$browser->request('GET', '/api/v' . $v . '/category'); | ||
$response = $browser->getResponse(); | ||
|
||
static::assertTrue($response->headers->has('X-Frame-Options')); | ||
static::assertTrue($response->headers->has('X-Content-Type-Options')); | ||
static::assertTrue($response->headers->has('Content-Security-Policy')); | ||
|
||
static::assertTrue($response->headers->has('Strict-Transport-Security')); | ||
} | ||
|
||
public function testStorefrontNoCsp(): void | ||
{ | ||
$browser = $this->getBrowser(); | ||
$browser->request('GET', $_SERVER['APP_URL']); | ||
$response = $browser->getResponse(); | ||
|
||
static::assertTrue($response->headers->has('X-Frame-Options')); | ||
static::assertTrue($response->headers->has('X-Content-Type-Options')); | ||
static::assertFalse($response->headers->has('Content-Security-Policy')); | ||
} | ||
|
||
public function testAdminHasCsp(): void | ||
{ | ||
$browser = $this->getBrowser(); | ||
$browser->request('GET', $_SERVER['APP_URL'] . '/admin'); | ||
$response = $browser->getResponse(); | ||
|
||
static::assertTrue($response->headers->has('X-Frame-Options')); | ||
static::assertTrue($response->headers->has('X-Content-Type-Options')); | ||
static::assertTrue($response->headers->has('Content-Security-Policy')); | ||
|
||
$request = $browser->getRequest(); | ||
static::assertTrue($request->attributes->has(PlatformRequest::ATTRIBUTE_CSP_NONCE)); | ||
$nonce = $request->attributes->get(PlatformRequest::ATTRIBUTE_CSP_NONCE); | ||
|
||
static::assertRegExp( | ||
'/.*script-src[^;]+nonce-' . preg_quote($nonce, '/') . '.*/', | ||
$response->headers->get('Content-Security-Policy'), | ||
'CSP should contain the nonce' | ||
); | ||
} | ||
|
||
public function testSwaggerHasCsp(): void | ||
{ | ||
$browser = $this->getBrowser(); | ||
$v = $this->getContainer()->getParameter('kernel.supported_api_versions')[0]; | ||
|
||
$browser->request('GET', '/api/v' . $v . '/_info/swagger.html'); | ||
$response = $browser->getResponse(); | ||
|
||
static::assertTrue($response->headers->has('X-Frame-Options')); | ||
static::assertTrue($response->headers->has('X-Content-Type-Options')); | ||
static::assertTrue($response->headers->has('Content-Security-Policy')); | ||
|
||
$request = $browser->getRequest(); | ||
static::assertTrue($request->attributes->has(PlatformRequest::ATTRIBUTE_CSP_NONCE)); | ||
$nonce = $request->attributes->get(PlatformRequest::ATTRIBUTE_CSP_NONCE); | ||
|
||
static::assertRegExp( | ||
'/.*script-src[^;]+nonce-' . preg_quote($nonce, '/') . '.*/', | ||
$response->headers->get('Content-Security-Policy'), | ||
'CSP should contain the nonce' | ||
); | ||
} | ||
} |
Oops, something went wrong.