View source
<?php
namespace Drupal\Tests\csp\Unit;
use Drupal\csp\Csp;
use Drupal\Tests\UnitTestCase;
class CspOptimizationTest extends UnitTestCase {
public function testDuplicate() {
$policy = new Csp();
$policy
->setDirective('default-src', [
Csp::POLICY_SELF,
Csp::POLICY_SELF,
]);
$policy
->setDirective('script-src', 'one.example.com one.example.com');
$policy
->setDirective('style-src', [
'two.example.com',
'two.example.com',
]);
$policy
->appendDirective('style-src', [
'two.example.com',
'two.example.com',
]);
$this
->assertEquals("default-src 'self'; script-src one.example.com; style-src two.example.com", $policy
->getHeaderValue());
}
public function testDefaultSrcFallback() {
$policy = new Csp();
$policy
->setDirective('default-src', Csp::POLICY_SELF);
$policy
->setDirective('script-src', Csp::POLICY_SELF);
$policy
->setDirective('style-src', Csp::POLICY_SELF);
$policy
->setDirective('worker-src', Csp::POLICY_SELF);
$policy
->setDirective('child-src', Csp::POLICY_SELF);
$policy
->setDirective('connect-src', Csp::POLICY_SELF);
$policy
->setDirective('manifest-src', Csp::POLICY_SELF);
$policy
->setDirective('prefetch-src', Csp::POLICY_SELF);
$policy
->setDirective('object-src', Csp::POLICY_SELF);
$policy
->setDirective('frame-src', Csp::POLICY_SELF);
$policy
->setDirective('media-src', Csp::POLICY_SELF);
$policy
->setDirective('font-src', Csp::POLICY_SELF);
$policy
->setDirective('img-src', Csp::POLICY_SELF);
$policy
->setDirective('base-uri', Csp::POLICY_SELF);
$policy
->setDirective('form-action', Csp::POLICY_SELF);
$policy
->setDirective('frame-ancestors', Csp::POLICY_SELF);
$policy
->setDirective('navigate-to', Csp::POLICY_SELF);
$this
->assertEquals("default-src 'self'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; navigate-to 'self'", $policy
->getHeaderValue());
}
public function testWorkerSrcFallback() {
$policy = new Csp();
$policy
->setDirective('worker-src', Csp::POLICY_SELF);
$this
->assertEquals("worker-src 'self'", $policy
->getHeaderValue());
$policy
->setDirective('child-src', Csp::POLICY_SELF);
$this
->assertEquals("child-src 'self'", $policy
->getHeaderValue());
$policy
->setDirective('script-src', Csp::POLICY_SELF);
$this
->assertEquals("script-src 'self'", $policy
->getHeaderValue());
$policy
->setDirective('default-src', Csp::POLICY_SELF);
$this
->assertEquals("default-src 'self'", $policy
->getHeaderValue());
$policy
->removeDirective('child-src');
$this
->assertEquals("default-src 'self'", $policy
->getHeaderValue());
$policy
->setDirective('child-src', [
Csp::POLICY_SELF,
'example.com',
]);
$this
->assertEquals("default-src 'self'; child-src 'self' example.com; worker-src 'self'", $policy
->getHeaderValue());
$policy
->setDirective('worker-src', [
Csp::POLICY_SELF,
'example.com',
]);
$this
->assertEquals("default-src 'self'; child-src 'self' example.com", $policy
->getHeaderValue());
}
public function testScriptSrcFallback() {
$policy = new Csp();
$policy
->setDirective('default-src', Csp::POLICY_SELF);
$policy
->setDirective('script-src', [
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
]);
$policy
->setDirective('script-src-elem', Csp::POLICY_SELF);
$policy
->setDirective('script-src-attr', Csp::POLICY_UNSAFE_INLINE);
$this
->assertEquals("default-src 'self'; script-src 'self' 'unsafe-inline'; script-src-attr 'unsafe-inline'; script-src-elem 'self'", $policy
->getHeaderValue());
$policy
->setDirective('script-src-attr', [
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
]);
$this
->assertEquals("default-src 'self'; script-src 'self' 'unsafe-inline'; script-src-elem 'self'", $policy
->getHeaderValue());
}
public function testStyleSrcFallback() {
$policy = new Csp();
$policy
->setDirective('default-src', Csp::POLICY_SELF);
$policy
->setDirective('style-src', [
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
]);
$policy
->setDirective('style-src-elem', Csp::POLICY_SELF);
$policy
->setDirective('style-src-attr', Csp::POLICY_UNSAFE_INLINE);
$this
->assertEquals("default-src 'self'; style-src 'self' 'unsafe-inline'; style-src-attr 'unsafe-inline'; style-src-elem 'self'", $policy
->getHeaderValue());
$policy
->setDirective('style-src-attr', [
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
]);
$this
->assertEquals("default-src 'self'; style-src 'self' 'unsafe-inline'; style-src-elem 'self'", $policy
->getHeaderValue());
}
public function testReduceSourceListWithNone() {
$policy = new Csp();
$policy
->setDirective('object-src', [
Csp::POLICY_NONE,
'example.com',
"'hash-123abc'",
]);
$this
->assertEquals("object-src 'none'", $policy
->getHeaderValue());
}
public function testReduceSourceListAny() {
$policy = new Csp();
$policy
->setDirective('script-src', [
Csp::POLICY_ANY,
'example.com',
'https://example.com',
'http:',
'https:',
'ftp:',
'ws:',
'wss:',
'data:',
Csp::POLICY_UNSAFE_INLINE,
"'hash-123abc'",
"'nonce-abc123'",
]);
$this
->assertEquals("script-src * data: 'unsafe-inline' 'hash-123abc' 'nonce-abc123'", $policy
->getHeaderValue());
}
public function testReduceSourceListWithHttp() {
$policy = new Csp();
$policy
->setDirective('script-src', [
'http:',
'example.com',
'http://example.org',
'https://example.net',
'ftp:',
'data:',
Csp::POLICY_UNSAFE_INLINE,
"'hash-123abc'",
"'nonce-abc123'",
]);
$this
->assertEquals("script-src http: example.com ftp: data: 'unsafe-inline' 'hash-123abc' 'nonce-abc123'", $policy
->getHeaderValue());
}
public function testReduceSourceListWithHttps() {
$policy = new Csp();
$policy
->setDirective('script-src', [
'https:',
'example.com',
'http://example.org',
'https://example.net',
'ftp:',
'data:',
Csp::POLICY_UNSAFE_INLINE,
"'hash-123abc'",
"'nonce-abc123'",
]);
$this
->assertEquals("script-src https: example.com http://example.org ftp: data: 'unsafe-inline' 'hash-123abc' 'nonce-abc123'", $policy
->getHeaderValue());
}
public function testReduceSourceListWithWs() {
$policy = new Csp();
$policy
->setDirective('script-src', [
'https:',
'ws:',
'example.com',
'ws://connect.example.org',
'wss://connect.example.net',
'ftp:',
'data:',
Csp::POLICY_UNSAFE_INLINE,
"'hash-123abc'",
"'nonce-abc123'",
]);
$this
->assertEquals("script-src https: ws: example.com ftp: data: 'unsafe-inline' 'hash-123abc' 'nonce-abc123'", $policy
->getHeaderValue());
}
public function testReduceSourceListWithWss() {
$policy = new Csp();
$policy
->setDirective('script-src', [
'https:',
'wss:',
'example.com',
'ws://connect.example.org',
'wss://connect.example.net',
'ftp:',
'data:',
Csp::POLICY_UNSAFE_INLINE,
"'hash-123abc'",
"'nonce-abc123'",
]);
$this
->assertEquals("script-src https: wss: example.com ws://connect.example.org ftp: data: 'unsafe-inline' 'hash-123abc' 'nonce-abc123'", $policy
->getHeaderValue());
}
public function testReduceAttrSourceListNetworkSource() {
$policy = new Csp();
$policy
->setDirective('script-src-attr', [
Csp::POLICY_UNSAFE_INLINE,
'https:',
'wss:',
'example.com',
'https://example.com',
'ws://connect.example.org',
'ftp:',
'data:',
]);
$this
->assertEquals("script-src-attr 'unsafe-inline'", $policy
->getHeaderValue());
}
public function testReduceAttrSourceListWildcard() {
$policy = new Csp();
$policy
->setDirective('script-src-attr', [
Csp::POLICY_UNSAFE_INLINE,
Csp::POLICY_ANY,
]);
$this
->assertEquals("script-src-attr 'unsafe-inline'", $policy
->getHeaderValue());
}
public function testReduceAttrSourceListNoUnsafeHash() {
$policy = new Csp();
$policy
->setDirective('script-src-attr', [
Csp::POLICY_UNSAFE_INLINE,
"'sha256-BnZSlC9IkS7BVcseRf0CAOmLntfifZIosT2C1OMQ088='",
]);
$this
->assertEquals("script-src-attr 'unsafe-inline'", $policy
->getHeaderValue());
}
public function testReduceAttrSourceListUnsafeHash() {
$policy = new Csp();
$policy
->setDirective('script-src-attr', [
Csp::POLICY_UNSAFE_INLINE,
Csp::POLICY_UNSAFE_HASHES,
"'sha256-BnZSlC9IkS7BVcseRf0CAOmLntfifZIosT2C1OMQ088='",
]);
$this
->assertEquals("script-src-attr 'unsafe-inline' 'unsafe-hashes' 'sha256-BnZSlC9IkS7BVcseRf0CAOmLntfifZIosT2C1OMQ088='", $policy
->getHeaderValue());
}
public function testReduceAttrSourceListNonce() {
$policy = new Csp();
$policy
->setDirective('script-src-attr', [
Csp::POLICY_SELF,
"'nonce-qskCbxYHEcwf3tBVzkngCA'",
]);
$this
->assertEquals("script-src-attr 'self'", $policy
->getHeaderValue());
}
public function testReduceAttrSourceListOriginallyEmpty() {
$policy = new Csp();
$policy
->setDirective('script-src', [
Csp::POLICY_SELF,
'https://example.com',
]);
$policy
->setDirective('script-src-attr', []);
$this
->assertEquals("script-src 'self' https://example.com", $policy
->getHeaderValue());
}
public function testReduceAttrSourceListEmpty() {
$policy = new Csp();
$policy
->setDirective('script-src', [
Csp::POLICY_SELF,
'https://example.com',
]);
$policy
->setDirective('script-src-attr', [
'https://example.com',
]);
$this
->assertEquals("script-src 'self' https://example.com; script-src-attr 'none'", $policy
->getHeaderValue());
}
public function testReduceAttrSourceListFallback() {
$policy = new Csp();
$directiveValue = [
Csp::POLICY_SELF,
'https://example.com',
Csp::POLICY_UNSAFE_HASHES,
"'sha256-BnZSlC9IkS7BVcseRf0CAOmLntfifZIosT2C1OMQ088='",
];
$policy
->setDirective('script-src', $directiveValue);
$policy
->setDirective('script-src-attr', $directiveValue);
$this
->assertEquals("script-src 'self' https://example.com 'unsafe-hashes' 'sha256-BnZSlC9IkS7BVcseRf0CAOmLntfifZIosT2C1OMQ088='", $policy
->getHeaderValue());
}
}