View source
<?php
namespace Drupal\Tests\csp\Unit;
use Drupal\csp\Csp;
use Drupal\Tests\UnitTestCase;
class CspTest extends UnitTestCase {
public function testHash() {
$this
->assertEquals('sha256-BnZSlC9IkS7BVcseRf0CAOmLntfifZIosT2C1OMQ088=', Csp::calculateHash('alert("Hello World");'));
$this
->assertEquals('sha256-BnZSlC9IkS7BVcseRf0CAOmLntfifZIosT2C1OMQ088=', Csp::calculateHash('alert("Hello World");', 'sha256'));
$this
->assertEquals('sha384-iZxROpttQr5JcGhwPlHbUPBm+IHbO2CwTxLGhVoZXCIIpjSZo+Ourcmqw1QHOpGM', Csp::calculateHash('alert("Hello World");', 'sha384'));
$this
->assertEquals('sha512-6/WbXCJEH9R1/effxooQuXLAsm6xIsfGMK6nFa7TG76VuHZJVRZHIirKrXi/Pib8QbQmkzpo5K/3Ye+cD46ADQ==', Csp::calculateHash('alert("Hello World");', 'sha512'));
}
public function testInvalidHashAlgo() {
$this
->expectException(\InvalidArgumentException::class);
Csp::calculateHash('alert("Hello World");', 'md5');
}
public function testReportOnly() {
$policy = new Csp();
$this
->assertFalse($policy
->isReportOnly());
$this
->assertEquals("Content-Security-Policy", $policy
->getHeaderName());
$policy
->reportOnly();
$this
->assertTrue($policy
->isReportOnly());
$this
->assertEquals("Content-Security-Policy-Report-Only", $policy
->getHeaderName());
$policy
->reportOnly(FALSE);
$this
->assertFalse($policy
->isReportOnly());
$this
->assertEquals("Content-Security-Policy", $policy
->getHeaderName());
}
public function testSetInvalidPolicy() {
$this
->expectException(\InvalidArgumentException::class);
$policy = new Csp();
$policy
->setDirective('foo', Csp::POLICY_SELF);
}
public function testAppendInvalidPolicy() {
$this
->expectException(\InvalidArgumentException::class);
$policy = new Csp();
$policy
->appendDirective('foo', Csp::POLICY_SELF);
}
public function testSetSingle() {
$policy = new Csp();
$policy
->setDirective('default-src', Csp::POLICY_SELF);
$this
->assertTrue($policy
->hasDirective('default-src'));
$this
->assertEquals($policy
->getDirective('default-src'), [
"'self'",
]);
$this
->assertEquals("default-src 'self'", $policy
->getHeaderValue());
}
public function testAppendSingle() {
$policy = new Csp();
$policy
->appendDirective('default-src', Csp::POLICY_SELF);
$this
->assertTrue($policy
->hasDirective('default-src'));
$this
->assertEquals($policy
->getDirective('default-src'), [
"'self'",
]);
$this
->assertEquals("default-src 'self'", $policy
->getHeaderValue());
}
public function testSetMultiple() {
$policy = new Csp();
$policy
->setDirective('default-src', Csp::POLICY_ANY);
$policy
->setDirective('default-src', [
Csp::POLICY_SELF,
'one.example.com',
]);
$policy
->setDirective('script-src', Csp::POLICY_SELF . ' two.example.com');
$policy
->setDirective('upgrade-insecure-requests', TRUE);
$policy
->setDirective('report-uri', 'example.com/report-uri');
$this
->assertEquals("upgrade-insecure-requests; default-src 'self' one.example.com; script-src 'self' two.example.com; report-uri example.com/report-uri", $policy
->getHeaderValue());
}
public function testAppendMultiple() {
$policy = new Csp();
$policy
->appendDirective('default-src', Csp::POLICY_SELF);
$policy
->appendDirective('script-src', [
Csp::POLICY_SELF,
'two.example.com',
]);
$policy
->appendDirective('default-src', 'one.example.com');
$this
->assertEquals("default-src 'self' one.example.com; script-src 'self' two.example.com", $policy
->getHeaderValue());
}
public function testSetEmpty() {
$policy = new Csp();
$policy
->setDirective('default-src', Csp::POLICY_SELF);
$policy
->setDirective('script-src', [
Csp::POLICY_SELF,
]);
$policy
->setDirective('script-src', []);
$this
->assertEquals("default-src 'self'", $policy
->getHeaderValue());
$policy = new Csp();
$policy
->setDirective('default-src', Csp::POLICY_SELF);
$policy
->setDirective('script-src', [
Csp::POLICY_SELF,
]);
$policy
->setDirective('script-src', '');
$this
->assertEquals("default-src 'self'", $policy
->getHeaderValue());
}
public function testAppendEmpty() {
$policy = new Csp();
$policy
->appendDirective('default-src', Csp::POLICY_SELF);
$this
->assertEquals("default-src 'self'", $policy
->getHeaderValue());
$policy
->appendDirective('default-src', '');
$policy
->appendDirective('script-src', []);
$this
->assertEquals("default-src 'self'", $policy
->getHeaderValue());
}
public function testFallbackAwareAppendIfEnabled() {
$policy = new Csp();
$policy
->setDirective('style-src', Csp::POLICY_SELF);
$policy
->fallbackAwareAppendIfEnabled('script-src-attr', Csp::POLICY_UNSAFE_INLINE);
$this
->assertFalse($policy
->hasDirective('default-src'));
$this
->assertFalse($policy
->hasDirective('script-src'));
$this
->assertFalse($policy
->hasDirective('script-src-attr'));
$policy = new Csp();
$policy
->setDirective('default-src', Csp::POLICY_SELF);
$policy
->fallbackAwareAppendIfEnabled('script-src-attr', Csp::POLICY_UNSAFE_INLINE);
$this
->assertEquals([
Csp::POLICY_SELF,
], $policy
->getDirective('default-src'));
$this
->assertFalse($policy
->hasDirective('script-src'));
$this
->assertEquals([
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
], $policy
->getDirective('script-src-attr'));
$policy = new Csp();
$policy
->setDirective('script-src', Csp::POLICY_SELF);
$policy
->fallbackAwareAppendIfEnabled('script-src-attr', Csp::POLICY_UNSAFE_INLINE);
$this
->assertFalse($policy
->hasDirective('default-src'));
$this
->assertEquals([
Csp::POLICY_SELF,
], $policy
->getDirective('script-src'));
$this
->assertEquals([
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
], $policy
->getDirective('script-src-attr'));
$policy = new Csp();
$policy
->setDirective('script-src', Csp::POLICY_SELF);
$policy
->setDirective('script-src-attr', []);
$policy
->fallbackAwareAppendIfEnabled('script-src-attr', Csp::POLICY_UNSAFE_INLINE);
$this
->assertFalse($policy
->hasDirective('default-src'));
$this
->assertEquals([
Csp::POLICY_SELF,
], $policy
->getDirective('script-src'));
$this
->assertEquals([
Csp::POLICY_UNSAFE_INLINE,
], $policy
->getDirective('script-src-attr'));
}
public function testFallbackAwareAppendIfEnabledNone() {
$policy = new Csp();
$policy
->setDirective('default-src', Csp::POLICY_NONE);
$policy
->fallbackAwareAppendIfEnabled('script-src-attr', Csp::POLICY_UNSAFE_INLINE);
$this
->assertEquals([
Csp::POLICY_NONE,
], $policy
->getDirective('default-src'));
$this
->assertFalse($policy
->hasDirective('script-src'));
$this
->assertEquals([
Csp::POLICY_UNSAFE_INLINE,
], $policy
->getDirective('script-src-attr'));
$policy = new Csp();
$policy
->setDirective('script-src', [
Csp::POLICY_NONE,
'https://example.org',
]);
$policy
->fallbackAwareAppendIfEnabled('script-src-attr', Csp::POLICY_UNSAFE_INLINE);
$this
->assertEquals([
Csp::POLICY_UNSAFE_INLINE,
], $policy
->getDirective('script-src-attr'));
}
public function testRemove() {
$policy = new Csp();
$policy
->setDirective('default-src', [
Csp::POLICY_SELF,
]);
$policy
->setDirective('script-src', 'example.com');
$policy
->removeDirective('script-src');
$this
->assertEquals("default-src 'self'", $policy
->getHeaderValue());
}
public function testRemoveInvalid() {
$this
->expectException(\InvalidArgumentException::class);
$policy = new Csp();
$policy
->removeDirective('foo');
}
public function testInvalidValue() {
$this
->expectException(\InvalidArgumentException::class);
$policy = new Csp();
$policy
->appendDirective('default-src', 12);
}
public function testToString() {
$policy = new Csp();
$policy
->setDirective('default-src', Csp::POLICY_SELF);
$policy
->setDirective('script-src', [
Csp::POLICY_SELF,
'example.com',
]);
$this
->assertEquals("Content-Security-Policy: default-src 'self'; script-src 'self' example.com", $policy
->__toString());
}
}