View source
<?php
namespace Drupal\Tests\csp\Unit\EventSubscriber;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Render\HtmlResponse;
use Drupal\csp\Csp;
use Drupal\csp\CspEvents;
use Drupal\csp\EventSubscriber\ResponseCspSubscriber;
use Drupal\csp\LibraryPolicyBuilder;
use Drupal\csp\ReportingHandlerPluginManager;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ResponseCspSubscriberTest extends UnitTestCase {
protected $response;
protected $event;
private $libraryPolicy;
private $reportingHandlerPluginManager;
private $eventDispatcher;
public function setUp() : void {
parent::setUp();
$this->response = $this
->getMockBuilder(HtmlResponse::class)
->disableOriginalConstructor()
->getMock();
$this->response->headers = $this
->getMockBuilder(ResponseHeaderBag::class)
->disableOriginalConstructor()
->getMock();
$responseCacheableMetadata = $this
->getMockBuilder(CacheableMetadata::class)
->getMock();
$this->response
->method('getCacheableMetadata')
->willReturn($responseCacheableMetadata);
$this->event = $this
->getMockBuilder(FilterResponseEvent::class)
->disableOriginalConstructor()
->getMock();
$this->event
->expects($this
->any())
->method('isMasterRequest')
->willReturn(TRUE);
$this->event
->expects($this
->any())
->method('getResponse')
->willReturn($this->response);
$this->libraryPolicy = $this
->getMockBuilder(LibraryPolicyBuilder::class)
->disableOriginalConstructor()
->getMock();
$this->reportingHandlerPluginManager = $this
->getMockBuilder(ReportingHandlerPluginManager::class)
->disableOriginalConstructor()
->getMock();
$this->eventDispatcher = $this
->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()
->getMock();
}
public function testSubscribedEvents() {
$this
->assertArrayHasKey(KernelEvents::RESPONSE, ResponseCspSubscriber::getSubscribedEvents());
}
public function testPolicyAlterEvent() {
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => FALSE,
],
'csp.settings' => [
'report-only' => [
'enable' => TRUE,
'directives' => [
'style-src' => [
'base' => 'any',
],
],
],
'enforce' => [
'enable' => TRUE,
'directives' => [
'script-src' => [
'base' => 'self',
],
],
],
],
]);
$this->eventDispatcher
->expects($this
->exactly(2))
->method('dispatch')
->with($this
->equalTo(CspEvents::POLICY_ALTER), $this
->callback(function ($event) {
$policy = $event
->getPolicy();
return $policy
->hasDirective($policy
->isReportOnly() ? 'style-src' : 'script-src');
}))
->willReturnCallback(function ($eventName, $event) {
$policy = $event
->getPolicy();
$policy
->setDirective('font-src', [
Csp::POLICY_SELF,
]);
});
$this->response->headers
->expects($this
->exactly(2))
->method('set')
->withConsecutive([
$this
->equalTo('Content-Security-Policy-Report-Only'),
$this
->equalTo("font-src 'self'; style-src *"),
], [
$this
->equalTo('Content-Security-Policy'),
$this
->equalTo("font-src 'self'; script-src 'self'"),
]);
$subscriber = new ResponseCspSubscriber($configFactory, $this->libraryPolicy, $this->reportingHandlerPluginManager, $this->eventDispatcher);
$subscriber
->onKernelResponse($this->event);
}
public function testEmptyDirective() {
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => FALSE,
],
'csp.settings' => [
'report-only' => [
'enable' => TRUE,
'directives' => [],
],
'enforce' => [
'enable' => TRUE,
],
],
]);
$subscriber = new ResponseCspSubscriber($configFactory, $this->libraryPolicy, $this->reportingHandlerPluginManager, $this->eventDispatcher);
$this->response->headers
->expects($this
->never())
->method('set');
$this->response
->getCacheableMetadata()
->expects($this
->once())
->method('addCacheTags')
->with([
'config:csp.settings',
]);
$subscriber
->onKernelResponse($this->event);
}
public function testUnoptimizedResponse() {
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => FALSE,
],
'csp.settings' => [
'report-only' => [
'enable' => TRUE,
'directives' => [
'script-src' => [
'base' => 'self',
'flags' => [
'unsafe-inline',
],
],
'style-src' => [
'base' => 'self',
],
],
],
'enforce' => [
'enable' => FALSE,
],
],
]);
$this->libraryPolicy
->expects($this
->any())
->method('getSources')
->willReturn([]);
$subscriber = new ResponseCspSubscriber($configFactory, $this->libraryPolicy, $this->reportingHandlerPluginManager, $this->eventDispatcher);
$this->response->headers
->expects($this
->once())
->method('set')
->with($this
->equalTo('Content-Security-Policy-Report-Only'), $this
->equalTo("script-src 'self' 'unsafe-inline'; style-src 'self'"));
$this->response
->getCacheableMetadata()
->expects($this
->once())
->method('addCacheTags')
->with([
'config:csp.settings',
]);
$subscriber
->onKernelResponse($this->event);
}
public function testOptimizedResponse() {
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => TRUE,
],
'csp.settings' => [
'report-only' => [
'enable' => TRUE,
'directives' => [
'script-src' => [
'base' => 'self',
'flags' => [
'unsafe-inline',
],
],
'style-src' => [
'base' => 'self',
],
],
],
'enforce' => [
'enable' => FALSE,
],
],
]);
$this->libraryPolicy
->expects($this
->any())
->method('getSources')
->willReturn([]);
$subscriber = new ResponseCspSubscriber($configFactory, $this->libraryPolicy, $this->reportingHandlerPluginManager, $this->eventDispatcher);
$this->response->headers
->expects($this
->once())
->method('set')
->with($this
->equalTo('Content-Security-Policy-Report-Only'), $this
->equalTo("script-src 'self' 'unsafe-inline'; style-src 'self'"));
$subscriber
->onKernelResponse($this->event);
}
public function testEnforcedResponse() {
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => TRUE,
],
'csp.settings' => [
'enforce' => [
'enable' => TRUE,
'directives' => [
'script-src' => [
'base' => 'self',
'flags' => [
'unsafe-inline',
],
],
'style-src' => [
'base' => 'self',
],
],
],
'report-only' => [
'enable' => FALSE,
],
],
]);
$this->libraryPolicy
->expects($this
->any())
->method('getSources')
->willReturn([]);
$subscriber = new ResponseCspSubscriber($configFactory, $this->libraryPolicy, $this->reportingHandlerPluginManager, $this->eventDispatcher);
$this->response->headers
->expects($this
->once())
->method('set')
->with($this
->equalTo('Content-Security-Policy'), $this
->equalTo("script-src 'self' 'unsafe-inline'; style-src 'self'"));
$subscriber
->onKernelResponse($this->event);
}
public function testBothPolicies() {
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => TRUE,
],
'csp.settings' => [
'report-only' => [
'enable' => TRUE,
'directives' => [
'script-src' => [
'base' => 'any',
'flags' => [
'unsafe-inline',
],
],
'style-src' => [
'base' => 'any',
'flags' => [
'unsafe-inline',
],
],
],
],
'enforce' => [
'enable' => TRUE,
'directives' => [
'script-src' => [
'base' => 'self',
],
'style-src' => [
'base' => 'self',
],
],
],
],
]);
$this->libraryPolicy
->expects($this
->any())
->method('getSources')
->willReturn([]);
$subscriber = new ResponseCspSubscriber($configFactory, $this->libraryPolicy, $this->reportingHandlerPluginManager, $this->eventDispatcher);
$this->response->headers
->expects($this
->exactly(2))
->method('set')
->withConsecutive([
$this
->equalTo('Content-Security-Policy-Report-Only'),
$this
->equalTo("script-src * 'unsafe-inline'; style-src * 'unsafe-inline'"),
], [
$this
->equalTo('Content-Security-Policy'),
$this
->equalTo("script-src 'self'; style-src 'self'"),
]);
$subscriber
->onKernelResponse($this->event);
}
public function testWithLibraryDirective() {
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => TRUE,
],
'csp.settings' => [
'report-only' => [
'enable' => TRUE,
'directives' => [
'script-src' => [
'base' => 'any',
'flags' => [
'unsafe-inline',
],
],
'style-src' => [
'base' => 'self',
'flags' => [
'unsafe-inline',
],
],
'style-src-elem' => [
'base' => 'self',
],
],
],
],
]);
$this->libraryPolicy
->expects($this
->any())
->method('getSources')
->willReturn([
'style-src' => [
'example.com',
],
'style-src-elem' => [
'example.com',
],
]);
$subscriber = new ResponseCspSubscriber($configFactory, $this->libraryPolicy, $this->reportingHandlerPluginManager, $this->eventDispatcher);
$this->response->headers
->expects($this
->once())
->method('set')
->with($this
->equalTo('Content-Security-Policy-Report-Only'), $this
->equalTo("script-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline' example.com; style-src-elem 'self' example.com"));
$subscriber
->onKernelResponse($this->event);
}
public function testDisabledLibraryDirective() {
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => TRUE,
],
'csp.settings' => [
'report-only' => [
'enable' => TRUE,
'directives' => [
'script-src' => [
'base' => 'any',
'flags' => [
'unsafe-inline',
],
],
'style-src' => [
'base' => 'self',
'flags' => [
'unsafe-inline',
],
],
],
],
],
]);
$this->libraryPolicy
->expects($this
->any())
->method('getSources')
->willReturn([
'style-src' => [
'example.com',
],
'style-src-elem' => [
'example.com',
],
]);
$subscriber = new ResponseCspSubscriber($configFactory, $this->libraryPolicy, $this->reportingHandlerPluginManager, $this->eventDispatcher);
$this->response->headers
->expects($this
->once())
->method('set')
->with($this
->equalTo('Content-Security-Policy-Report-Only'), $this
->equalTo("script-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline' example.com"));
$subscriber
->onKernelResponse($this->event);
}
}